From e20e537e978b5f175449d8d7d76c4c7669558ef5 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 5 Oct 2025 18:05:01 +0100 Subject: [PATCH] feat: Player controller --- code/core/impl/math.c | 14 +++++++++ code/core/math.h | 3 ++ code/first.c | 21 +++---------- code/game/impl/npc.c | 6 ++-- code/game/impl/player.c | 70 +++++++++++++++++++++++++++-------------- code/game/impl/world.c | 4 ++- code/game/npc.h | 2 +- code/game/player.h | 14 ++++++++- code/game/world.h | 2 +- 9 files changed, 89 insertions(+), 47 deletions(-) diff --git a/code/core/impl/math.c b/code/core/impl/math.c index 8859ff0..440cb77 100644 --- a/code/core/impl/math.c +++ b/code/core/impl/math.c @@ -1,8 +1,22 @@ +#include V2f V2F(F32 x, F32 y) { V2f result = { x, y }; return result; } +function V2f V2f_Scale(V2f x, F32 scale) { + return V2F(x.x * scale, x.y * scale); +} + +V2f NormaliseV2F(V2f x) { + F32 magnitude = sqrtf((x.x * x.x) + (x.y +x.y)); + if(magnitude > 0.0){ + F32 inverse = 1.0f/magnitude; + return V2F(x.x*inverse, x.y*inverse); + } + return x; +} + V3f V3F(F32 x, F32 y, F32 z) { V3f result = { x, y, z }; return result; diff --git a/code/core/math.h b/code/core/math.h index 7c916a6..3ed95d6 100644 --- a/code/core/math.h +++ b/code/core/math.h @@ -121,6 +121,9 @@ function V3f M4x4F_VMul3(Mat4x4F m, V3f v); function Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp); function Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p); +function V2f NormaliseV2F(V2f x); +function V2f V2f_Scale(V2f x, F32 scale); + // Random function Random Random_Seed(U64 seed); diff --git a/code/first.c b/code/first.c index ea0c706..cf01a09 100644 --- a/code/first.c +++ b/code/first.c @@ -63,12 +63,11 @@ int main(int argc, char **argv) camera->farp = 1000.0f; game->draw.camera = camera; - game->camera.p.z = 200; World *world = M_ArenaPush(arena, World); game->world = world; world->random = Random_Seed(29237489723847); - world->npcCount = 100; - for(int i = 0; i < 100; i++) { + world->npcCount = 1023; + for(int i = 0; i < world->npcCount; i++) { NPC *npc1 = &world->npcs[i]; npc1->collision.pos.x = 15; npc1->collision.pos.y = 15; @@ -115,23 +114,11 @@ int main(int argc, char **argv) { running = false; } - if (e.type == SDL_EVENT_KEY_DOWN) { - if(e.key.key == SDLK_DOWN) { - game->camera.p.y += 5; - } - if(e.key.key == SDLK_UP) { - game->camera.p.y -= 5; - } - if(e.key.key == SDLK_RIGHT) { - game->camera.p.x += 5; - } - if(e.key.key == SDLK_LEFT) { - game->camera.p.x -= 5; - } - } ProcessEvents(&e, game->world); } UpdateWorld(1.0 / 60.0, game->world); + game->camera.p.x = game->world->player.pos.x; + game->camera.p.y = game->world->player.pos.y; int w, h; SDL_GetWindowSizeInPixels(window, &w, &h); diff --git a/code/game/impl/npc.c b/code/game/impl/npc.c index 00b03fa..0893aad 100644 --- a/code/game/impl/npc.c +++ b/code/game/impl/npc.c @@ -10,7 +10,9 @@ void updateNPC(F32 delta, NPC *npc, World *world) { if(npc->waitTime > npc->maxWaitTime) { npc->mode = NPC_ACTION_WALKING; // TODO change targets to poi's rather than just random nodes - npc->targetNavNode = Random_U32(&world->random, 0, world->navMesh->nodeCount); + do { + npc->targetNavNode = Random_U32(&world->random, 0, world->navMesh->nodeCount); + } while(npc->targetNavNode == npc->currentNavNode); npc->path = Nav_Path(world->navMesh, npc->currentNavNode, npc->targetNavNode); npc->walkTimer = 0; } @@ -21,7 +23,7 @@ void updateNPC(F32 delta, NPC *npc, World *world) { npc->walkTimer = 0; if(npc->path.nodeCount == npc->pathIndex+1){ npc->mode = NPC_ACTION_WAITING; - npc->maxWaitTime = Random_F32(&world->random, 10, 40); + npc->maxWaitTime = Random_F32(&world->random, 1, 2); npc->waitTime = 0; npc->currentNavNode = npc->targetNavNode; npc->pathIndex = 0; diff --git a/code/game/impl/player.c b/code/game/impl/player.c index 043a729..9f998da 100644 --- a/code/game/impl/player.c +++ b/code/game/impl/player.c @@ -1,33 +1,36 @@ #include "../player.h" +#include #include -#include -void PlayerUpdate(SDL_Event *event, Player *player) +void PlayerInput(SDL_Event *event, Player *player) { SDL_KeyboardEvent key = event->key; SDL_MouseButtonEvent mouseBtn = event->button; - switch (key.key) - { - case SDLK_W: - { - player->pos.y += 10; - break; - } - case SDLK_A: - { - player->pos.x -= 10; - break; - } - case SDLK_D: - { - player->pos.x += 10; - break; - } - case SDLK_S: - { - player->pos.y -= 10; - break; - } + if(event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) { + bool val = event->type == SDL_EVENT_KEY_DOWN; + switch (key.key) + { + case SDLK_W: + { + player->controls.upDown = val; + break; + } + case SDLK_A: + { + player->controls.leftDown = val; + break; + } + case SDLK_D: + { + player->controls.rightDown = val; + break; + } + case SDLK_S: + { + player->controls.downDown = val; + break; + } + } } if (mouseBtn.clicks == 1) { @@ -35,3 +38,22 @@ void PlayerUpdate(SDL_Event *event, Player *player) player->bulletsLoaded -= 1; } } + +void PlayerUpdate(F32 delta, Player *player) { + V2f dir = V2F(0, 0); + if(player->controls.upDown) { + dir.y -= 1; + } + if(player->controls.downDown) { + dir.y += 1; + } + if(player->controls.leftDown) { + dir.x -= 1; + } + if(player->controls.rightDown) { + dir.x += 1; + } + dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta); + player->pos.x += dir.x; + player->pos.y += dir.y; +} diff --git a/code/game/impl/world.c b/code/game/impl/world.c index a06fd6b..e6aec5a 100644 --- a/code/game/impl/world.c +++ b/code/game/impl/world.c @@ -6,6 +6,7 @@ void UpdateWorld(F32 delta, World *world) { UpdateNPCs(delta, world); + PlayerUpdate(delta, &world->player); } void UpdateNPCs(F32 delta, World *world) @@ -18,11 +19,12 @@ void UpdateNPCs(F32 delta, World *world) void ProcessEvents(SDL_Event *event, World *world) { - PlayerUpdate(event, &world->player); + PlayerInput(event, &world->player); } void RenderWorld(World *world, D_Context *draw) { for(int i = 0; i < world->npcCount; i++) { D_Rect(draw, world->npcs[i].collision.pos.x, world->npcs[i].collision.pos.y, .texture = 1); } + D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1); } diff --git a/code/game/npc.h b/code/game/npc.h index b0f77aa..0692f09 100644 --- a/code/game/npc.h +++ b/code/game/npc.h @@ -5,7 +5,7 @@ #include "npc_look.h" #include "../core/types.h" -#define NPC_SPEED 1.0f +#define NPC_SPEED 0.1f typedef enum NPC_ACTION NPC_ACTION; enum NPC_ACTION { diff --git a/code/game/player.h b/code/game/player.h index fddf014..dffdc0e 100644 --- a/code/game/player.h +++ b/code/game/player.h @@ -6,13 +6,25 @@ #include +#define PLAYER_SPEED 10.0f + +typedef struct ControlState ControlState; +struct ControlState { + bool rightDown; + bool leftDown; + bool upDown; + bool downDown; +}; + typedef struct Player Player; struct Player { V2f pos; U32 bulletsLoaded; + ControlState controls; }; -function void PlayerUpdate(SDL_Event *event, Player *player); +function void PlayerInput(SDL_Event *event, Player *player); +function void PlayerUpdate(F32 delta, Player *player); #endif // LD_GAME_PLAYER_H_ diff --git a/code/game/world.h b/code/game/world.h index b8f1763..896faae 100644 --- a/code/game/world.h +++ b/code/game/world.h @@ -24,7 +24,7 @@ struct World { //// NPCs U32 npcCount; - NPC npcs[128]; + NPC npcs[1024]; ////Bandit // The bandit the player is after.