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

@@ -16,8 +16,8 @@ void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player);
void addEntityToInspected(ecs_entity_t entity, Game *game);
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size));
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size);
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos));
static void iterRemovePaths(ecs_entity_t entity, Position *pos);
void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces);
@@ -305,23 +305,27 @@ void drawPlayerInputUIGround() {
rlSetLineWidth(2.0f);
while (ecs_query_next(&it)) {
Position *pos = ecs_field(&it, Position, 1);
Size *size = ecs_field(&it, Size, 2);
HitBox *hitbox = ecs_field(&it, HitBox , 2);
for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i];
f32 radius = BZ_MIN(size[i].x, size[i].y);
radius *= 0.5f;
f32 radius = BZ_MAX(hitbox[i].width, hitbox[i].height);
radius *= 0.8f;
const f32 lineThickness = 1.0f;
if (ecs_has(ECS, entity, Building)) {
const f32 padding = 2.0f;
Rectangle bounds = {
pos[i].x - size[i].x * 0.5f - padding,
pos[i].y - size[i].y * 0.5f - padding,
size[i].x + padding * 2,
size[i].y + padding * 2,
pos[i].x + hitbox[i].x - padding,
pos[i].y - hitbox[i].y - padding,
hitbox[i].width + padding * 2,
hitbox[i].height + padding * 2,
};
DrawRectangleLinesEx(bounds, lineThickness, GREEN);
} else {
DrawRing(pos[i], radius, radius + lineThickness, 0, 360, 12, GREEN);
Position center = {
pos[i].x + hitbox[i].x + hitbox[i].width * 0.5f,
pos[i].y + hitbox[i].y + hitbox[i].height * 0.5f,
};
DrawRing(center, radius, radius + lineThickness, 0, 360, 12, GREEN);
}
}
}
@@ -367,10 +371,10 @@ ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t
if (!ecs_has_id(ECS, entity, Selectable)) continue;
if (!ecs_has_id(ECS, entity, tag)) continue;
Vector2 pos;
Rectangle bounds;
if (!getEntityBounds(entity, &pos, NULL, &bounds)) continue;
Rectangle hitbox;
if (!getEntityHitBox(entity, &pos, &hitbox)) continue;
if (!CheckCollisionPointRec(point, bounds)) continue;
if (!CheckCollisionPointRec(point, hitbox)) continue;
f32 curDst = Vector2Distance(point, pos);
if (closestDst > curDst) {
@@ -405,10 +409,10 @@ void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player) {
if (owner.player != player) continue;
}
if (!ecs_has_id(ECS, entity, ecs_id(Unit))) continue;
Rectangle bounds;
if (!getEntityBounds(entity, NULL, NULL, &bounds)) continue;
Rectangle hitbox;
if (!getEntityHitBox(entity, NULL, &hitbox)) continue;
if (!CheckCollisionRecs(area, bounds)) continue;
if (!CheckCollisionRecs(area, hitbox)) continue;
ecs_add(ECS, entity, Selected);
}
}
@@ -463,20 +467,19 @@ void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTil
}
}
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size)) {
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos)) {
ecs_iter_t it = ecs_query_iter(ECS, query);
while (ecs_iter_next(&it)) {
Position *pos = ecs_field(&it, Position, 1);
Size *size = ecs_field(&it, Size, 2);
for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i];
fn(entity, pos + i, size + i);
fn(entity, pos + i);
}
}
}
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size) {
static void iterRemovePaths(ecs_entity_t entity, Position *pos) {
ecs_remove(ECS, entity, Path);
}