Add tower firing

This commit is contained in:
2024-02-10 11:25:02 +01:00
parent e654c47446
commit 1c6fab51c6
6 changed files with 119 additions and 12 deletions

View File

@@ -337,8 +337,7 @@ void updateTower(ecs_iter_t *it) {
Rectangle bounds = {
center.x - range,
center.y - range,
range * 2,
range * 2,
range * 2.0f, range * 2.0f
};
BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid,
bounds.x, bounds.y,
@@ -361,7 +360,7 @@ void updateTower(ecs_iter_t *it) {
continue;
Vector2 otherCenter = {
otherBounds.x + otherBounds.width * 0.5f,
otherBounds.y - otherBounds.height * 0.5f
otherBounds.y + otherBounds.height * 0.5f
};
f32 dst = Vector2Distance(center, otherCenter);
if (dst > range)
@@ -375,14 +374,96 @@ void updateTower(ecs_iter_t *it) {
if (target == 0) continue;
Vector2 dir = Vector2Subtract(targetPos, center);
dir = Vector2Normalize(dir);
dir = Vector2Scale(dir, tower[i].projectileSpeed);
ecs_entity_t proj = entityCreateEmpty();
ecs_set(ECS, proj, Position, { center.x, center.y });
ecs_set(ECS, proj, Velocity, { dir.x, dir.y });
ecs_set(ECS, proj, Projectile, {
.damage = randFloatRange(tower[i].minDamage, tower[i].maxDamage),
.target = targetPos,
.radius = tower[i].projectileRadius,
.damageCount = tower[i].projectileDamageCount
});
ecs_set(ECS, proj, Owner, { owner[i].player });
tower[i].fireElapsed = 0.0f;
bzLogInfo("FIRE!");
}
}
void updateProjectile(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Owner *owner = ecs_field(it, Owner, 1);
Projectile *proj = ecs_field(it, Projectile, 2);
Position *pos = ecs_field(it, Position, 3);
Velocity *vel = ecs_field(it, Velocity, 4);
f32 dt = GetFrameTime();
for (i32 i = 0; i < it->count; i++) {
const f32 radius = proj[i].radius;
const f32 hRadius = radius * 0.5f;
const Rectangle bounds = {
pos[i].x - hRadius,
pos[i].y + hRadius,
radius, radius
};
BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid,
bounds.x, bounds.y,
bounds.width, bounds.height);
while (bzSpatialGridQueryNext(&spatialIt)) {
ecs_entity_t other = *(ecs_entity_t *) spatialIt.data;
if (proj[i].lastDamaged == other)
continue;
if (!ecs_has(ECS, other, Owner))
continue;
Owner otherOwner = *ecs_get(ECS, other, Owner);
if (otherOwner.player == owner[i].player)
continue;
if (!ecs_has(ECS, other, Health))
continue;
Vector2 otherPos;
Rectangle otherHB;
if (!entityGetHitBox(other, &otherPos, &otherHB))
continue;
if (!CheckCollisionRecs(bounds, otherHB))
continue;
damageEvent(other, (DamageEvent) {
.amount = proj[i].damage,
.hitbox = otherHB
});
proj[i].lastDamaged = other;
proj[i].damageCount--;
break;
}
if (proj[i].damageCount <= 0) {
ecs_delete(ECS, it->entities[i]);
continue;
}
DrawRectangleV((Vector2) {bounds.x, bounds.y}, (Vector2) {bounds.width, bounds.height},
RED);
pos[i] = Vector2Add(pos[i], Vector2Scale(vel[i], dt));
if (pos[i].x < 0.0f || pos[i].y < 0.0f ||
pos[i].x >= game->map.width * game->map.tileWidth ||
pos[i].y >= game->map.height * game->map.tileHeight) {
ecs_delete(ECS, it->entities[i]);
}
}
}
void renderHealthBar(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);