Add hitboxes
This commit is contained in:
@@ -28,7 +28,7 @@ bool canPlaceBuilding(Game *game, BuildingType type, BzTile tileX, BzTile tileY)
|
||||
BzSpatialGridIter it = bzSpatialGridIter(game->entityGrid, buildArea.x, buildArea.y, buildArea.width, buildArea.height);
|
||||
while (bzSpatialGridQueryNext(&it)) { ecs_entity_t entity = *(ecs_entity_t *) it.data;
|
||||
Rectangle bounds;
|
||||
if (!getEntityBounds(entity, NULL, NULL, &bounds)) continue;
|
||||
if (!getEntityHitBox(entity, NULL, &bounds)) continue;
|
||||
|
||||
if (CheckCollisionRecs(buildArea, bounds))
|
||||
return false;
|
||||
@@ -55,21 +55,26 @@ ecs_entity_t placeBuilding(Game *game, BuildingType type,
|
||||
.size = (Vec2i) { sizeX, sizeY }
|
||||
});
|
||||
Position pos = {
|
||||
.x = posX * tileWidth + sizeX * tileWidth * 0.5f,
|
||||
.y = posY * tileHeight + sizeY * tileHeight * 0.5f
|
||||
.x = posX * tileWidth,
|
||||
.y = posY * tileHeight
|
||||
};
|
||||
Size size = {
|
||||
.x = sizeX * tileWidth,
|
||||
.y = sizeY * tileHeight
|
||||
.y = sizeY * tileHeight,
|
||||
};
|
||||
HitBox hitbox = {
|
||||
.x = 0.0f, .y = 0.0f,
|
||||
.width = size.x,
|
||||
.height = size.y
|
||||
};
|
||||
|
||||
ecs_set_ptr(ECS, building, Position, &pos);
|
||||
ecs_set_ptr(ECS, building, Size, &size);
|
||||
ecs_set_ptr(ECS, building, HitBox, &hitbox);
|
||||
ecs_set(ECS, building, Rotation, {0});
|
||||
|
||||
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &building,
|
||||
pos.x - size.x * 0.5f, pos.y - size.y * 0.5f,
|
||||
size.x, size.y);
|
||||
pos.x, pos.y, hitbox.width, hitbox.height);
|
||||
ecs_set_ptr(ECS, building, SpatialGridID, &gridID);
|
||||
ecs_set(ECS, building, Owner, {player});
|
||||
BzTileset *tileset = &game->tileset;
|
||||
@@ -154,11 +159,12 @@ bool canAffordBuilding(BuildingType type, PlayerResources res) {
|
||||
Vector2 getPositionNearBuilding(ecs_entity_t building, Vector2 fromPos) {
|
||||
BZ_ASSERT(ecs_is_alive(ECS, building));
|
||||
BZ_ASSERT(ecs_has(ECS, building, Position));
|
||||
BZ_ASSERT(ecs_has(ECS, building, Size));
|
||||
BZ_ASSERT(ecs_has(ECS, building, HitBox));
|
||||
|
||||
Vector2 pos = *ecs_get(ECS, building, Position);
|
||||
Vector2 size = *ecs_get(ECS, building, Size);
|
||||
HitBox hitbox = *ecs_get(ECS, building, HitBox);
|
||||
|
||||
Vector2 size = {hitbox.width, hitbox.height};
|
||||
size = Vector2SubtractValue(size, 10.0f);
|
||||
|
||||
Vector2 dir = Vector2Normalize(Vector2Subtract(fromPos, pos));
|
||||
|
||||
@@ -23,6 +23,7 @@ ECS_COMPONENT_DECLARE(TextureRegion);
|
||||
ECS_COMPONENT_DECLARE(Animation);
|
||||
ECS_COMPONENT_DECLARE(Easing);
|
||||
|
||||
ECS_COMPONENT_DECLARE(HitBox);
|
||||
ECS_COMPONENT_DECLARE(Arms);
|
||||
ECS_COMPONENT_DECLARE(Arm);
|
||||
ECS_COMPONENT_DECLARE(BzBTState);
|
||||
@@ -64,6 +65,7 @@ void initComponentIDs(ecs_world_t *ecs) {
|
||||
ECS_COMPONENT_DEFINE(ecs, Animation);
|
||||
ECS_COMPONENT_DEFINE(ecs, Easing);
|
||||
|
||||
ECS_COMPONENT_DEFINE(ecs, HitBox);
|
||||
ECS_COMPONENT_DEFINE(ecs, Arms);
|
||||
ECS_COMPONENT_DEFINE(ecs, Arm);
|
||||
ECS_COMPONENT_DEFINE(ecs, BzBTState);
|
||||
|
||||
@@ -136,6 +136,9 @@ extern ECS_COMPONENT_DECLARE(Easing);
|
||||
* Gameplay components
|
||||
*********************************************************/
|
||||
|
||||
typedef Rectangle HitBox;
|
||||
extern ECS_COMPONENT_DECLARE(HitBox);
|
||||
|
||||
typedef struct WeaponMelee {
|
||||
ecs_entity_t weapon;
|
||||
f32 reach;
|
||||
|
||||
@@ -11,17 +11,18 @@ ecs_entity_t entityCreateEmpty() {
|
||||
ecs_entity_t entityCreateBaseUnit(const Position position, Player player,
|
||||
EntityType type, AnimType startAnim, Game *game) {
|
||||
BzTileset *tileset = &game->tileset;
|
||||
BzTileID tileID = getEntityTile(type);
|
||||
TextureRegion region = {
|
||||
tileset->tiles,
|
||||
getTextureRect(getEntityTile(type))
|
||||
getTextureRect(tileID)
|
||||
};
|
||||
const Size size = {10.0f * region.rec.width / 16.0f, 10.0f * region.rec.height / 16.0f};
|
||||
HitBox hitbox = getEntityHitBoxRec(tileID);
|
||||
ecs_entity_t e = entityCreateEmpty();
|
||||
ecs_set_ptr(ECS, e, Position, &position);
|
||||
ecs_set_ptr(ECS, e, Size, &size);
|
||||
//ecs_set_ptr(ECS, e, HitBox, &hitbox);
|
||||
BzSpatialGridID spatialID = bzSpatialGridInsert(game->entityGrid, &e,
|
||||
position.x - size.x * 0.5f, position.y - size.y * 0.5f,
|
||||
size.x, size.y);
|
||||
position.x + hitbox.x, position.y + hitbox.y,
|
||||
hitbox.width, hitbox.height);
|
||||
ecs_set(ECS, e, SpatialGridID, { spatialID });
|
||||
ecs_set(ECS, e, Rotation, { 0.0f });
|
||||
ecs_set(ECS, e, Orientation, {0.0f});
|
||||
@@ -78,7 +79,6 @@ ecs_entity_t entityCreateWorker(const Position position, Player player, Game *ga
|
||||
ecs_set_ptr(ECS, e, Arms, &arms);
|
||||
ecs_set(ECS, right, Arm, {.offset = 45.0f, 4.5f});
|
||||
|
||||
ecs_set(ECS, right, Size, {8, 8});
|
||||
ecs_set(ECS, right, Rotation, { 0.0f });
|
||||
TextureRegion daggerRegion = {
|
||||
tileset->tiles,
|
||||
|
||||
@@ -1686,7 +1686,56 @@ static Vector2 getItemHandlePoint(BzTile tile) {
|
||||
case 8199: return (Vector2) {5, 11};
|
||||
case 8200: return (Vector2) {4, 12};
|
||||
case 8201: return (Vector2) {4, 12};
|
||||
default: return (Vector2) {0, 0};
|
||||
default: return (Vector2) {0.0f, 0.0f};
|
||||
}
|
||||
}
|
||||
static bool hasEntityHitBoxRec(BzTile tile) {
|
||||
switch (tile) {
|
||||
case 27:
|
||||
case 539:
|
||||
case 1051:
|
||||
case 1563:
|
||||
case 2075:
|
||||
case 2587:
|
||||
case 5888:
|
||||
case 5889:
|
||||
case 5890:
|
||||
case 5891:
|
||||
case 6146:
|
||||
case 6147:
|
||||
case 6402:
|
||||
case 6403:
|
||||
case 6656:
|
||||
case 6912:
|
||||
case 7170:
|
||||
case 7171:
|
||||
case 7172:
|
||||
return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
static Rectangle getEntityHitBoxRec(BzTile tile) {
|
||||
switch (tile) {
|
||||
case 27: return (Rectangle) {4, 2, 8, 12};
|
||||
case 539: return (Rectangle) {4, 0, 8, 14};
|
||||
case 1051: return (Rectangle) {4, 0, 8, 14};
|
||||
case 1563: return (Rectangle) {4, 0, 8, 14};
|
||||
case 2075: return (Rectangle) {4, 1, 8, 13};
|
||||
case 2587: return (Rectangle) {4, 3, 8, 11};
|
||||
case 5888: return (Rectangle) {4, 4, 8, 9};
|
||||
case 5889: return (Rectangle) {3, 7, 10, 7};
|
||||
case 5890: return (Rectangle) {6, 4, 4, 10};
|
||||
case 5891: return (Rectangle) {6, 4, 4, 10};
|
||||
case 6146: return (Rectangle) {6, 4, 4, 10};
|
||||
case 6147: return (Rectangle) {6, 4, 4, 10};
|
||||
case 6402: return (Rectangle) {6, 4, 4, 10};
|
||||
case 6403: return (Rectangle) {6, 4, 4, 10};
|
||||
case 6656: return (Rectangle) {6, 4, 4, 10};
|
||||
case 6912: return (Rectangle) {6, 4, 4, 10};
|
||||
case 7170: return (Rectangle) {3, 3, 10, 10};
|
||||
case 7171: return (Rectangle) {3, 4, 10, 9};
|
||||
case 7172: return (Rectangle) {1, 3, 14, 10};
|
||||
default: return (Rectangle) { 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
}
|
||||
}
|
||||
#endif // GAME_TILESET_H
|
||||
|
||||
27
game/main.c
27
game/main.c
@@ -128,7 +128,7 @@ bool init(void *userData) {
|
||||
input->queries.selected = ecs_query(ECS, {
|
||||
.filter.terms = {
|
||||
{ecs_id(Position)},
|
||||
{ecs_id(Size)},
|
||||
{ecs_id(HitBox)},
|
||||
{ecs_id(Selected)}
|
||||
}
|
||||
});
|
||||
@@ -382,21 +382,18 @@ static void renderGame(Game *game, float dt) {
|
||||
Rectangle camBounds = getCameraBounds(camera);
|
||||
while (ecs_iter_next(&it)) {
|
||||
Position *p = ecs_field(&it, Position, 1);
|
||||
Size *s = ecs_field(&it, Size, 2);
|
||||
Size *s = ecs_field(&it, Size , 2);
|
||||
Rotation *r = ecs_field(&it, Rotation, 3);
|
||||
TextureRegion *t = ecs_field(&it, TextureRegion, 4);
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
Rectangle dst = {p[i].x, p[i].y, s[i].x, s[i].y};
|
||||
Vector2 origin = {dst.width * 0.5f, dst.height};
|
||||
dst.x += origin.x - dst.width * 0.5f;
|
||||
dst.y += origin.y - dst.height * 0.5f;
|
||||
Rectangle collider = {
|
||||
p[i].x - s[i].x * 0.5f,
|
||||
p[i].y - s[i].y * 0.5f,
|
||||
.width = s[i].x,
|
||||
.height = s[i].y
|
||||
};
|
||||
if (!CheckCollisionRecs(camBounds, collider))
|
||||
f32 sclX = s[i].x / t[i].rec.width;
|
||||
f32 sclY = s[i].y / t[i].rec.height;
|
||||
Rectangle dst = {p[i].x, p[i].y,
|
||||
t[i].rec.width * sclX, t[i].rec.height * sclY};
|
||||
Vector2 origin = {0.0f, 0.0f};
|
||||
//dst.x += origin.x - dst.width * 0.5f;
|
||||
//dst.y += origin.y - dst.height * 0.5f;
|
||||
if (!CheckCollisionRecs(camBounds, dst))
|
||||
continue;
|
||||
Rectangle src = t[i].rec;
|
||||
// Fixes texture bleeding issue
|
||||
@@ -432,7 +429,7 @@ static void renderGame(Game *game, float dt) {
|
||||
DrawData draw = drawData[i];
|
||||
Vector2 pos = {
|
||||
draw.dst.x,
|
||||
draw.dst.y - draw.dst.height * 0.5f,
|
||||
draw.dst.y,
|
||||
};
|
||||
Color c = WHITE;
|
||||
if (draw.canHaveAlpha) {
|
||||
@@ -572,7 +569,7 @@ void igInspectWindow(ecs_entity_t entity, bool *open) {
|
||||
igInspectComp("Owner", entity, ecs_id(Owner), igOwner);
|
||||
igInspectComp("SpatialGridID", entity, ecs_id(SpatialGridID), igSpatialGridID);
|
||||
igInspectComp("Position", entity, ecs_id(Position), igVec2Comp);
|
||||
igInspectComp("Size", entity, ecs_id(Size), igVec2Comp);
|
||||
//igInspectComp("Size", entity, ecs_id(Size), igVec2Comp);
|
||||
igInspectComp("Velocity", entity, ecs_id(Velocity), igVec2Comp);
|
||||
igInspectComp("TargetPosition", entity, ecs_id(TargetPosition), igVec2Comp);
|
||||
igInspectComp("Steering", entity, ecs_id(Steering), igVec2Comp);
|
||||
|
||||
@@ -130,7 +130,7 @@ bool initRocksLayer(BzTileMap *map, BzTileLayer *layer) {
|
||||
posX += sizeX * 0.5f;
|
||||
posY += sizeY * 0.5f;
|
||||
ecs_set(ECS, e, Position, {posX, posY});
|
||||
ecs_set(ECS, e, Size, {sizeX, sizeY});
|
||||
ecs_set(ECS, e, HitBox, {sizeX, sizeY});
|
||||
ecs_set(ECS, e, Rotation, {0});
|
||||
BzTile tileID = bzTilesetGetTileID(tileset, layerTile);
|
||||
ecs_set(ECS, e, TextureRegion, {tileset->tiles, bzTilesetGetTileRegion(tileset, tileID)});
|
||||
@@ -155,20 +155,24 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
|
||||
tile = bzTilesetGetTileID(tileset, tile);
|
||||
if (tile == -1) continue; // Not a tree
|
||||
|
||||
BzTile tileID = bzTilesetGetTileID(tileset, layerTile);
|
||||
BZ_ASSERT(hasEntityHitBoxRec(tileID));
|
||||
HitBox hb = getEntityHitBoxRec(tileID);
|
||||
|
||||
f32 sizeX = tileset->tileWidth;
|
||||
f32 sizeY = tileset->tileHeight;
|
||||
f32 posX = layer->offsetX + x * sizeX;
|
||||
f32 posY = layer->offsetY + y * sizeY;
|
||||
ecs_entity_t e = entityCreateEmpty();
|
||||
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &e, posX, posY, sizeX, sizeY);
|
||||
SpatialGridID gridID = bzSpatialGridInsert(game->entityGrid, &e,
|
||||
posX + hb.x, posY + hb.y,
|
||||
hb.width, hb.height);
|
||||
ecs_set(ECS, e, SpatialGridID, {gridID});
|
||||
posX += sizeX * 0.5f;
|
||||
posY += sizeY * 0.5f;
|
||||
ecs_set(ECS, e, Position, {posX, posY});
|
||||
ecs_set(ECS, e, Size, {sizeX, sizeY});
|
||||
ecs_set_ptr(ECS, e, HitBox, &hb);
|
||||
ecs_set(ECS, e, Rotation, {0});
|
||||
BzTile tileID = bzTilesetGetTileID(tileset, layerTile);
|
||||
ecs_set(ECS, e, TextureRegion, {tileset->tiles, bzTilesetGetTileRegion(tileset, tileID)});
|
||||
ecs_set(ECS, e, TextureRegion, {tileset->tiles, getTextureRect(tileID)});
|
||||
ecs_set(ECS, e, Resource, {RES_WOOD, 20});
|
||||
ecs_add_id(ECS, e, Selectable);
|
||||
ecs_add_id(ECS, e, Harvestable);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player);
|
||||
|
||||
void addEntityToInspected(ecs_entity_t entity, Game *game);
|
||||
|
||||
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size));
|
||||
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size);
|
||||
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos));
|
||||
static void iterRemovePaths(ecs_entity_t entity, Position *pos);
|
||||
|
||||
void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces);
|
||||
|
||||
@@ -305,23 +305,27 @@ void drawPlayerInputUIGround() {
|
||||
rlSetLineWidth(2.0f);
|
||||
while (ecs_query_next(&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++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
f32 radius = BZ_MIN(size[i].x, size[i].y);
|
||||
radius *= 0.5f;
|
||||
f32 radius = BZ_MAX(hitbox[i].width, hitbox[i].height);
|
||||
radius *= 0.8f;
|
||||
const f32 lineThickness = 1.0f;
|
||||
if (ecs_has(ECS, entity, Building)) {
|
||||
const f32 padding = 2.0f;
|
||||
Rectangle bounds = {
|
||||
pos[i].x - size[i].x * 0.5f - padding,
|
||||
pos[i].y - size[i].y * 0.5f - padding,
|
||||
size[i].x + padding * 2,
|
||||
size[i].y + padding * 2,
|
||||
pos[i].x + hitbox[i].x - padding,
|
||||
pos[i].y - hitbox[i].y - padding,
|
||||
hitbox[i].width + padding * 2,
|
||||
hitbox[i].height + padding * 2,
|
||||
};
|
||||
DrawRectangleLinesEx(bounds, lineThickness, GREEN);
|
||||
} else {
|
||||
DrawRing(pos[i], radius, radius + lineThickness, 0, 360, 12, GREEN);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -367,10 +371,10 @@ ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t
|
||||
if (!ecs_has_id(ECS, entity, Selectable)) continue;
|
||||
if (!ecs_has_id(ECS, entity, tag)) continue;
|
||||
Vector2 pos;
|
||||
Rectangle bounds;
|
||||
if (!getEntityBounds(entity, &pos, NULL, &bounds)) continue;
|
||||
Rectangle hitbox;
|
||||
if (!getEntityHitBox(entity, &pos, &hitbox)) continue;
|
||||
|
||||
if (!CheckCollisionPointRec(point, bounds)) continue;
|
||||
if (!CheckCollisionPointRec(point, hitbox)) continue;
|
||||
|
||||
f32 curDst = Vector2Distance(point, pos);
|
||||
if (closestDst > curDst) {
|
||||
@@ -405,10 +409,10 @@ void selectUnits(BzSpatialGrid *entityGrid, Rectangle area, Player player) {
|
||||
if (owner.player != player) continue;
|
||||
}
|
||||
if (!ecs_has_id(ECS, entity, ecs_id(Unit))) continue;
|
||||
Rectangle bounds;
|
||||
if (!getEntityBounds(entity, NULL, NULL, &bounds)) continue;
|
||||
Rectangle hitbox;
|
||||
if (!getEntityHitBox(entity, NULL, &hitbox)) continue;
|
||||
|
||||
if (!CheckCollisionRecs(area, bounds)) continue;
|
||||
if (!CheckCollisionRecs(area, hitbox)) continue;
|
||||
ecs_add(ECS, entity, Selected);
|
||||
}
|
||||
}
|
||||
@@ -463,20 +467,19 @@ void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTil
|
||||
}
|
||||
}
|
||||
|
||||
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size)) {
|
||||
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos)) {
|
||||
ecs_iter_t it = ecs_query_iter(ECS, query);
|
||||
while (ecs_iter_next(&it)) {
|
||||
Position *pos = ecs_field(&it, Position, 1);
|
||||
Size *size = ecs_field(&it, Size, 2);
|
||||
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
|
||||
fn(entity, pos + i, size + i);
|
||||
fn(entity, pos + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size) {
|
||||
static void iterRemovePaths(ecs_entity_t entity, Position *pos) {
|
||||
ecs_remove(ECS, entity, Path);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,37 +2,25 @@
|
||||
|
||||
#include "../game_state.h"
|
||||
|
||||
Rectangle calculateEntityBounds(Position pos, Size size) {
|
||||
return (Rectangle) {
|
||||
pos.x - size.x * 0.5f,
|
||||
pos.y - size.x * 0.5f,
|
||||
size.x, size.y
|
||||
};
|
||||
}
|
||||
|
||||
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds) {
|
||||
bool getEntityHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox) {
|
||||
if (!ecs_is_alive(ECS, entity))
|
||||
return false;
|
||||
|
||||
const Position *pos = ecs_get(ECS, entity, Position);
|
||||
if (!pos)
|
||||
return false;
|
||||
const Size *size = ecs_get(ECS, entity, Size);
|
||||
if (!size)
|
||||
return false;
|
||||
|
||||
if (outPos) {
|
||||
if (outPos)
|
||||
*outPos = *pos;
|
||||
|
||||
const HitBox *hitbox = ecs_get(ECS, entity, HitBox);
|
||||
if (!hitbox)
|
||||
return false;
|
||||
if (outHitBox) {
|
||||
*outHitBox = *hitbox;
|
||||
outHitBox->x += pos->x;
|
||||
outHitBox->y += pos->y;
|
||||
}
|
||||
if (outSize) {
|
||||
*outSize = *size;
|
||||
}
|
||||
if (outBounds) {
|
||||
*outBounds = (Rectangle) {
|
||||
pos->x - size->x * 0.5f,
|
||||
pos->y - size->y * 0.5f,
|
||||
size->x, size->y
|
||||
};
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -115,7 +103,7 @@ void setupSystems() {
|
||||
ECS_OBSERVER(ECS, entityConsumePopCapacity, EcsOnSet, ConsumePopCapacity, Owner);
|
||||
ECS_OBSERVER(ECS, entityUnConsumePopCapacity, EcsOnRemove, ConsumePopCapacity, Owner);
|
||||
|
||||
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID);
|
||||
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID);
|
||||
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Velocity, Steering, Unit);
|
||||
|
||||
ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Velocity, TargetPosition, Steering);
|
||||
@@ -126,11 +114,11 @@ void setupSystems() {
|
||||
|
||||
ECS_SYSTEM(ECS, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
|
||||
ECS_SYSTEM(ECS, updateAnimation, EcsOnUpdate, Animation, TextureRegion);
|
||||
ECS_SYSTEM(ECS, updateEasingSystem, EcsOnUpdate, Easing, Position, Size, Rotation);
|
||||
ECS_SYSTEM(ECS, updateEasingSystem, EcsOnUpdate, Easing, Position, HitBox, Rotation);
|
||||
|
||||
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
|
||||
|
||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, HitBox);
|
||||
ECS_SYSTEM(ECS, renderOrientationDirection, EcsOnUpdate, Position, Orientation);
|
||||
ECS_SYSTEM(ECS, renderArmPosition, EcsOnUpdate, Position, Arm);
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ void entityUnConsumePopCapacity(ecs_iter_t *it);
|
||||
/*
|
||||
* 0: Game (singleton) for entity map
|
||||
* 1: Position
|
||||
* 2: Size
|
||||
* 2: HitBox
|
||||
* 3: Velocity (only entities with velocity change position)
|
||||
* 4: SpatialGridID
|
||||
*/
|
||||
@@ -132,7 +132,7 @@ void entityUpdateArms(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: Size
|
||||
* 2: HitBox
|
||||
*/
|
||||
void renderColliders(ecs_iter_t *it);
|
||||
|
||||
@@ -199,8 +199,7 @@ void drawSettingsUI(Game *game, f32 dt);
|
||||
* Utils
|
||||
**********************************/
|
||||
|
||||
Rectangle calculateEntityBounds(Position pos, Size size);
|
||||
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds);
|
||||
bool getEntityHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox);
|
||||
|
||||
/**********************************
|
||||
* MISC
|
||||
|
||||
Reference in New Issue
Block a user