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();
                }
            }
        }