public bool HasCollisionBelow(IEntity entity) { Debug.Assert(entity != null); Rect r = entity.CreateCollisionRectangle(); float initialMid = r.Top + r.Height / 2f; r.X -= JUMP_X_TOLERANCE / 2f; r.Width += JUMP_X_TOLERANCE; r.Y += JUMP_Y_TOLERANCE; // move it down a bit var tiles = World.Map.GetTouchedTiles(r); foreach (var tile in tiles) { if (tile.Key.Intersects(r) && // we have a collision (tile.Value == CollisionType.Block || tile.Value == CollisionType.Platform) && // it is a blocking collision tile.Key.Top >= initialMid) { // the block is below us (below our feet) int x = World.Map.GetTileXIndex(tile.Key); int y = World.Map.GetTileYIndex(tile.Key); // If the tile above the current tile is passable, we may jump ! if (World.Map.IsValidXIndex(x) && World.Map.IsValidYIndex(y) && World.Map.GetCollision(x, y - 1) == CollisionType.Passable) return true; } } return false; }
/// <summary> /// Resolves the collisions between an entity and the world it is in. /// </summary> public void UndoCollisions(IEntity entity) { Debug.Assert(entity != null); var collisions = World.GetTouchedObjects(entity.CreateCollisionRectangle()); HandleCollisionGroup(entity, collisions); }
public bool HasCollisions(IEntity entity) { Debug.Assert(entity != null); var rect = entity.CreateCollisionRectangle(); var collisions = World.GetTouchedObjects(rect); foreach (var collision in collisions) { if (collision.Value != CollisionType.Passable && rect.Intersects(collision.Key)) { return true; } } return false; }
/// <summary> /// Handles the collision of an object with multiple entities. /// </summary> void HandleCollisionGroup( IEntity entity, List<KeyValuePair<Rect, CollisionType>> collisions) { Debug.Assert(entity != null); Debug.Assert(collisions != null); foreach (KeyValuePair<Rect, CollisionType> collision in collisions) { // We recreate an entity rectangle on every loop // because multiple collisions might affect the entity in the same frame. Rect entityRect = entity.CreateCollisionRectangle(); Rect rect = collision.Key; CollisionType type = collision.Value; switch (type) { case CollisionType.Block: UndoCollision(entity, entityRect, rect); break; case CollisionType.Passable: break; // do nothing then default: throw new NotImplementedException("Collision type not implemented."); } } }