Esempio n. 1
0
        private static async void RunGame(CancellationToken stoppingToken)
        {
            // MAIN LOOP
            const int   pollInterval        = 1000;              // ms
            int         timeToNextPoll      = pollInterval;
            int         timeToNextWorldTick = WorldTickInterval; // 1 worldTick = 10 seconds
            const int   sleepTime           = 100;
            const ulong sleepTimeLong       = (ulong)sleepTime;

            while (!stoppingToken.IsCancellationRequested && Running)
            {
                // Timing control
                await Task.Delay(sleepTime, stoppingToken);

                if (!Paused)
                {
                    WorldTime           += sleepTimeLong;
                    timeToNextPoll      -= sleepTime;
                    timeToNextWorldTick -= sleepTime;
                }

                // Check for and remove undetectable disconnected clients
                if (timeToNextPoll <= 0)
                {
                    NetworkServer.GetConnectionPolling();
                    timeToNextPoll += pollInterval;
                }

                // World Tick
                if (timeToNextWorldTick <= 0)
                {
                    GameEngine.RunWorldTick();
                    timeToNextWorldTick += WorldTickInterval;
                }

                // Player Turns
                GetActors().ForEach(p => p.DoTurn());

                // Update Changes to Clients
                Players.FindAll(p => p.HasClient && p.Client.HasUpdates).ForEach(p => p.Client.SendUpdate(p.Client.GetFullUpdateForClient()));
            }
        }
Esempio n. 2
0
        private static void HandleKeyInput(ConsoleKey key)
        {
            bool subdueMenuRepeat = false;

            if (!GameEngine.Running)
            {
                switch (key)
                {
                case ConsoleKey.X:
                case ConsoleKey.Q:
                    WriteLine("Exiting...");
                    done = true;
                    break;

                case ConsoleKey.S:
                    GameTask = Task.Factory.StartNew(() => LaunchGameWithThreadName());
                    while (!GameEngine.Running)
                    {
                        Thread.Sleep(100);
                    }
                    break;

                case ConsoleKey.L:
                    GameTask = Task.Factory.StartNew(() => LaunchGameWithThreadName());
                    while (!GameEngine.Running)
                    {
                        Thread.Sleep(100);
                    }
                    GameEngine.PlayAsServer();
                    break;

                case ConsoleKey.K:
                    GameTask = Task.Factory.StartNew(() => LaunchGameWithThreadName(false));
                    while (!GameEngine.Running)
                    {
                        Thread.Sleep(100);
                    }
                    GameEngine.PlayAsServer();
                    break;

                case ConsoleKey.C:
                    StandaloneClient.RunClient();
                    break;

                case ConsoleKey.D:
                    StandaloneClient.RunClient("dstults.net", 11111);
                    break;

                default:
                    subdueMenuRepeat = true;
                    break;
                }
            }
            else
            {
                switch (key)
                {
                case ConsoleKey.X:
                case ConsoleKey.Q:
                    WriteLine("Exiting...");
                    GameEngine.Shutdown();
                    GameTask.Wait();
                    done = true;
                    break;

                case ConsoleKey.S:
                    WriteLine(WorldMap.GetLocationList());
                    Write($" Where? > ");
                    Location unitLoc = WorldMap.GetLocation(Console.ReadLine());
                    if (unitLoc == null)
                    {
                        WriteLine("Invalid location name");
                        break;
                    }
                    WriteLine(Prefabs.GetPrefabUnitList());
                    Write($" What unit? > ");
                    Player myUnit = Prefabs.SpawnUnitAtLocation(Console.ReadLine(), unitLoc);
                    if (myUnit != null)
                    {
                        WriteLine($"Playable unit [{myUnit.Name}] spawned at [{unitLoc.Name}].");
                    }
                    else
                    {
                        WriteLine($"Invalid unit type or incomplete unit creation.");
                    }
                    break;

                case ConsoleKey.I:
                    WriteLine(WorldMap.GetLocationList());
                    Write($" Where? > ");
                    Location itemLoc = WorldMap.GetLocation(Console.ReadLine());
                    if (itemLoc == null)
                    {
                        WriteLine("Invalid location name");
                        break;
                    }
                    WriteLine(Prefabs.GetPrefabItemList());
                    Write($" What item? > ");
                    Item myItem = Prefabs.SpawnItemAtLocation(Console.ReadLine(), itemLoc);
                    if (myItem != null)
                    {
                        WriteLine($"Item [{myItem.Title}] spawned at [{itemLoc.Name}].");
                        GameEngine.SayToLocation(itemLoc, $"All of the sudden {myItem.SetName()} appears!");
                    }
                    else
                    {
                        WriteLine($"Invalid item name.");
                    }
                    break;

                case ConsoleKey.P:
                    WriteLine("Polling current clients:");
                    WriteLine(NetworkServer.GetConnectionPolling(verbose: true, detailed: false));
                    break;

                case ConsoleKey.D:
                    WriteLine("Polling current clients (detailed):");
                    WriteLine(NetworkServer.GetConnectionPolling(verbose: true, detailed: true));
                    break;

                case ConsoleKey.G:
                    WriteLine(GameEngine.GameInfo());
                    break;

                case ConsoleKey.V:
                    WriteLine(TextUtils.Borderize(TextUtils.Columnize(TextUtils.GetCustomListFromNamedList(GameEngine.Players, numbered: true, lowered: true))));
                    Write($" ? (1-{GameEngine.Players.Count}) > ");
                    Player viewPlayer = GameEngine.GetPlayer(Console.ReadLine());
                    if (viewPlayer != null)
                    {
                        WriteLine("\n================================================================");
                        WriteLine(viewPlayer.GetFullInGameView());
                    }
                    else
                    {
                        WriteLine("No such player.");
                    }
                    break;

                case ConsoleKey.O:
                    WriteLine(TextUtils.Borderize(TextUtils.Columnize(TextUtils.GetCustomListFromNamedList(GameEngine.Players, numbered: true, lowered: true))));
                    Write($" ? (1-{GameEngine.Players.Count}) > ");
                    Player possessPlayer = GameEngine.GetPlayer(Console.ReadLine());
                    if (possessPlayer != null)
                    {
                        GameEngine.PlayAsServer(possessPlayer);
                    }
                    else
                    {
                        WriteLine("No such player.");
                    }
                    break;

                case ConsoleKey.Z:
                    GameEngine.TogglePause();
                    break;

                default:
                    subdueMenuRepeat = true;
                    break;
                }
            }

            if (!subdueMenuRepeat && !done)
            {
                ShowMenu();
            }
        }