-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cpp
261 lines (231 loc) · 8.46 KB
/
Controller.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "pch.h"
#include <conio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include "Config.h"
#include "Controller.h"
#include "main.h"
#include "Map.h"
//jump/crunch
int maxJumpHeight = int(1.1 * sqResh); //jump this high
int maxCrunchHeight = -int(0.5 * sqResh); //crunch as most as this low
float fFPS = 30; //approximate FPS
int acceleratedMotion[200];
int maxJump_idx, maxCrunch_idx;
int verticalAdvance = 0;
int jumping = 0, crunching = 0;
int zC = 0; //same unit as sqRes
static clock_t keyTime = clock();
HANDLE hStdInput;
HWND hWnd;
bool wndHasFocus = true;
//mouse cursor reference position
int refMousePosX = screenWh * 8, refMousePosY = screenH * 16 - 8;
int initController() {
float fDist = 0, fSpeed = 0, G = 981.f * sqRes / 50; //G was empirically chosen as we don't have a proper world scale here
for (int i = 0; i < 200; i++) {
acceleratedMotion[i] = (int)fDist;
fSpeed += G / fFPS;
fDist += fSpeed / fFPS;
}
//search for the acceleratedMotion entry so that we'll decelerate to zero speed at max jump height
for (maxJump_idx = 0; maxJump_idx < 200; maxJump_idx++)
if (acceleratedMotion[maxJump_idx] > maxJumpHeight)
break;
if (maxJump_idx >= 200) maxJump_idx = 199;
elevation_perc = 100 * zC / sqResh; //as percentage from wall half height
//init mouse input
hStdInput = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hStdInput, ENABLE_EXTENDED_FLAGS | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
wchar_t windowTitle[1000];
GetConsoleTitleW(windowTitle, 1000);
hWnd = FindWindowW(NULL, windowTitle);
//set cursor to a reference position
POINT mousePoint = {refMousePosX, refMousePosY};
ClientToScreen(hWnd, &mousePoint);
SetCursorPos(mousePoint.x, mousePoint.y);
return 0;
}
void move(int& x, int& y, int angle) {
float rad = angle * 6.2831f / around;
int xTest = x + int(MOVE_SPD * cos(rad));
int yTest = y + int(MOVE_SPD * sin(rad));
//check for wall collision
int safetyX = ((aroundq < angle) && (angle < around3q)) ? +safety_dist : -safety_dist;
int safetyY = (angle < aroundh) ? -safety_dist : +safety_dist;
int adjXMap = ((aroundq < angle) && (angle < around3q)) ? -1 : 0;
int adjYMap = (angle > aroundh) ? -1 : 0;
int xWall, yWall;
int wallID = Cast(angle, xWall, yWall);
if (sq(x - xTest) + sq(y - yTest) >= sq(x - xWall) + sq(y - yWall)) { //inside wall
if (wallID % 2 == 0) { //vertical wall ||
x = xWall + safetyX;
y = yTest; // __
if (Map[y / sqRes][x / sqRes] != 0) //it's a corner |
y = (yTest / sqRes - adjYMap) * sqRes + safetyY;
}
else { //horizontal wall ==
x = xTest;
y = yWall + safetyY; // __
if (Map[y / sqRes][x / sqRes] != 0) //it's a corner |
x = (xTest / sqRes - adjXMap) * sqRes + safetyX;
}
}
else //free cell
x = xTest, y = yTest;
}
void rotate(int& angle, int dir, int around, int rotate_spd = ROTATE_SPD) {
angle = (angle + dir * rotate_spd + around) % around;
}
int loopController(int& x, int& y, int& angle, int around) {
static int firstTime = 1;
if (firstTime) {
firstTime = 0;
return 1; //always render first frame
}
int sign = 1;
#ifdef INVERT_COORDINATE_SYSTEM
sign = -1;
#endif
int did = 0;
//check the mouse (and focus)
INPUT_RECORD InputRecord[128];
DWORD RecordsRead = 0;
GetNumberOfConsoleInputEvents(hStdInput, &RecordsRead);
if (RecordsRead > 0)
ReadConsoleInput(hStdInput, InputRecord, RecordsRead, &RecordsRead);
for (DWORD i = 0; i < RecordsRead; i++) {
if (InputRecord[i].EventType == FOCUS_EVENT) {
wndHasFocus = InputRecord[i].Event.FocusEvent.bSetFocus;
}
if (InputRecord[i].EventType == MOUSE_EVENT) {
if(InputRecord[i].Event.MouseEvent.dwEventFlags == MOUSE_MOVED) {
//int mousePosX = InputRecord[i].Event.MouseEvent.dwMousePosition.X; //char level accuracy
POINT mousePoint;
GetCursorPos(&mousePoint);
ScreenToClient(hWnd, &mousePoint);
int mousePosX = mousePoint.x; //get pixel level accuracy
rotate(angle, (refMousePosX - mousePosX) / 2 * sign, around, 1);
//reset cursor to the reference position
POINT refMousePoint = {refMousePosX, refMousePosY};
ClientToScreen(hWnd, &refMousePoint);
SetCursorPos(refMousePoint.x, refMousePoint.y);
did = 1;
}
}
}
if (!wndHasFocus)
return 0;
#ifdef USE_MULTIPLE_KEYS_SIMULTANEOUSLY
{
unsigned char ch = 255;
#else
if (_kbhit()) { //check user's input
unsigned char ch = toupper(_getch());
#endif
if ((ch == 27) || (GetAsyncKeyState(VK_ESCAPE) & 0x8000)) //ASCII code for the Esc key
return -1; //end the game
if ((ch == 'R') || (GetAsyncKeyState('R') & 0x8000)) { //reset viewer's position
xC = xInit;
yC = yInit;
angleC = angleInit;
did = 1;
}
if ((ch == 'M') || (GetAsyncKeyState('M') & 0x8000)) { //show/hide map
if (float(clock() - keyTime) / CLOCKS_PER_SEC > 0.2f) { //avoid quick map show/hide
keyTime = clock();
showMap = 1 - showMap;
did = 1;
}
}
if ((ch == 'W') || (GetAsyncKeyState('W') & 0x8000)) { //pedal forward
move(x, y, angle);
did = 1;
}
if ((ch == 'S') || (GetAsyncKeyState('S') & 0x8000)) { //pedal backward
move(x, y, (angle + around / 2) % around);
did = 1;
}
if ((ch == 'A') || (GetAsyncKeyState('A') & 0x8000)) { //strafe left
move(x, y, (angle + sign * around / 4 + around) % around);
did = 1;
}
if ((ch == 'D') || (GetAsyncKeyState('D') & 0x8000)) { //strafe right
move(x, y, (angle - sign * around / 4 + around) % around);
did = 1;
}
//jump/crunch
static int jump_idx;
if (((ch == 'E') || (GetAsyncKeyState('E') & 0x8000)) && !jumping && !crunching) {
jumping = 1;
verticalAdvance = 1;
jump_idx = maxJump_idx - 1;
zC = maxJumpHeight - acceleratedMotion[jump_idx];
did = 1;
}
else
if (jumping) {
if (verticalAdvance > 0) {
if (jump_idx > 0) {
jump_idx--;
zC = maxJumpHeight - acceleratedMotion[jump_idx];
}
else {
verticalAdvance = -1;
zC = maxJumpHeight;
}
did = 1;
}
else
if (verticalAdvance < 0) {
if (zC > 0) {
jump_idx++;
zC = max(0, maxJumpHeight - acceleratedMotion[jump_idx]);
}
else {
verticalAdvance = 0;
jumping = 0;
}
did = 1;
}
}
//crunch
if (((ch == 'C') || (GetAsyncKeyState('C') & 0x8000)) && !jumping) {
crunching = 1;
if (zC > maxCrunchHeight) {
zC -= VERTICAL_SPD;
if (zC < maxCrunchHeight)
zC = maxCrunchHeight;
did = 1;
}
}
else
if (crunching) {
zC += VERTICAL_SPD;
if (zC >= 0) {
zC = 0;
crunching = 0;
}
did = 1;
}
elevation_perc = 100 * zC / sqResh; //as percentage from wall half height
#ifdef USE_MULTIPLE_KEYS_SIMULTANEOUSLY
{
#else
if (ch == 224) { //it's a key that generates two bytes when being pressed, the first one being 224
ch = _getch();
#endif
if ((ch == 75) || (GetAsyncKeyState(VK_LEFT) & 0x8000)) { //the left arrow key => do turn left
rotate(angle, +1 * sign, around);
did = 1;
}
if ((ch == 77) || (GetAsyncKeyState(VK_RIGHT) & 0x8000)) { //the right arrow key => do turn right
rotate(angle, -1 * sign, around);
did = 1;
}
}
} //if (_kbhit())
return did;
}