public virtual void HandleCollisions(CVGameTime gameTime) { if (this.WorldPosition.Y > Game.iScreenModelWidth && IsDying == false) { Die(gameTime); } Rectangle actorbounds = this.BoundingBox(); bool wasonground = this.IsOnGround; // get nearest tile below player. this.IsOnGround = false; int leftTile = (int)Math.Floor((float)actorbounds.Left / CurrentStage.TileWidth); int rightTile = (int)Math.Ceiling(((float)actorbounds.Right / CurrentStage.TileWidth)) - 1; int topTile = (int)Math.Floor((float)actorbounds.Top / CurrentStage.TileHeight); int bottomTile = (int)Math.Ceiling(((float)actorbounds.Bottom / CurrentStage.TileHeight)) - 1; // For each potentially colliding platform tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { StageTile stageTile = CurrentStage.getStageTileByGridPosition(x, y); if (stageTile != null) { if (stageTile.IsImpassable()) { Rectangle tilebounds = CurrentStage.getTileBoundsByGridPosition(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(actorbounds, tilebounds); if (actorbounds.Intersects(tilebounds)) { WorldPosition = new Vector2(WorldPosition.X + depth.X, WorldPosition.Y); actorbounds = this.BoundingBox(); } } else if (stageTile.IsPlatform() && y == bottomTile) { List <Platform> tileboundsList = CurrentStage.getTilePlatformBoundsByGridPosition(x, bottomTile); foreach (Platform platformbounds in tileboundsList) { Rectangle tilebounds = platformbounds.PlatformBounds; Vector2 depth = RectangleExtensions.GetIntersectionDepth(actorbounds, tilebounds); if (this.PreviousBottom <= tilebounds.Top && Velocity.Y >= 0 && actorbounds.Intersects(tilebounds)) //if (Velocity.Y >= 0 && (depth.Y < 0)) // || this.IgnoreNextPlatform)) { this.IsOnGround = true; this.JumpInProgress = false; //this.Velocity.X = 0f; this.WorldPosition.Y += depth.Y; // perform further collisions with the new bounds actorbounds = this.BoundingBox(); } } } } } } if (wasonground && !IsOnGround) { Velocity.Y = 0; } this.PreviousBottom = actorbounds.Bottom; }
public override void Move(CVGameTime gameTime) { Vector2 proposedPosition; if (Velocity.X > 0) { proposedPosition = new Vector2(WorldPosition.X + BoundingBox().Width * 0.9f, WorldPosition.Y); } else if (Velocity.X < 0) { proposedPosition = new Vector2(WorldPosition.X - BoundingBox().Width * 0.9f, WorldPosition.Y); } else { proposedPosition = WorldPosition; } // simulate gravity to determine if proposed position would collide with platform tile or not. // otherwise proposedPosition.Y += 1; Rectangle proposedbounds = this.BoundingBox(proposedPosition); bool bChangeDirection = false; bool bWouldBeOnGround = false; int leftTile = (int)Math.Floor((float)proposedbounds.Left / CurrentStage.TileWidth); int rightTile = (int)Math.Ceiling(((float)proposedbounds.Right / CurrentStage.TileWidth)) - 1; int topTile = (int)Math.Floor((float)proposedbounds.Top / CurrentStage.TileHeight); int bottomTile = (int)Math.Ceiling(((float)proposedbounds.Bottom / CurrentStage.TileHeight)) - 1; // For each potentially colliding platform tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { StageTile stageTile = CurrentStage.getStageTileByGridPosition(x, y); if (stageTile != null) { if (stageTile.IsImpassable()) { // bump into wall- reverse direction Rectangle tilebounds = CurrentStage.getTileBoundsByGridPosition(x, y); if (proposedbounds.Intersects(tilebounds)) { bChangeDirection = true; } } if (stageTile.IsPlatform() && y == bottomTile) { List <Platform> tileboundsList = CurrentStage.getTilePlatformBoundsByGridPosition(x, bottomTile); foreach (Platform platformbounds in tileboundsList) { Rectangle tilebounds = platformbounds.PlatformBounds; if (proposedbounds.Bottom >= tilebounds.Top && proposedbounds.Intersects(tilebounds)) { bWouldBeOnGround = true; } } } } } } if (bChangeDirection) { ChangeDirection(); } else if (bWouldBeOnGround == false) { ChangeDirection(); } else if (this.direction == Player.PlayerDirection.Left) { Velocity.X = -EnemyMoveSpeed; } else if (this.direction == Player.PlayerDirection.Right) { Velocity.X = EnemyMoveSpeed; } }