Пример #1
0
        public Car(Vector2 position, Texture2D texture, Texture2D deathTexture, int health, float fuel, int nitro, float maxSpeed, Track track, int player)
            : base(position, texture)
        {
            this.deathTexture = deathTexture;

            lastCheckpoint = track.finish;

            this.health = health;
            this.maxHealth = health;
            this.fuel = fuel;
            this.maxFuel = fuel;

            this.nitro = nitro;
            this.player = player;

            this.maxSpeed = maxSpeed;

            this.track = track;

            Rotation = 600;
        }
Пример #2
0
        /// <summary>
        /// Returns different tiles depending on the color.
        /// </summary>
        /// <param name="trackTexture"></param>
        /// <param name="x">X coordinate inside the trackTexture</param>
        /// <param name="y">Y coordinate inside the trackTexture</param>
        /// <returns></returns>
        public Tile LoadTile(Texture2D trackTexture, int x, int y)
        {
            Color currentColor = GetPixelColor(trackTexture, x, y);

            horn       = Content.Load <SoundEffect>("Sounds/horn");       // Loading the horn sound
            gameOverSP = Content.Load <SoundEffect>("Sounds/GameOverSP"); // Loading the GameOverSP sound
            gameOverMP = Content.Load <SoundEffect>("Sounds/GameOverMP"); // Loading the GameOverSP sound
            winner     = Content.Load <SoundEffect>("Sounds/winner");     // Loading the winner sound

            if (currentColor.Equals(new Color(255, 127, 39)))
            {
                // Orange = A Car

                // Only add the amount of cars needed!
                if (cars.Count != numberOfPlayers)
                {
                    cars.Add(new Car(new Vector2(x * Tile.Width, y * Tile.Height),
                                     Content.Load <Texture2D>("Cars/Car" + cars.Count),
                                     Content.Load <Texture2D>("Cars/CarDead"),
                                     100,
                                     100,
                                     0,
                                     1000.0f,
                                     this,
                                     cars.Count));
                }

                return(new Tile(Content.Load <Texture2D>("Tiles/Road"), TileCollision.Road));
            }
            else if (currentColor.Equals(new Color(195, 195, 195)))
            {
                // Grey = the road
                return(new Tile(Content.Load <Texture2D>("Tiles/Road"), TileCollision.Road));
            }

            /* Y: Could be used if we want Dirt on the map.
             * else if (currentColor.Equals(new Color(239, 228, 176)))
             * {
             *  // Beige = the dirt
             *  return new Tile(Content.Load<Texture2D>("Tiles/Dirt"), TileCollision.Road);
             * }
             */
            else if (currentColor.Equals(new Color(000, 255, 000)))
            {
                // Green = Grass
                return(new Tile(Content.Load <Texture2D>("Tiles/Grass"), TileCollision.Grass));
            }
            else if (currentColor.Equals(new Color(000, 162, 232)))
            {
                // Blue = Water
                return(new Tile(Content.Load <Texture2D>("Tiles/Water"), TileCollision.Water));
            }
            else if (currentColor.Equals(new Color(185, 122, 87)))
            {
                // Beige = Powerup
                powerups.Add(new Powerup(PowerupType.Fuel, new Vector2(x * Tile.Width, y * Tile.Height), Content.Load <Texture2D>("Tiles/Powerup"), false, false));

                return(new Tile(Content.Load <Texture2D>("Tiles/Road"), TileCollision.Road));
            }
            else if (currentColor.Equals(new Color(0, 0, 255)))
            {
                // Pure Blue = Health Pistop
                return(new Tile(Content.Load <Texture2D>("Tiles/PitstopHealth"), TileCollision.PitstopHealth));
            }
            else if (currentColor.Equals(new Color(255, 0, 0)))
            {
                // Pure Red = Fuel Pistop
                return(new Tile(Content.Load <Texture2D>("Tiles/PitstopFuel"), TileCollision.PitstopFuel));
            }
            else if (currentColor.Equals(new Color(163, 073, 164)))
            {
                // Purple = Strip
                return(new Tile(Content.Load <Texture2D>("Tiles/Strip"), TileCollision.Strip));
            }
            else if (currentColor.Equals(new Color(255, 242, 0)))
            {
                // Yellow = Pitstop
                return(new Tile(Content.Load <Texture2D>("Tiles/PitstopStripRed"), TileCollision.PitstopStrip));
            }
            else if (currentColor.Equals(new Color(202, 156, 0)))
            {
                // Dark Yellow = Pitstop
                return(new Tile(Content.Load <Texture2D>("Tiles/PitstopStripWhite"), TileCollision.PitstopStrip));
            }
            else if (currentColor.Equals(new Color(0, 0, 0)))
            {
                // Black = Finish / Start

                if (finish != null && finish.BoundingRectangle.Contains(new Point(x * Tile.Width, y * Tile.Height)))
                {
                    return(new Tile(Content.Load <Texture2D>("Tiles/Finish"), TileCollision.Checkpoint));
                }

                Point endFinishTile = getEndTile(x, y, currentColor);

                int widthFinish  = endFinishTile.X - x;
                int heightFinish = endFinishTile.Y - y;

                finish = new Obstacle(new Rectangle(x * Tile.Width, y * Tile.Height, widthFinish * Tile.Width, heightFinish * Tile.Height));

                return(new Tile(Content.Load <Texture2D>("Tiles/Finish"), TileCollision.Checkpoint));
            }
            else if (currentColor.Equals(new Color(255, 255, 255)))
            {
                // White = Checkpoint

                //check if this tile is already included in the checkpoints list
                //
                foreach (Obstacle iterator in checkpoints)
                {
                    if (iterator.BoundingRectangle.Contains(new Point(x * Tile.Width, y * Tile.Height)))
                    {
                        return(new Tile(Content.Load <Texture2D>("Tiles/Checkpoint"), TileCollision.Checkpoint));
                    }
                }

                Color rightTileColor  = GetPixelColor(trackTexture, x + 1, y);
                Color bottomTileColor = GetPixelColor(trackTexture, x, y + 1);

                Point endCheckpointTile = getEndTile(x, y, currentColor);

                // Make the Checkpoint Object and add it to the CheckPoints-List.
                int widthCheckpoint  = endCheckpointTile.X - x;
                int heightCheckpoint = endCheckpointTile.Y - y;

                Obstacle checkpoint = new Obstacle(new Rectangle(x * Tile.Width, y * Tile.Height, widthCheckpoint * Tile.Width, heightCheckpoint * Tile.Height));
                checkpoints.Add(checkpoint);


                return(new Tile(Content.Load <Texture2D>("Tiles/Checkpoint"), TileCollision.Checkpoint));
            }
            else if (currentColor.Equals(new Color(136, 0, 21)))
            {
                // Red = Wall / Solid

                return(new Tile(Content.Load <Texture2D>("Tiles/Solid"), TileCollision.Solid));
            }
            else
            {
                //throw new NotSupportedException(String.Format("Unsupported tile Color {0} at position {1}, {2}.", tileColor, x, y));
                return(new Tile(Content.Load <Texture2D>("Tiles/Road"), TileCollision.Road)); // Instead of throwing an exception we load unknown tiles with a road texture.
            }
        }
Пример #3
0
        public void Update(GameTime gameTime, List<Obstacle> checkpoints, Obstacle finish)
        {
            base.Update(gameTime);

            // Use fuel if acceleration higher or lower than 5.
            if (acceleration > 5 || acceleration < -5)
            {
                fuel = fuel - 0.05f;
            }

            // Stop the car if there is no more fuel.
            if (fuel < 1)
            {
                acceleration = 0;
            }

            CalculateMovement(gameTime);

            // Don't respond to user input when the car is dead
            if (!isDead)
            {
                GetInput(gameTime);
            }

            HandleCollisions();

            // Check if the car has reached the finish
            if (BoundingRectangle.Intersects(track.finish.BoundingRectangle))
            {
                lastCheckpoint = track.finish;
            }

            // Check for Checkpoint collision
            foreach (Obstacle checkpoint in checkpoints)
            {
                bool isTouching = BoundingRectangle.Intersects(checkpoint.BoundingRectangle);

                if (isTouching && lastCheckpoint != checkpoint)
                {
                    lastCheckpoint = checkpoint;
                    checkpointsSurpassed++;
                    Console.WriteLine("A car just drove over checkpoint No.:" + checkpointsSurpassed);
                }
            }

            // Check if the car has collided with the finish & if it has been to all the checkpoints.
            if (BoundingRectangle.Intersects(finish.BoundingRectangle) && checkpointsSurpassed >= checkpoints.Count() && checkpointsSurpassed != 0)
            {
                lapsDriven++;
                checkpointsSurpassed = 0;
                Console.WriteLine("Laps driven: " + lapsDriven);
            }

            // Car has suffered too much damage
            if (health < 1 && !isDead)
            {
                isDead = true;
                texture = deathTexture;
            }
        }
Пример #4
0
        /// <summary>
        /// Returns different tiles depending on the color.
        /// </summary>
        /// <param name="trackTexture"></param>
        /// <param name="x">X coordinate inside the trackTexture</param>
        /// <param name="y">Y coordinate inside the trackTexture</param>
        /// <returns></returns>
        public Tile LoadTile(Texture2D trackTexture, int x, int y)
        {
            Color currentColor = GetPixelColor(trackTexture, x, y);

            horn = Content.Load<SoundEffect>("Sounds/horn"); // Loading the horn sound
            gameOverSP = Content.Load<SoundEffect>("Sounds/GameOverSP"); // Loading the GameOverSP sound
            gameOverMP = Content.Load<SoundEffect>("Sounds/GameOverMP"); // Loading the GameOverSP sound
            winner = Content.Load<SoundEffect>("Sounds/winner"); // Loading the winner sound

            if (currentColor.Equals(new Color(255, 127, 39)))
            {
                // Orange = A Car

                // Only add the amount of cars needed!
                if (cars.Count != numberOfPlayers)
                {
                    cars.Add(new Car(   new Vector2(x * Tile.Width, y * Tile.Height),
                                        Content.Load<Texture2D>("Cars/Car" + cars.Count),
                                        Content.Load<Texture2D>("Cars/CarDead"),
                                        100,
                                        100,
                                        0,
                                        1000.0f,
                                        this,
                                        cars.Count));
                }

                return new Tile(Content.Load<Texture2D>("Tiles/Road"), TileCollision.Road);
            }
            else if (currentColor.Equals(new Color(195, 195, 195)))
            {
                // Grey = the road
                return new Tile(Content.Load<Texture2D>("Tiles/Road"), TileCollision.Road);
            }
            /* Y: Could be used if we want Dirt on the map.
            else if (currentColor.Equals(new Color(239, 228, 176)))
            {
                // Beige = the dirt
                return new Tile(Content.Load<Texture2D>("Tiles/Dirt"), TileCollision.Road);
            }
            */
            else if (currentColor.Equals(new Color(000, 255, 000)))
            {
                // Green = Grass
                return new Tile(Content.Load<Texture2D>("Tiles/Grass"), TileCollision.Grass);
            }
            else if (currentColor.Equals(new Color(000, 162, 232)))
            {
                // Blue = Water
                return new Tile(Content.Load<Texture2D>("Tiles/Water"), TileCollision.Water);
            }
            else if (currentColor.Equals(new Color(185, 122, 87)))
            {
                // Beige = Powerup
                powerups.Add(new Powerup(PowerupType.Fuel, new Vector2(x * Tile.Width, y * Tile.Height), Content.Load<Texture2D>("Tiles/Powerup"), false, false));

                return new Tile(Content.Load<Texture2D>("Tiles/Road"), TileCollision.Road);
            }
            else if (currentColor.Equals(new Color(0, 0, 255)))
            {
                // Pure Blue = Health Pistop
                return new Tile(Content.Load<Texture2D>("Tiles/PitstopHealth"), TileCollision.PitstopHealth);
            }
            else if (currentColor.Equals(new Color(255, 0, 0)))
            {
                // Pure Red = Fuel Pistop
                return new Tile(Content.Load<Texture2D>("Tiles/PitstopFuel"), TileCollision.PitstopFuel);
            }
            else if (currentColor.Equals(new Color(163, 073, 164)))
            {
                // Purple = Strip
                return new Tile(Content.Load<Texture2D>("Tiles/Strip"), TileCollision.Strip);
            }
            else if (currentColor.Equals(new Color(255, 242, 0)))
            {
                // Yellow = Pitstop
                return new Tile(Content.Load<Texture2D>("Tiles/PitstopStripRed"), TileCollision.PitstopStrip);
            }
            else if (currentColor.Equals(new Color(202, 156, 0)))
            {
                // Dark Yellow = Pitstop
                return new Tile(Content.Load<Texture2D>("Tiles/PitstopStripWhite"), TileCollision.PitstopStrip);
            }
            else if (currentColor.Equals(new Color(0, 0, 0)))
            {
                // Black = Finish / Start

                if (finish != null && finish.BoundingRectangle.Contains(new Point(x * Tile.Width, y * Tile.Height)))
                    return new Tile(Content.Load<Texture2D>("Tiles/Finish"), TileCollision.Checkpoint);

                Point endFinishTile = getEndTile(x, y, currentColor);

                int widthFinish = endFinishTile.X - x;
                int heightFinish = endFinishTile.Y - y;

                finish = new Obstacle(new Rectangle(x * Tile.Width, y * Tile.Height, widthFinish * Tile.Width, heightFinish * Tile.Height));

                return new Tile(Content.Load<Texture2D>("Tiles/Finish"), TileCollision.Checkpoint);
            }
            else if (currentColor.Equals(new Color(255, 255, 255)))
            {
                // White = Checkpoint

                //check if this tile is already included in the checkpoints list
                //
                foreach (Obstacle iterator in checkpoints)
                {
                    if (iterator.BoundingRectangle.Contains(new Point(x * Tile.Width, y * Tile.Height)))
                        return new Tile(Content.Load<Texture2D>("Tiles/Checkpoint"), TileCollision.Checkpoint);
                }

                Color rightTileColor = GetPixelColor(trackTexture, x + 1, y);
                Color bottomTileColor = GetPixelColor(trackTexture, x, y + 1);

                Point endCheckpointTile = getEndTile(x, y, currentColor);

                // Make the Checkpoint Object and add it to the CheckPoints-List.
                int widthCheckpoint = endCheckpointTile.X - x;
                int heightCheckpoint = endCheckpointTile.Y - y;

                Obstacle checkpoint = new Obstacle(new Rectangle(x * Tile.Width, y * Tile.Height, widthCheckpoint * Tile.Width, heightCheckpoint * Tile.Height));
                checkpoints.Add(checkpoint);

                return new Tile(Content.Load<Texture2D>("Tiles/Checkpoint"), TileCollision.Checkpoint);
            }
            else if (currentColor.Equals(new Color(136, 0, 21)))
            {
                // Red = Wall / Solid

                return new Tile(Content.Load<Texture2D>("Tiles/Solid"), TileCollision.Solid);
            }
            else
            {
                //throw new NotSupportedException(String.Format("Unsupported tile Color {0} at position {1}, {2}.", tileColor, x, y));
                return new Tile(Content.Load<Texture2D>("Tiles/Road"), TileCollision.Road); // Instead of throwing an exception we load unknown tiles with a road texture.
            }
        }