コード例 #1
0
ファイル: GameUpdate.cs プロジェクト: andy-uq/Echo
        public void Update()
        {
            var world = new MockUniverse();
            var universe = Universe.Builder.Build(world.Universe).Materialise();

            var sol = universe.StarClusters
                .SelectMany(x => x.SolarSystems)
                .Single(x => x.Id == world.SolarSystem.ObjectId);

            var earth = sol.Satellites
                .Single(x => x.Id == world.Earth.ObjectId);

            var game = new Game(universe, u => Orbit(Satellites(u)));

            var ticksRemaining = game.Update();
            ticksRemaining.ShouldBeGreaterThan(0);

            earth.Position.LocalCoordinates.ShouldNotBe(world.Earth.LocalCoordinates);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: andy-uq/Echo
        private static void SaveState(Game game, string universeFileName)
        {
            UniverseState state = Universe.Builder.Save(game.Universe);

            using (StreamWriter fs = File.CreateText(universeFileName))
            {
                var writer = new JsonTextWriter(fs) {Formatting = Formatting.Indented};
                var serialiser = new JsonSerializer();
                serialiser.TypeNameHandling = TypeNameHandling.None;
                serialiser.Converters.Add(new UInt64Converter());
                serialiser.Converters.Add(new VectorConverter());
                serialiser.ConstructorHandling = ConstructorHandling.Default;

                serialiser.Serialize(writer, state);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: andy-uq/Echo
        private static void Render(Game game)
        {
            var curPosition = new Vector(Console.CursorLeft, Console.CursorTop);

            Console.CursorVisible = false;
            DrawState(game);

            Console.SetCursorPosition((int )curPosition.X, (int )curPosition.Y);
            Console.CursorVisible = true;
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: andy-uq/Echo
        private static void Main(string[] args)
        {
            string universeFileName = args.FirstOrDefault() ?? "universe.txt";
            UniverseState universe;

            if (File.Exists(universeFileName))
            {
                using (StreamReader fs = File.OpenText(universeFileName))
                {
                    var reader = new JsonTextReader(fs);
                    var serialiser = new JsonSerializer();
                    serialiser.Converters.Add(new VectorConverter());
                    universe = serialiser.Deserialize<UniverseState>(reader);
                }
            }
            else
            {
                universe = CreateNewUniverse();
            }

            ObjectBuilder<Universe> universeBuilder = Universe.Builder.Build(universe);
            var resolver = new IdResolutionContext(universeBuilder.FlattenObjectTree());

            var game = new Game(universeBuilder.Build(resolver), Registrations);
            var gameThread = new Thread(() => GameThread(game));
            gameThread.Start();

            var render = new System.Threading.Timer(state => Render(game));
            render.Change(0, 200);

            int? saveSlot = null;
            bool recordInput = false;
            string command = "";
            WriteHelp();
            while (_alive)
            {
                ConsoleKeyInfo key = Console.ReadKey(intercept: true);
                if (recordInput)
                {
                    switch (key.Key)
                    {
                        case ConsoleKey.Escape:
                            recordInput = false;
                            command = "";
                            break;
                        case ConsoleKey.Enter:
                            recordInput = false;
                            command = "";
                            break;
                        default:
                            command += key.KeyChar;
                            break;
                    }

                    WritePrompt(": " + command);
                    continue;
                }

                switch (key.KeyChar)
                {
                    case 'q':
                        _alive = false;
                        break;
                    case 's':
                    case 'l':
                        if (saveSlot != null)
                        {
                            if (key.KeyChar == 's')
                            {
                                RequestSave(saveSlot);
                            }
                            else
                            {
                                RequestRewind(saveSlot);
                            }
                        }
                        else
                        {
                            WritePrompt("Please enter save slot [0-9]");
                        }
                        break;
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        saveSlot = key.KeyChar - '0';
                        break;
                    case ':':
                    case ';':
                        recordInput = true;
                        WritePrompt(": ");
                        break;
                }
            }

            gameThread.Join();
            SaveState(game, universeFileName);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: andy-uq/Echo
        private static void GameThread(Game game)
        {
            Stopwatch frameTimer = Stopwatch.StartNew();
            while (_alive)
            {
                frameTimer.Start();
                while (_commandQueue.Any())
                {
                    ICommand cmd = _commandQueue.Dequeue();

                }

                game.Update();

                long remaining = Game.TicksPerSlice - frameTimer.ElapsedTicks;
                if (remaining > 0)
                {
                    Thread.Sleep(TimeSpan.FromTicks(remaining));
                }

                frameTimer.Reset();
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: andy-uq/Echo
        private static void DrawState(Game game)
        {
            Console.SetCursorPosition(0, 0);
            Console.Write("IDLE: {0:n2}%, Tick: {1:x8}", game.IdleTimer.Idle, game.Tick);

            DrawSolarSystem(game.Universe.StarClusters.SelectMany(s => s.SolarSystems).SingleOrDefault());
        }