feat: Added basic shooting

This commit is contained in:
2025-10-05 22:35:34 +01:00
parent 3eb8683ce3
commit 319bb441ed
10 changed files with 39 additions and 13 deletions

View File

@@ -222,3 +222,12 @@ F32 Random_Bilateral(Random *rnd) {
F32 result = -1.0f + (2.0f * Random_Unilateral(rnd)); F32 result = -1.0f + (2.0f * Random_Unilateral(rnd));
return result; return result;
} }
V2f V2f_Clip(V2f screen_xy, V2f screen_size) {
V2f result;
result.x = ((screen_xy.x / screen_size.w) * 2.0f) - 1.0f;
result.y = ((screen_xy.y / screen_size.h) * 2.0f) - 1.0f;
return result;
}

View File

@@ -147,4 +147,6 @@ function U32 Random_U32(Random *rnd, U32 min, U32 max);
function F32 Random_Unilateral(Random *rnd); function F32 Random_Unilateral(Random *rnd);
function F32 Random_Bilateral(Random *rnd); function F32 Random_Bilateral(Random *rnd);
V2f V2f_Clip(V2f screen_xy, V2f screen_size);
#endif // LD_CORE_MATH_H_ #endif // LD_CORE_MATH_H_

View File

@@ -27,6 +27,9 @@
#include "game/impl/bandit.c" #include "game/impl/bandit.c"
#include "game/testnavmesh.h" #include "game/testnavmesh.h"
const int width = 1280;
const int height = 720;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
(void)argc; (void)argc;
@@ -80,16 +83,12 @@ int main(int argc, char **argv)
npc1->collision.pos.x = 15; npc1->collision.pos.x = 15;
npc1->collision.pos.y = 15; npc1->collision.pos.y = 15;
npc1->collision.size.x = 1; npc1->collision.size.x = 1;
npc1->collision.size.y = 1; npc1->collision.size.y = 2;
npc1->name = S("Matt"); npc1->name = S("Matt");
npc1->mode = NPC_ACTION_WAITING; npc1->mode = NPC_ACTION_WAITING;
npc1->waitTime = 0; npc1->waitTime = 0;
npc1->maxWaitTime = 5; npc1->maxWaitTime = 5000;
npc1->currentNavNode = 0; npc1->currentNavNode = 0;
npc1->collision.pos.x = 15;
npc1->collision.pos.y = 15;
npc1->collision.size.x = 10;
npc1->collision.size.y = 10;
} }
Bandit *badman = &world->bandit; Bandit *badman = &world->bandit;
@@ -107,6 +106,7 @@ int main(int argc, char **argv)
world->navMesh = &TestNavMesh; world->navMesh = &TestNavMesh;
world->npcPOI[0] = 100; world->npcPOI[0] = 100;
world->player.world = world;
world->player.pos.x = 0; world->player.pos.x = 0;
world->player.pos.y = 0; world->player.pos.y = 0;
world->player.bulletsLoaded = PLAYER_BULLET_COUNT; world->player.bulletsLoaded = PLAYER_BULLET_COUNT;
@@ -126,7 +126,11 @@ int main(int argc, char **argv)
{ {
running = false; running = false;
} }
V3f projection = G_CameraUnproject(&game->camera, V2f_Clip(
V2F(e.button.x, e.button.y),
V2F(width, height)
));
game->world->mouseProjected = V2F(projection.x, projection.y);
ProcessEvents(&e, game->world); ProcessEvents(&e, game->world);
} }

View File

@@ -13,5 +13,6 @@ struct AABB
function bool AABB_Collide(AABB a, AABB b); function bool AABB_Collide(AABB a, AABB b);
function bool AABB_Point(AABB a, V2f v); function bool AABB_Point(AABB a, V2f v);
function bool AABB_Slab(V2f origin, V2f point, AABB a); function bool AABB_Slab(V2f origin, V2f point, AABB a);
function V2f AABB_Centre(AABB a);
#endif // LD_GAME_AABB_H_ #endif // LD_GAME_AABB_H_

View File

@@ -34,3 +34,7 @@ bool AABB_Slab(V2f origin, V2f point, AABB a)
return tMax >= tMin; return tMax >= tMin;
} }
V2f AABB_Centre(AABB a) {
return V2F(a.pos.x + a.size.x/2, a.pos.y + a.size.y/2);
}

View File

@@ -42,8 +42,8 @@ void PlayerInput(SDL_Event *event, Player *player)
// shooting // shooting
player->bulletsLoaded -= 1; player->bulletsLoaded -= 1;
player->controls.shot = true; player->controls.shot = true;
player->shotPos = V2F(mouseBtn.x, mouseBtn.y); player->shotPos = player->world->mouseProjected;
printf("shot %f %f\n", mouseBtn.x, mouseBtn.y); printf("shot %f %f\n", player->shotPos.x, player->shotPos.y);
} else if(player->reloadTimer == 0) { } else if(player->reloadTimer == 0) {
player->reloadTimer = PLAYER_RELOAD_TIME; player->reloadTimer = PLAYER_RELOAD_TIME;
printf("reloading\n"); printf("reloading\n");

View File

@@ -1,6 +1,7 @@
#include "../world.h" #include "../world.h"
#include "../npc.h" #include "../npc.h"
#include "../player.h" #include "../player.h"
#include "../aabb.h"
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.h>
void UpdateWorld(F32 delta, World *world) void UpdateWorld(F32 delta, World *world)
@@ -30,8 +31,9 @@ void ProcessEvents(SDL_Event *event, World *world)
void RenderWorld(World *world, D_Context *draw) { void RenderWorld(World *world, D_Context *draw) {
for(U32 i = 0; i < world->npcCount; i++) { for(U32 i = 0; i < world->npcCount; i++) {
NPC npc = world->npcs[i]; NPC npc = world->npcs[i];
D_Rect(draw, npc.collision.pos.x, npc.collision.pos.y, .texture = 1); V2f drawPos = AABB_Centre(npc.collision);
D_Rect(draw, npc.collision.pos.x, npc.collision.pos.y, .texture = 0, .dim = npc.collision.size); 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);
} }
D_Rect(draw, world->bandit.collision.pos.x, world->bandit.collision.pos.y, .texture = 9); 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); D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1);

View File

@@ -22,7 +22,9 @@ struct ControlState {
typedef struct Player Player; typedef struct Player Player;
struct Player struct Player
{ {
World *world;
V2f pos; V2f pos;
World_Area currentArea;
U32 bulletsLoaded; U32 bulletsLoaded;
ControlState controls; ControlState controls;
V2f shotPos; V2f shotPos;

View File

@@ -1,7 +1,6 @@
#if !defined(LD_GAME_WORLD_H_) #if !defined(LD_GAME_WORLD_H_)
#define LD_GAME_WORLD_H_ #define LD_GAME_WORLD_H_
#include "player.h"
#include "npc.h" #include "npc.h"
#include "bandit.h" #include "bandit.h"
#include "../core/math.h" #include "../core/math.h"
@@ -14,10 +13,13 @@ enum World_Area {
}; };
typedef struct World World; typedef struct World World;
#include "player.h"
struct World { struct World {
//// Static stuff //// Static stuff
NavMesh *navMesh; NavMesh *navMesh;
Random random; Random random;
V2f mouseProjected;
//// Player //// Player
Player player; Player player;

2
linux
View File

@@ -57,7 +57,7 @@ glslangValidator -o "assets/shaders/basic.frag.spv" --target-env "vulkan1.3" "..
echo "[Building source]" echo "[Building source]"
COMPILER_OPTS="-Wall -Wno-missing-braces -Wno-unused-function -Ideps/stb -I../code" COMPILER_OPTS="-Wall -Wno-override-init-side-effects -Wno-missing-braces -Wno-unused-function -Ideps/stb -I../code"
LINKER_OPTS="-lSDL3 -lm" LINKER_OPTS="-lSDL3 -lm"
if [[ $release == 1 ]] if [[ $release == 1 ]]