Compare commits

2 Commits

Author SHA1 Message Date
d54b4df4c2 player health 2025-10-06 17:45:58 +01:00
fcc7adfb22 added badman shooting and reloading 2025-10-06 17:30:49 +01:00
8 changed files with 89 additions and 14 deletions

View File

@@ -125,15 +125,25 @@ int main(int argc, char **argv)
badman->waitTime = 0; badman->waitTime = 0;
badman->maxWaitTime = 2; badman->maxWaitTime = 2;
badman->poiCount = 2; badman->poiCount = 2;
badman->shootoutTimer = 1.5;
badman->agroRadius = 600.0;
badman->bullets = 6;
badman->shootDelay = 1;
badman->accuracyRange = 0.25;
badman->reloadTime = 2.5;
badman->reloadTimer = 0;
badman->pointsOfInterest[0] = 937; badman->pointsOfInterest[0] = 937;
badman->pointsOfInterest[1] = 12; badman->pointsOfInterest[1] = 12;
world->npcPOI[0] = 100; world->npcPOI[0] = 100;
world->player.world = world; world->player.world = world;
world->player.pos.x = 0; world->player.collision.pos.x = 0;
world->player.pos.y = 0; world->player.collision.pos.y = 0;
world->player.collision.size.x = 1;
world->player.collision.size.y = 2;
world->player.bulletsLoaded = PLAYER_BULLET_COUNT; world->player.bulletsLoaded = PLAYER_BULLET_COUNT;
world->player.reloadTimer = 0; world->player.reloadTimer = 0;
world->player.health = 3;
world->player.currentArea = WORLD_AREA_OUTSIDE; world->player.currentArea = WORLD_AREA_OUTSIDE;
for(int i =0; i< 4200; i++) { for(int i =0; i< 4200; i++) {
world->map[i] = map[i]; world->map[i] = map[i];
@@ -216,7 +226,7 @@ int main(int argc, char **argv)
world->propTypes[12].assetHandle=D_ImageHandle(&game->draw, S("house_int")); world->propTypes[12].assetHandle=D_ImageHandle(&game->draw, S("house_int"));
world->propTypes[12].scale=6.875f; world->propTypes[12].scale=6.875f;
} }
game->editor.enabled = true; game->editor.enabled = false;
game->editor.mode = G_EDITOR_MODE_TILE; game->editor.mode = G_EDITOR_MODE_TILE;
game->editor.currentLevel = WORLD_AREA_OUTSIDE; game->editor.currentLevel = WORLD_AREA_OUTSIDE;
@@ -343,8 +353,8 @@ int main(int argc, char **argv)
if(!game->editor.enabled) { if(!game->editor.enabled) {
UpdateWorld(1.0f / 60.0f, game->world); UpdateWorld(1.0f / 60.0f, game->world);
game->camera.p.x = game->world->player.pos.x; game->camera.p.x = game->world->player.collision.pos.x;
game->camera.p.y = game->world->player.pos.y; game->camera.p.y = game->world->player.collision.pos.y;
} }
D_AnimationUpdate(&animation, 1.0f / 250.0f); D_AnimationUpdate(&animation, 1.0f / 250.0f);

View File

@@ -14,5 +14,6 @@ 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); function V2f AABB_Centre(AABB a);
function bool AABB_Circle(F32 rad, V2f radOrigin, AABB a);
#endif // LD_GAME_AABB_H_ #endif // LD_GAME_AABB_H_

View File

@@ -2,11 +2,13 @@
#define LD_GAME_BANDIT_H_ #define LD_GAME_BANDIT_H_
typedef enum BANDIT_ACTION BANDIT_ACTION; typedef enum BANDIT_ACTION BANDIT_ACTION;
enum BANDIT_ACTION { enum BANDIT_ACTION
{
BANDIT_WAITING, BANDIT_WAITING,
BANDIT_WALKING, BANDIT_WALKING,
BANDIT_RUNNING, BANDIT_RUNNING,
BANDIT_SHOOTING, BANDIT_SHOOTING,
BANDIT_SHOOTOUT,
}; };
typedef struct Bandit Bandit; typedef struct Bandit Bandit;
@@ -50,12 +52,18 @@ struct Bandit {
F32 shootDelay; F32 shootDelay;
// After each shot this is set to shootDelay; // After each shot this is set to shootDelay;
F32 shootCooldownTimer; F32 shootCooldownTimer;
// Countdown to shootout
F32 shootoutTimer;
// How long it takes them to reload. // How long it takes them to reload.
F32 reloadTime; F32 reloadTime;
// When gun is empty this is set to reloadTime.
F32 reloadTimer;
// Accuracy, their shots can vary between this angle either side (rads) // Accuracy, their shots can vary between this angle either side (rads)
F32 accuracyRange; F32 accuracyRange;
// A the circle around the bandit where they will trigger the quicktime reaction scene // A the circle around the bandit where they will trigger the quicktime reaction scene
F32 agroRadius; F32 agroRadius;
}; };
function V2f shootTowards(Bandit* bandit, V2f target, Random* r);
#endif // LD_GAME_BANDIT_H_ #endif // LD_GAME_BANDIT_H_

View File

@@ -38,3 +38,11 @@ bool AABB_Slab(V2f origin, V2f point, AABB a)
V2f AABB_Centre(AABB a) { V2f AABB_Centre(AABB a) {
return V2F(a.pos.x + a.size.x/2, a.pos.y + a.size.y/2); return V2F(a.pos.x + a.size.x/2, a.pos.y + a.size.y/2);
} }
bool AABB_Circle(F32 rad, V2f radOrigin, AABB a)
{
V2f aCentre = AABB_Centre(a);
F32 xSq = (Abs(aCentre.x) - Abs(radOrigin.x)) * (Abs(aCentre.x) - Abs(radOrigin.x));
F32 ySq = (Abs(aCentre.y) - Abs(radOrigin.y)) * (Abs(aCentre.y) - Abs(radOrigin.y));
return SDL_sqrt(xSq + ySq) < rad;
}

View File

@@ -1,13 +1,27 @@
#include "game/world.h" #include "game/world.h"
#include "game/bandit.h" #include "game/bandit.h"
V2f shootTowards(Bandit *bandit, V2f target, Random* r)
{
V2f shooterV2 = bandit->collision.pos;
F32 randX = Random_F32(r, -bandit->accuracyRange, bandit->accuracyRange);
F32 randY = Random_F32(r, -bandit->accuracyRange, bandit->accuracyRange);
return V2F(shooterV2.x + (target.x - shooterV2.x) * (1 + randX), shooterV2.x + (target.y - shooterV2.y) * (1 + randY));
}
void UpdateBandit(F32 delta, Bandit *bandit, World *world) { void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
if ( if (
world->player.controls.shot && AABB_Slab(world->player.pos, world->player.shotPos, bandit->collision) && bandit->currentArea == world->player.currentArea) world->player.controls.shot && AABB_Slab(world->player.collision.pos, world->player.shotPos, bandit->collision) && bandit->currentArea == world->player.currentArea)
{ {
printf("You shot the bandit %*.s\n", Sv(bandit->name)); printf("You shot the bandit %*.s\n", Sv(bandit->name));
bandit->health--; bandit->health--;
} }
if (AABB_Circle(bandit->agroRadius, AABB_Centre(bandit->collision), world->player.collision) && !(bandit->mode == BANDIT_SHOOTING || bandit->mode == BANDIT_SHOOTOUT))
{
printf("begin shootout");
// shootout time o.o
bandit->mode = BANDIT_SHOOTOUT;
}
switch (bandit->mode) { switch (bandit->mode) {
case BANDIT_WAITING: case BANDIT_WAITING:
bandit->waitTime+=delta; bandit->waitTime+=delta;
@@ -41,7 +55,33 @@ void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
bandit->collision.pos.x = cNav.pos.x * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.x * bandit->walkTimer/NPC_SPEED; bandit->collision.pos.x = cNav.pos.x * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.x * bandit->walkTimer/NPC_SPEED;
bandit->collision.pos.y = cNav.pos.y * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.y * bandit->walkTimer/NPC_SPEED; bandit->collision.pos.y = cNav.pos.y * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.y * bandit->walkTimer/NPC_SPEED;
break; break;
// TODO Shooting case BANDIT_SHOOTOUT:
bandit->shootoutTimer-=delta;
if(bandit->shootoutTimer < 0){
bandit->mode=BANDIT_SHOOTING;
}
break;
case BANDIT_SHOOTING:
bandit->shootCooldownTimer -= delta;
bandit->reloadTimer -= delta;
if (bandit->shootCooldownTimer < 0 && bandit->reloadTimer < 0)
{
printf("shoot at player");
bandit->bullets--;
bandit->shootCooldownTimer = bandit->shootDelay;
V2f banditShot = shootTowards(bandit, world->player.collision.pos, &world->random);
if(AABB_Slab(bandit->collision.pos, banditShot, world->player.collision)){
// gets shot lmao
printf("hit");
world->player.health--;
}
if(bandit->bullets == 0){
printf("enemy reload");
bandit->bullets = 6;
bandit->reloadTimer = bandit->reloadTime;
}
}
break;
// TODO Running away // TODO Running away
} }
} }

View File

@@ -53,6 +53,10 @@ void PlayerInput(SDL_Event *event, Player *player)
void PlayerUpdate(F32 delta, Player *player) { void PlayerUpdate(F32 delta, Player *player) {
player->controls.shot = false; player->controls.shot = false;
V2f dir = V2F(0, 0); V2f dir = V2F(0, 0);
if(player->health == 0){
printf("dead :(");
player->health = 3;
}
if(player->controls.upDown) { if(player->controls.upDown) {
dir.y -= 1; dir.y -= 1;
} }
@@ -73,6 +77,6 @@ void PlayerUpdate(F32 delta, Player *player) {
} }
} }
dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta); dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta);
player->pos.x += dir.x; player->collision.pos.x += dir.x;
player->pos.y += dir.y; player->collision.pos.y += dir.y;
} }

View File

@@ -9,6 +9,9 @@
void UpdateWorld(F32 delta, World *world) void UpdateWorld(F32 delta, World *world)
{ {
if(world->bandit.mode == BANDIT_SHOOTOUT){
delta = delta/4;
}
UpdateBandit(delta, &world->bandit, world); UpdateBandit(delta, &world->bandit, world);
UpdateNPCs(delta, world); UpdateNPCs(delta, world);
PlayerUpdate(delta, &world->player); PlayerUpdate(delta, &world->player);
@@ -21,7 +24,7 @@ void UpdateNPCs(F32 delta, World *world)
NPC *npc = &world->npcs[i]; NPC *npc = &world->npcs[i];
UpdateNPC(delta, npc, world); UpdateNPC(delta, npc, world);
if ( if (
world->player.controls.shot && AABB_Slab(world->player.pos, world->player.shotPos, npc->collision) && npc->currentArea == world->player.currentArea) world->player.controls.shot && AABB_Slab(world->player.collision.pos, world->player.shotPos, npc->collision) && npc->currentArea == world->player.currentArea)
{ {
printf("You shot %*.s\n", Sv(world->npcs[i].name)); printf("You shot %*.s\n", Sv(world->npcs[i].name));
} }
@@ -69,7 +72,7 @@ void RenderWorld(World *world, D_Context *draw) {
V2f drawPos = AABB_Centre(world->bandit.collision); V2f drawPos = AABB_Centre(world->bandit.collision);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 9); D_Rect(draw, drawPos.x, drawPos.y, .texture = 9);
} }
D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1); D_Rect(draw, world->player.collision.pos.x, world->player.collision.pos.y, .texture = 1);
} }
void SaveWorld(M_Arena *arena, World *world) { void SaveWorld(M_Arena *arena, World *world) {

View File

@@ -5,6 +5,7 @@
#include "../core/macros.h" #include "../core/macros.h"
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.h>
#include "aabb.h"
#define PLAYER_SPEED 10.0f #define PLAYER_SPEED 10.0f
#define PLAYER_RELOAD_TIME 1.5f #define PLAYER_RELOAD_TIME 1.5f
@@ -23,12 +24,12 @@ typedef struct Player Player;
struct Player struct Player
{ {
World *world; World *world;
V2f pos; AABB collision;
World_Area currentArea; World_Area currentArea;
U32 bulletsLoaded; U32 bulletsLoaded;
ControlState controls; ControlState controls;
V2f shotPos; V2f shotPos;
U32 health;
F32 reloadTimer; F32 reloadTimer;
}; };