#include "../game.h" #include "SDL_scancode.h" #include "objlist.h" #include "world/trace.h" #include #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( "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 capture_mouse( PLAYER* p ) { F32* yaw = &p->input.cam.pos.y; F32* pitch = &p->input.cam.pos.x; if( input.keys[SDL_SCANCODE_F1] ) { input_capture_mouse( false ); } if( input.mouse_captured ) { *yaw += input.mouse.pos_delta.x * input.mouse_sensitivity; *pitch -= input.mouse.pos_delta.y * input.mouse_sensitivity; if( *pitch > 89.f ) *pitch = 89.f; if( *pitch < -89.f ) *pitch = -89.f; input_reset_mouse_accumulator(); } *yaw = remainderf( *yaw, 360.f ); p->rot.x = *pitch; p->rot.y = *yaw; } void capture_move_keys( PLAYER* p ) { VEC2* move = &p->input.move; if( input.keys[input.binds.fwd] ) { if( !p->input.fwd_held ) move->x = 1.f; p->input.fwd_held = true; } else { if( p->input.fwd_held ) { if( p->input.bk_held ) move->x = -1.f; else move->x = 0.f; } p->input.fwd_held = false; } if( input.keys[input.binds.back] ) { if( !p->input.bk_held ) move->x = -1.f; p->input.bk_held = true; } else { if( p->input.bk_held ) { if( p->input.fwd_held ) move->x = 1.f; else move->x = 0.f; } p->input.bk_held = false; } if( input.keys[input.binds.left] ) { if( !p->input.left_held ) move->y = -1.f; p->input.left_held = true; } else { if( p->input.left_held ) { if( p->input.right_held ) move->y = 1.f; else move->y = 0.f; } p->input.left_held = false; } if( input.keys[input.binds.right] ) { if( !p->input.right_held ) move->y = 1.f; p->input.right_held = true; } else { if( p->input.right_held ) { if( p->input.left_held ) move->y = -1.f; else move->y = 0.f; } p->input.right_held = false; } } void player_input( GAME_DATA* game, PLAYER* p ) { if( !p ) return; capture_mouse( p ); capture_move_keys( p ); } void player_move( GAME_DATA* game, PLAYER* p ) { VEC2 move = p->input.move; F32 yawrad = m_deg2rad( p->rot.y ); VEC3 wishdir = { move.x * cosf( yawrad ) - move.y * sinf( yawrad ), move.y * cosf( yawrad ) + move.x * sinf( yawrad ), 0 }; VEC3 vel = wishdir * 70.f; p->velocity = vel; VEC3 wishmove = vel * 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 = p->pos; tr.in_end = tr.in_start + wishmove; aabb.min = p->mins; aabb.max = p->maxs; bsp_trace( &tr, game->state.map->bsp, aabb ); if( !tr.hit ) p->pos += wishmove; else { p->pos += wishmove * tr.frac; } } } VEC3 player_get_view_pos( PLAYER* p ) { return VEC3( p->pos.x, p->pos.y, p->pos.z + p->eyeoffset ); }