/// <summary> /// Paces back and forth along a platform, waiting at either end. /// </summary> public virtual void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; // Calculate tile position based on the side we are walking towards. float posX = Position.X + localBounds.Width / 2 * (int)direction; int tileX = (int)Math.Floor(posX / Tile.Width) - (int)direction; int tileY = (int)Math.Floor(Position.Y / Tile.Height); //if (waitTime > 0) //{ // // Wait for some amount of time. // waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds); // if (waitTime <= 0.0f) // { // // Then turn around. // direction = (FaceDirection)(-(int)direction); // } //} //else //{ // If we are about to run into a wall or off a cliff, start waiting. if (Level.GetCollision(tileX + (int)direction, tileY - 1) == TileCollision.Impassable || Level.GetCollision(tileX + (int)direction, tileY) == TileCollision.Passable) { if (state == State.DeadSpinning) { state = State.Dead; } else if (direction == FaceDirection.Left) { direction = FaceDirection.Right; } else { direction = FaceDirection.Left; } } else { // Move in the current direction. Vector2 Velocity = new Vector2((int)direction * velocity * elapsed, 0.0f); position += Velocity; } //} }
/// <summary> /// Detects and resolves all collisions between the collectable and the neighboring /// tiles. When a collision is detected, the collectable 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 collectable's bounding rectangle and find neighboring tiles. Rectangle bounds = BoundingRectangle; 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 / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // Reset flag to search for ground collision. isOnGround = false; // 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) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); // Resolve the collision along the shallow axis. if (absDepthY < absDepthX || collision == TileCollision.Platform) { // If we crossed the top of a tile, we are on the ground. if (previousBottom <= tileBounds.Top) { isOnGround = true; } // Ignore platforms, unless we are on the ground. if (collision == TileCollision.Impassable || isOnGround) { // Resolve the collision along the Y axis. Position = new Vector2(Position.X, Position.Y + depth.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } else if (collision == TileCollision.Impassable) // Ignore platforms. { // Resolve the collision along the X axis. Position = new Vector2(Position.X + depth.X, Position.Y); movement = -movement; // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } } } } // Save the new bounds bottom. previousBottom = bounds.Bottom; }