public override void Process(Entity entity) { Entity player = (Entity)BlackBoard.GetEntry("Player"); CollisionBox playerBox = player.GetComponent<CollisionBox>(); Position playerPosition = player.GetComponent<Position>(); Rectangle playerRectangle = new Rectangle( (int)playerPosition.X + playerBox.OffsetX, (int)playerPosition.Y + playerBox.OffsetY, playerBox.Width, playerBox.Height); CollisionBox enemyBox = entity.GetComponent<CollisionBox>(); Position enemyPosition = entity.GetComponent<Position>(); Rectangle enemyRectangle = new Rectangle( (int)enemyPosition.X + enemyBox.OffsetX, (int)enemyPosition.Y + enemyBox.OffsetY, enemyBox.Width, enemyBox.Height); var type = playerRectangle.GetCollisionType(enemyRectangle); if (type == CollisionType.None || !entity.HasComponent<Script>()) return; Script script = entity.GetComponent<Script>(); _scriptManager.QueueScript(script.FilePath, entity, "OnTouch"); }
private void ProcessLevelCollisions(Entity entity) { Entity level = _world.EntityManager.GetEntities(Aspect.All(typeof(Map)))[0]; if (level == null) return; Map map = level.GetComponent<Map>(); CollisionBox box = entity.GetComponent<CollisionBox>(); Position position = entity.GetComponent<Position>(); Rectangle bounds = new Rectangle( (int)position.X + box.OffsetX, (int)position.Y + box.OffsetY, box.Width, box.Height ); int leftTile = (int) Math.Floor((float) bounds.Left/TilesetConstants.TileWidth); int rightTile = (int)Math.Ceiling(((float)bounds.Right / TilesetConstants.TileWidth)) - 1; int topTile = (int) Math.Floor((float) bounds.Top/TilesetConstants.TileHeight); int bottomTile = (int) Math.Ceiling(((float) bounds.Bottom/TilesetConstants.TileHeight)) - 1; for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { if (x >= map.Width || x < 0 || y >= map.Height || y < 0) continue; TileData tile = map.Tiles[x, y]; if (tile != null && tile.IsCollidable) { Rectangle tileBounds = new Rectangle( x * TilesetConstants.TileWidth, y * TilesetConstants.TileHeight, TilesetConstants.TileWidth, TilesetConstants.TileHeight ); Vector2 depth = bounds.GetIntersectionDepth(tileBounds); var type = bounds.GetCollisionType(tileBounds); // Entity's bottom collided with top of tile. if (type == CollisionType.Top) { position.Y += depth.Y; position.IsOnGround = true; break; } else if (type == CollisionType.Left) { } else if (type == CollisionType.Right) { } else if (type == CollisionType.Bottom) { } } } } }