/// <summary> /// Detects and resolves all collisions between the player and his neighboring /// tiles. When a collision is detected, the player is pushed away along one /// axis to prevent overlapping. There is some special logic for the Y axis to /// handle platforms which behave differently depending on direction of movement. /// </summary> private void HandleCollisions() { // Get the player's bounding rectangle and find neighboring tiles. Rectangle bounds = BoundingRectangle; bounds.X -= offSetX; bounds.Y -= offSetY; int leftTile = (int)Math.Floor((float)(bounds.Left) / Tile.Width); int rightTile = (int)Math.Ceiling(((float)(bounds.Right) / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)(bounds.Top) / ((float)Tile.Height * ((float)SCREENHEIGHT / 1200f))); int bottomTile = (int)Math.Ceiling(((float)(bounds.Bottom) / ((float)Tile.Height * ((float)SCREENHEIGHT / 1200f)))) - 1; bounds = BoundingRectangle; // Reset flag to search for ground collision. isOnGround = true; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { // If this tile is collidable, TileCollision collision = Level.GetCollision(x, y); if (collision != TileCollision.Passable) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = Level.GetBounds(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds); if (depth != Vector2.Zero) { colliding = true; } else { colliding = false; } } } } }
/// <summary> /// Update the position and direction based on input from BCI2000 /// </summary> void UpdateFromBCI20002(TimeSpan elapsedTime) { AutoMovePlayer2(0, elapsedTime); //Check to see if a new command is available. if ((bool)game.Services.GetService(typeof(bool)) == true) { int Val = (int)game.Services.GetService(typeof(int)); //update based on commnad switch (Val) { case 0: //Dont move isMoving = false; break; case 1: //move up if (!collisionDetect(x, y - 1) && movementComplete) { isMoving = true; y -= 1; newPosition = RectangleExtensions.GetBottomCenter(Level.GetBounds(x, y)); AutoMovePlayer2(1, elapsedTime); } break; case 2: //move down if (!collisionDetect(x, y + 1) && movementComplete) { isMoving = true; y += 1; newPosition = RectangleExtensions.GetBottomCenter(Level.GetBounds(x, y)); AutoMovePlayer2(2, elapsedTime); } break; case 3: //move right if (!collisionDetect(x + 1, y) && movementComplete) { isMoving = true; x += 1; newPosition = RectangleExtensions.GetBottomCenter(Level.GetBounds(x, y)); AutoMovePlayer2(3, elapsedTime); } break; case 4: //move left if (!collisionDetect(x - 1, y) && movementComplete) { isMoving = true; x -= 1; newPosition = RectangleExtensions.GetBottomCenter(Level.GetBounds(x, y)); AutoMovePlayer2(4, elapsedTime); } break; case 99: isMoving = false; break; default: AutoMovePlayer(0, elapsedTime); break; } //set the new command status to false game.Services.RemoveService(typeof(bool)); game.Services.AddService(typeof(bool), false); } }