Remove multiple colliders, add spatial index component

This commit is contained in:
2023-11-17 15:13:30 +01:00
parent 208cdc6b6f
commit 021df6d77a
11 changed files with 122 additions and 95 deletions

View File

@@ -37,30 +37,27 @@ bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile ti
if (tile == BUILDINGS_ROAD)
return false;
}
BzTileCollider collider = bzTileMapGetCollider(map, x, y);
BzTileShape shape = bzTileMapGetCollider(map, x, y);
f32 posX = x * map->tileWidth;
f32 posY = y * map->tileHeight;
for (int i = 0; i < BZ_MAP_COLLIDER_DEPTH; i++) {
BzTileShape shape = collider.shapes[i];
shape.x += posX;
shape.y += posY;
switch (shape.type) {
case BZ_TILE_SHAPE_NONE:
case BZ_TILE_SHAPE_POINT:
break;
case BZ_TILE_SHAPE_RECT: {
Rectangle shapeRec = {shape.x, shape.y, shape.sizeX, shape.sizeY};
if (CheckCollisionRecs(buildArea, shapeRec))
return false;
break;
}
case BZ_TILE_SHAPE_ELLIPSE: {
Vector2 pos = {shape.x, shape.y};
f32 radius = (shape.sizeX + shape.sizeY) * 0.5f;
if (CheckCollisionCircleRec(pos, radius, buildArea))
return false;
break;
}
shape.x += posX;
shape.y += posY;
switch (shape.type) {
case BZ_TILE_SHAPE_NONE:
case BZ_TILE_SHAPE_POINT:
break;
case BZ_TILE_SHAPE_RECT: {
Rectangle shapeRec = {shape.x, shape.y, shape.sizeX, shape.sizeY};
if (CheckCollisionRecs(buildArea, shapeRec))
return false;
break;
}
case BZ_TILE_SHAPE_ELLIPSE: {
Vector2 pos = {shape.x, shape.y};
f32 radius = (shape.sizeX + shape.sizeY) * 0.5f;
if (CheckCollisionCircleRec(pos, radius, buildArea))
return false;
break;
}
}
}
@@ -82,9 +79,9 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
// Create entity
ecs_entity_t e = ecs_new_id(ECS);
ecs_set(ECS, e, TilePosition, {.x=tileX, .y=tileY});
ecs_set(ECS, e, TileSize, {.sizeX=sizeX, .sizeY=sizeY});
ecs_set(ECS, e, Owner, {.playerID=BUILDINGS_PLAYER_RED});
ecs_set(ECS, e, TilePosition, { .x = tileX, .y = tileY });
ecs_set(ECS, e, TileSize, { .sizeX = sizeX, .sizeY = sizeY });
ecs_set(ECS, e, Owner, { .playerID = BUILDINGS_PLAYER_RED });
for (i32 y = tileY; y < tileY + sizeY; y++) {
for (i32 x = tileX; x < tileX + sizeX; x++) {
@@ -92,7 +89,7 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
buildingTile++;
bzTileMapUpdateCollider(map, x, y);
bzTileMapUpdateColliders(map, x, y, 1, 1);
}
buildingTile += buildingTileset->width - sizeX;
}