Exemplo n.º 1
0
        public void TransferredInputOnce()
        {
            var clock      = new ManualClock();
            var transport1 = new TestTransport("1");
            var transport2 = new TestTransport("2");
            var network1   = new HiddenSwitch.Multiplayer.Network <GameState, GameInput> (clock: clock, transport: transport1, startState: null);
            var network2   = new HiddenSwitch.Multiplayer.Network <GameState, GameInput> (clock: clock, transport: transport2, startState: new GameState()
            {
                count = 52
            });

            network1.AddPeer("2", 4);
            network1.QueueInput(new GameInput()
            {
                direction = Vector3.up
            }, frameIndex: 0);
            network2.QueueInput(new GameInput()
            {
                direction = Vector3.back
            }, frameIndex: 0);
            clock.Step();
            var input1to2 = network2.GetSimulationFrame(0).Inputs [network1.MyPeerId] as GameInput;

            Assert.IsNotNull(input1to2);
            Assert.AreEqual(Vector3.up, input1to2.direction);
            var input2to1 = network1.GetSimulationFrame(0).Inputs [network2.MyPeerId] as GameInput;

            Assert.IsNotNull(input2to1);
            Assert.AreEqual(Vector3.back, input2to1.direction);
        }
Exemplo n.º 2
0
        public void ExecutesInputs()
        {
            var manualClock     = new ManualClock();
            var comparisonState = new GameStateWithInputs();
            var simulation      = new Simulation <GameStateWithInputs, GameInput> (manualClock, new GameStateWithInputs());

            simulation.InputHandler = delegate(GameStateWithInputs mutableState, System.Collections.Generic.KeyValuePair <int, GameInput>[] inputs, int frameIndex) {
                foreach (var kv in inputs)
                {
                    var input = kv.Value;
                    mutableState.Move(input.direction);
                }
            };
            var myPeerId     = 0;
            var moves        = 20;
            var systemRandom = new System.Random();

            for (var i = 0; i < moves; i++)
            {
                var direction = new Vector3((float)systemRandom.NextDouble(), (float)systemRandom.NextDouble(), (float)systemRandom.NextDouble());

                comparisonState.location += direction;
                var input = new GameInput()
                {
                    direction = direction
                };
                simulation.SetInput(input, myPeerId, simulation.ElapsedFrameCount);
                manualClock.Step();
            }

            Assert.AreEqual(comparisonState.location, simulation.LatestState.location);
        }
Exemplo n.º 3
0
        public void SendUnacknowledgedInputs()
        {
            var clock      = new ManualClock();
            var transport1 = new TestTransport("1");
            var transport2 = new TestTransport("2");
            var network1   = new HiddenSwitch.Multiplayer.Network <GameState, GameInput> (clock: clock, transport: transport1, startState: null);
            var network2   = new HiddenSwitch.Multiplayer.Network <GameState, GameInput> (clock: clock, transport: transport2, startState: new GameState()
            {
                count = 52
            });

            network1.AddPeer("2", 4);
            for (var i = 0; i < 8; i++)
            {
                network1.QueueInput(new GameInput()
                {
                    direction = Vector3.up
                }, i);
                network2.QueueInput(new GameInput()
                {
                    direction = Vector3.back
                }, i);

                transport1.FailNextSend();
                clock.Step();
            }
            Assert.AreEqual(-1, network1.LatestAcknowledgedFrame);
            clock.Step();
            Assert.AreEqual(7, network1.LatestAcknowledgedFrame);
            var input1to2 = network2.GetSimulationFrame(0).Inputs [network1.MyPeerId] as GameInput;

            Assert.IsNotNull(input1to2);
            Assert.AreEqual(Vector3.up, input1to2.direction);
            var input2to1 = network1.GetSimulationFrame(0).Inputs [network2.MyPeerId] as GameInput;

            Assert.IsNotNull(input2to1);
            Assert.AreEqual(Vector3.back, input2to1.direction);
        }