feat: Player controller

This commit is contained in:
2025-10-05 18:05:01 +01:00
parent 98095c907f
commit e20e537e97
9 changed files with 89 additions and 47 deletions

View File

@@ -1,8 +1,22 @@
#include <math.h>
V2f V2F(F32 x, F32 y) { V2f V2F(F32 x, F32 y) {
V2f result = { x, y }; V2f result = { x, y };
return result; 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 V3F(F32 x, F32 y, F32 z) {
V3f result = { x, y, z }; V3f result = { x, y, z };
return result; return result;

View File

@@ -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_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp);
function Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p); 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 // Random
function Random Random_Seed(U64 seed); function Random Random_Seed(U64 seed);

View File

@@ -63,12 +63,11 @@ int main(int argc, char **argv)
camera->farp = 1000.0f; camera->farp = 1000.0f;
game->draw.camera = camera; game->draw.camera = camera;
game->camera.p.z = 200;
World *world = M_ArenaPush(arena, World); World *world = M_ArenaPush(arena, World);
game->world = world; game->world = world;
world->random = Random_Seed(29237489723847); world->random = Random_Seed(29237489723847);
world->npcCount = 100; world->npcCount = 1023;
for(int i = 0; i < 100; i++) { for(int i = 0; i < world->npcCount; i++) {
NPC *npc1 = &world->npcs[i]; NPC *npc1 = &world->npcs[i];
npc1->collision.pos.x = 15; npc1->collision.pos.x = 15;
npc1->collision.pos.y = 15; npc1->collision.pos.y = 15;
@@ -115,23 +114,11 @@ int main(int argc, char **argv)
{ {
running = false; 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); ProcessEvents(&e, game->world);
} }
UpdateWorld(1.0 / 60.0, 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; int w, h;
SDL_GetWindowSizeInPixels(window, &w, &h); SDL_GetWindowSizeInPixels(window, &w, &h);

View File

@@ -10,7 +10,9 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
if(npc->waitTime > npc->maxWaitTime) { if(npc->waitTime > npc->maxWaitTime) {
npc->mode = NPC_ACTION_WALKING; npc->mode = NPC_ACTION_WALKING;
// TODO change targets to poi's rather than just random nodes // TODO change targets to poi's rather than just random nodes
do {
npc->targetNavNode = Random_U32(&world->random, 0, world->navMesh->nodeCount); 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->path = Nav_Path(world->navMesh, npc->currentNavNode, npc->targetNavNode);
npc->walkTimer = 0; npc->walkTimer = 0;
} }
@@ -21,7 +23,7 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
npc->walkTimer = 0; npc->walkTimer = 0;
if(npc->path.nodeCount == npc->pathIndex+1){ if(npc->path.nodeCount == npc->pathIndex+1){
npc->mode = NPC_ACTION_WAITING; 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->waitTime = 0;
npc->currentNavNode = npc->targetNavNode; npc->currentNavNode = npc->targetNavNode;
npc->pathIndex = 0; npc->pathIndex = 0;

View File

@@ -1,37 +1,59 @@
#include "../player.h" #include "../player.h"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_keycode.h> #include <SDL3/SDL_keycode.h>
#include <stdio.h>
void PlayerUpdate(SDL_Event *event, Player *player) void PlayerInput(SDL_Event *event, Player *player)
{ {
SDL_KeyboardEvent key = event->key; SDL_KeyboardEvent key = event->key;
SDL_MouseButtonEvent mouseBtn = event->button; SDL_MouseButtonEvent mouseBtn = event->button;
if(event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
bool val = event->type == SDL_EVENT_KEY_DOWN;
switch (key.key) switch (key.key)
{ {
case SDLK_W: case SDLK_W:
{ {
player->pos.y += 10; player->controls.upDown = val;
break; break;
} }
case SDLK_A: case SDLK_A:
{ {
player->pos.x -= 10; player->controls.leftDown = val;
break; break;
} }
case SDLK_D: case SDLK_D:
{ {
player->pos.x += 10; player->controls.rightDown = val;
break; break;
} }
case SDLK_S: case SDLK_S:
{ {
player->pos.y -= 10; player->controls.downDown = val;
break; break;
} }
} }
}
if (mouseBtn.clicks == 1) if (mouseBtn.clicks == 1)
{ {
// shooting // shooting
player->bulletsLoaded -= 1; 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;
}

View File

@@ -6,6 +6,7 @@
void UpdateWorld(F32 delta, World *world) void UpdateWorld(F32 delta, World *world)
{ {
UpdateNPCs(delta, world); UpdateNPCs(delta, world);
PlayerUpdate(delta, &world->player);
} }
void UpdateNPCs(F32 delta, World *world) void UpdateNPCs(F32 delta, World *world)
@@ -18,11 +19,12 @@ void UpdateNPCs(F32 delta, World *world)
void ProcessEvents(SDL_Event *event, World *world) void ProcessEvents(SDL_Event *event, World *world)
{ {
PlayerUpdate(event, &world->player); PlayerInput(event, &world->player);
} }
void RenderWorld(World *world, D_Context *draw) { void RenderWorld(World *world, D_Context *draw) {
for(int i = 0; i < world->npcCount; i++) { 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->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);
} }

View File

@@ -5,7 +5,7 @@
#include "npc_look.h" #include "npc_look.h"
#include "../core/types.h" #include "../core/types.h"
#define NPC_SPEED 1.0f #define NPC_SPEED 0.1f
typedef enum NPC_ACTION NPC_ACTION; typedef enum NPC_ACTION NPC_ACTION;
enum NPC_ACTION { enum NPC_ACTION {

View File

@@ -6,13 +6,25 @@
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.h>
#define PLAYER_SPEED 10.0f
typedef struct ControlState ControlState;
struct ControlState {
bool rightDown;
bool leftDown;
bool upDown;
bool downDown;
};
typedef struct Player Player; typedef struct Player Player;
struct Player struct Player
{ {
V2f pos; V2f pos;
U32 bulletsLoaded; 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_ #endif // LD_GAME_PLAYER_H_

View File

@@ -24,7 +24,7 @@ struct World {
//// NPCs //// NPCs
U32 npcCount; U32 npcCount;
NPC npcs[128]; NPC npcs[1024];
////Bandit ////Bandit
// The bandit the player is after. // The bandit the player is after.