Rework pathfinding to use Vector2, form formations

This commit is contained in:
2023-11-23 12:01:42 +01:00
parent 513168825c
commit 0dc8046d8f
6 changed files with 129 additions and 85 deletions

View File

@@ -192,58 +192,6 @@ void update(float dt, void *userData) {
updatePlayerInput(NULL);
}
static bool isUnitObstructed(f32 x, f32 y, BzTileMap *map) {
return bzTileMapHasCollision(map, x / map->tileWidth, y / map->tileHeight);
}
static bool canPlaceUnit(Vector2 pos, f32 space, BzTileMap *map) {
for (i32 y = -1; y <= 1; y++) {
for (i32 x = -1; x <= 1; x++) {
if (isUnitObstructed(pos.x + x * space, pos.y + y * space, map))
return false;
}
}
return true;
}
static void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTileMap *map, Vector2 **outPlaces) {
BZ_UNUSED(outPlaces);
f32 angle = Vector2Angle(start, end);
Vector2 size = {Vector2Distance(start, end), 20.0f};
Rectangle rec = {start.x, start.y, size.x, size.y};
Color color = RED;
color.a = 80;
Vector2 pos = Vector2Zero();
pos.x = unitSpacing;
for (i32 i = 0; i < numUnits; i++) {
if (pos.x + unitSpacing * 2.0f > size.x) {
pos.x = unitSpacing;
pos.y += unitSpacing * 2.0f;
}
Vector2 unitPos = Vector2Add(start, Vector2Rotate(pos, angle));
Color color = ORANGE;
if (!canPlaceUnit(unitPos, 4.0f, map)) {
color = RED;
color.a = 80;
i--;
} else {
bzArrayPush(*outPlaces, unitPos);
}
DrawCircle(unitPos.x, unitPos.y, 2.0f, color);
pos.x += unitSpacing * 2.0f;
}
static char buf[128];
snprintf(buf, sizeof(buf), "Num units: %d", bzArraySize(*outPlaces));
DrawText(buf, 800, 200, 32, BLUE);
}
void render(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);