Exemplo n.º 1
0
        private void UpdateLocalCar(Player player, Car car)
        {
            // Calculate real position
            if (Game.GetService <CameraComponent>().CurrentCamera is ThirdPersonCamera)
            {
                //Accelerate
                car.Speed = Math.Min(car.Speed + (car.Acceleration - car.Speed * 0.00305f) *
                                     input.GetState(Input.Thrust) -
                                     input.GetState(Input.Brake) * car.Deacceleration, car.MaxSpeed);

                // Turn Wheel
                car.WheelRotationY += (input.GetState(Input.Steer) * (car.TurnSpeed - Math.Abs(car.Speed) * 0.00005f));
                car.WheelRotationY  = MathHelper.Clamp(car.WheelRotationY, -car.MaxWheelTurn, car.MaxWheelTurn);
                if (Math.Abs(car.WheelRotationY) > .001f)
                {
                    car.WheelRotationY *= .95f;
                }
                else
                {
                    car.WheelRotationY = 0;
                }
            }

            //Friction if is not driving
            float friction = .995f;

            if (Math.Abs(input.GetState(Input.Thrust)) < .001f)
            {
                car.Speed *= (friction - Math.Abs(car.Speed - car.MaxSpeed) * 0.0005f);
            }

            // If in a network game, run parallell simulation of car
            var serverClient = Game.GetService <ServerClient>();

            if (Game.GetService <ServerClient>().connected)
            {
                bool isOverThreshold = simulationStrategy.UpdatePosition(player, car);

                // If car has deviated too much from simulated path, distribute position to server
                if (isOverThreshold)
                {
                    //Console.WriteLine(DateTime.Now + ": Local car over threshold, sending update!");
                    player.SetPosition(car.Position.X, car.Position.Y, car.Position.Z, car.Rotation, car.Speed, (byte)(player.LastReceived.Sequence + 1), DateTime.UtcNow);
                    serverClient.SendPlayerPosition();
                }
            }
        }
Exemplo n.º 2
0
        private void UpdateLocalCar(Player player, Car car)
        {
            // Calculate real position
            if (Game.GetService<CameraComponent>().CurrentCamera is ThirdPersonCamera)
            {
                //Accelerate
                car.Speed = Math.Min(car.Speed + (car.Acceleration - car.Speed * 0.00305f) *
                    input.GetState(Input.Thrust) -
                    input.GetState(Input.Brake) * car.Deacceleration, car.MaxSpeed);

                // Turn Wheel
                car.WheelRotationY += (input.GetState(Input.Steer) * (car.TurnSpeed - Math.Abs(car.Speed) * 0.00005f));
                car.WheelRotationY = MathHelper.Clamp(car.WheelRotationY, -car.MaxWheelTurn, car.MaxWheelTurn);
                if (Math.Abs(car.WheelRotationY) > .001f)
                    car.WheelRotationY *= .95f;
                else
                    car.WheelRotationY = 0;
            }

            //Friction if is not driving
            float friction = .995f;
            if (Math.Abs(input.GetState(Input.Thrust)) < .001f)
            {
                car.Speed *= (friction - Math.Abs(car.Speed-car.MaxSpeed) * 0.0005f);
            }

            // If in a network game, run parallell simulation of car
            var serverClient = Game.GetService<ServerClient>();
            if (Game.GetService<ServerClient>().connected)
            {
                bool isOverThreshold = simulationStrategy.UpdatePosition(player, car);

                // If car has deviated too much from simulated path, distribute position to server
                if (isOverThreshold)
                {
                    //Console.WriteLine(DateTime.Now + ": Local car over threshold, sending update!");
                    player.SetPosition(car.Position.X, car.Position.Y, car.Position.Z, car.Rotation, car.Speed, (byte)(player.LastReceived.Sequence + 1), DateTime.UtcNow);
                    serverClient.SendPlayerPosition();
                }
            }
        }
Exemplo n.º 3
0
        private void ParseDataPackage(NetIncomingMessage msg)
        {
            MessageType type = (MessageType)msg.ReadByte();
            Dictionary <byte, Player> PlayerList = ServerHandler.Players;
            Player player = null;

            if (IsPlayerMessage(type) && !PlayerList.TryGetValue(msg.ReadByte(), out player))
            {
                //Console.WriteLine("Received message from unknown player, discarding...");
                return;
            }
            switch (type)
            {
            case MessageType.PlayerPos:
                byte     sequenceNo = msg.ReadByte();
                float    x = msg.ReadFloat(); float y = msg.ReadFloat(); float z = msg.ReadFloat();
                float    rotation = msg.ReadFloat(); float velocity = msg.ReadFloat();
                DateTime sentTime = new DateTime(msg.ReadInt64());
                player.SetPosition(x, y, z, rotation, velocity, sequenceNo, sentTime);
                break;

            case MessageType.Chat:
                string chatMsg = msg.ReadString();
                Console.WriteLine("CHAT {0}: {1}", player.PlayerName, chatMsg);
                var chatTuple = new Tuple <string, string, DateTime>(player.PlayerName, chatMsg, DateTime.Now);
                ServerHandler.ChatHistory.AddLast(chatTuple);
                break;

            case MessageType.Debug:
                break;

            case MessageType.LobbyUpdate:
                if (DEBUG_MODE)
                {
                    Console.Write("Received LobbyUpdate message: ");
                }
                Dictionary <byte, Player> newPlayerList = new Dictionary <byte, Player>();

                byte playerCount = msg.ReadByte();
                for (int i = 0; i < playerCount; i++)
                {
                    byte    discoveredPlayerId    = msg.ReadByte();
                    string  discoveredPlayerName  = msg.ReadString();
                    bool    discoveredPlayerReady = msg.ReadBoolean();
                    Vector3 discoveredPlayerColor = new Color(msg.ReadByte(), msg.ReadByte(), msg.ReadByte()).ToVector3();

                    if (DEBUG_MODE)
                    {
                        Console.Write(discoveredPlayerName + ", ");
                    }

                    if (ServerHandler.LocalPlayer.ID != discoveredPlayerId)     // ignore info of local player
                    {
                        Player discoveredPlayer;
                        if (!PlayerList.TryGetValue(discoveredPlayerId, out discoveredPlayer))
                        {
                            ServerHandler.Game.GetService <HUDConsoleComponent>().WriteOutput("New remote player " + discoveredPlayerName + " discovered!");
                            var newPlayer = new Player(GameManager.GetInstance(), discoveredPlayerId, discoveredPlayerName);
                            newPlayer.State    = discoveredPlayerReady ? PlayerState.Game : PlayerState.Lobby;
                            newPlayer.CarColor = discoveredPlayerColor;
                            newPlayerList[discoveredPlayerId] = newPlayer;
                        }
                        else
                        {
                            discoveredPlayer.PlayerName       = discoveredPlayerName;  // maybe has changed name
                            discoveredPlayer.CarColor         = discoveredPlayerColor; // or color
                            discoveredPlayer.State            = discoveredPlayerReady ? PlayerState.Game : PlayerState.Lobby;
                            newPlayerList[discoveredPlayerId] = discoveredPlayer;
                            PlayerList.Remove(discoveredPlayerId);
                        }
                    }
                    ServerHandler.Players = newPlayerList;
                }
                // Remaining players in PlayerList are no longer connected to server.
                foreach (var disconnectedPlayer in PlayerList.Values)
                {
                    ServerHandler.Game.GetService <HUDConsoleComponent>().WriteOutput("Player " + disconnectedPlayer.PlayerName + " disconnected!");
                    var ccc = ServerHandler.Game.GetService <CarControlComponent>();
                    if (ccc != null)
                    {
                        ccc.RemoveCar(disconnectedPlayer);
                    }
                }
                if (DEBUG_MODE)
                {
                    Console.WriteLine();
                }
                break;

            case MessageType.OK:
                byte assignedID = msg.ReadByte();
                int  gameSeed   = msg.ReadInt32();
                if (DEBUG_MODE)
                {
                    Console.WriteLine("Received OK handshake from server with ID: " + assignedID + ", seed: " + gameSeed);
                }
                ServerHandler.LocalPlayer.ID = assignedID;
                ServerHandler.Seed           = gameSeed;
                ServerHandler.connected      = true;
                ServerHandler.Game.GetService <HUDConsoleComponent>().WriteOutput("Connected! (id " + assignedID + ")");
                break;

            case MessageType.Countdown:
                byte countdown = msg.ReadByte();
                if (DEBUG_MODE)
                {
                    Console.WriteLine("Received Countdown from server: " + countdown);
                }
                string[] countdownStr = { "3", "2", "1", "Go!" };
                ServerHandler.Game.GetService <HUDComponent>().ShowTextNotification(Color.AliceBlue, countdownStr[countdown], TimeSpan.FromSeconds(0.8));
                ServerHandler.GamePlay.mode.CountDownState = countdown;
                if (countdown == 3)
                {
                    ServerHandler.GamePlay.mode.GameStarted = true;
                }
                break;

            case MessageType.RaceTime:
                player.RaceTime = new TimeSpan(msg.ReadInt64());
                break;

            default:
                break;
            }
        }