/// <summary> /// test for player collision with a wall object /// </summary> /// <param name="wall">wall object to test</param> /// <returns>true if collision</returns> private bool WallCollision(Wall wall) { bool wallCollision = false; // create a Rectangle object for the new move's position Rectangle newPlayerPosition = player.BoundingRectangle; // test the new move's position for a collision with the wall switch (player.PlayerDirection) { case Player.Direction.Left: // set the position of the new move's rectangle newPlayerPosition.Offset(-player.SpeedHorizontal, 0); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(wall.BoundingRectangle.Right, player.Position.Y); } break; case Player.Direction.Right: // set the position of the new move's rectangle newPlayerPosition.Offset(player.SpeedHorizontal, 0); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(wall.BoundingRectangle.Left - player.BoundingRectangle.Width, player.Position.Y); } break; case Player.Direction.Up: // set the position of the new move's rectangle newPlayerPosition.Offset(0, -player.SpeedVertical); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(player.Position.X, wall.BoundingRectangle.Bottom); } break; case Player.Direction.Down: // set the position of the new move's rectangle newPlayerPosition.Offset(0, player.SpeedVertical); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(player.Position.X, wall.BoundingRectangle.Top - player.BoundingRectangle.Height); } break; default: break; } return wallCollision; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // add floors, walls, and ceilings walls = new List<Wall>(); wall01 = new Wall(Content, "wall", new Vector2(4 * CELL_WIDTH, 4 * CELL_HEIGHT)); wall01.Active = true; walls.Add(wall01); // add the player player = new Player(Content, new Vector2(2 * CELL_WIDTH, 2 * CELL_HEIGHT)); player.Active = true; // set the player's initial speed player.SpeedHorizontal = 10; player.SpeedVertical = 10; base.Initialize(); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // add floors, walls, and ceilings wall01 = new Wall(Content, "wall", new Vector2(0, WINDOW_HEIGHT - CELL_HEIGHT)); wall01.Active = true; wall02 = new Wall(Content, "wall", new Vector2(WINDOW_WIDTH - CELL_WIDTH, WINDOW_HEIGHT - CELL_HEIGHT)); wall02.Active = true; // add the player player = new Player(Content, new Vector2(CELL_WIDTH * 2, WINDOW_HEIGHT - CELL_HEIGHT)); player.Active = true; // set the player's initial speed player.SpeedHorizontal = 5; player.SpeedVertical = 5; base.Initialize(); }
/// <summary> /// test for player collision with a wall object /// </summary> /// <param name="wall">wall object to test</param> /// <returns>true if collision</returns> private bool PlayerHitWall(Wall wall) { return player.BoundingRectangle.Intersects(wall.BoundingRectangle); }
/// <summary> /// test for player collision with a wall object /// </summary> /// <param name="wall">wall object to test</param> /// <returns>true if collision</returns> private bool WallCollision(Wall wall) { bool wallCollision = false; // create a Rectangle object for the new move's position Rectangle newPlayerPosition = player.BoundingRectangle; // test the new move's position for a collision with the wall switch (player.PlayerDirection) { case Player.Direction.Left: // set the position of the new move's rectangle newPlayerPosition.Offset(-player.SpeedHorizontal, 0); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(wall.BoundingRectangle.Right, player.Position.Y); } break; case Player.Direction.Right: // set the position of the new move's rectangle newPlayerPosition.Offset(player.SpeedHorizontal, 0); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(wall.BoundingRectangle.Left - player.BoundingRectangle.Width, player.Position.Y); } break; case Player.Direction.Up: // set the position of the new move's rectangle newPlayerPosition.Offset(0, -player.SpeedVertical); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(player.Position.X, wall.BoundingRectangle.Bottom); } break; case Player.Direction.Down: // set the position of the new move's rectangle newPlayerPosition.Offset(0, player.SpeedVertical); // test for a collision with the new move and the wall if (newPlayerPosition.Intersects(wall.BoundingRectangle)) { wallCollision = true; // move player next to wall player.Position = new Vector2(player.Position.X, wall.BoundingRectangle.Top - player.BoundingRectangle.Height); } break; default: break; } return(wallCollision); }