/// <summary> /// Will check if the given character has collided with a tile /// in the level and then handle the position correction. /// ENSURE: the given character will not be colliding with any tiles. /// </summary> private void checkTileCollision(Character.Character character) { //for every tile in the level foreach (Tile tile in this.level_tiles) { //save the character's bounding box Rectangle character_bounds = character.BoundingBox(); //now get the character's current velocity Vector2 character_velocity = character.currentSpeed(); //compute the future position of the character Rectangle future_bounds = new Rectangle( character_bounds.X + (int)character_velocity.X, character_bounds.Y + (int)character_velocity.Y, character_bounds.Width, character_bounds.Height); //check if the character WILL collide with this tile if (tile.collidesWith(future_bounds)){ //find how much overlap is occuring Rectangle intersection = Rectangle.Intersect(tile.destinationRectangle, future_bounds); //alter the characters velocity to take the collision into account Vector2 offset = new Vector2(); //if the character has horizontal velocity //and isn't jumping or falling if ((character.isJumping() == false && character.isFalling() == false) && character_velocity.X != 0) { //compute the horizontal offset offset.X = (character_velocity.X > 0) ? (intersection.X - intersection.Right) : //negative offset if positive velocity (intersection.Right - intersection.X); //positive offset if negative velocity //add the offset to the velocity value //character_velocity.X = character_velocity.X + offset.X; } //if the character has vertical velocity if (character_velocity.Y != 0) { //they are jumping or falling into this object //first fix their vertical forces //if the character has a positive vertical velocity if (character_velocity.Y > 0) { //then the offset needs to be negative offset.Y = (intersection.Y - intersection.Bottom) -1; //this also means the character has hit ground //character.ground(); } //otherwise, the velocity is negative else { //so the offset needs to be positive offset.Y = (intersection.Bottom - intersection.Y); //the character hit the bottom of something, fall character.fall(); } //now they might have horizontal forces, //before we fix them, see if the character is no longer colliding //compute the future position of the character Rectangle new_future_bounds = new Rectangle( future_bounds.X + (int)offset.X, future_bounds.Y + (int)offset.Y, future_bounds.Width, future_bounds.Height); if (tile.collidesWith(new_future_bounds)) { //then fix the horizontal forces Rectangle new_intersection = Rectangle.Intersect(tile.destinationRectangle, new_future_bounds); //compute the horizontal offset offset.X = (character_velocity.X > 0) ? (intersection.X - intersection.Right) : //negative offset if positive velocity (intersection.Right - intersection.X); //positive offset if negative velocity } } character_velocity.X = character_velocity.X + offset.X; character_velocity.Y = character_velocity.Y + offset.Y; //give the player it's new position character.setVelocity(character_velocity); } } }
/// <summary> /// Returns whether or not the tile is under the given character. /// ENSURE: if the tile is immidiately below the character, /// return true /// otherwise, /// return false /// </summary> internal bool isUnder(Character.Character character) { bool result = false; Rectangle box = character.BoundingBox(); //if the box is within the correct horizontal scope if ((box.Left <= this.destinationRectangle.Right && box.Left >= this.destinationRectangle.Left) || (box.Right <= this.destinationRectangle.Right && box.Right >= this.destinationRectangle.Left)) { //if this tile is immidiate below the character if (box.Bottom < this.destinationRectangle.Top && box.Bottom > (this.destinationRectangle.Top - 5)) { result = true; } } return result; }
/// <summary> /// Will check if the given character has collided with another character. /// If so, the opposing character will be removed from play. /// </summary> private void checkCharacterCollision(Character.Character character) { //for every enemy alive foreach (Character.Enemy enemy in this.character_manager.enemyList()) { //if the enemy is alive if (enemy.isAlive() == true) { //check if it is overlapping with the given character if (enemy.BoundingBox().Intersects(character.BoundingBox())) { //then remove the enemy from play enemy.kill(); } } } }