672 lines
24 KiB
C
672 lines
24 KiB
C
#include "systems.h"
|
|
|
|
|
|
#include "../game_state.h"
|
|
#include "../input.h"
|
|
#include "../pathfinding.h"
|
|
#include "../entity_factory.h"
|
|
#include "../utils.h"
|
|
|
|
#include <math.h>
|
|
#include <raymath.h>
|
|
|
|
bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game) {
|
|
const Vector2 *pPath = ecs_get(ECS, entity, Position);
|
|
BZ_ASSERT(pPath);
|
|
const Vector2 start = *pPath;
|
|
|
|
Path path = {NULL, 0};
|
|
const bool foundPath = pathfindAStar(&(PathfindingDesc) {
|
|
.start = start,
|
|
.target = target,
|
|
.map = &game->map,
|
|
.outPath = &path,
|
|
.pool = game->pools.pathData,
|
|
.alloc = &game->stackAlloc,
|
|
});
|
|
if (foundPath) {
|
|
BZ_ASSERT(path.paths);
|
|
ecs_set_ptr(ECS, entity, Path, &path);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
void entityPathRemove(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
ecs_entity_t entity = it->entities[i];
|
|
ecs_remove(ECS, entity, TargetPosition);
|
|
}
|
|
}
|
|
|
|
void updateTextureOwnerTile(ecs_iter_t *it) {
|
|
TextureRegion *tex = ecs_field(it, TextureRegion, 1);
|
|
Owner *owner = ecs_field(it, Owner, 2);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Rectangle rec = tex[i].rec;
|
|
BzTileID base = ((int)rec.y / 16) * 256 + ((int) rec.x / 16);
|
|
Vector2 offset = getTileOffset(base, owner[i].player);
|
|
tex[i].rec.x += offset.x;
|
|
tex[i].rec.y += offset.y;
|
|
}
|
|
}
|
|
void buildingAddPopCapacity(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
AddPopCapacity *addPop = ecs_field(it, AddPopCapacity, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Player player = ecs_get(ECS, it->entities[i], Owner)->player;
|
|
PlayerResources *res = &game->playerResources[player];
|
|
res->popCapacity += addPop[i].amount;
|
|
}
|
|
}
|
|
void buildingRemovePopCapacity(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
AddPopCapacity *addPop = ecs_field(it, AddPopCapacity, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Player player = ecs_get(ECS, it->entities[i], Owner)->player;
|
|
PlayerResources *res = &game->playerResources[player];
|
|
res->popCapacity -= addPop[i].amount;
|
|
}
|
|
}
|
|
void entityConsumePopCapacity(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
ConsumePopCapacity *pop = ecs_field(it, ConsumePopCapacity, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Player player = ecs_get(ECS, it->entities[i], Owner)->player;
|
|
PlayerResources *res = &game->playerResources[player];
|
|
res->pop += pop[i].amount;
|
|
}
|
|
}
|
|
void entityUnConsumePopCapacity(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
ConsumePopCapacity *pop = ecs_field(it, ConsumePopCapacity, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Player player = ecs_get(ECS, it->entities[i], Owner)->player;
|
|
PlayerResources *res = &game->playerResources[player];
|
|
res->pop -= pop[i].amount;
|
|
}
|
|
}
|
|
|
|
void entityUpdateSpatialID(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
Position *position = ecs_field(it, Position, 1);
|
|
HitBox *hitbox = ecs_field(it, HitBox, 2);
|
|
Velocity *velocity = ecs_field(it, Velocity, 3);
|
|
SpatialGridID *id = ecs_field(it, SpatialGridID, 4);
|
|
BZ_UNUSED(velocity);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
HitBox hb = entityTransformHitBox(position[i], hitbox[i]);
|
|
bzSpatialGridUpdate(game->entityGrid, id[i], hb.x, hb.y, hb.width, hb.height);
|
|
}
|
|
|
|
}
|
|
|
|
void entityUpdateKinematic(ecs_iter_t *it) {
|
|
Position *position = ecs_field(it, Position, 1);
|
|
Velocity *velocity = ecs_field(it, Velocity, 2);
|
|
Steering *steering = ecs_field(it, Steering, 3);
|
|
Unit *unit = ecs_field(it, Unit, 4);
|
|
|
|
f32 dt = it->delta_time;
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
|
|
steering[i] = Vector2Normalize(steering[i]);
|
|
// velocity += steering * dt
|
|
Vector2 accel = Vector2Scale(steering[i], dt);
|
|
accel = Vector2Scale(accel, unit[i].acceleration);
|
|
velocity[i] = Vector2Add(velocity[i], accel);
|
|
|
|
// Apply deceleration
|
|
if (Vector2LengthSqr(steering[i]) == 0) {
|
|
// velocity *= (1.0 - decel)
|
|
velocity[i] = Vector2Scale(velocity[i], 1.0 - unit[i].deceleration);
|
|
}
|
|
|
|
// Reset steering
|
|
steering[i] = Vector2Zero();
|
|
|
|
// Check for speeding and clip
|
|
const f32 maxSpeed = unit[i].maxSpeed;
|
|
if (Vector2Length(velocity[i]) > maxSpeed) {
|
|
velocity[i] = Vector2Normalize(velocity[i]);
|
|
velocity[i] = Vector2Scale(velocity[i], maxSpeed);
|
|
}
|
|
|
|
// position += velocity * dt
|
|
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
|
}
|
|
}
|
|
void entityUpdate(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
Position *position = ecs_field(it, Position, 1);
|
|
HitBox *hitbox = ecs_field(it, HitBox, 2);
|
|
Velocity *velocity = ecs_field(it, Velocity, 3);
|
|
Unit *unit = ecs_field(it, Unit, 4);
|
|
Owner *owner = ecs_field(it, Owner, 5);
|
|
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 6);
|
|
|
|
f32 dt = it->delta_time;
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
// Attack thingies
|
|
unit[i].attackElapsed += dt;
|
|
bool canAttack = unit[i].attackElapsed > unit[i].attackCooldown;
|
|
|
|
// Only update "stationary" entities
|
|
bool stationary = Vector2Length(velocity[i]) <= 0.2f;
|
|
Rectangle bounds = entityTransformHitBox(position[i], hitbox[i]);
|
|
BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid, bounds.x, bounds.y,
|
|
bounds.width, bounds.height);
|
|
Vector2 dir = Vector2Zero();
|
|
f32 slowDown = 0.0f;
|
|
while (bzSpatialGridQueryNext(&spatialIt)) {
|
|
ecs_entity_t other = *(ecs_entity_t *) spatialIt.data;
|
|
if (other == it->entities[i])
|
|
continue;
|
|
Position otherPos;
|
|
Rectangle otherBounds;
|
|
if (!entityGetHitBox(other, &otherPos, &otherBounds))
|
|
continue;
|
|
|
|
if (!CheckCollisionRecs(bounds, otherBounds)) {
|
|
continue;
|
|
}
|
|
|
|
// Physics update
|
|
slowDown += 0.2f;
|
|
Position dif = Vector2Subtract(otherPos, position[i]);
|
|
dir = Vector2Add(dir, dif);
|
|
|
|
// Attack update
|
|
if (canAttack && ecs_has(ECS, other, Health) && ecs_has(ECS, other, Owner)) {
|
|
Player otherPlayer = ecs_get(ECS, other, Owner)->player;
|
|
|
|
if (otherPlayer != owner[i].player) {
|
|
Rectangle collisionRec = GetCollisionRec(bounds, otherBounds);
|
|
f32 percentageCovered = (collisionRec.width * collisionRec.height) / (bounds.width * bounds.height);
|
|
|
|
f32 dealDmg = randFloatRange(unit[i].minDamage, unit[i].maxDamage);
|
|
f32 multiplier = 1.0f + percentageCovered;
|
|
multiplier = Clamp(multiplier, 0.8f, 1.6f);
|
|
|
|
dealDmg *= multiplier;
|
|
|
|
damageEvent(other, (DamageEvent) {
|
|
.hitbox = otherBounds,
|
|
.amount = dealDmg
|
|
});
|
|
|
|
canAttack = false;
|
|
unit[i].attackElapsed = 0.0f;
|
|
if (ecs_has(ECS, other, Velocity)) {
|
|
Velocity *otherVelocity = ecs_get_mut(ECS, other, Velocity);
|
|
*otherVelocity = Vector2Add(*otherVelocity, Vector2Scale(Vector2Normalize(dif), 4000 * dt));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stationary) {
|
|
dir = Vector2Normalize(dir);
|
|
dir = Vector2Scale(dir, 4000 * dt);
|
|
velocity[i] = Vector2Subtract(velocity[i], dir);
|
|
}
|
|
|
|
slowDown = BZ_MIN(slowDown, 0.65f);
|
|
if (!stationary && slowDown > 0.0f) {
|
|
f32 speed = Vector2Length(velocity[i]);
|
|
slowDown = 1 - slowDown;
|
|
f32 maxSpeed = unit->maxSpeed * slowDown;
|
|
|
|
if (speed > maxSpeed) {
|
|
Vector2 norm = Vector2Normalize(velocity[i]);
|
|
velocity[i] = Vector2Scale(norm, maxSpeed);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void entityMoveToTarget(ecs_iter_t *it) {
|
|
Position *position = ecs_field(it, Position, 1);
|
|
HitBox *hitbox = ecs_field(it, HitBox, 2);
|
|
Velocity *velocity = ecs_field(it, Velocity, 3);
|
|
|
|
TargetPosition *targetPos = ecs_field(it, TargetPosition, 4);
|
|
Steering *steering = ecs_field(it, Steering, 5);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Position target = targetPos[i];
|
|
Vector2 center = entityGetCenter(position[i], hitbox[i]);
|
|
steering[i] = Vector2Subtract(target, center);
|
|
f32 dst = Vector2LengthSqr(steering[i]);
|
|
|
|
if (dst < 2.0f) {
|
|
// Arrived
|
|
ecs_remove(ECS, it->entities[i], TargetPosition);
|
|
}
|
|
}
|
|
}
|
|
|
|
void entityMoveSwarm(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
Position *pos = ecs_field(it, Position, 1);
|
|
Velocity *vel = ecs_field(it, Velocity, 2);
|
|
HitBox *hb = ecs_field(it, HitBox, 3);
|
|
Swarm *swarm = ecs_field(it, Swarm, 4);
|
|
Owner *owner = ecs_field(it, Owner, 5);
|
|
Steering *steer = ecs_field(it, Steering, 6);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
|
|
// Vector2 align = Vector2Zero(); // Alignment (match velocity)
|
|
Vector2 avoid = Vector2Zero(); // Separation
|
|
Vector2 target = Vector2Zero();
|
|
Vector2 cohesion = Vector2Zero(); // Cohesion (move towards center)
|
|
|
|
const f32 FRIEND_RADIUS = 22.0f;
|
|
const f32 ENEMY_RADIUS = 52.0f;
|
|
|
|
const Vector2 center = entityGetCenter(pos[i], hb[i]);
|
|
|
|
const f32 RANGE = BZ_MAX(FRIEND_RADIUS, ENEMY_RADIUS);
|
|
BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid,
|
|
center.x - RANGE, center.y - RANGE,
|
|
RANGE * 2.0f, RANGE * 2.0f);
|
|
|
|
i32 numFriends = 0;
|
|
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);
|
|
bool isFriend = owner[i].player == otherOwner.player;
|
|
|
|
if (!ecs_has(ECS, other, Position) || !ecs_has(ECS, other, HitBox))
|
|
continue;
|
|
Position otherPos = *ecs_get(ECS, other, Position);
|
|
Rectangle otherHB = *ecs_get(ECS, other, HitBox);;
|
|
Vector2 otherCenter = entityGetCenter(otherPos, otherHB);
|
|
f32 dst = Vector2Distance(center, otherCenter);
|
|
const f32 MIN_AVOID_DST = 18.0f;
|
|
const f32 MIN_ENEMY_DST = ENEMY_RADIUS;
|
|
if (isFriend) {
|
|
if (dst < MIN_AVOID_DST) {
|
|
Vector2 dif = Vector2Subtract(center, otherCenter);
|
|
dif = Vector2Scale(Vector2Normalize(dif), MIN_AVOID_DST - dst);
|
|
avoid = Vector2Add(avoid, dif);
|
|
}
|
|
Vector2Add(cohesion, otherCenter);
|
|
numFriends++;
|
|
} else {
|
|
if (dst < MIN_ENEMY_DST) {
|
|
//DrawCircleV(otherCenter, 2.0f, RED);
|
|
Vector2 dif = Vector2Subtract(otherCenter, center);
|
|
dif = Vector2Scale(Vector2Normalize(dif), MIN_ENEMY_DST - dst);
|
|
target = Vector2Add(target, dif);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (numFriends > 0) {
|
|
cohesion = Vector2Divide(cohesion, (Vector2) { numFriends, numFriends });
|
|
cohesion = Vector2Subtract(cohesion, center);
|
|
}
|
|
|
|
|
|
//bzLogInfo("%d %d", numFriends, numEnemies);
|
|
|
|
const f32 noiseRange = 10.0f;
|
|
Vector2 noise = {
|
|
randFloatRange(-noiseRange, noiseRange),
|
|
randFloatRange(-noiseRange, noiseRange)
|
|
};
|
|
Vector2 followWaypoint = Vector2Zero();
|
|
if (swarm[i].currWaypoint < game->swamNumWaypoints) {
|
|
Vector2 waypoint = game->swarmWaypoints[swarm[i].currWaypoint];
|
|
Vector2 waypointDir = Vector2Subtract(waypoint, center);
|
|
followWaypoint = waypointDir;
|
|
|
|
f32 dst = Vector2Distance(center, waypoint);
|
|
const f32 WAYPOINT_THRESHOLD = 54.0f;
|
|
if (dst < WAYPOINT_THRESHOLD) {
|
|
swarm[i].currWaypoint++;
|
|
}
|
|
}
|
|
const f32 AVOID_FACTOR = 1.0f;
|
|
const f32 TARGET_FACTOR = 2.2f;
|
|
const f32 COHESION_FACTOR = 0.10f;
|
|
const f32 WAYPOINT_FACTOR = 0.45f;
|
|
const f32 NOISE_FACTOR = 0.2f;
|
|
Vector2 move = Vector2Zero();
|
|
move = Vector2Add(move, Vector2Scale(avoid, AVOID_FACTOR));
|
|
move = Vector2Add(move, Vector2Scale(target, TARGET_FACTOR));
|
|
//move = Vector2Add(move, Vector2Scale(cohesion, COHESION_FACTOR));
|
|
move = Vector2Add(move, Vector2Scale(followWaypoint, WAYPOINT_FACTOR));
|
|
move = Vector2Add(move, Vector2Scale(noise, NOISE_FACTOR));
|
|
|
|
//bzLogInfo("%.2f %.2f", move.x, move.y);
|
|
//DrawLineV(center, Vector2Add(center, Vector2Scale(Vector2Normalize(avoid), 100)), ORANGE);
|
|
|
|
steer[i] = move;
|
|
}
|
|
}
|
|
|
|
void entityFollowPath(ecs_iter_t *it) {
|
|
const Game *game = ecs_singleton_get(ECS, Game);
|
|
|
|
Path *path = ecs_field(it, Path, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
const ecs_entity_t entity = it->entities[i];
|
|
|
|
if (!ecs_has(ECS, entity, TargetPosition)) {
|
|
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
|
path[i].curWaypoint = 0;
|
|
PathData *oldPath = path[i].paths;
|
|
bzObjectPoolRelease(game->pools.pathData, oldPath);
|
|
path[i].paths = path[i].paths->next;
|
|
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
|
}
|
|
if (path[i].paths) {
|
|
TargetPosition target = path[i].paths->waypoints[path[i].curWaypoint];
|
|
path[i].curWaypoint++;
|
|
ecs_set_ptr(ECS, entity, TargetPosition, &target);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
void updateBuildingRecruitment(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
Owner *owner = ecs_field(it, Owner, 1);
|
|
Building *building = ecs_field(it, Building, 2);
|
|
BuildingRecruitInfo *info = ecs_field(it, BuildingRecruitInfo, 3);
|
|
|
|
f32 dt = GetFrameTime();
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Player player = owner[i].player;
|
|
Vector2 placePos = {
|
|
(building[i].pos.x + building[i].size.x * 0.5f) * 16.0f,
|
|
(building[i].pos.y + building[i].size.y + 0.5f) * 16.0f
|
|
};
|
|
placePos.x += GetRandomValue(-building[i].size.x, building[i].size.x);
|
|
placePos.y += GetRandomValue(-4, 4);
|
|
|
|
for (i32 slotIdx = 0; slotIdx < info[i].numSlots; slotIdx++) {
|
|
BuildingRecruitSlot *slot = &info[i].slots[slotIdx];
|
|
|
|
if (slot->numRecruiting <= 0) continue;
|
|
slot->elapsed += dt;
|
|
if (slot->elapsed >= slot->recruitTime) {
|
|
slot->elapsed = 0;
|
|
PlayerResources *playerRes = &game->playerResources[player];
|
|
playerRes->pop--;
|
|
entityCreate(slot->entityType, placePos, player, game);
|
|
slot->numRecruiting--;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
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.0f, range * 2.0f
|
|
};
|
|
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) {
|
|
const f32 CHECK_INTERVAL = 0.2f; // 5 times a second
|
|
tower[i].fireElapsed = tower[i].fireCooldown - CHECK_INTERVAL;
|
|
continue;
|
|
}
|
|
|
|
Vector2 dir = Vector2Subtract(targetPos, center);
|
|
dir = Vector2Normalize(dir);
|
|
dir = Vector2Scale(dir, tower[i].projSpeed);
|
|
|
|
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].projMinDamage, tower[i].projMaxDamage),
|
|
.target = targetPos,
|
|
.radius = tower[i].projRadius,
|
|
.damageCount = tower[i].projDamageCount
|
|
});
|
|
ecs_set(ECS, proj, Owner, { owner[i].player });
|
|
|
|
f32 lifespan = randFloatRange(tower[i].projMinLifespan, tower[i].projMaxLifespan);
|
|
ecs_set(ECS, proj, DelayDelete, { .time = lifespan });
|
|
|
|
ecs_entity_t projEmitter = entityCreateEmpty();
|
|
ParticleEmitter emitter = GET_FIREBALL_EMITTER();
|
|
emitter.targetParticles = ecs_id(ParticleLayer1);
|
|
emitter.pos = center;
|
|
ecs_set_ptr(ECS, projEmitter, ParticleEmitter, &emitter);
|
|
ecs_set(ECS, projEmitter, EmitterAttachment, { .baseEntity = proj });
|
|
|
|
|
|
tower[i].fireElapsed = 0.0f;
|
|
}
|
|
|
|
}
|
|
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--;
|
|
proj[i].damage *= 0.9f;
|
|
break;
|
|
}
|
|
|
|
if (proj[i].damageCount <= 0) {
|
|
ecs_delete(ECS, it->entities[i]);
|
|
continue;
|
|
}
|
|
|
|
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);
|
|
HitBox *hitbox = ecs_field(it, HitBox, 2);
|
|
Health *health = ecs_field(it, Health, 3);
|
|
|
|
f32 time = GetTime();
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
f32 lastChange = time - health[i].lastChanged;
|
|
if (lastChange > 2.0f) continue;
|
|
|
|
HitBox hb = entityTransformHitBox(pos[i], hitbox[i]);
|
|
const f32 HP_WIDTH = 10.0f;
|
|
const f32 HP_HEIGHT = 1.8f;
|
|
const f32 HP_OFFSET = 2.0f;
|
|
|
|
const Color BG_COLOR = {0, 0, 0, 60};
|
|
const Color HP_COLOR = {255, 0, 0, 220};
|
|
|
|
const f32 PADDING = 0.15f;
|
|
|
|
Vector2 hpPos = {
|
|
hb.x + (hb.width - HP_WIDTH) * 0.5f,
|
|
hb.y - HP_OFFSET - HP_HEIGHT
|
|
};
|
|
Vector2 size = {
|
|
HP_WIDTH, HP_HEIGHT
|
|
};
|
|
f32 hpPercent = health[i].hp / health[i].startHP;
|
|
Vector2 hpSize = {
|
|
HP_WIDTH * hpPercent - PADDING * 2.0f,
|
|
HP_HEIGHT - PADDING * 2.0f
|
|
};
|
|
|
|
DrawRectangleV(hpPos, size, BG_COLOR);
|
|
hpPos.x += PADDING;
|
|
hpPos.y += PADDING;
|
|
DrawRectangleV(hpPos, hpSize, HP_COLOR);
|
|
}
|
|
}
|
|
|
|
void renderColliders(ecs_iter_t *it) {
|
|
Position *pos = ecs_field(it, Position, 1);
|
|
HitBox *hitbox = ecs_field(it, HitBox, 2);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
HitBox hb = entityTransformHitBox(pos[i], hitbox[i]);
|
|
DrawRectangleLinesEx(hb, 1.0f, RED);
|
|
}
|
|
}
|
|
|
|
void renderOrientationDirection(ecs_iter_t *it) {
|
|
Position *pos = ecs_field(it, Position, 1);
|
|
Orientation *orientation = ecs_field(it, Orientation, 2);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Vector2 v = {6.0f, 0.0f};
|
|
v = Vector2Rotate(v, orientation[i]);
|
|
v = Vector2Add(v, pos[i]);
|
|
DrawLine(pos[i].x, pos[i].y, v.x, v.y, RED);
|
|
}
|
|
}
|
|
|
|
void renderDebugPath(ecs_iter_t *it) {
|
|
Path *path = ecs_field(it, Path, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
PathData *pathData = path[i].paths;
|
|
bool first = true;
|
|
while (pathData) {
|
|
for (i32 iPath = 0; iPath < pathData->numWaypoints; iPath++) {
|
|
Color color = RED;
|
|
if (first && iPath < path[i].curWaypoint)
|
|
color = GREEN;
|
|
else if (first && iPath == path[i].curWaypoint)
|
|
color = ORANGE;
|
|
color.a = 180;
|
|
|
|
Position pos = pathData->waypoints[iPath];
|
|
DrawCircle(pos.x, pos.y, 3, color);
|
|
}
|
|
first = false;
|
|
pathData = pathData->next;
|
|
}
|
|
}
|
|
}
|