-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.c
141 lines (124 loc) · 3.53 KB
/
events.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "events.h"
#include "raycast.h"
vec3_t get_wanted_movement_vector(InputState * state, Camera * camera)
{
vec3_t movement = vec3(0, 0, 0);
if (state->moving_forward) {
movement = v3_add(movement, get_Camera_forward(camera));
}
if (state->moving_backward) {
movement = v3_sub(movement, get_Camera_forward(camera));
}
if (state->moving_left) {
movement = v3_sub(movement, get_Camera_right(camera));
}
if (state->moving_right) {
movement = v3_add(movement, get_Camera_right(camera));
}
// normalize the vector
movement = v3_norm(movement);
return movement;
}
bool handle_event(SDL_Event * e, Camera * camera, Physics * physics, Map * map,
SFXContext * sfx_context, unsigned int delta_ticks,
InputState * state)
{
(void) delta_ticks;
if (e->type == SDL_QUIT) {
return false;
} else if (e->type == SDL_MOUSEMOTION) {
camera->rotation.x -= e->motion.xrel / 100.f;
camera->rotation.y -= e->motion.yrel / 100.f;
camera->rotation.y =
CLAMP(camera->rotation.y, -M_PI / 2 + 0.025, M_PI / 2 - 0.025);
} else if (e->type == SDL_KEYDOWN) {
switch (e->key.keysym.sym) {
case SDLK_SPACE:
if (physics->touches_ground) {
physics->velocity.y = .2f;
state->jumping = true;
}
break;
case SDLK_w:
state->moving_forward = true;
break;
case SDLK_s:
state->moving_backward = true;
break;
case SDLK_a:
state->moving_left = true;
break;
case SDLK_d:
state->moving_right = true;
break;
case SDLK_LSHIFT:
state->running = true;
break;
}
} else if (e->type == SDL_KEYUP) {
switch (e->key.keysym.sym) {
case SDLK_SPACE:
state->jumping = false;
break;
case SDLK_w:
state->moving_forward = false;
break;
case SDLK_s:
state->moving_backward = false;
break;
case SDLK_a:
state->moving_left = false;
break;
case SDLK_d:
state->moving_right = false;
break;
case SDLK_LSHIFT:
state->running = false;
break;
}
} else if (e->type == SDL_MOUSEBUTTONDOWN) {
if (e->button.button == SDL_BUTTON_LEFT) {
vec3_t normal = vec3(0, 0, 0);
vec3_t position = vec3(0, 0, 0);
vec3_t real_position = vec3(camera->position.x / BLOCK_SIZE,
camera->position.y / BLOCK_SIZE,
camera->position.z / BLOCK_SIZE);
if (raycast_block
(real_position, get_Camera_lookAt(camera), map, &position,
&normal)) {
play_Audio_at(sfx_context, &sfx_context->effects.break_block,
&position);
set_block_type(map, position.x, position.y, position.z, 0, 0);
}
} else if (e->button.button == SDL_BUTTON_RIGHT) {
vec3_t normal = vec3(0, 0, 0);
vec3_t real_position = vec3(camera->position.x / BLOCK_SIZE,
camera->position.y / BLOCK_SIZE,
camera->position.z / BLOCK_SIZE);
vec3_t position = vec3(0, 0, 0);
if (raycast_block
(real_position, get_Camera_lookAt(camera), map, &position,
&normal)) {
play_Audio_at(sfx_context, &sfx_context->effects.place_block,
&position);
vec3_t temp = v3_add(position, normal);
set_block_type(map, temp.x, temp.y, temp.z, 0, 1);
}
}
}
return true;
}
void handle_input(InputState * state, Camera * camera, Physics * physics)
{
float AXIS_VELOCITY = 0.1;
if (state->running) {
AXIS_VELOCITY *= 2.0;
}
vec3_t new_vel =
!physics->
touches_ground ? physics->velocity : get_wanted_movement_vector(state,
camera);
new_vel = v3_muls(v3_norm(new_vel), AXIS_VELOCITY);
physics->velocity.x = new_vel.x;
physics->velocity.z = new_vel.z;
}