protected override void updatePlayers(int time)
        {
            if (!isPrediction)
            {
                return;
            }
            if (UnacknowledgedInputs.Count == 0)
            {
                return;
            }
            MessagePlayerAction messageAction = LastMessage;
            var player = GetThisPlayer();

            if (player == null)
            {
                return;
            }
            applyInput(player, messageAction.Action);
            updatePhysicsPlayer(player, time);
            player.PlayerState = player.PlayerState & (PlayerState.Killed | PlayerState.Shoot);
        }
Example #2
0
        void gameLoop()
        {
            Stopwatch watch           = Stopwatch.StartNew();
            int       lastTime        = 0;
            int       accumulatedTime = 0;
            int       messageCounter  = 0;

            // client.SendMessage(new MessageRegistration() { Name = playerName });
            gameState = GameState.Run;
            while (isConnected)
            {
                int thisTime    = (int)watch.ElapsedMilliseconds;
                int elapsedTime = thisTime - lastTime;
                lastTime         = thisTime;
                accumulatedTime += elapsedTime;

                if (elapsedTime != 0)
                {
                    fps = 1000 / elapsedTime;
                }
                while (accumulatedTime > PhysicsUpdateInterval)
                {
                    PlayerActionType action = fetchInput();
                    if ((action & PlayerActionType.Pause) != 0)
                    {
                        gameState = GameState.Pause;
                        FormPause formPause  = null;
                        Action    actionForm = delegate {
                            formPause = new FormPause(gameLogic.Players.Values.ToArray());
                            formPause.ShowDialog();
                        };
                        Invoke(actionForm);
                        gameState = formPause.GameState;
                        if (gameState == GameState.Exit)
                        {
                            actionForm = delegate
                            {
                                this.Close();
                            };
                            Invoke(actionForm);
                        }
                    }
                    if (action != PlayerActionType.None)
                    {
                        MessagePlayerAction messageAction = new MessagePlayerAction()
                        {
                            Action = action, InputNumber = messageCounter
                        };
                        messageCounter++;
                        client.SendMessage(messageAction);
                        gameLogic.LastMessage = messageAction;
                        gameLogic.UnacknowledgedInputs.Enqueue(messageAction);
                    }
                    gameLogic.LastMessage = new MessagePlayerAction()
                    {
                        Action = action
                    };
                    accumulatedTime -= PhysicsUpdateInterval;
                    gameLogic.UpdateGame(PhysicsUpdateInterval);
                }
                updateAnimatedPlayers(elapsedTime);
                firstPerson.UpdateAnimation(elapsedTime);
                //     updateStatusView();
                updateView();
                checkRespawn();
                // Invalidate();
            }
        }