Partial tower implementation

This commit is contained in:
2024-02-09 15:46:53 +01:00
parent e9b9c68f6b
commit bc7da3c7a3
8 changed files with 171 additions and 3 deletions

View File

@@ -310,6 +310,75 @@ void updateBuildingRecruitment(ecs_iter_t *it) {
}
}
void updateTower(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Owner *owner = ecs_field(it, Owner, 1);
Tower *tower = ecs_field(it, Tower, 2);
Position *position = ecs_field(it, Position, 3);
Size *size = ecs_field(it, Size, 4);
f32 dt = GetFrameTime();
for (i32 i = 0; i < it->count; i++) {
tower[i].fireElapsed += dt;
if (tower[i].fireElapsed < tower[i].fireCooldown)
continue;
Vector2 center = {
position[i].x + size[i].x * 0.5f,
position[i].y - size[i].y * 0.5f,
};
f32 range = tower[i].range;
Rectangle bounds = {
center.x - range,
center.y - range,
range * 2,
range * 2,
};
BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid,
bounds.x, bounds.y,
bounds.width, bounds.height);
ecs_entity_t target = 0;
Vector2 targetPos = Vector2Zero();
f32 targetDst = INFINITY;
while (bzSpatialGridQueryNext(&spatialIt)) {
ecs_entity_t other = *(ecs_entity_t *) spatialIt.data;
if (!ecs_has(ECS, other, Owner))
continue;
Owner otherOwner = *ecs_get(ECS, other, Owner);
if (owner[i].player == otherOwner.player)
continue;
if (other == it->entities[i]) continue;
Position otherPos;
Rectangle otherBounds;
if (!entityGetHitBox(other, &otherPos, &otherBounds))
continue;
Vector2 otherCenter = {
otherBounds.x + otherBounds.width * 0.5f,
otherBounds.y - otherBounds.height * 0.5f
};
f32 dst = Vector2Distance(center, otherCenter);
if (dst > range)
continue;
if (targetDst == INFINITY || dst < targetDst) {
target = other;
targetPos = otherCenter;
targetDst = dst;
}
}
if (target == 0) continue;
tower[i].fireElapsed = 0.0f;
bzLogInfo("FIRE!");
}
}
void renderHealthBar(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
HitBox *hitbox = ecs_field(it, HitBox, 2);