-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
190 lines (171 loc) · 4.78 KB
/
input.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "input.h"
void chadKey(Game*);
void startAstar(Game*);
Input::Input(Game *g, View *v, Openal *o)
{
view = v;
game = g;
openal = o;
dpy = view->GetDisplay();
view->CenterCursor();
openal->initopenal();
}
int Input::CheckInput()
{
while (XPending(dpy)) {
XEvent e;
XNextEvent(dpy, &e);
CheckMouse(&e);
int done = CheckKeys(&e);
if (done == 1)
return 1;
}
return 0;
}
int Input::CheckKeys(XEvent *e)
{
if (e->type == KeyPress) {
int key = XLookupKeysym(&e->xkey, 0);
game->Continue();
if (key == XK_Escape) {
openal->clean_al();
leaderboard(game);
return 1;
}
if (game->playerHP >0){
if (key == XK_w) {
game->moveX = 1;
}
if (key == XK_s) {
game->moveX = -1;
}
if (key == XK_a) {
game->moveY = 1;
}
if (key == XK_d) {
game->moveY = -1;
}
if (key == XK_r) {
//Fire Roy's key
game->togPortal ^=1;
}
if (key == XK_f) {
//Fire Roy's key
if (game->togPortal == 1)
game->setPortal ^=1;
}
if (key == XK_n) {
//Fire Nick's key
game->partyMode ^= 1;
}
if (key == XK_l) {
game->lkey^=1;
raptorsound();
}
if (key == XK_m) {
//Fire A* algorithm.
startAstar(this->game);
}
if (key == XK_c) {
//Fire Chad's key
//cout << "c key pressed.";
game->noScoreReport = 1;
chadKey(this->game, this->view);
//respawn_mobs(this->game, 10);
}
if (key == XK_1) {
setGun(game,0);
game->maxZoom = .2f;
}
if (key == XK_2) {
setGun(game,1);
game->maxZoom = .55f;
}
if (key == XK_3) {
setGun(game,2);
game->maxZoom = .25f;
}
if (key == XK_space
&& game->setReloadDelay == 0
&& game->nbullets != game->maxbullets) {
reloadAmmo(game);
}
}
}else if (e->type == KeyRelease) {
int key = XLookupKeysym(&e->xkey, 0);
if (key == XK_w) {
game->moveX = 0;
}
if (key == XK_s) {
game->moveX = 0;
}
if (key == XK_a) {
game->moveY = 0;
}
if (key == XK_d) {
game->moveY = 0;
}
}
return 0;
}
void Input::CheckMouse(XEvent *e)
{
if (e->type == ButtonRelease) {
if (e->xbutton.button==3 && game->playerHP > 0) {
//Right button was released
game->zoom = 0;
game->aiming = 0;
return;
}
return;
}
if (e->type == ButtonPress) {
if (game->Continue())
return;
if (e->xbutton.button==1
&& game->playerHP > 0
&& game->setReloadDelay == 0) {
//Left button was pressed
if (game->nbullets <1) {
emptysound(game);
return;
}
openal_sound();
game->Shoot();
game->nbullets -= 1;
return;
}
if (e->xbutton.button==3 && game->playerHP > 0) {
//Right button was pressed
if (game->guntype==1)
game->zoom =1.5;
else
game->zoom = 1;
game->aiming = 1;
return;
}
}
if (game->playerHP > 0){
// Ignore first 5 move inputs
// First few are garbage.
static int start = 0;
if (start < 5 || game->gameRunning == 0) {
start++;
view->CenterCursor();
return;
}
int dx = e->xbutton.x - (view->GetWidth() / 2);
int dy = e->xbutton.y - (view->GetHeight() / 2);
game->direction.x -=(float) dx / 2000.0 / (game->depth / game->minZoom);
game->direction.y -=(float) dy / 2000.0 / (game->depth / game->minZoom);
game->direction.x = fmod(game->direction.x,2.0*PI);
if (game->direction.x < 0.0)
game->direction.x += PI * 2.0;
if (game->direction.y > PI / 2.0 - 0.01)
game->direction.y = PI / 2.0 - 0.01;
if (game->direction.y < -PI / 2.0 + 0.01)
game->direction.y = -PI / 2.0 + 0.01;
if (dx != 0 || dy != 0)
view->CenterCursor();
}
}