feat: Basic NPC wandering
This commit is contained in:
50
code/game/impl/npc.c
Normal file
50
code/game/impl/npc.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "../npc.h"
|
||||
#include "../world.h"
|
||||
#include "../../core/types.h"
|
||||
#include "nav.c"
|
||||
#include <stdio.h>
|
||||
|
||||
void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
switch (npc->mode) {
|
||||
case NPC_ACTION_WAITING:
|
||||
npc->waitTime+=delta;
|
||||
if(npc->waitTime > npc->maxWaitTime) {
|
||||
npc->mode = NPC_ACTION_WALKING;
|
||||
U32 next = npc->targetNavNode == 100 ? 20 : 100;
|
||||
npc->targetNavNode = next; // TODO RANDOM
|
||||
printf("Starting to nav path\n");
|
||||
npc->path = Nav_Path(world->navMesh, npc->currentNavNode, npc->targetNavNode);
|
||||
printf("done\n");
|
||||
npc->walkTimer = 0;
|
||||
printf("%*.s started walking to %d\n", Sv(npc->name), npc->targetNavNode);
|
||||
}
|
||||
break;
|
||||
case NPC_ACTION_WALKING:
|
||||
npc->walkTimer+=delta;
|
||||
if(npc->walkTimer >= NPC_SPEED){
|
||||
npc->walkTimer = 0;
|
||||
if(npc->path.nodeCount == npc->pathIndex+1){
|
||||
printf("Finished! so I'm waiting");
|
||||
npc->mode = NPC_ACTION_WAITING;
|
||||
npc->maxWaitTime = 20; // TODO RANDOM
|
||||
npc->waitTime = 0;
|
||||
npc->pathIndex = 0;
|
||||
return;
|
||||
}
|
||||
npc->pathIndex+=1;
|
||||
npc->currentNavNode = npc->path.indexes[npc->pathIndex];
|
||||
}
|
||||
NavNode cNav = world->navMesh.nodes[npc->currentNavNode];
|
||||
NavNode tNav = world->navMesh.nodes[npc->pathIndex];
|
||||
npc->collision.pos.x = cNav.pos.x * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.x * npc->walkTimer/NPC_SPEED;
|
||||
npc->collision.pos.y = cNav.pos.y * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.y * npc->walkTimer/NPC_SPEED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateNPCs(F32 delta, World *world) {
|
||||
for(int i = 0; i < world->npcCount; i++) {
|
||||
updateNPC(delta, &world->npcs[i], world);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user