예제 #1
0
        public void Collision(Rectangle newRectangle, int xOffset, int yOffset, Camera camera)
        {
            Vector2 position = camera.getVisualCoords(player.getPosition());
            Rectangle rectangle = new Rectangle((int)position.X, (int)position.Y, 32, 32);

            if (rectangle.TouchTop(newRectangle))
            {
                position.Y = newRectangle.Y - rectangle.Height;

                player.TouchingFloor = true;
                player.CanJump = true;
                player.CanJumpAgain = true;
            }

            if (rectangle.TouchLeft(newRectangle))
            {
                position.X = newRectangle.X - rectangle.Width;
                player.speed.X = -0.05f;

            }
            if (rectangle.TouchRight(newRectangle))
            {
                position.X = newRectangle.X + newRectangle.Width;
                player.speed.X = 0.05f;
            }

            if (rectangle.TouchBottom(newRectangle))
            {
                player.speed.Y = 0.5f;
            }

            if (position.X < 0)
            {
                position.X = 0 + rectangle.Width;
                player.speed.X = 0.05f;
            }

            if (position.X > xOffset - rectangle.Width)
            {
                position.X = xOffset - rectangle.Width;
                player.speed.X = -0.05f;
            }

            // If player falls, set IsAlive to false. Use this later to fix game over etc.
            if (position.Y > yOffset - rectangle.Height)
            {
                player.IsAlive = false;
                position.Y = yOffset - rectangle.Height;
                Console.WriteLine("Player alive: " + player.IsAlive);
            }

            // If player leaves top of a rectangle.
            if(position.X > newRectangle.X + newRectangle.Width)
            {
                player.TouchingFloor = false;
                player.CanJump = false;
            }
        }