Ejemplo n.º 1
0
        /// <summary>
        /// Check if a collision occured and preform tasks specific to the collided object type.
        /// </summary>
        public void CheckPossibleCollision()
        {
            //Check every tile in the game:
            for (int i = 0; i < listTiles.Count; ++i)
            {
                //If an abstract circle around the tile is intersecting with the player:
                if (listTiles[i].CircleCollision(player))
                {
                    //Check which tile has a possible collision
                    if (listTiles[i] is Trampoline)
                    {
                        //Preform actual collision detection between the tile and player to make sure
                        Trampoline tempTramp = (Trampoline)listTiles[i];
                        if (CalcRectangleSides(player.GetCorners, tempTramp.GetCorners))
                        {
                            //Check which side of the trampoline player hit.
                            Vector2[] testPoints = new Vector2[3] {
                                tempTramp.GetMidpoint, tempTramp.GetCorners[(int)MapTile.Corners.topLeft], tempTramp.GetCorners[(int)MapTile.Corners.topRight]
                            };
                            for (int k = 0; k < testPoints.Length; ++k)
                            {
                                //Check to see if the collision being checked is with the midpoint
                                bool isMidpoint = false;
                                if (testPoints[k] == tempTramp.GetMidpoint)
                                {
                                    isMidpoint = true;
                                }

                                //Preform point to rectangle collision with the player to check for a collision on the specific side
                                tempTramp.HitSide = !SurfaceCollision(player.GetSprite.GetBounds, testPoints[k], isMidpoint);

                                //If there is a collision with the specific side break out of the loop
                                if (!tempTramp.HitSide)
                                {
                                    break;
                                }
                            }

                            //Change the players' trajectory based on the side that was hit
                            tempTramp.ChangePlayerTraj(player);
                        }
                    }
                    if (listTiles[i] is Spike)
                    {
                        //Perform actual collision detection between the tile and player to make sure
                        Spike tempSpike = (Spike)listTiles[i];
                        if (CalcRectangleSides(player.GetCorners, tempSpike.GetCorners))
                        {
                            //Check which side of the trampoline player hit.
                            Vector2[] testPoints = new Vector2[3] {
                                tempSpike.GetMidpoint, tempSpike.GetCorners[(int)MapTile.Corners.topLeft], tempSpike.GetCorners[(int)MapTile.Corners.topRight]
                            };
                            for (int k = 0; k < testPoints.Length; ++k)
                            {
                                //Check to see if the collision being checked is with the midpoint
                                bool isMidpoint = false;
                                if (testPoints[k] == tempSpike.GetMidpoint)
                                {
                                    isMidpoint = true;
                                }

                                //Perform point to rectangle collision with the player to check for a collision on the specific side
                                tempSpike.HitSide = !SurfaceCollision(player.GetSprite.GetBounds, testPoints[k], isMidpoint);

                                //If there is a collision with the specific side break out of the loop
                                if (!tempSpike.HitSide)
                                {
                                    break;
                                }
                            }

                            //Change player trajectory (or possibly kill him) based on the side that was hit
                            tempSpike.ChangePlayerTraj(player);
                        }
                    }
                    if (listTiles[i] is Bubble)
                    {
                        if (!player.InBubble && !player.PoppedBubble)
                        {
                            //Preform actual collision detection between the tile and player to make sure
                            if (CalcRectangleSides(player.GetCorners, listTiles[i].GetCorners))
                            {
                                //Change the properties of both the player and the bubble
                                player.InBubble = true;
                                ((Bubble)listTiles[i]).FollowPlayer = true;
                            }
                        }

                        //Change the acceleraion of the player based on the velocity
                        if (player.PoppedBubble && !player.InBubble)
                        {
                            player.PoppedBubble = false;
                        }
                        else if (player.PoppedBubble && player.InBubble)
                        {
                            listTiles.RemoveAt(i);
                            player.PoppedBubble = false;
                            player.InBubble     = false;
                        }
                    }
                    if (listTiles[i] is Apple)
                    {
                        //Preform actual collision detection between the tile and player to make sure
                        if (CalcRectangleSides(player.GetCorners, listTiles[i].GetCorners))
                        {
                            //Change the properties of the apple to make the level complete
                            ((Apple)listTiles[i]).LevelComplete = true;
                        }
                    }
                    if (listTiles[i] is Brick)
                    {
                        //Preform actual collision detection between the tile and player to make sure
                        Brick tempBrick = (Brick)listTiles[i];

                        //player .GetSprite.SetPosition(player.GetSprite.GetPosition + (player.Trajectory * 2));
                        //player.CalcCorners();

                        if (CalcRectangleSides(player.GetCorners, tempBrick.GetCorners))
                        {
                            //Check which side of the brick player hit.
                            Vector2[] testPoints = new Vector2[4]
                            {
                                tempBrick.GetMidpoints[1],
                                tempBrick.GetMidpoints[3],
                                tempBrick.GetMidpoints[0],
                                tempBrick.GetMidpoints[2],
                            };

                            for (int k = 0; k < testPoints.Length; ++k)
                            {
                                //Check to see if the player collided with any of the midpoints of the brick
                                if (SurfaceCollision(player.GetSprite.GetBounds, testPoints[k], true))
                                {
                                    //Stop the creation of particles
                                    player.NoParticles = true;

                                    //Check which midpoint was caught to ne colliding with the player
                                    if (k == 2)
                                    {
                                        //Change the players trajectory and remove gravity effects since it hit the top of the brick.
                                        tempBrick.ChangePlayerTraj("top", player);

                                        //If the player is not in the bubble
                                        if (!player.InBubble)
                                        {
                                            player.CancelGravity = true;
                                        }
                                    }
                                    else
                                    {
                                        //Reapply gravity
                                        player.CancelGravity = false;

                                        //Change the player's trajectory or location based on the side that was hit.
                                        if (k == 0)
                                        {
                                            tempBrick.ChangePlayerTraj("right", player);
                                        }
                                        else if (k == 1)
                                        {
                                            // Player.gameState = Player.States.test;
                                            tempBrick.ChangePlayerTraj("left", player);
                                        }
                                        else if (k == 3)
                                        {
                                            tempBrick.ChangePlayerTraj("bottom", player);
                                        }

                                        break;
                                    }
                                }
                            }
                        }
                    }
                    //Reactivate the particles if no specific tile was hit.
                    else
                    {
                        player.NoParticles = false;
                    }
                }
            }
        }