Add chase/evade behaviour
This commit is contained in:
@@ -115,6 +115,16 @@ void inputUnitAction(Game *game, InputState *input) {
|
||||
const i32 numUnits = ecs_query_entity_count(query);
|
||||
BZ_ASSERT(numUnits > 0);
|
||||
|
||||
ecs_entity_t *units = bzStackAlloc(&game->stackAlloc, sizeof(*units) * numUnits);
|
||||
i32 unitIdx = 0;
|
||||
ecs_iter_t it = ecs_query_iter(ECS, query);
|
||||
while (ecs_query_next(&it)) {
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
units[unitIdx++] = entity;
|
||||
}
|
||||
}
|
||||
|
||||
const MouseButton actionBtn = input->mapping.secondaryBtn;
|
||||
|
||||
input->cursor = CURSOR_NONE;
|
||||
@@ -166,47 +176,46 @@ void inputUnitAction(Game *game, InputState *input) {
|
||||
qsort(harvestables, numHarvestables, sizeof(*harvestables), entityFloatPairCmp);
|
||||
i32 idxHarvestable = 0;
|
||||
|
||||
ecs_defer_begin(ECS);
|
||||
ecs_iter_t it = ecs_query_iter(ECS, query);
|
||||
while (ecs_query_next(&it)) {
|
||||
for (i32 i = 0; i < it.count; i++) {
|
||||
if (idxHarvestable >= numHarvestables)
|
||||
break;
|
||||
ecs_entity_t entity = it.entities[i];
|
||||
for (i32 i = 0; i < unitIdx; i++) {
|
||||
if (idxHarvestable >= numHarvestables)
|
||||
break;
|
||||
ecs_entity_t entity = units[i];
|
||||
|
||||
EntityFloatPair harvestEntity = {0, 0};
|
||||
while (idxHarvestable < numHarvestables) {
|
||||
harvestEntity = harvestables[idxHarvestable++];
|
||||
Harvestable *harvestable = ecs_get_mut(ECS, harvestEntity.entity, Harvestable);
|
||||
if (harvestable->harvestCount >= harvestable->harvestLimit)
|
||||
continue;
|
||||
harvestable->harvestCount++;
|
||||
break;
|
||||
}
|
||||
Position target = *ecs_get(ECS, harvestEntity.entity, Position);
|
||||
HitBox targetHB = *ecs_get(ECS, harvestEntity.entity, HitBox);
|
||||
target = entityGetCenter(target, targetHB);
|
||||
|
||||
f32 proximity = 4.0f;
|
||||
|
||||
Worker *worker = ecs_get_mut(ECS, entity, Worker);
|
||||
worker->carryRes = resource.type;
|
||||
|
||||
setAIBehaviour(entity, game->BTs.workerHarvest, &(AIBlackboard) {
|
||||
.as.worker = {
|
||||
.harvestType = resource.type,
|
||||
.harvestTarget = harvestEntity.entity,
|
||||
.harvestPos = target,
|
||||
},
|
||||
.proximity = proximity,
|
||||
});
|
||||
EntityFloatPair harvestEntity = {0, 0};
|
||||
while (idxHarvestable < numHarvestables) {
|
||||
harvestEntity = harvestables[idxHarvestable++];
|
||||
Harvestable *harvestable = ecs_get_mut(ECS, harvestEntity.entity, Harvestable);
|
||||
if (harvestable->harvestCount >= harvestable->harvestLimit)
|
||||
continue;
|
||||
harvestable->harvestCount++;
|
||||
break;
|
||||
}
|
||||
Position target = *ecs_get(ECS, harvestEntity.entity, Position);
|
||||
HitBox targetHB = *ecs_get(ECS, harvestEntity.entity, HitBox);
|
||||
target = entityGetCenter(target, targetHB);
|
||||
|
||||
f32 proximity = 4.0f;
|
||||
|
||||
Worker *worker = ecs_get_mut(ECS, entity, Worker);
|
||||
worker->carryRes = resource.type;
|
||||
|
||||
setAIBehaviour(entity, game->BTs.worker, &(AIBlackboard) {
|
||||
.as.worker = {
|
||||
.harvestType = resource.type,
|
||||
.harvestTarget = harvestEntity.entity,
|
||||
.harvestPos = target,
|
||||
},
|
||||
.proximity = proximity,
|
||||
});
|
||||
}
|
||||
ecs_defer_end(ECS);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ((taskEntity = queryEntity(game->entityGrid, input->mouseWorld, ecs_id(Owner))) && ecs_get(ECS, taskEntity, Owner)->player != game->player) {
|
||||
input->cursor = CURSOR_ATTACK;
|
||||
}
|
||||
|
||||
// Unit place position
|
||||
Vector2 *positions = bzStackAlloc(&game->stackAlloc, sizeof(*positions) * numUnits);
|
||||
Vector2 start = Vector2Zero();
|
||||
@@ -254,13 +263,14 @@ void inputUnitAction(Game *game, InputState *input) {
|
||||
|
||||
for (i32 i = 0; i < unitIdx; i++) {
|
||||
ecs_entity_t entity = entities[i].entity;
|
||||
setAIBehaviour(entity, game->BTs.moveTo, &(AIBlackboard) {
|
||||
setAIBehaviour(entity, game->BTs.unit, &(AIBlackboard) {
|
||||
.moveToPos = positions[i],
|
||||
.proximity = 1.0f,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
bzStackAllocFree(&game->stackAlloc, units);
|
||||
}
|
||||
|
||||
void updatePlayerInput() {
|
||||
@@ -450,7 +460,7 @@ void drawPlayerInputUIGround() {
|
||||
}
|
||||
}
|
||||
|
||||
if (input->unitPlacePos) {
|
||||
if (input->unitPlacePos && input->cursor == CURSOR_NONE && input->numUnits >= 2) {
|
||||
for (i32 i = 0; i < input->numUnits; i++) {
|
||||
DrawCircleV(input->unitPlacePos[i], 2.0f, RED);
|
||||
}
|
||||
|
||||
@@ -406,11 +406,11 @@ void drawMainMenuUI(Game *game, f32 dt) {
|
||||
if (uiMainMenuButton("Play", true)) {
|
||||
setScreen(game, SCREEN_GAME);
|
||||
unloadMap(game);
|
||||
//loadMap(game, "assets/maps/tree_test.tmj");
|
||||
//loadMap(game, "assets/maps/entity_test.tmj");
|
||||
//loadMap(game, "assets/maps/worker_test.tmj");
|
||||
//loadMap(game, "assets/maps/battle_test.tmj");
|
||||
loadMap(game, "assets/maps/map_01.tmj", false);
|
||||
//loadMap(game, "assets/maps/tree_test.tmj", false);
|
||||
//loadMap(game, "assets/maps/entity_test.tmj", false);
|
||||
//loadMap(game, "assets/maps/worker_test.tmj", false);
|
||||
loadMap(game, "assets/maps/battle_test.tmj", false);
|
||||
//loadMap(game, "assets/maps/map_01.tmj", false);
|
||||
}
|
||||
if (uiMainMenuButton("Settings", true)) {
|
||||
setScreen(game, SCREEN_SETTINGS);
|
||||
|
||||
Reference in New Issue
Block a user