summaryrefslogtreecommitdiff
path: root/src/game/player.cpp
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
committernavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
commitf8b92ce3aa08b1445c9f956d8166830946562d12 (patch)
tree94e63a5aec9f8f52b577f56799e0c9201fd976a5 /src/game/player.cpp
a
Diffstat (limited to 'src/game/player.cpp')
-rw-r--r--src/game/player.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/game/player.cpp b/src/game/player.cpp
new file mode 100644
index 0000000..87786df
--- /dev/null
+++ b/src/game/player.cpp
@@ -0,0 +1,82 @@
+#include "../game.h"
+#include "SDL_scancode.h"
+#include "objlist.h"
+#include "world/trace.h"
+#include "player.h"
+
+F32 PLAYER_HULL_HEIGHT = 50.f;
+F32 PLAYER_HULL_WIDTH = 32.f;
+
+PLAYER* player_create( VEC3 origin, F32 yaw ) {
+ PLAYER* p = obj_add<PLAYER>( "localplayer" );
+ p->pos = origin;
+ p->rot = { 0, yaw, 0 };
+ p->health = 100;
+ p->keeponlevel = 1;
+ p->velocity = {};
+
+ p->mins = {
+ -PLAYER_HULL_WIDTH * .5f,
+ -PLAYER_HULL_WIDTH * .5f,
+ 0
+ };
+
+ p->maxs = {
+ PLAYER_HULL_WIDTH * .5f,
+ PLAYER_HULL_WIDTH * .5f,
+ PLAYER_HULL_HEIGHT
+ };
+
+ return p;
+}
+
+void player_input( GAME_DATA* game, PLAYER* p ) {
+ F32* yaw = &p->rot.y;
+ VEC3* pos = &p->pos;
+
+ if( input.keys[(U8)'a'] )
+ *yaw -= 360.f * TICK_INTERVAL * 0.5f;
+ if( input.keys[(U8)'d'] )
+ *yaw += 360.f * TICK_INTERVAL * 0.5f;
+
+ if( input.keys[SDL_SCANCODE_UP] ) {
+ if( p->rot.x < 89.f )
+ p->rot.x += 360.f * TICK_INTERVAL * 0.5f;
+ }
+ if( input.keys[SDL_SCANCODE_DOWN] ) {
+ if( p->rot.x > -89.f )
+ p->rot.x -= 360.f * TICK_INTERVAL * 0.5f;
+ }
+
+ *yaw = remainderf( *yaw, 360.f );
+
+
+ VEC3 wishmove;
+ VEC3 dir = m_radial_offset( *yaw, 70.f );
+ if( input.keys[(U8)'w'] ) {
+ wishmove = dir * TICK_INTERVAL;
+ }
+ if( input.keys[(U8)'s'] ) {
+ wishmove = dir * -TICK_INTERVAL;
+ }
+
+ // todo : should never be false
+ if( vec_len( wishmove ) > BSP_TRACE_EPSILON && game->state.map->bsp ) {
+ BSP_TRACE tr{};
+ AABB aabb{};
+ tr.in_start = *pos;
+ tr.in_end = *pos + wishmove * 2.f;
+ aabb.min = p->mins;
+ aabb.max = p->maxs;
+
+ bsp_trace( &tr, game->state.map->bsp, aabb );
+ if( !tr.hit )
+ *pos += wishmove;
+ else
+ *pos += wishmove * tr.frac;
+ }
+}
+
+VEC3 player_get_view_pos( PLAYER* p ) {
+ return VEC3( p->pos.x, p->pos.y, p->pos.z + p->eyeoffset );
+}