Finish main menu screen

This commit is contained in:
2024-02-12 17:18:37 +01:00
parent 10c6d74e90
commit abaa778aab
9 changed files with 759 additions and 491 deletions

View File

@@ -10,6 +10,7 @@
#include "map_layers.h"
#include "utils.h"
#include "systems/systems.h"
#include "raymath.h"
i32 getSwarmWaypointIdx(u32 id) {
char buf[4];
@@ -260,7 +261,7 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) {
drawPos.y += (float) tileset->tileHeight;
}
}
void loadMap(Game *game, const char *path) {
void loadMap(Game *game, const char *path, bool mainMenu) {
game->map = bzTileMapCreate(&(BzTileMapDesc) {
.path=path,
@@ -293,7 +294,8 @@ void loadMap(Game *game, const char *path) {
game->camera.zoom = 3.0f;
bzMemSet(game->playerResources, 0, sizeof(*game->playerResources));
game->waveInfo = getWaveInfo(0);
game->waves = mainMenu ? getMainMenuWaves() : getDefaultWaves();
game->waveInfo = getWaveInfo(&game->waves, 0);
bzTileMapAddLayerCollisions(&game->map, LAYER_TERRAIN, COLL_LAYER_TERRAIN);
@@ -309,6 +311,90 @@ void loadMap(Game *game, const char *path) {
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
if (mainMenu) {
// Auto assign jobs
i32 numWorkers = ecs_count_id(ECS, ecs_id(Worker));
i32 numHarvestables = ecs_count_id(ECS, ecs_id(Harvestable));
typedef struct PosPair {
ecs_entity_t entity;
Position pos;
} PosPair;
PosPair *workers = bzStackAlloc(&game->stackAlloc, sizeof(*workers) * numWorkers);
PosPair *harvestables = bzStackAlloc(&game->stackAlloc, sizeof(*harvestables) * numWorkers);
i32 workerIdx = 0;
ecs_filter_t *workerFilter = ecs_filter(ECS, {
.terms = {{ecs_id(Position)}, {ecs_id(Worker)}}
});
ecs_iter_t it = ecs_filter_iter(ECS, workerFilter);
while (ecs_filter_next(&it)) {
Position *pos = ecs_field(&it, Position, 1);
for (i32 i = 0; i < it.count; i++) {
if (workerIdx >= numWorkers)
break;
workers[workerIdx++] = (PosPair) {
it.entities[i],
pos[i]
};
}
}
ecs_filter_fini(workerFilter);
i32 harvestableIdx = 0;
ecs_filter_t *harvestableFilter = ecs_filter(ECS, {
.terms = {{ecs_id(Position)}, {ecs_id(Harvestable)}}
});
it = ecs_filter_iter(ECS, harvestableFilter);
while (ecs_filter_next(&it)) {
Position *pos = ecs_field(&it, Position, 1);
for (i32 i = 0; i < it.count; i++) {
if (harvestableIdx >= numHarvestables)
break;
harvestables[harvestableIdx++] = (PosPair) {
it.entities[i],
pos[i]
};
}
}
ecs_filter_fini(harvestableFilter);
bzLogInfo("%d %d", workerIdx, harvestableIdx);
for (i32 i = 0; i < workerIdx; i++) {
PosPair nearest = harvestables[0];
f32 dst = INFINITY;
for (i32 j = 1; j < harvestableIdx; j++) {
if (nearest.entity == 0) continue;
f32 jDst = Vector2Distance(workers[i].pos, harvestables[j].pos);
if (jDst < dst) {
nearest = harvestables[j];
dst = jDst;
}
}
if (nearest.entity == 0) continue;
ResourceType resType = ecs_get(ECS, nearest.entity, Resource)->type;
setAIBehaviour(workers[i].entity, game->BTs.workerHarvest, &(AIBlackboard) {
.as.worker = {
.harvestType = resType,
.harvestTarget = nearest.entity,
.harvestPos = nearest.pos
},
.proximity = 3.0f,
});
Worker *worker = ecs_get_mut(ECS, workers[i].entity, Worker);
worker->carryRes = resType;
}
bzStackAllocFree(&game->stackAlloc, harvestables);
bzStackAllocFree(&game->stackAlloc, workers);
}
}
void unloadMap(Game *game) {
ecs_delete_with(ECS, GameEntity);