Esempio n. 1
0
    private void NetworkOnMessageReceived(MessageTag messageTag, IDeserializer reader)
    {
        switch (messageTag)
        {
        case MessageTag.StartSimulation:
            var pkt = new Init();
            pkt.Deserialize(reader);
            Time.fixedDeltaTime = 1f / pkt.TargetFPS;


            _simulation.Init(pkt.Seed);

            _simulationStarted = true;
            break;

        case MessageTag.Frame:
            //Server sends in ReliableOrdered-mode, so we only care about the latest frame
            //Also possible could be high-frequency unreliable messages and use redundant frames to fill up a framebuffer in case of frame loss during transmission
            _simulation.AddFrame(new Frame {
                Commands = _inputParser.DeserializeInput(reader)
            });

            //TODO: only for debugging, frames should be buffered & later executed in FixedUpdate
            _simulation.Simulate();

            LockstepNetwork.Instance.SendHashCode(new HashCode
            {
                FrameNumber = _simulation.FrameCounter,
                Value       = _simulation.HashCode
            });
            break;
        }
    }
Esempio n. 2
0
        public void TestSimpleNavigationService()
        {
            var contexts    = new Contexts();
            var destination = new Vector2(111, 22);

            var container = new ServiceContainer()
                            .Register <IGameService>(new MakeEveryEntityNavigable())
                            .Register <ILogService>(new TestLogger(_output));


            //Initialize a new simulation and add a gameentity by adding a spawncommand to the input
            var sim = new Simulation(contexts, container)
                      .Init(0)
                      .AddFrame(new Frame
            {
                Commands = new ICommand[]
                {
                    new SpawnCommand()
                }
            })
                      .Simulate();

            var e      = contexts.game.GetEntities().First();
            var before = e.position.value;

            sim.AddFrame(new Frame
            {
                Commands = new ICommand[] { new NavigateCommand(e.id.value, destination) }
            }).Simulate();

            for (int i = 0; i < 500; i++)
            {
                sim.AddFrame(new Frame()).Simulate();
            }

            e.position.value.X.ShouldNotBe(before.X);
            e.position.value.Y.ShouldNotBe(before.Y);
        }
Esempio n. 3
0
        public void TestInputGetsCalled()
        {
            var command = new Mock <ICommand>();

            var sim = new Simulation(new Contexts(), new ServiceContainer());

            sim.Init(0);

            sim.AddFrame(new Frame {
                Commands = new[] { command.Object }
            });
            sim.Simulate();

            command.Verify(c => c.Execute(It.IsAny <InputContext>()), Times.Exactly(1));
        }