Files
ld58/code/game/impl/npc.c

51 lines
1.7 KiB
C
Raw Normal View History

#include "game/npc.h"
#include "game/world.h"
#include "core/types.h"
2025-10-05 00:25:37 +01:00
#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);
2025-10-05 00:25:37 +01:00
}
break;
case NPC_ACTION_WALKING:
npc->walkTimer+=delta;
if(npc->walkTimer >= NPC_SPEED){
npc->walkTimer = 0;
if(npc->path.nodeCount == npc->pathIndex+1){
2025-10-05 00:31:52 +01:00
printf("Finished! so I'm waiting\n");
2025-10-05 00:25:37 +01:00
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];
2025-10-05 00:25:37 +01:00
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(U32 i = 0; i < world->npcCount; i++) {
2025-10-05 00:25:37 +01:00
updateNPC(delta, &world->npcs[i], world);
}
}
2025-10-05 00:25:37 +01:00