Compare commits

..

2 Commits

Author SHA1 Message Date
50efa6d8c0 Merge remote-tracking branch 'origin' 2025-10-05 00:30:33 +01:00
ab96fa3eeb feat: Basic NPC wandering 2025-10-05 00:25:37 +01:00
6 changed files with 35125 additions and 2 deletions

View File

@@ -7,11 +7,16 @@
#include "core/core.h"
#include "core/types.h"
#include "game/npc.h"
#include "os/core.h"
#include "vulkan/core.h"
#include "game/core.h"
#include "game/world.h"
#include "game/impl/npc.c"
#include "game/testnavmesh.h"
int main(int argc, char **argv) {
(void) argc;
(void) argv;
@@ -73,6 +78,28 @@ int main(int argc, char **argv) {
Player player;
player.pos.x = 0;
player.pos.y = 0;
World world = {
.npcCount = 2,
.npcs = {
{
.collision = {{10, 10}, {10, 10}},
.name = S("Matt"),
.mode = NPC_ACTION_WAITING,
.waitTime = 0,
.maxWaitTime = 5,
.currentNavNode = 87
},{
.collision = {{15, 15}, {10, 10}},
.name = S("James"),
.mode = NPC_ACTION_WAITING,
.waitTime = 0,
.maxWaitTime = 10,
.currentNavNode = 0
}
},
.navMesh = TestNavMesh,
.npcPOI = {100}
};
while (running)
{
SDL_Event e;
@@ -84,6 +111,7 @@ int main(int argc, char **argv) {
running = false;
}
}
UpdateNPCs(1.0/60.0, &world);
int w, h;
SDL_GetWindowSizeInPixels(window, &w, &h);

View File

@@ -1,6 +1,5 @@
#if !defined(LD_GAME_AABB_H_)
#define LD_GAME_AABB_H_
#include "../core/types.h"
#include "../core/macros.h"

49
code/game/impl/npc.c Normal file
View File

@@ -0,0 +1,49 @@
#include "../npc.h"
#include "../world.h"
#include "../../core/types.h"
#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);
}
}

View File

@@ -1,10 +1,31 @@
#if !defined(LD_GAME_NPC_H_)
#define LD_GAME_NPC_H_
#include "aabb.h"
#include "nav.h"
#include "../core/types.h"
#define NPC_SPEED 0.2
typedef enum NPC_ACTION NPC_ACTION;
enum NPC_ACTION {
// Waiting can be at any point of interest
NPC_ACTION_WAITING,
// Walking is when they are actively moving somewhere
NPC_ACTION_WALKING,
};
typedef struct NPC NPC;
struct NPC {
AABB collision;
Str8 name;
NPC_ACTION mode;
F32 waitTime;
F32 maxWaitTime;
NavPath path;
U32 currentNavNode;
U32 pathIndex;
U32 targetNavNode;
F32 walkTimer;
};
#endif // LD_GAME_NPC_H_

35012
code/game/testnavmesh.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,24 @@
#if !defined(LD_GAME_WORLD_H_)
#define LD_GAME_WORLD_H_
#include "player.h"
#include "npc.h"
// Areas are which
enum AREA {
WORLD_AREA_OUTSIDE = 1,
WORLD_AREA_SALOON = 1 << 1,
};
typedef struct World World;
struct World {
NPC npcs[128];
U32 npcCount;
NPC npcs[128];
NavMesh navMesh;
// NPC points of interest, places to walk to.
U32 npcPOI[256];
};
function void updateWorld(F32 delta, World *world);
function void UpdateNPCs(F32 delta, World *world);
#endif // LD_GAME_WORLD_H_