コード例 #1
0
        /// <summary>
        ///     Example console app for game simulation entry point.
        /// </summary>
        public static int Main()
        {
            // Create console with title, no cursor, make CTRL-C act as input.
            Console.Title = "Oregon Trail Clone";
            Console.WriteLine("Starting...");
            Console.CursorVisible   = false;
            Console.CancelKeyPress += Console_CancelKeyPress;

            // Because I want things to look correct like progress bars.
            Console.OutputEncoding = System.Text.Encoding.Unicode;

            // Create game simulation singleton instance, and start it.
            GameSimulationApp.Create();

            // Hook event to know when screen buffer wants to redraw the entire console screen.
            GameSimulationApp.Instance.SceneGraph.ScreenBufferDirtyEvent += Simulation_ScreenBufferDirtyEvent;

            // Prevent console session from closing.
            while (GameSimulationApp.Instance != null)
            {
                // Simulation takes any numbers of pulses to determine seconds elapsed.
                GameSimulationApp.Instance.OnTick(true);

                // Check if a key is being pressed, without blocking thread.
                if (Console.KeyAvailable)
                {
                    // GetModule the key that was pressed, without printing it to console.
                    var key = Console.ReadKey(true);

                    // If enter is pressed, pass whatever we have to simulation.
                    // ReSharper disable once SwitchStatementMissingSomeCases
                    switch (key.Key)
                    {
                    case ConsoleKey.Enter:
                        GameSimulationApp.Instance.InputManager.SendInputBufferAsCommand();
                        break;

                    case ConsoleKey.Backspace:
                        GameSimulationApp.Instance.InputManager.RemoveLastCharOfInputBuffer();
                        break;

                    default:
                        GameSimulationApp.Instance.InputManager.AddCharToInputBuffer(key.KeyChar);
                        break;
                    }
                }

                // Do not consume all of the CPU, allow other messages to occur.
                Thread.Sleep(1);
            }

            // Make user press any key to close out the simulation completely, this way they know it closed without error.
            Console.Clear();
            Console.WriteLine("Goodbye!");
            Console.WriteLine("Press ANY KEY to close this window...");
            Console.ReadKey();
            return(0);
        }
コード例 #2
0
        /// <summary>
        ///     Creates new instance of game simulation. Complains if instance already exists.
        /// </summary>
        public static void Create()
        {
            if (Instance != null)
            {
                throw new InvalidOperationException(
                          "Unable to create new instance of game simulation since it already exists!");
            }

            Instance = new GameSimulationApp();
            Instance.OnPostCreate();
        }
コード例 #3
0
        /// <summary>
        ///     Creates new instance of game simulation. Complains if instance already exists.
        /// </summary>
        public static void Create()
        {
            if (Instance != null)
                throw new InvalidOperationException(
                    "Unable to create new instance of game simulation since it already exists!");

            Instance = new GameSimulationApp();
            Instance.OnPostCreate();
        }