Beispiel #1
0
        public void HandleCollisions()
        {
            // 1. Haal de car-Rectangle op. Deze is op te halen via het veld: BoundingRectangle
            // 2. Zoek uit met welke tiles de Car in contact komt.
            // 3. Loop door deze tiles heen en controleer vervolgens welke collision-type deze tile heeft
            //      3b. Is deze tile passable?
            //      3c. Is deze tile NIET passable? dan is er een collision.
            //

            // this.BoundingRectangle wordt gebruikt om de tiles rondom de auto te vinden.

            int xLeftTile   = (int)Math.Floor((float)BoundingRectangle.Left / Tile.Width);
            int xRightTile  = (int)Math.Ceiling((float)BoundingRectangle.Right / Tile.Width) - 1;
            int yTopTile    = (int)Math.Floor((float)BoundingRectangle.Top / Tile.Height);
            int yBottomTile = (int)Math.Ceiling((float)BoundingRectangle.Bottom / Tile.Height) - 1;

            // Loop door de verticale tiles
            for (int y = yTopTile; y <= yBottomTile; ++y)
            {
                // En door de horizontale tiles
                for (int x = xLeftTile; x <= xRightTile; ++x)
                {
                    TileCollision collision       = track.GetCollisionOfTile(x, y); // Haal collision-type op van de current Tile
                    Rectangle     tileBoundingBox = new Rectangle(x * Tile.Width, y * Tile.Height, Tile.Width, Tile.Height);

                    if (!tileBoundingBox.Intersects(BoundingRectangle) || !BoundingRectangle.Intersects(tileBoundingBox))
                    {
                        break; // ?
                    }

                    switch (collision)
                    {
                    case TileCollision.Road:
                        isOnRoad     = true;
                        isOnGrass    = false;
                        isOnStrip    = false;
                        isOnPSFuel   = false;
                        isOnPSHealth = false;

                        /* Y: Could be used if we want Dirt on the map.
                         * isOnDirt = false;
                         */
                        break;

                    case TileCollision.Grass:
                        isOnRoad     = false;
                        isOnGrass    = true;
                        isOnStrip    = false;
                        isOnPSFuel   = false;
                        isOnPSHealth = false;

                        /* Y: Could be used if we want Dirt on the map.
                         * isOnDirt = false;
                         */
                        health -= 0.05f;
                        break;

                    case TileCollision.Strip:
                        isOnRoad     = false;
                        isOnGrass    = false;
                        isOnStrip    = true;
                        isOnPSFuel   = false;
                        isOnPSHealth = false;

                        /* Y: Could be used if we want Dirt on the map.
                         * isOnDirt = false;
                         */
                        health -= 0.05f;
                        break;

                    case TileCollision.PitstopStrip:
                        this.position.X = lastCheckpoint.BoundingRectangle.Location.X;
                        this.position.Y = lastCheckpoint.BoundingRectangle.Location.Y;
                        position.X     += lastCheckpoint.BoundingRectangle.Width / 2;
                        position.Y     += lastCheckpoint.BoundingRectangle.Height / 2;
                        break;

                    case TileCollision.PitstopFuel:
                        isOnRoad     = false;
                        isOnGrass    = false;
                        isOnStrip    = false;
                        isOnPSFuel   = true;
                        isOnPSHealth = false;

                        /* Y: Could be used if we want Dirt on the map.
                         * isOnDirt = false;
                         */
                        IncreaseFuel(1);
                        break;

                    case TileCollision.PitstopHealth:
                        isOnRoad     = false;
                        isOnGrass    = false;
                        isOnStrip    = false;
                        isOnPSFuel   = false;
                        isOnPSHealth = true;

                        /* Y: Could be used if we want Dirt on the map.
                         * isOnDirt = false;
                         */
                        IncreaseHealth(1);
                        break;

                    /* Y: Could be used if we want Dirt on the map.
                     * case TileCollision.Dirt:
                     *  isOnRoad = false;
                     *  isOnGrass = false;
                     *  isOnStrip = false;
                     *  isOnPSFuel = false;
                     *  isOnPSHealth = false;
                     *  isOnDirt = true;
                     *  break;
                     */

                    case TileCollision.Water:
                        this.position.X = lastCheckpoint.BoundingRectangle.Location.X;
                        this.position.Y = lastCheckpoint.BoundingRectangle.Location.Y;
                        position.X     += lastCheckpoint.BoundingRectangle.Width / 2;
                        position.Y     += lastCheckpoint.BoundingRectangle.Height / 2;
                        break;

                    case TileCollision.Solid:
                        this.position.X = lastCheckpoint.BoundingRectangle.Location.X;
                        this.position.Y = lastCheckpoint.BoundingRectangle.Location.Y;
                        position.X     += lastCheckpoint.BoundingRectangle.Width / 2;
                        position.Y     += lastCheckpoint.BoundingRectangle.Height / 2;
                        break;
                    }
                }
            }
        }