Basic wood chopping

This commit is contained in:
2023-12-10 11:08:40 +01:00
parent b410867902
commit 11832ec1cc
10 changed files with 224 additions and 45 deletions

View File

@@ -3,18 +3,44 @@
#include "game_state.h"
#include "input.h"
#include "pathfinding.h"
#include <math.h>
#include <raymath.h>
void entityClearTasks(const ecs_entity_t entity) {
ecs_remove(ECS, entity, HarvestTask);
}
bool entitySetPath(const ecs_entity_t entity, const Vector2 target, Game *game) {
const Vector2 *pPath = ecs_get(ECS, entity, Position);
BZ_ASSERT(pPath);
const Vector2 start = *pPath;
Path path = {NULL, 0};
const bool foundPath = pathfindAStar(&(PathfindingDesc) {
.start = start,
.target = target,
.map = &game->map,
.outPath = &path,
.pool = game->pools.pathData,
.alloc = &game->stackAlloc,
});
if (foundPath) {
BZ_ASSERT(path.paths);
ecs_set_ptr(ECS, entity, Path, &path);
return true;
}
return false;
}
static Position getBottomLeftPos(Position pos, Size size) {
return (Position) {pos.x - size.x * 0.5f, pos.y - size.y * 0.5f};
}
void entitySpatialRemove(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *pos = ecs_field(it, Position, 1);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID , 2);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 1);
for (i32 i = 0; i < it->count; i++) {
bzSpatialGridRemove(game->entityGrid, spatialID[i]);
@@ -169,6 +195,80 @@ void entityFollowPath(ecs_iter_t *it) {
}
}
static ecs_entity_t findNearestStorage(ResourceType type) {
ecs_filter_t *storageFilter = ecs_filter(ECS, {
.terms = {{ecs_id(Storage)} }
});
ecs_iter_t it = ecs_filter_iter(ECS, storageFilter);
ecs_entity_t closest = 0;
while (ecs_filter_next(&it)) {
Storage *storage = ecs_field(&it, Storage, 1);
for (i32 i = 0; i < it.count; i++) {
if (true || storage[i].capacity[type]) {
closest = it.entities[i];
}
}
}
ecs_filter_fini(storageFilter);
return closest;
}
void entityHarvestTaskSystem(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
const Position *position = ecs_field(it, Position, 1);
const Rotation *rotation = ecs_field(it, Rotation, 2);
const HarvestTask *harvestTask = ecs_field(it, HarvestTask, 3);
for (i32 i = 0; i < it->count; i++) {
const ecs_entity_t entity = it->entities[i];
const ecs_entity_t targetEntity = harvestTask[i].entity;
const Position *pTarget = ecs_get(ECS, targetEntity, Position);
BZ_ASSERT(pTarget);
const Position target = *pTarget;
const f32 DST_LIMIT = 10.0f;
Resource *resource = ecs_get_mut(ECS, targetEntity, Resource);
if (resource->amount <= 0) {
ecs_delete(ECS, targetEntity);
ecs_remove(ECS, entity, HarvestTask);
continue;
}
const f32 dst = Vector2Distance(position[i], target);
if (!ecs_has(ECS, entity, Path) && dst > DST_LIMIT) {
bzLogInfo("%.2f", dst);
// Pathfind to target
entitySetPath(entity, target, game);
continue;
} else if (dst < DST_LIMIT && !ecs_has(ECS, entity, Path)) {
if (!ecs_has(ECS, entity, Path)) {
bzLogInfo("Mine");
resource->amount -= 5;
}
ecs_remove(ECS, entity, Path);
// MINE
// find nearest warehouse for wood
ecs_entity_t storage = findNearestStorage(RES_WOOD);
if (storage) {
const Position *storagePos = ecs_get(ECS, storage, Position);
BZ_ASSERT(storagePos);
entitySetPath(entity, *storagePos, game);
}
}
// Harvest
const i32 carryCapacity = 5;
}
}
void entityUpdateAnimationState(ecs_iter_t *it) {
Velocity *velocity = ecs_field(it, Velocity, 1);
//AnimationType *animType = ecs_field(it, AnimationType , 2);