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

@@ -31,12 +31,6 @@ bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game)
}
static Position getBottomLeftPos(Position pos, Size size) {
return (Position) {pos.x - size.x * 0.5f, pos.y - size.y * 0.5f};
}
void entityPathRemove(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
for (i32 i = 0; i < it->count; i++) {
@@ -101,13 +95,18 @@ void entityUnConsumePopCapacity(ecs_iter_t *it) {
void entityUpdateSpatialID(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *position = ecs_field(it, Position, 1);
Position *size = ecs_field(it, Size, 2);
//Velocity *velocity = ecs_field(it, Velocity, 3);
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++) {
Position pos = getBottomLeftPos(position[i], size[i]);
bzSpatialGridUpdate(game->entityGrid, id[i], pos.x, pos.y, size[i].x, size[i].y);
Rectangle rec = {
position[i].x + hitbox[i].x,
position[i].y + hitbox[i].y,
hitbox[i].width, hitbox[i].height
};
bzSpatialGridUpdate(game->entityGrid, id[i], rec.x, rec.y, rec.width, rec.height);
}
}
@@ -225,12 +224,13 @@ void entityUpdateArms(ecs_iter_t *it) {
void renderColliders(ecs_iter_t *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++) {
f32 posX = pos[i].x - size[i].x * 0.5f;
f32 posY = pos[i].y - size[i].y * 0.5f;
DrawRectangleLines(posX, posY, size[i].x, size[i].y, RED);
HitBox hb = hitbox[i];
hb.x += pos[i].x;
hb.y += pos[i].y;
DrawRectangleLinesEx(hb, 1.0f, RED);
}
}