Add hitboxes

This commit is contained in:
2024-01-28 11:00:32 +01:00
parent 0a4c1fd154
commit a61768e912
15 changed files with 494 additions and 200 deletions

View File

@@ -28,7 +28,7 @@ bool canPlaceBuilding(Game *game, BuildingType type, BzTile tileX, BzTile tileY)
BzSpatialGridIter it = bzSpatialGridIter(game->entityGrid, buildArea.x, buildArea.y, buildArea.width, buildArea.height);
while (bzSpatialGridQueryNext(&it)) { ecs_entity_t entity = *(ecs_entity_t *) it.data;
Rectangle bounds;
if (!getEntityBounds(entity, NULL, NULL, &bounds)) continue;
if (!getEntityHitBox(entity, NULL, &bounds)) continue;
if (CheckCollisionRecs(buildArea, bounds))
return false;
@@ -55,21 +55,26 @@ ecs_entity_t placeBuilding(Game *game, BuildingType type,
.size = (Vec2i) { sizeX, sizeY }
});
Position pos = {
.x = posX * tileWidth + sizeX * tileWidth * 0.5f,
.y = posY * tileHeight + sizeY * tileHeight * 0.5f
.x = posX * tileWidth,
.y = posY * tileHeight
};
Size size = {
.x = sizeX * tileWidth,
.y = sizeY * tileHeight
.y = sizeY * tileHeight,
};
HitBox hitbox = {
.x = 0.0f, .y = 0.0f,
.width = size.x,
.height = size.y
};
ecs_set_ptr(ECS, building, Position, &pos);
ecs_set_ptr(ECS, building, Size, &size);
ecs_set_ptr(ECS, building, HitBox, &hitbox);
ecs_set(ECS, building, Rotation, {0});
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &building,
pos.x - size.x * 0.5f, pos.y - size.y * 0.5f,
size.x, size.y);
pos.x, pos.y, hitbox.width, hitbox.height);
ecs_set_ptr(ECS, building, SpatialGridID, &gridID);
ecs_set(ECS, building, Owner, {player});
BzTileset *tileset = &game->tileset;
@@ -154,11 +159,12 @@ bool canAffordBuilding(BuildingType type, PlayerResources res) {
Vector2 getPositionNearBuilding(ecs_entity_t building, Vector2 fromPos) {
BZ_ASSERT(ecs_is_alive(ECS, building));
BZ_ASSERT(ecs_has(ECS, building, Position));
BZ_ASSERT(ecs_has(ECS, building, Size));
BZ_ASSERT(ecs_has(ECS, building, HitBox));
Vector2 pos = *ecs_get(ECS, building, Position);
Vector2 size = *ecs_get(ECS, building, Size);
HitBox hitbox = *ecs_get(ECS, building, HitBox);
Vector2 size = {hitbox.width, hitbox.height};
size = Vector2SubtractValue(size, 10.0f);
Vector2 dir = Vector2Normalize(Vector2Subtract(fromPos, pos));