using System; using Sandbox.Diagnostics; namespace Braxnet; public static class GameState { // public static Guid CurrentMap { get; set; } public static Guid CurrentMap => Game.ActiveScene?.Id ?? Guid.Empty; private static GameManager GameManager => Game.ActiveScene.GetAllComponents().FirstOrDefault(); public static List GetMaps() { // var levels = FileSystem.Mounted.FindFile( "Scenes/levels", "*.scene" ); var levels = ResourceLibrary.GetAll().Where( x => x.ResourcePath.StartsWith( "scenes/levels" ) ) .ToList(); return levels.ToList(); } public static void LoadMap( SceneFile levelFile ) { if ( CurrentMap == levelFile.Id ) { XLog.Warning( $"Map {levelFile.Title} is already loaded (id: {levelFile.Id})" ); return; } Assert.NotNull( levelFile, "Level file is null" ); Assert.NotNull( levelFile.Title, "Level file title is null" ); XLog.Info( $"Loading map {levelFile.Title}..." ); // CurrentMap = levelFile.Id; /*var pkgRef = levelFile.GetReferencedPackages(); foreach ( var p in pkgRef ) { var pkg = await Package.Fetch( p, false ); if ( pkg is not null ) { Log.Info( $"Mounting package {pkg.Title}" ); await pkg.MountAsync(); } }*/ var opts = new SceneLoadOptions(); opts.SetScene( levelFile ); opts.ShowLoadingScreen = true; Game.ActiveScene.Load( opts ); // TODO: s&box disconnects after loading a new scene Game.ActiveScene.StartLoading(); } public static void LoadRandomMap() { var level = Random.Shared.FromList( GetMaps().Where( x => x.Id != CurrentMap ).ToList() ); if ( level is null ) { XLog.Error( "No more maps to load" ); return; } LoadMap( level ); } [ConCmd( "mtp_map", Help = "Load a map by name" )] public static void Map( string name ) { if ( !Rpc.Caller.IsHost ) return; var level = GetMaps().FirstOrDefault( x => x.Title.ToLower().Contains( name.ToLower() ) ); if ( level is null ) { XLog.Error( $"Map '{name}' not found" ); return; } LoadMap( level ); } [ConCmd( "mtp_endround", Help = "End the current round" )] public static void Restart() { if ( !Rpc.Caller.IsHost ) return; GameManager.EndRound(); } [ConCmd( "mtp_endgame", Help = "End the current game" )] public static void EndGame() { if ( !Rpc.Caller.IsHost ) return; GameManager.EndGame(); } }