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);

View File

@@ -90,6 +90,16 @@ ECS_MOVE(Building, dst, src, {
*src = (Building) {.type = 0};
})
ECS_DTOR(AttachedEntity, attacked, {
if (ecs_is_alive(ECS, attacked->entity))
ecs_delete(ECS, attacked->entity);
attacked->entity = 0;
})
ECS_MOVE(AttachedEntity, dst, src, {
*dst = *src;
src->entity = 0;
})
void delayDeleteUpdate(ecs_iter_t *it) {
DelayDelete *delay = ecs_field(it, DelayDelete, 1);
@@ -117,6 +127,10 @@ void setupSystems() {
.dtor = ecs_dtor(Building),
.move_dtor = ecs_move(Building)
});
ecs_set_hooks(ECS, AttachedEntity, {
.dtor = ecs_dtor(AttachedEntity),
.move_dtor = ecs_move(AttachedEntity)
});
ECS_OBSERVER(ECS, entityPathRemove, EcsOnRemove, Path);
@@ -140,6 +154,8 @@ void setupSystems() {
ECS_SYSTEM(ECS, resetHarvestCount, EcsOnUpdate, Harvestable);
ECS_SYSTEM(ECS, updateAISystem, EcsOnUpdate, BzBTState);
ECS_SYSTEM(ECS, updateTower, EcsOnUpdate, Owner, Tower, Position, Size);
ECS_SYSTEM(ECS, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateAnimation, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateEasingSystem, EcsOnUpdate, Easing, Position, HitBox, Rotation);

View File

@@ -157,6 +157,15 @@ void entityFollowPath(ecs_iter_t *it);
*/
void updateBuildingRecruitment(ecs_iter_t *it);
/*
* 0: Game (singleton for querying entities)
* 1: Owner
* 2: Tower
* 3: Position
* 4: Size
*/
void updateTower(ecs_iter_t *it);
/*
* 1: Position
* 2: HitBox