-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleEvents.c
More file actions
71 lines (63 loc) · 1.91 KB
/
handleEvents.c
File metadata and controls
71 lines (63 loc) · 1.91 KB
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
#include "headers/handleEvents.h"
#include "headers/physics2d.h"
int eventHandler(Agent *agent) {
SDL_Event event;
int done = 0;
float normalSpeed = 3;
float moveDirection[4] = {0, 0, 0, 0};
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
}
}
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
if (keystate[SDL_SCANCODE_A]) {
moveDirection[0] = normalSpeed;
move(agent, moveDirection);
agent->facingLeft = 1;
moveDirection[0] = 0;
}
else if (keystate[SDL_SCANCODE_D]) {
moveDirection[1] = normalSpeed;
move(agent, moveDirection);
agent->facingLeft = 0;
moveDirection[1] = 0;
}
if (keystate[SDL_SCANCODE_W]) {
if (agent->jumpAgain == true){
agent->dy = -12;
agent->jumpAgain = false;
}
// moveDirection[2] = normalSpeed;
// move(agent, moveDirection);
// moveDirection[2] = 0;
}
else if (keystate[SDL_SCANCODE_S]) {
moveDirection[3] = normalSpeed;
move(agent, moveDirection);
moveDirection[3] = 0;
}
return done;
}
void move(Agent *agent, float moveDirection[4]) {
if (moveDirection[0] != 0) {
agent->x += -moveDirection[0];
agent->coll.leftEdge += -moveDirection[0];
agent->coll.rightEdge += -moveDirection[0];
}
if (moveDirection[1] != 0) {
agent->x += moveDirection[1];
agent->coll.leftEdge += moveDirection[1];
agent->coll.rightEdge += moveDirection[1];
}
if (moveDirection[2] != 0) {
agent->y += -moveDirection[2];
agent->coll.top += -moveDirection[2];
agent->coll.bottom += -moveDirection[2];
}
if (moveDirection[3] != 0) {
agent->y += moveDirection[3];
agent->coll.top += moveDirection[3];
agent->coll.bottom += moveDirection[3];
}
}