feat: Initial bandit roaming
This commit is contained in:
41
code/game/impl/bandit.c
Normal file
41
code/game/impl/bandit.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "game/world.h"
|
||||
#include "game/bandit.h"
|
||||
|
||||
void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
|
||||
switch (bandit->mode) {
|
||||
case BANDIT_WAITING:
|
||||
bandit->waitTime+=delta;
|
||||
if(bandit->waitTime > bandit->maxWaitTime) {
|
||||
bandit->mode = BANDIT_WALKING;
|
||||
do {
|
||||
U32 randomChoice = Random_U32(&world->random, 0, bandit->poiCount);
|
||||
bandit->targetNavNode = bandit->pointsOfInterest[randomChoice];
|
||||
} while(bandit->targetNavNode == bandit->currentNavNode);
|
||||
bandit->path = Nav_Path(world->navMesh, bandit->currentNavNode, bandit->targetNavNode);
|
||||
bandit->walkTimer = 0;
|
||||
}
|
||||
break;
|
||||
case BANDIT_WALKING:
|
||||
bandit->walkTimer+=delta;
|
||||
if(bandit->walkTimer >= NPC_SPEED){
|
||||
bandit->walkTimer = 0;
|
||||
if(bandit->path.nodeCount == bandit->pathIndex+1){
|
||||
bandit->mode = BANDIT_WAITING;
|
||||
bandit->maxWaitTime = Random_F32(&world->random, 20, 140);
|
||||
bandit->waitTime = 0;
|
||||
bandit->currentNavNode = bandit->targetNavNode;
|
||||
bandit->pathIndex = 0;
|
||||
return;
|
||||
}
|
||||
bandit->currentNavNode = bandit->path.indexes[bandit->pathIndex];
|
||||
bandit->pathIndex+=1;
|
||||
}
|
||||
NavNode cNav = world->navMesh->nodes[bandit->currentNavNode];
|
||||
NavNode tNav = world->navMesh->nodes[bandit->path.indexes[bandit->pathIndex]];
|
||||
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;
|
||||
break;
|
||||
// TODO Shooting
|
||||
// TODO Running away
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "core/types.h"
|
||||
#include "core/math.h"
|
||||
|
||||
void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
void UpdateNPC(F32 delta, NPC *npc, World *world) {
|
||||
switch (npc->mode) {
|
||||
case NPC_ACTION_WAITING:
|
||||
npc->waitTime+=delta;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#include "../player.h"
|
||||
#include <SDL3/SDL_events.h>
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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) {
|
||||
@@ -32,11 +34,21 @@ void PlayerInput(SDL_Event *event, Player *player)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mouseBtn.clicks == 1)
|
||||
{
|
||||
// shooting
|
||||
player->bulletsLoaded -= 1;
|
||||
}
|
||||
if (
|
||||
event->type == SDL_EVENT_MOUSE_BUTTON_DOWN
|
||||
&& mouseBtn.button == SDL_BUTTON_LEFT
|
||||
) {
|
||||
if(player->bulletsLoaded > 0) {
|
||||
// shooting
|
||||
player->bulletsLoaded -= 1;
|
||||
player->controls.shot = true;
|
||||
player->shotPos = V2F(mouseBtn.x, mouseBtn.y);
|
||||
printf("shot %f %f\n", mouseBtn.x, mouseBtn.y);
|
||||
} else if(player->reloadTimer == 0) {
|
||||
player->reloadTimer = PLAYER_RELOAD_TIME;
|
||||
printf("reloading\n");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerUpdate(F32 delta, Player *player) {
|
||||
@@ -53,6 +65,13 @@ void PlayerUpdate(F32 delta, Player *player) {
|
||||
if(player->controls.rightDown) {
|
||||
dir.x += 1;
|
||||
}
|
||||
if(player->reloadTimer > 0) {
|
||||
player->reloadTimer-=delta;
|
||||
if(player->reloadTimer <= 0) {
|
||||
player->bulletsLoaded = PLAYER_BULLET_COUNT;
|
||||
player->reloadTimer = 0;
|
||||
}
|
||||
}
|
||||
dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta);
|
||||
player->pos.x += dir.x;
|
||||
player->pos.y += dir.y;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
void UpdateWorld(F32 delta, World *world)
|
||||
{
|
||||
UpdateBandit(delta, &world->bandit, world);
|
||||
UpdateNPCs(delta, world);
|
||||
PlayerUpdate(delta, &world->player);
|
||||
}
|
||||
@@ -13,7 +14,11 @@ void UpdateNPCs(F32 delta, World *world)
|
||||
{
|
||||
for (U32 i = 0; i < world->npcCount; i++)
|
||||
{
|
||||
updateNPC(delta, &world->npcs[i], world);
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +29,10 @@ void ProcessEvents(SDL_Event *event, World *world)
|
||||
|
||||
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);
|
||||
NPC npc = world->npcs[i];
|
||||
D_Rect(draw, npc.collision.pos.x, npc.collision.pos.y, .texture = 1);
|
||||
D_Rect(draw, npc.collision.pos.x, npc.collision.pos.y, .texture = 0, .dim = npc.collision.size);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user