Change Storage to tag (since we have global storage)

This commit is contained in:
2023-12-29 14:59:40 +01:00
parent 681080f3ed
commit 5190c86316
4 changed files with 12 additions and 22 deletions

View File

@@ -29,15 +29,14 @@ ECS_COMPONENT_DECLARE(UnitAction);
ECS_TAG_DECLARE(Selectable); ECS_TAG_DECLARE(Selectable);
ECS_TAG_DECLARE(Selected); ECS_TAG_DECLARE(Selected);
ECS_TAG_DECLARE(Unit);
ECS_COMPONENT_DECLARE(Worker); ECS_COMPONENT_DECLARE(Worker);
ECS_TAG_DECLARE(Unit);
ECS_TAG_DECLARE(Storage);
ECS_TAG_DECLARE(Harvestable); ECS_TAG_DECLARE(Harvestable);
ECS_TAG_DECLARE(Buildable); ECS_TAG_DECLARE(Buildable);
ECS_TAG_DECLARE(Workable); ECS_TAG_DECLARE(Workable);
ECS_TAG_DECLARE(Attackable); ECS_TAG_DECLARE(Attackable);
ECS_COMPONENT_DECLARE(Storage);
void initComponentIDs(ecs_world_t *ecs) { void initComponentIDs(ecs_world_t *ecs) {
ECS_COMPONENT_DEFINE(ecs, Resource); ECS_COMPONENT_DEFINE(ecs, Resource);
@@ -65,12 +64,12 @@ void initComponentIDs(ecs_world_t *ecs) {
ECS_TAG_DEFINE(ecs, Selectable); ECS_TAG_DEFINE(ecs, Selectable);
ECS_TAG_DEFINE(ecs, Selected); ECS_TAG_DEFINE(ecs, Selected);
ECS_TAG_DEFINE(ecs, Unit);
ECS_COMPONENT_DEFINE(ecs, Worker); ECS_COMPONENT_DEFINE(ecs, Worker);
ECS_TAG_DEFINE(ecs, Unit);
ECS_TAG_DEFINE(ecs, Storage);
ECS_TAG_DEFINE(ecs, Harvestable); ECS_TAG_DEFINE(ecs, Harvestable);
ECS_TAG_DEFINE(ecs, Buildable); ECS_TAG_DEFINE(ecs, Buildable);
ECS_TAG_DEFINE(ecs, Workable); ECS_TAG_DEFINE(ecs, Workable);
ECS_TAG_DEFINE(ecs, Attackable); ECS_TAG_DEFINE(ecs, Attackable);
ECS_COMPONENT_DEFINE(ecs, Storage);
} }

View File

@@ -163,10 +163,6 @@ extern ECS_COMPONENT_DECLARE(UnitAI);
extern ECS_TAG_DECLARE(Selectable); extern ECS_TAG_DECLARE(Selectable);
extern ECS_TAG_DECLARE(Selected); extern ECS_TAG_DECLARE(Selected);
// Unit can:
// - Attack
extern ECS_TAG_DECLARE(Unit);
// Worker can: // Worker can:
// - Harvest // - Harvest
// - Build // - Build
@@ -183,19 +179,15 @@ typedef struct Worker {
} Worker; } Worker;
extern ECS_COMPONENT_DECLARE(Worker); extern ECS_COMPONENT_DECLARE(Worker);
// Unit can:
// - Attack
extern ECS_TAG_DECLARE(Unit);
extern ECS_TAG_DECLARE(Storage);
extern ECS_TAG_DECLARE(Harvestable); extern ECS_TAG_DECLARE(Harvestable);
extern ECS_TAG_DECLARE(Buildable); extern ECS_TAG_DECLARE(Buildable);
extern ECS_TAG_DECLARE(Workable); extern ECS_TAG_DECLARE(Workable);
extern ECS_TAG_DECLARE(Attackable); extern ECS_TAG_DECLARE(Attackable);
typedef struct Storage {
int capacity[RES_COUNT];
int amount[RES_COUNT];
int reserved[RES_COUNT];
int pending[RES_COUNT];
} Storage;
extern ECS_COMPONENT_DECLARE(Storage);
void initComponentIDs(ecs_world_t *ecs); void initComponentIDs(ecs_world_t *ecs);

View File

@@ -91,7 +91,7 @@ bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
// ecs_set(ECS, e, Storage, {}); // ecs_set(ECS, e, Storage, {});
//} //}
if (buildingTile == BUILDING_KEEP) { if (buildingTile == BUILDING_KEEP) {
ecs_set(ECS, e, Storage, {.capacity[RES_WOOD] = 1000}); ecs_add_id(ECS, e, Storage);
} }
//bzTileMapUpdateCollider(&GAME.map, x, y); //bzTileMapUpdateCollider(&GAME.map, x, y);

View File

@@ -7,7 +7,7 @@
static ecs_entity_t findNearestStorage(Position pos, ResourceType type, Position *outPos) { static ecs_entity_t findNearestStorage(Position pos, ResourceType type, Position *outPos) {
ecs_filter_t *storageFilter = ecs_filter(ECS, { ecs_filter_t *storageFilter = ecs_filter(ECS, {
.terms = {{ecs_id(Storage)}, {ecs_id(Position)} } .terms = {{ecs_id(Position)}, {ecs_id(Storage)}},
}); });
ecs_iter_t it = ecs_filter_iter(ECS, storageFilter); ecs_iter_t it = ecs_filter_iter(ECS, storageFilter);
@@ -15,12 +15,11 @@ static ecs_entity_t findNearestStorage(Position pos, ResourceType type, Position
f32 closestDst = INFINITY; f32 closestDst = INFINITY;
Position closestPos = Vector2Zero(); Position closestPos = Vector2Zero();
while (ecs_filter_next(&it)) { while (ecs_filter_next(&it)) {
Storage *storage = ecs_field(&it, Storage, 1); Position *storagePos = ecs_field(&it, Position, 1);
Position *storagePos = ecs_field(&it, Position, 2);
for (i32 i = 0; i < it.count; i++) { for (i32 i = 0; i < it.count; i++) {
f32 dst = Vector2Distance(pos, storagePos[i]); f32 dst = Vector2Distance(pos, storagePos[i]);
if (storage[i].capacity[type] && dst < closestDst) { if (dst < closestDst) {
closest = it.entities[i]; closest = it.entities[i];
closestDst = dst; closestDst = dst;
closestPos = storagePos[i]; closestPos = storagePos[i];