From 64a84e3a8d0fb8b34711095f4b431c1523a88c5a Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 5 Oct 2025 23:36:50 +0100 Subject: [PATCH] feat: different rooms added feat: shooting bandit added --- code/first.c | 11 ++++++----- code/game/bandit.h | 1 + code/game/impl/bandit.c | 8 ++++++++ code/game/impl/player.c | 2 +- code/game/impl/world.c | 26 ++++++++++++++++++-------- code/game/npc.h | 1 + code/game/world.h | 4 ++-- 7 files changed, 37 insertions(+), 16 deletions(-) diff --git a/code/first.c b/code/first.c index 63d1dce..d80911f 100644 --- a/code/first.c +++ b/code/first.c @@ -14,7 +14,6 @@ #include "core/core.h" #include "core/types.h" -#include "game/npc.h" #include "os/core.h" #include "vulkan/core.h" @@ -77,7 +76,7 @@ int main(int argc, char **argv) World *world = M_ArenaPush(arena, World); game->world = world; world->random = Random_Seed(29237489723847); - world->npcCount = 1; + world->npcCount = 2; for(U32 i = 0; i < world->npcCount; i++) { NPC *npc1 = &world->npcs[i]; npc1->collision.pos.x = 15; @@ -86,16 +85,17 @@ int main(int argc, char **argv) npc1->collision.size.y = 2; npc1->name = S("Matt"); npc1->mode = NPC_ACTION_WAITING; + npc1->currentArea = i; npc1->waitTime = 0; - npc1->maxWaitTime = 5000; + npc1->maxWaitTime = 5; npc1->currentNavNode = 0; } Bandit *badman = &world->bandit; badman->collision.pos.x = 15; badman->collision.pos.y = 15; - badman->collision.size.x = 10; - badman->collision.size.y = 10; + badman->collision.size.x = 1; + badman->collision.size.y = 2; badman->name = S("Leroy Brown"); badman->mode = BANDIT_WAITING; badman->waitTime = 0; @@ -111,6 +111,7 @@ int main(int argc, char **argv) world->player.pos.y = 0; world->player.bulletsLoaded = PLAYER_BULLET_COUNT; world->player.reloadTimer = 0; + world->player.currentArea = 0; } bool running = true; diff --git a/code/game/bandit.h b/code/game/bandit.h index 3814017..a55f973 100644 --- a/code/game/bandit.h +++ b/code/game/bandit.h @@ -14,6 +14,7 @@ struct Bandit { //// Personal AABB collision; Str8 name; + World_Area currentArea; //// Actions BANDIT_ACTION mode; diff --git a/code/game/impl/bandit.c b/code/game/impl/bandit.c index 5aeb2a4..f8e3f35 100644 --- a/code/game/impl/bandit.c +++ b/code/game/impl/bandit.c @@ -2,6 +2,14 @@ #include "game/bandit.h" void UpdateBandit(F32 delta, Bandit *bandit, World *world) { + if( + world->player.controls.shot + && AABB_Point(bandit->collision, world->player.shotPos) + && bandit->currentArea == world->player.currentArea + ) { + printf("You shot the bandit %*.s\n", Sv(bandit->name)); + bandit->health--; + } switch (bandit->mode) { case BANDIT_WAITING: bandit->waitTime+=delta; diff --git a/code/game/impl/player.c b/code/game/impl/player.c index 07d57eb..8375d1e 100644 --- a/code/game/impl/player.c +++ b/code/game/impl/player.c @@ -5,7 +5,6 @@ void PlayerInput(SDL_Event *event, Player *player) { - player->controls.shot = false; SDL_KeyboardEvent key = event->key; SDL_MouseButtonEvent mouseBtn = event->button; if(event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) { @@ -52,6 +51,7 @@ void PlayerInput(SDL_Event *event, Player *player) } void PlayerUpdate(F32 delta, Player *player) { + player->controls.shot = false; V2f dir = V2F(0, 0); if(player->controls.upDown) { dir.y -= 1; diff --git a/code/game/impl/world.c b/code/game/impl/world.c index bbca252..3c36632 100644 --- a/code/game/impl/world.c +++ b/code/game/impl/world.c @@ -15,10 +15,14 @@ void UpdateNPCs(F32 delta, World *world) { for (U32 i = 0; i < world->npcCount; i++) { - UpdateNPC(delta, &world->npcs[i], world); - if(world->player.controls.shot && AABB_Point(world->npcs[i].collision, world->player.shotPos)) { - // TODO we need to unproject the mouse location !!! - printf("You shot %.*s\n", Sv(world->npcs[i].name)); + NPC *npc = &world->npcs[i]; + UpdateNPC(delta, npc, world); + if( + world->player.controls.shot + && AABB_Point(npc->collision, world->player.shotPos) + && npc->currentArea == world->player.currentArea + ) { + printf("You shot %*.s\n", Sv(world->npcs[i].name)); } } } @@ -31,11 +35,17 @@ void ProcessEvents(SDL_Event *event, World *world) void RenderWorld(World *world, D_Context *draw) { for(U32 i = 0; i < world->npcCount; i++) { NPC npc = world->npcs[i]; - V2f drawPos = AABB_Centre(npc.collision); - D_Rect(draw, drawPos.x, drawPos.y, .texture = 1); - D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = npc.collision.size, .flags = D_RECT_IGNORE_ASPECT); + if(npc.currentArea == world->player.currentArea) { + V2f drawPos = AABB_Centre(npc.collision); + D_Rect(draw, drawPos.x, drawPos.y, .texture = 1); + D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = npc.collision.size, .flags = D_RECT_IGNORE_ASPECT); + } + } + if(world->bandit.currentArea == world->player.currentArea) { + V2f drawPos = AABB_Centre(world->bandit.collision); + D_Rect(draw, drawPos.x, drawPos.y, .texture = 9); + D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = world->bandit.collision.size, .flags = D_RECT_IGNORE_ASPECT); } - D_Rect(draw, world->bandit.collision.pos.x, world->bandit.collision.pos.y, .texture = 9); 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 3805ba4..eff6157 100644 --- a/code/game/npc.h +++ b/code/game/npc.h @@ -22,6 +22,7 @@ struct NPC { AABB collision; Str8 name; NPC_LOOK look; + World_Area currentArea; bool customPOI; U32 customPOICount; diff --git a/code/game/world.h b/code/game/world.h index eeaaf8a..be8a8aa 100644 --- a/code/game/world.h +++ b/code/game/world.h @@ -1,8 +1,6 @@ #if !defined(LD_GAME_WORLD_H_) #define LD_GAME_WORLD_H_ -#include "npc.h" -#include "bandit.h" #include "../core/math.h" // Areas are which @@ -14,6 +12,8 @@ enum World_Area { typedef struct World World; #include "player.h" +#include "npc.h" +#include "bandit.h" struct World { //// Static stuff