Defer component mutation

This commit is contained in:
2023-12-02 14:31:52 +01:00
parent 17bfe7f52d
commit ce47bfb110

View File

@@ -4,12 +4,16 @@
#include "pathfinding.h" #include "pathfinding.h"
#include <rlImGui.h> #include <rlImGui.h>
#include <raymath.h> #include <raymath.h>
#include <rlgl.h>
Rectangle calculateEntityBounds(Position pos, Size size); Rectangle calculateEntityBounds(Position pos, Size size);
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds); bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds);
void pickEntity(BzSpatialGrid *entityGrid, Vector2 point); void pickEntity(BzSpatialGrid *entityGrid, Vector2 point);
void pickEntities(BzSpatialGrid *entityGrid, Rectangle area); void pickEntities(BzSpatialGrid *entityGrid, Rectangle area);
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);
void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces); void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces);
static bool isInputDragged(InputState *input) { static bool isInputDragged(InputState *input) {
@@ -53,7 +57,7 @@ void updatePlayerInput() {
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera); Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
BzTile tileX = 0, tileY = 0; BzTile tileX = 0, tileY = 0;
bzTileMapPosToTile(map, worldPos, &tileX, &tileY); bzTileMapPosToTile(map, worldPos, &tileX, &tileY);
i32 selectedUnitCount = ecs_count_id(ECS, UnitSelected); i32 selectedUnitCount = ecs_query_entity_count(input->unitSelectedQuery);
switch (input->state) { switch (input->state) {
case INPUT_NONE: case INPUT_NONE:
if (wasInputDragged(input)) { if (wasInputDragged(input)) {
@@ -112,13 +116,18 @@ void updatePlayerInput() {
BZ_ASSERT(bzArraySize(input->unitPositions) == numUnits); BZ_ASSERT(bzArraySize(input->unitPositions) == numUnits);
i32 unitPosIdx = 0; i32 unitPosIdx = 0;
ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery); ecs_defer_begin(ECS);
iterateSelectedUnits(input->unitSelectedQuery, iterRemovePaths);
ecs_defer_end(ECS);
ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery);
ecs_defer_begin(ECS);
while (ecs_iter_next(&it)) { while (ecs_iter_next(&it)) {
Position *pos = ecs_field(&it, Position, 1); Position *pos = ecs_field(&it, Position, 1);
for (i32 i = 0; i < it.count; i++) { for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i]; ecs_entity_t entity = it.entities[i];
ecs_remove(ECS, entity, Path);
fflush(stdout);
Position target = bzArrayGet(input->unitPositions, unitPosIdx); Position target = bzArrayGet(input->unitPositions, unitPosIdx);
unitPosIdx++; unitPosIdx++;
@@ -136,9 +145,15 @@ void updatePlayerInput() {
} }
} }
ecs_defer_end(ECS);
} else if (wasInputClicked(input)) { } else if (wasInputClicked(input)) {
ecs_defer_begin(ECS);
iterateSelectedUnits(input->unitSelectedQuery, iterRemovePaths);
ecs_defer_end(ECS);
ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery); ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery);
ecs_defer_begin(ECS);
while (ecs_iter_next(&it)) { while (ecs_iter_next(&it)) {
Position *pos = ecs_field(&it, Position, 1); Position *pos = ecs_field(&it, Position, 1);
for (i32 i = 0; i < it.count; i++) { for (i32 i = 0; i < it.count; i++) {
@@ -158,6 +173,7 @@ void updatePlayerInput() {
} }
} }
ecs_defer_end(ECS);
} }
break; break;
case INPUT_SELECTED_OBJECT: case INPUT_SELECTED_OBJECT:
@@ -231,6 +247,7 @@ void drawPlayerInputUI() {
break; break;
} }
ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery); ecs_iter_t it = ecs_query_iter(ECS, input->unitSelectedQuery);
rlSetLineWidth(2.0f);
while (ecs_query_next(&it)) { while (ecs_query_next(&it)) {
Position *pos = ecs_field(&it, Position, 1); Position *pos = ecs_field(&it, Position, 1);
Size *size = ecs_field(&it, Size, 2); Size *size = ecs_field(&it, Size, 2);
@@ -355,3 +372,21 @@ void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTil
pos.x += unitSpacing * 2.0f; pos.x += unitSpacing * 2.0f;
} }
} }
static void iterateSelectedUnits(ecs_query_t *query, void (*fn)(ecs_entity_t entity, Position *pos, Size *size)) {
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);
}
}
}
static void iterRemovePaths(ecs_entity_t entity, Position *pos, Size *size) {
ecs_remove(ECS, entity, Path);
}