feat: Player controller
This commit is contained in:
@@ -10,7 +10,9 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
if(npc->waitTime > npc->maxWaitTime) {
|
||||
npc->mode = NPC_ACTION_WALKING;
|
||||
// TODO change targets to poi's rather than just random nodes
|
||||
npc->targetNavNode = Random_U32(&world->random, 0, world->navMesh->nodeCount);
|
||||
do {
|
||||
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->walkTimer = 0;
|
||||
}
|
||||
@@ -21,7 +23,7 @@ void updateNPC(F32 delta, NPC *npc, World *world) {
|
||||
npc->walkTimer = 0;
|
||||
if(npc->path.nodeCount == npc->pathIndex+1){
|
||||
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->currentNavNode = npc->targetNavNode;
|
||||
npc->pathIndex = 0;
|
||||
|
||||
@@ -1,33 +1,36 @@
|
||||
#include "../player.h"
|
||||
#include <SDL3/SDL_events.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_MouseButtonEvent mouseBtn = event->button;
|
||||
switch (key.key)
|
||||
{
|
||||
case SDLK_W:
|
||||
{
|
||||
player->pos.y += 10;
|
||||
break;
|
||||
}
|
||||
case SDLK_A:
|
||||
{
|
||||
player->pos.x -= 10;
|
||||
break;
|
||||
}
|
||||
case SDLK_D:
|
||||
{
|
||||
player->pos.x += 10;
|
||||
break;
|
||||
}
|
||||
case SDLK_S:
|
||||
{
|
||||
player->pos.y -= 10;
|
||||
break;
|
||||
}
|
||||
if(event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
|
||||
bool val = event->type == SDL_EVENT_KEY_DOWN;
|
||||
switch (key.key)
|
||||
{
|
||||
case SDLK_W:
|
||||
{
|
||||
player->controls.upDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_A:
|
||||
{
|
||||
player->controls.leftDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_D:
|
||||
{
|
||||
player->controls.rightDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_S:
|
||||
{
|
||||
player->controls.downDown = val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mouseBtn.clicks == 1)
|
||||
{
|
||||
@@ -35,3 +38,22 @@ void PlayerUpdate(SDL_Event *event, Player *player)
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
void UpdateWorld(F32 delta, World *world)
|
||||
{
|
||||
UpdateNPCs(delta, world);
|
||||
PlayerUpdate(delta, &world->player);
|
||||
}
|
||||
|
||||
void UpdateNPCs(F32 delta, World *world)
|
||||
@@ -18,11 +19,12 @@ void UpdateNPCs(F32 delta, World *world)
|
||||
|
||||
void ProcessEvents(SDL_Event *event, World *world)
|
||||
{
|
||||
PlayerUpdate(event, &world->player);
|
||||
PlayerInput(event, &world->player);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user