summaryrefslogtreecommitdiff
path: root/src/game/player.cpp
blob: 993292cdf7d1de5c2e3b3a55a82c62278f3ab218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#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[SDL_SCANCODE_F1] ) {
    input_capture_mouse( false );
  }

  if( input.mouse_captured ) {
    *yaw += input.mouse.pos_delta.x * input.mouse_sensitivity;
    p->rot.x -= input.mouse.pos_delta.y * input.mouse_sensitivity;
    
    if( p->rot.x > 89.f ) p->rot.x = 89.f;
    if( p->rot.x < -89.f ) p->rot.x = -89.f;
  }

  if( !input.mouse_captured ) {
    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 );
}