-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASCII Shooter.cpp
More file actions
364 lines (300 loc) · 13 KB
/
ASCII Shooter.cpp
File metadata and controls
364 lines (300 loc) · 13 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <iostream>
#include <chrono>
#include <Windows.h>
#include <string>
#include <vector>
#include <algorithm>
#include "olcConsoleGameEngine.h"
class FPS : public olcConsoleGameEngine
{
private:
float fPlayerX = 8.0f;
float fPlayerY = 8.0f;
float fPlayerAngle = 0.0f;
float fSpeed = 5.0;
int map_height = 32;
int map_width = 32;
float fFOV = 3.14159 / 4.0;
// Coresponds to the map size
// TODO: Dont forget to change this when doing dynamic maps
float fDepth = 16.0f;
std::wstring map;
olcSprite* spriteWall;
olcSprite* spriteLamp;
olcSprite* spriteBullet;
float* fDepthBuffer = nullptr;
struct sObject
{
float x;
float y;
float vx;
float vy;
bool bRemove;
olcSprite* sprite;
};
std::list<sObject> listObjects;
public:
FPS()
{
m_sAppName = L"First Person Shooter";
}
virtual bool OnUserCreate()
{
map += L"#########.......#########.......";
map += L"#...............#...............";
map += L"#.......#########.......########";
map += L"#..............##..............#";
map += L"#......##......##......##......#";
map += L"#......##..............##......#";
map += L"#..............##..............#";
map += L"###............####............#";
map += L"##.............###.............#";
map += L"#............####............###";
map += L"#..............................#";
map += L"#..............##..............#";
map += L"#..............##..............#";
map += L"#...........#####...........####";
map += L"#..............................#";
map += L"###..####....########....#######";
map += L"####.####.......######..........";
map += L"#...............#...............";
map += L"#.......#########.......##..####";
map += L"#..............##..............#";
map += L"#......##......##.......#......#";
map += L"#......##......##......##......#";
map += L"#..............##..............#";
map += L"###............####............#";
map += L"##.............###.............#";
map += L"#............####............###";
map += L"#..............................#";
map += L"#..............................#";
map += L"#..............##..............#";
map += L"#...........##..............####";
map += L"#..............##..............#";
map += L"################################";
spriteWall = new olcSprite(L"../fps_wall1.spr");
spriteLamp = new olcSprite(L"../fps_lamp1.spr");
spriteBullet = new olcSprite(L"../fps_fireball1.spr");
fDepthBuffer = new float[ScreenWidth()];
listObjects = {
{ 8.5f, 8.5f, 0.0f, 0.0f, false, spriteLamp },
{ 7.5f, 7.5f, 0.0f, 0.0f, false, spriteLamp },
{ 10.5f, 3.5f, 0.0f, 0.0f, false, spriteLamp },
};
return true;
}
virtual bool OnUserUpdate(float fElapsedTime)
{
// Player Control
// Handle CCW Rotation
if (m_keys[L'A'].bHeld)
fPlayerAngle -= (0.8f) * fElapsedTime;
if (m_keys[L'D'].bHeld)
fPlayerAngle += (0.8f) * fElapsedTime;
if (m_keys[L'W'].bHeld)
{
fPlayerX += sinf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY += cosf(fPlayerAngle) * fSpeed * fElapsedTime;
if (map.c_str()[(int)fPlayerX * map_width + (int)fPlayerY] == '#')
{
fPlayerX -= sinf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY -= cosf(fPlayerAngle) * fSpeed * fElapsedTime;
}
}
if (m_keys[L'S'].bHeld)
{
fPlayerX -= sinf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY -= cosf(fPlayerAngle) * fSpeed * fElapsedTime;
if (map.c_str()[(int)fPlayerX * map_width + (int)fPlayerY] == '#')
{
fPlayerX += sinf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY += cosf(fPlayerAngle) * fSpeed * fElapsedTime;
}
}
// Handle Strafe Right movement & collision
if (m_keys[L'E'].bHeld)
{
fPlayerX += cosf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY -= sinf(fPlayerAngle) * fSpeed * fElapsedTime;
if (map.c_str()[(int)fPlayerX * map_width + (int)fPlayerY] == '#')
{
fPlayerX -= cosf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY += sinf(fPlayerAngle) * fSpeed * fElapsedTime;
}
}
// Handle Strafe Left movement & collision
if (m_keys[L'Q'].bHeld)
{
fPlayerX -= cosf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY += sinf(fPlayerAngle) * fSpeed * fElapsedTime;
if (map.c_str()[(int)fPlayerX * map_width + (int)fPlayerY] == '#')
{
fPlayerX += cosf(fPlayerAngle) * fSpeed * fElapsedTime;
fPlayerY -= sinf(fPlayerAngle) * fSpeed * fElapsedTime;
}
}
// Fire Bullets
if (m_keys[VK_SPACE].bReleased)
{
sObject o;
o.x = fPlayerX;
o.y = fPlayerY;
float fNoise = (((float)rand() / (float)RAND_MAX) - 0.5f) * 0.1f;
o.vx = sinf(fPlayerAngle + fNoise) * 8.0f;
o.vy = cosf(fPlayerAngle + fNoise) * 8.0f;
o.sprite = spriteBullet;
o.bRemove = false;
listObjects.push_back(o);
}
// TODO: Compatibility for Y axis
for (int x = 0; x < ScreenWidth(); x++)
{
// for each column on the screen, calculate the projected ray angle into the space
float fFOVAngle = (fPlayerAngle - fFOV / 2.0f) + ((float)x / (float)ScreenWidth()) * fFOV;
float fDistanceToWall = 0;
float fStepSize = 0.01f;
bool bHitWall = false;
bool bBoundary = false;
// Unit vectors for direction player is looking
float fEyeX = sinf(fFOVAngle);
float fEyeY = cosf(fFOVAngle);
float fSampleX = 0.0f;
// this (poorly) calculates the distance to the wall for a ray trace
// TODO: improve this
while (!bHitWall && fDistanceToWall < fDepth)
{
fDistanceToWall += fStepSize;
int nTestX = (int)(fPlayerX + fEyeX * fDistanceToWall);
int nTestY = (int)(fPlayerY + fEyeY * fDistanceToWall);
// Test if ray is out of bounds
// TODO: update for dynamic map
if (nTestX < 0 || nTestX >= map_width || nTestY < 0 || nTestY >= map_height)
{
bHitWall = true;
fDistanceToWall = fDepth;
}
else
{
// Ray is inbounds so test to see if the ray cell is a wall block
if (map.c_str()[nTestX * map_width + nTestY] == '#')
{
bHitWall = true;
// determines if ray has hit wall
// gets midpoint of cell
float fBlockMidX = (float)nTestX + 0.5f;
float fBlockMidY = (float)nTestY + 0.5f;
float fTestPointX = fPlayerX + fEyeX * fDistanceToWall;
float fTestPointY = fPlayerY + fEyeY * fDistanceToWall;
float fTestAngle = atan2f((fTestPointY - fBlockMidY), (fTestPointX - fBlockMidX));
if (fTestAngle >= -3.14159f * 0.25f && fTestAngle < 3.14159f * 0.25f)
fSampleX = fTestPointY - (float)nTestY;
if (fTestAngle >= 3.14159f * 0.25f && fTestAngle < 3.14159f * 0.75f)
fSampleX = fTestPointX - (float)nTestX;
if (fTestAngle < -3.14159f * 0.25f && fTestAngle >= -3.14159f * 0.75f)
fSampleX = fTestPointX - (float)nTestX;
if (fTestAngle >= 3.14159f * 0.75f || fTestAngle < -3.14159f * 0.75f)
fSampleX = fTestPointY - (float)nTestY;
}
}
}
// Calculate the distance to ceiling and floor
// Take the midpoint of screen, subtract relative height WRT distance to wall
int nCeiling = (float)(ScreenHeight() / 2.0) - ScreenHeight() / ((float)fDistanceToWall);
int nFloor = ScreenHeight() - nCeiling;
// update depth buffer
fDepthBuffer[x] = fDistanceToWall;
for (int y = 0; y < ScreenHeight(); y++)
{
// Each Row
if (y <= nCeiling)
Draw(x, y, L' ');
else if (y > nCeiling && y <= nFloor)
{
if (fDistanceToWall < fDepth)
{
// Wall
float fSampleY = ((float)y - (float)nCeiling) / ((float)nFloor - (float)nCeiling);
Draw(x, y, spriteWall->SampleGlyph(fSampleX, fSampleY), spriteWall->SampleColour(fSampleX, fSampleY));
}
else {
Draw(x, y, PIXEL_SOLID, 0);
}
}
else // Floor
{
Draw(x, y, PIXEL_SOLID, FG_DARK_BLUE);
}
}
}
// Update & Draw Objects
for (auto& object : listObjects)
{
// Update Object Physics
object.x += object.vx * fElapsedTime;
object.y += object.vy * fElapsedTime;
// Check if object is inside wall - set flag for removal
if (map.c_str()[(int)object.x * map_width + (int)object.y] == '#')
object.bRemove = true;
// Can object be seen?
float fVecX = object.x - fPlayerX;
float fVecY = object.y - fPlayerY;
float fDistanceFromPlayer = sqrtf(fVecX * fVecX + fVecY * fVecY);
float fEyeX = sinf(fPlayerAngle);
float fEyeY = cosf(fPlayerAngle);
// Calculate angle between lamp and players feet, and players looking direction
// to determine if the lamp is in the players field of view
float fObjectAngle = atan2f(fEyeY, fEyeX) - atan2f(fVecY, fVecX);
if (fObjectAngle < -3.14159f)
fObjectAngle += 2.0f * 3.14159f;
if (fObjectAngle > 3.14159f)
fObjectAngle -= 2.0f * 3.14159f;
bool bInPlayerFOV = fabs(fObjectAngle) < fFOV / 2.0f;
if (bInPlayerFOV && fDistanceFromPlayer >= 0.5f && fDistanceFromPlayer < fDepth && !object.bRemove)
{
float fObjectCeiling = (float)(ScreenHeight() / 2.0) - ScreenHeight() / ((float)fDistanceFromPlayer);
float fObjectFloor = ScreenHeight() - fObjectCeiling;
float fObjectHeight = fObjectFloor - fObjectCeiling;
float fObjectAspectRatio = (float)object.sprite->nHeight / (float)object.sprite->nWidth;
float fObjectWidth = fObjectHeight / fObjectAspectRatio;
float fMiddleOfObject = (0.5f * (fObjectAngle / (fFOV / 2.0f)) + 0.5f) * (float)ScreenWidth();
// Draw Lamp
for (float lx = 0; lx < fObjectWidth; lx++)
{
for (float ly = 0; ly < fObjectHeight; ly++)
{
float fSampleX = lx / fObjectWidth;
float fSampleY = ly / fObjectHeight;
wchar_t c = object.sprite->SampleGlyph(fSampleX, fSampleY);
int nObjectColumn = (int)(fMiddleOfObject + lx - (fObjectWidth / 2.0f));
if (nObjectColumn >= 0 && nObjectColumn < ScreenWidth())
{
// dont render lamps if:
if (c != L' ' && fDepthBuffer[nObjectColumn] >= fDistanceFromPlayer)
{
Draw(nObjectColumn, fObjectCeiling + ly, c, object.sprite->SampleColour(fSampleX, fSampleY));
fDepthBuffer[nObjectColumn] = fDistanceFromPlayer;
}
}
}
}
}
}
// Remove dead objects from object list
listObjects.remove_if([](sObject& o) {return o.bRemove; });
// display Map
for (int nx = 0; nx < map_width; nx++)
for (int ny = 0; ny < map_width; ny++)
{
Draw(nx + 1, ny + 1, map[ny * map_width + nx]);
}
Draw(1 + (int)fPlayerY, 1 + (int)fPlayerX, L'P');
return true;
}
};
int main()
{
FPS game;
game.ConstructConsole(320, 240, 4, 4);
game.Start();
return 0;
}