std::vector<Polygon2d> GenerateHitboxes(TileSet &tileSet, TileMap &tileMap) { std::vector<Polygon2d> hitboxes; const int tileWidth = tileSet.tileSize.x; const int tileHeight = tileSet.tileSize.y; if(tileSet.IsDirty()) return hitboxes; for(int layer = 0; layer < 3; layer++) { for(int col = 0; col < tileMap.GetColumnsCount(); col++) { for(int row = 0; row < tileMap.GetRowsCount(); row++) { //Note : a hitbox is also added for empty/non-collidable tiles to ease the hitbox update when changing a tile Polygon2d newPolygon; if(tileMap.GetTile(layer, col, row) != -1 && tileSet.IsTileCollidable(tileMap.GetTile(layer, col, row))) { newPolygon = tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).hitbox; } newPolygon.Move(col * tileWidth, row * tileHeight); hitboxes.push_back(newPolygon); } } } return hitboxes; }
void UpdateHitboxes(std::vector<Polygon2d> &polygons, sf::Vector2f position, int layer, int col, int row, TileSet &tileSet, TileMap &tileMap) { if(tileSet.IsDirty()) return; const int vertexPos = layer * tileMap.GetColumnsCount() * tileMap.GetRowsCount() + col * tileMap.GetRowsCount() + row; const int tileWidth = tileSet.tileSize.x; const int tileHeight = tileSet.tileSize.y; if(tileMap.GetTile(layer, col, row) != -1 && tileSet.IsTileCollidable(tileMap.GetTile(layer, col, row))) { polygons[vertexPos] = tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).hitbox; } else { polygons[vertexPos] = Polygon2d(); } polygons[vertexPos].Move(position.x + col * tileWidth, position.y + row * tileHeight); }