Fix hitboxes for entities
This commit is contained in:
@@ -101,12 +101,8 @@ void entityUpdateSpatialID(ecs_iter_t *it) {
|
||||
BZ_UNUSED(velocity);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
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);
|
||||
HitBox hb = entityTransformHitBox(position[i], hitbox[i]);
|
||||
bzSpatialGridUpdate(game->entityGrid, id[i], hb.x, hb.y, hb.width, hb.height);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -227,9 +223,7 @@ void renderColliders(ecs_iter_t *it) {
|
||||
HitBox *hitbox = ecs_field(it, HitBox , 2);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
HitBox hb = hitbox[i];
|
||||
hb.x += pos[i].x;
|
||||
hb.y += pos[i].y;
|
||||
HitBox hb = entityTransformHitBox(pos[i], hitbox[i]);
|
||||
DrawRectangleLinesEx(hb, 1.0f, RED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,22 +309,19 @@ void drawPlayerInputUIGround() {
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
f32 radius = BZ_MAX(hitbox[i].width, hitbox[i].height);
|
||||
Position center = entityGetCenter(pos[i], hitbox[i]);
|
||||
radius *= 0.8f;
|
||||
const f32 lineThickness = 1.0f;
|
||||
if (ecs_has(ECS, entity, Building)) {
|
||||
const f32 padding = 2.0f;
|
||||
Rectangle bounds = {
|
||||
pos[i].x + hitbox[i].x - padding,
|
||||
pos[i].y - hitbox[i].y - padding,
|
||||
pos[i].y - hitbox[i].height - padding,
|
||||
hitbox[i].width + padding * 2,
|
||||
hitbox[i].height + padding * 2,
|
||||
};
|
||||
DrawRectangleLinesEx(bounds, lineThickness, GREEN);
|
||||
} else {
|
||||
Position center = {
|
||||
pos[i].x + hitbox[i].x + hitbox[i].width * 0.5f,
|
||||
pos[i].y + hitbox[i].y + hitbox[i].height * 0.5f,
|
||||
};
|
||||
DrawRing(center, radius, radius + lineThickness, 0, 360, 12, GREEN);
|
||||
}
|
||||
}
|
||||
@@ -372,7 +369,7 @@ ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t
|
||||
if (!ecs_has_id(ECS, entity, tag)) continue;
|
||||
Vector2 pos;
|
||||
Rectangle hitbox;
|
||||
if (!getEntityHitBox(entity, &pos, &hitbox)) continue;
|
||||
if (!entityGetHitBox(entity, &pos, &hitbox)) continue;
|
||||
|
||||
if (!CheckCollisionPointRec(point, hitbox)) continue;
|
||||
|
||||
@@ -410,7 +407,7 @@ void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player) {
|
||||
}
|
||||
if (!ecs_has_id(ECS, entity, ecs_id(Unit))) continue;
|
||||
Rectangle hitbox;
|
||||
if (!getEntityHitBox(entity, NULL, &hitbox)) continue;
|
||||
if (!entityGetHitBox(entity, NULL, &hitbox)) continue;
|
||||
|
||||
if (!CheckCollisionRecs(area, hitbox)) continue;
|
||||
ecs_add(ECS, entity, Selected);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "../game_state.h"
|
||||
|
||||
bool getEntityHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox) {
|
||||
bool entityGetHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox) {
|
||||
if (!ecs_is_alive(ECS, entity))
|
||||
return false;
|
||||
|
||||
@@ -18,11 +18,25 @@ bool getEntityHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox
|
||||
if (outHitBox) {
|
||||
*outHitBox = *hitbox;
|
||||
outHitBox->x += pos->x;
|
||||
outHitBox->y += pos->y;
|
||||
outHitBox->y = pos->y - hitbox->y - hitbox->height;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Rectangle entityTransformHitBox(Position position, HitBox hitBox) {
|
||||
return (Rectangle) {
|
||||
position.x + hitBox.x,
|
||||
position.y - hitBox.y - hitBox.height,
|
||||
hitBox.width,
|
||||
hitBox.height
|
||||
};
|
||||
}
|
||||
Vector2 entityGetCenter(Position position, HitBox hitBox) {
|
||||
return (Vector2) {
|
||||
position.x + hitBox.x + hitBox.width * 0.5f,
|
||||
position.y - hitBox.y - hitBox.height * 0.5f
|
||||
};
|
||||
}
|
||||
|
||||
ecs_entity_t renderCollidersSystem;
|
||||
ecs_entity_t renderOrientDirSystem;
|
||||
|
||||
@@ -199,7 +199,9 @@ void drawSettingsUI(Game *game, f32 dt);
|
||||
* Utils
|
||||
**********************************/
|
||||
|
||||
bool getEntityHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox);
|
||||
bool entityGetHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox);
|
||||
Rectangle entityTransformHitBox(Position position, HitBox hitBox);
|
||||
Vector2 entityGetCenter(Position position, HitBox hitBox);
|
||||
|
||||
/**********************************
|
||||
* MISC
|
||||
|
||||
Reference in New Issue
Block a user