forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneLoneCoder_PlatformGame1.cpp
334 lines (283 loc) · 10.7 KB
/
OneLoneCoder_PlatformGame1.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
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
/*
OneLoneCoder.com - Code-It-Yourself! Simple Tile Based Platform Game
"Its-a meee-a Jario!" - @Javidx9
License
~~~~~~~
Copyright (C) 2018 Javidx9
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://www.github.com/onelonecoder
https://www.onelonecoder.com
https://www.youtube.com/javidx9
GNU GPLv3
https://github.com/OneLoneCoder/videos/blob/master/LICENSE
From Javidx9 :)
~~~~~~~~~~~~~~~
Hello! Ultimately I don't care what you use this for. It's intended to be
educational, and perhaps to the oddly minded - a little bit of fun.
Please hack this, change it and use it in any way you see fit. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
Cheers!
Background
~~~~~~~~~~
Tile maps are fundamental to most 2D games. This program explores emulating a classic 2D platformer
using floating point truncation to implement robust collision between a moving tile and a tilemap
representing the level.
Controls
~~~~~~~~
Left and Right arrow keys move Jario, Space bar jumps.
(Up and Down also move jario)
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
YouTube: www.youtube.com/javidx9
Discord: https://discord.gg/WhwHUMV
Video:
~~~~~~
https://youtu.be/oJvJZNyW_rw
Last Updated: 04/02/2018
*/
#include <iostream>
#include <string>
using namespace std;
#include "olcConsoleGameEngine.h"
class OneLoneCoder_Platformer : public olcConsoleGameEngine
{
public:
OneLoneCoder_Platformer()
{
m_sAppName = L"Tile Based Platform Game";
}
private:
// Level storage
wstring sLevel;
int nLevelWidth;
int nLevelHeight;
// Player Properties
float fPlayerPosX = 1.0f;
float fPlayerPosY = 1.0f;
float fPlayerVelX = 0.0f;
float fPlayerVelY = 0.0f;
bool bPlayerOnGround = false;
// Camera properties
float fCameraPosX = 0.0f;
float fCameraPosY = 0.0f;
// Sprite Resources
olcSprite *spriteTiles = nullptr;
olcSprite *spriteMan = nullptr;
// Sprite selection flags
int nDirModX = 0;
int nDirModY = 0;
protected:
virtual bool OnUserCreate()
{
nLevelWidth = 64;
nLevelHeight = 16;
sLevel += L"................................................................";
sLevel += L"................................................................";
sLevel += L".......ooooo....................................................";
sLevel += L"........ooo.....................................................";
sLevel += L".......................########.................................";
sLevel += L".....BB?BBBB?BB.......###..............#.#......................";
sLevel += L"....................###................#.#......................";
sLevel += L"...................####.........................................";
sLevel += L"GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG.##############.....########";
sLevel += L"...................................#.#...............###........";
sLevel += L"........................############.#............###...........";
sLevel += L"........................#............#.........###..............";
sLevel += L"........................#.############......###.................";
sLevel += L"........................#................###....................";
sLevel += L"........................#################.......................";
sLevel += L"................................................................";
spriteTiles = new olcSprite(L"JarioSprites/leveljario.spr");
spriteMan = new olcSprite(L"JarioSprites/minijario.spr");
return true;
}
virtual bool OnUserUpdate(float fElapsedTime)
{
// Utility Lambdas
auto GetTile = [&](int x, int y)
{
if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
return sLevel[y * nLevelWidth + x];
else
return L' ';
};
auto SetTile = [&](int x, int y, wchar_t c)
{
if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
sLevel[y*nLevelWidth + x] = c;
};
//fPlayerVelX = 0.0f;
//fPlayerVelY = 0.0f;
// Handle Input
if (IsFocused())
{
if (GetKey(VK_UP).bHeld)
{
fPlayerVelY = -6.0f;
}
if (GetKey(VK_DOWN).bHeld)
{
fPlayerVelY = 6.0f;
}
if (GetKey(VK_LEFT).bHeld)
{
fPlayerVelX += (bPlayerOnGround ? -25.0f : -15.0f) * fElapsedTime;
nDirModY = 1;
}
if (GetKey(VK_RIGHT).bHeld)
{
fPlayerVelX += (bPlayerOnGround ? 25.0f : 15.0f) * fElapsedTime;
nDirModY = 0;
}
if (GetKey(VK_SPACE).bPressed)
{
if (fPlayerVelY == 0)
{
fPlayerVelY = -12.0f;
nDirModX = 1;
}
}
}
// Gravity
fPlayerVelY += 20.0f * fElapsedTime;
// Drag
if (bPlayerOnGround)
{
fPlayerVelX += -3.0f * fPlayerVelX * fElapsedTime;
if (fabs(fPlayerVelX) < 0.01f)
fPlayerVelX = 0.0f;
}
// Clamp velocities
if (fPlayerVelX > 10.0f)
fPlayerVelX = 10.0f;
if (fPlayerVelX < -10.0f)
fPlayerVelX = -10.0f;
if (fPlayerVelY > 100.0f)
fPlayerVelY = 100.0f;
if (fPlayerVelY < -100.0f)
fPlayerVelY = -100.0f;
// Calculate potential new position
float fNewPlayerPosX = fPlayerPosX + fPlayerVelX * fElapsedTime;
float fNewPlayerPosY = fPlayerPosY + fPlayerVelY * fElapsedTime;
// Check for pickups!
if (GetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 0.0f) == L'o')
SetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 0.0f, L'.');
if (GetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 1.0f) == L'o')
SetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 1.0f, L'.');
if (GetTile(fNewPlayerPosX + 1.0f, fNewPlayerPosY + 0.0f) == L'o')
SetTile(fNewPlayerPosX + 1.0f, fNewPlayerPosY + 0.0f, L'.');
if (GetTile(fNewPlayerPosX + 1.0f, fNewPlayerPosY + 1.0f) == L'o')
SetTile(fNewPlayerPosX + 1.0f, fNewPlayerPosY + 1.0f, L'.');
// Check for Collision
if (fPlayerVelX <= 0) // Moving Left
{
if (GetTile(fNewPlayerPosX + 0.0f, fPlayerPosY + 0.0f) != L'.' || GetTile(fNewPlayerPosX + 0.0f, fPlayerPosY + 0.9f) != L'.')
{
fNewPlayerPosX = (int)fNewPlayerPosX + 1;
fPlayerVelX = 0;
}
}
else // Moving Right
{
if (GetTile(fNewPlayerPosX + 1.0f, fPlayerPosY + 0.0f) != L'.' || GetTile(fNewPlayerPosX + 1.0f, fPlayerPosY + 0.9f) != L'.')
{
fNewPlayerPosX = (int)fNewPlayerPosX;
fPlayerVelX = 0;
}
}
bPlayerOnGround = false;
if (fPlayerVelY <= 0) // Moving Up
{
if (GetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY) != L'.' || GetTile(fNewPlayerPosX + 0.9f, fNewPlayerPosY) != L'.')
{
fNewPlayerPosY = (int)fNewPlayerPosY + 1;
fPlayerVelY = 0;
}
}
else // Moving Down
{
if (GetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 1.0f) != L'.' || GetTile(fNewPlayerPosX + 0.9f, fNewPlayerPosY + 1.0f) != L'.')
{
fNewPlayerPosY = (int)fNewPlayerPosY;
fPlayerVelY = 0;
bPlayerOnGround = true; // Player has a solid surface underfoot
nDirModX = 0;
}
}
// Apply new position
fPlayerPosX = fNewPlayerPosX;
fPlayerPosY = fNewPlayerPosY;
// Link camera to player position
fCameraPosX = fPlayerPosX;
fCameraPosY = fPlayerPosY;
// Draw Level
int nTileWidth = 16;
int nTileHeight = 16;
int nVisibleTilesX = ScreenWidth() / nTileWidth;
int nVisibleTilesY = ScreenHeight() / nTileHeight;
// Calculate Top-Leftmost visible tile
float fOffsetX = fCameraPosX - (float)nVisibleTilesX / 2.0f;
float fOffsetY = fCameraPosY - (float)nVisibleTilesY / 2.0f;
// Clamp camera to game boundaries
if (fOffsetX < 0) fOffsetX = 0;
if (fOffsetY < 0) fOffsetY = 0;
if (fOffsetX > nLevelWidth - nVisibleTilesX) fOffsetX = nLevelWidth - nVisibleTilesX;
if (fOffsetY > nLevelHeight - nVisibleTilesY) fOffsetY = nLevelHeight - nVisibleTilesY;
// Get offsets for smooth movement
float fTileOffsetX = (fOffsetX - (int)fOffsetX) * nTileWidth;
float fTileOffsetY = (fOffsetY - (int)fOffsetY) * nTileHeight;
// Draw visible tile map
for (int x = -1; x < nVisibleTilesX + 1; x++)
{
for (int y = -1; y < nVisibleTilesY + 1; y++)
{
wchar_t sTileID = GetTile(x + fOffsetX, y + fOffsetY);
switch (sTileID)
{
case L'.': // Sky
Fill(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, (x + 1) * nTileWidth - fTileOffsetX, (y + 1) * nTileHeight - fTileOffsetY, PIXEL_SOLID, FG_CYAN);
break;
case L'#': // Solid Block
//Fill(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, (x + 1) * nTileWidth - fTileOffsetX, (y + 1) * nTileHeight - fTileOffsetY, PIXEL_SOLID, FG_RED);
DrawPartialSprite(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, spriteTiles, 2 * nTileWidth, 0 * nTileHeight, nTileWidth, nTileHeight);
break;
case L'G': // Ground Block
DrawPartialSprite(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, spriteTiles, 0 * nTileWidth, 0 * nTileHeight, nTileWidth, nTileHeight);
break;
case L'B': // Brick Block
DrawPartialSprite(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, spriteTiles, 0 * nTileWidth, 1 * nTileHeight, nTileWidth, nTileHeight);
break;
case L'?': // Question Block
DrawPartialSprite(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, spriteTiles, 1 * nTileWidth, 1 * nTileHeight, nTileWidth, nTileHeight);
break;
case L'o': // Coin
Fill(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, (x + 1) * nTileWidth - fTileOffsetX, (y + 1) * nTileHeight - fTileOffsetY, PIXEL_SOLID, FG_CYAN);
DrawPartialSprite(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, spriteTiles, 3 * nTileWidth, 0 * nTileHeight, nTileWidth, nTileHeight);
break;
default:
Fill(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, (x + 1) * nTileWidth - fTileOffsetX, (y + 1) * nTileHeight - fTileOffsetY, PIXEL_SOLID, FG_BLACK);
break;
}
}
}
// Draw Player
//Fill((fPlayerPosX - fOffsetX) * nTileWidth, (fPlayerPosY - fOffsetY) * nTileWidth, (fPlayerPosX - fOffsetX + 1.0f) * nTileWidth, (fPlayerPosY - fOffsetY + 1.0f) * nTileHeight, PIXEL_SOLID, FG_GREEN);
DrawPartialSprite((fPlayerPosX - fOffsetX) * nTileWidth, (fPlayerPosY - fOffsetY) * nTileWidth, spriteMan, nDirModX * nTileWidth, nDirModY * nTileHeight, nTileWidth, nTileHeight);
return true;
}
};
int main()
{
OneLoneCoder_Platformer game;
if (game.ConstructConsole(256, 240, 4, 4))
game.Start();
return 0;
}