feat: Player controller
This commit is contained in:
@@ -1,8 +1,22 @@
|
||||
#include <math.h>
|
||||
V2f V2F(F32 x, F32 y) {
|
||||
V2f result = { x, y };
|
||||
return result;
|
||||
}
|
||||
|
||||
function V2f V2f_Scale(V2f x, F32 scale) {
|
||||
return V2F(x.x * scale, x.y * scale);
|
||||
}
|
||||
|
||||
V2f NormaliseV2F(V2f x) {
|
||||
F32 magnitude = sqrtf((x.x * x.x) + (x.y +x.y));
|
||||
if(magnitude > 0.0){
|
||||
F32 inverse = 1.0f/magnitude;
|
||||
return V2F(x.x*inverse, x.y*inverse);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
V3f V3F(F32 x, F32 y, F32 z) {
|
||||
V3f result = { x, y, z };
|
||||
return result;
|
||||
|
||||
@@ -121,6 +121,9 @@ function V3f M4x4F_VMul3(Mat4x4F m, V3f v);
|
||||
function Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp);
|
||||
function Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p);
|
||||
|
||||
function V2f NormaliseV2F(V2f x);
|
||||
function V2f V2f_Scale(V2f x, F32 scale);
|
||||
|
||||
// Random
|
||||
|
||||
function Random Random_Seed(U64 seed);
|
||||
|
||||
21
code/first.c
21
code/first.c
@@ -63,12 +63,11 @@ int main(int argc, char **argv)
|
||||
camera->farp = 1000.0f;
|
||||
|
||||
game->draw.camera = camera;
|
||||
game->camera.p.z = 200;
|
||||
World *world = M_ArenaPush(arena, World);
|
||||
game->world = world;
|
||||
world->random = Random_Seed(29237489723847);
|
||||
world->npcCount = 100;
|
||||
for(int i = 0; i < 100; i++) {
|
||||
world->npcCount = 1023;
|
||||
for(int i = 0; i < world->npcCount; i++) {
|
||||
NPC *npc1 = &world->npcs[i];
|
||||
npc1->collision.pos.x = 15;
|
||||
npc1->collision.pos.y = 15;
|
||||
@@ -115,23 +114,11 @@ int main(int argc, char **argv)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
if (e.type == SDL_EVENT_KEY_DOWN) {
|
||||
if(e.key.key == SDLK_DOWN) {
|
||||
game->camera.p.y += 5;
|
||||
}
|
||||
if(e.key.key == SDLK_UP) {
|
||||
game->camera.p.y -= 5;
|
||||
}
|
||||
if(e.key.key == SDLK_RIGHT) {
|
||||
game->camera.p.x += 5;
|
||||
}
|
||||
if(e.key.key == SDLK_LEFT) {
|
||||
game->camera.p.x -= 5;
|
||||
}
|
||||
}
|
||||
ProcessEvents(&e, game->world);
|
||||
}
|
||||
UpdateWorld(1.0 / 60.0, game->world);
|
||||
game->camera.p.x = game->world->player.pos.x;
|
||||
game->camera.p.y = game->world->player.pos.y;
|
||||
|
||||
int w, h;
|
||||
SDL_GetWindowSizeInPixels(window, &w, &h);
|
||||
|
||||
@@ -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
|
||||
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,37 +1,59 @@
|
||||
#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;
|
||||
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->pos.y += 10;
|
||||
player->controls.upDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_A:
|
||||
{
|
||||
player->pos.x -= 10;
|
||||
player->controls.leftDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_D:
|
||||
{
|
||||
player->pos.x += 10;
|
||||
player->controls.rightDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_S:
|
||||
{
|
||||
player->pos.y -= 10;
|
||||
player->controls.downDown = val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mouseBtn.clicks == 1)
|
||||
{
|
||||
// shooting
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "npc_look.h"
|
||||
#include "../core/types.h"
|
||||
|
||||
#define NPC_SPEED 1.0f
|
||||
#define NPC_SPEED 0.1f
|
||||
|
||||
typedef enum NPC_ACTION NPC_ACTION;
|
||||
enum NPC_ACTION {
|
||||
|
||||
@@ -6,13 +6,25 @@
|
||||
|
||||
#include <SDL3/SDL_events.h>
|
||||
|
||||
#define PLAYER_SPEED 10.0f
|
||||
|
||||
typedef struct ControlState ControlState;
|
||||
struct ControlState {
|
||||
bool rightDown;
|
||||
bool leftDown;
|
||||
bool upDown;
|
||||
bool downDown;
|
||||
};
|
||||
|
||||
typedef struct Player Player;
|
||||
struct Player
|
||||
{
|
||||
V2f pos;
|
||||
U32 bulletsLoaded;
|
||||
ControlState controls;
|
||||
};
|
||||
|
||||
function void PlayerUpdate(SDL_Event *event, Player *player);
|
||||
function void PlayerInput(SDL_Event *event, Player *player);
|
||||
function void PlayerUpdate(F32 delta, Player *player);
|
||||
|
||||
#endif // LD_GAME_PLAYER_H_
|
||||
|
||||
@@ -24,7 +24,7 @@ struct World {
|
||||
|
||||
//// NPCs
|
||||
U32 npcCount;
|
||||
NPC npcs[128];
|
||||
NPC npcs[1024];
|
||||
|
||||
////Bandit
|
||||
// The bandit the player is after.
|
||||
|
||||
Reference in New Issue
Block a user