Пример #1
0
        public SimpleRaceMode(GameManager gameInstance, int laps, int noOfCheckpoints, RaceTrack raceTrack, Car localCar)
            : base(gameInstance)
        {
            this.laps = laps;
            this.checkpoints = noOfCheckpoints;
            this.raceTrack = raceTrack;
            this.car = localCar;
            this.placementRasterization = raceTrack.GetCurveRasterization(100);
            PlayerPlace = 1;
            GameStarted = false;
            TotalRaceTime = TimeSpan.Zero;

            var player = gameInstance.GetService<Player>();
            player.Lap = 0;
            players.Add(player);

            Initialize();
        }
Пример #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();
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Update a remote car with dead reckoning.
 /// </summary>
 /// <param name="player"></param>
 /// <param name="car"></param>
 private void UpdateNetworkCar(Player player, Car car)
 {
     car.MaterialDiffuse = player.CarColor;
     simulationStrategy.UpdatePosition(player, car);
 }
Пример #4
0
        private Tuple<int, float> CalculateClosestPoint(Car car)
        {
            var orderedPoints = placementRasterization.Points.OrderBy(point => Vector3.DistanceSquared(car.Position, point.Position));
            var closestPoint = orderedPoints.ElementAt(0);
            int closestIndex = placementRasterization.Points.IndexOf(closestPoint);
            var nextClosestPoint = orderedPoints.ElementAt(1);
            int nextClosestIndex = placementRasterization.Points.IndexOf(nextClosestPoint);

            int newClosestIndex = closestIndex;
            float newClosestDistance = Vector3.DistanceSquared(car.Position, closestPoint.Position);
            if (Math.Abs(closestIndex - nextClosestIndex) > 1)
            {
                // cases too god damn annoying, don't have time to code, just return same as before
                //return cachedClosestPoint;
                return new Tuple<int, float>(int.MaxValue, 0f);
                /*var thirdClosest = placementRasterization.Points.IndexOf(orderedPoints.ElementAt(3));
                Console.WriteLine("WOW, ABOUT TO LAP, closest: " + closestIndex + "(" + newClosestDistance + "), nextclosest: " + nextClosestIndex + "(" + Vector3.DistanceSquared(car.Position, nextClosestPoint.Position) + "), third:" + thirdClosest);
                if (Math.Abs(thirdClosest - closestIndex) > 2)//Vector3.DistanceSquared(closestPoint.Position, thirdClosest.Position) > Vector3.DistanceSquared(nextClosestPoint.Position, thirdClosest.Position))
                {
                    newClosestIndex = nextClosestIndex;
                    newClosestDistance = Vector3.DistanceSquared(car.Position, nextClosestPoint.Position);
                }*/
            }
            cachedClosestPoint = new Tuple<int, float>(newClosestIndex, newClosestDistance);
            return cachedClosestPoint;
        }
Пример #5
0
 /// <summary>
 /// Factory for creating a simulated car, i.e. a car that is not drawn.
 /// </summary>
 /// <param name="game"></param>
 /// <returns>A brand new porsche.</returns>
 public static Car CreateSimulatedCar(Game game)
 {
     var car = new Car(game, null, carWheelRadius);
     car.Visible = false;
     return car;
 }
Пример #6
0
        /// <summary>
        /// Our porsche factory.
        /// </summary>
        /// <param name="game"></param>
        /// <returns>A brand new porsche.</returns>
        public static Car CreateCar(Game game)
        {
            if (carModel == null)
            {
                carModel = game.Content.Load<Model>(@"Models/Cars/porsche");
                InitializeEffects(game.Content);

                // Keep some parameters from imported modeleffect
                foreach (var mesh in carModel.Meshes)
                {
                    foreach (var part in mesh.MeshParts)
                    {
                        var basicEffect = part.Effect as BasicEffect;

                        part.Effect = meshEffect[mesh.Name].Clone();

                        if (part.Effect.Parameters["DiffuseMap"] != null)
                            part.Effect.Parameters["DiffuseMap"].SetValue(basicEffect.Texture);

                        if (part.Effect.Parameters["MaterialDiffuse"] != null)
                            part.Effect.Parameters["MaterialDiffuse"].SetValue(basicEffect.DiffuseColor);

                        if (part.Effect.Parameters["MaterialAmbient"] != null)
                            part.Effect.Parameters["MaterialAmbient"].SetValue(basicEffect.DiffuseColor);

                        if (part.Effect.Parameters["MaterialSpecular"] != null)
                            part.Effect.Parameters["MaterialSpecular"].SetValue(basicEffect.DiffuseColor);
                    }

                    if (mesh.Name.StartsWith("wheel"))
                    {
                        if (mesh.Name.EndsWith("001") || mesh.Name.EndsWith("002"))
                            mesh.Tag = 2;
                        else
                            mesh.Tag = 1;
                    }
                    else
                        mesh.Tag = 0;
                }
            }

            var car = new Car(game, carModel, carWheelRadius);
            car.Initialize();
            return car;
        }
Пример #7
0
        public void Render(Matrix view, TerrainModel[,] terrainSegments, int terrainSegmentsCount, Car car)
        {
            this.terrainSegments = terrainSegments;
            this.terrainSegmentsCount = terrainSegmentsCount;
            //this.spotLights = spotLights;

            this.car = car;

            RenderDepthNormal(view);
            RenderLight(view * LightProjection);

            for (int z = 0; z < terrainSegmentsCount; z++)
            {
                for (int x = 0; x < terrainSegmentsCount; x++)
                {
                    if (x == 0 && z == 0)
                        continue;

                    var terrain = terrainSegments[x, z];
                    terrain.Effect.Parameters["LightTexture"].SetValue(LightTarget);
                    terrain.Effect.Parameters["PrelightProjection"].SetValue(LightProjection);
                    terrain.Effect.Parameters["viewportWidth"].SetValue(device.Viewport.Width);
                    terrain.Effect.Parameters["viewportHeight"].SetValue(device.Viewport.Height);

                }
            }

            foreach (GameObject obj in otherGameObjects)
            {
                foreach (ModelMesh mesh in obj.Model.Meshes)
                {
                    foreach (ModelMeshPart part in mesh.MeshParts)
                    {
                        part.Effect.Parameters["LightTexture"].SetValue(LightTarget);
                        part.Effect.Parameters["PrelightProjection"].SetValue(LightProjection);
                        part.Effect.Parameters["viewportWidth"].SetValue(device.Viewport.Width);
                        part.Effect.Parameters["viewportHeight"].SetValue(device.Viewport.Height);
                    }
                }

            }

            // TODO: CARMOVE
            // Car
            car.PreparePrelighting(LightTarget, LightProjection, device.Viewport.Width, device.Viewport.Height);
        }