-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLevel.cpp
More file actions
317 lines (280 loc) · 8.13 KB
/
Level.cpp
File metadata and controls
317 lines (280 loc) · 8.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
#include "Settings.h"
#include "CollisionDistances.h"
#include "Character.h"
#include "Level.h"
#include "Engine.h"
Level::Level()
{
// Default nothing map
for (int i = 0; i < LEVEL_HEIGHT; i++)
{
for (int j = 0; j < LEVEL_WIDTH; j++)
{
levelMatrix[i][j] = -1;
levelSolids[i][j] = false;
levelCollectible[i][j] = false;
}
}
// Load map from file
std::ifstream myfile("level.txt");
if (myfile.is_open())
{
for (int i = 0; i < LEVEL_HEIGHT; i++)
{
for (int j = 0; j < LEVEL_WIDTH; j++)
{
myfile >> levelMatrix[i][j];
levelSolids[i][j] = tileIsSolid(levelMatrix[i][j]);
levelCollectible[i][j] = tileIsCollectible(levelMatrix[i][j]);
}
}
}
}
void Level::Draw(ID2D1HwndRenderTarget* m_pRenderTarget)
{
// Loads images if needed
if (tileSetImg == NULL)
{
tileSetImg = engine->LoadImageW(L"tileset.png");
}
if (cloudsImg == NULL)
{
cloudsImg = engine->LoadImageW(L"clouds.png");
}
// Draw the Clouds, but only offset them by half, to implement parallaxing
double start_x = Engine::offset / 2.0;
while (start_x > RESOLUTION_X)
{
start_x -= RESOLUTION_X;
}
D2D1_RECT_F cloudsRect1 = D2D1::RectF(
0 - start_x, 0,
RESOLUTION_X - start_x, RESOLUTION_Y
);
D2D1_RECT_F cloudsRect2 = D2D1::RectF(
RESOLUTION_X - start_x, 0,
2 * RESOLUTION_X - start_x, RESOLUTION_Y
);
m_pRenderTarget->DrawBitmap(cloudsImg, cloudsRect1);
m_pRenderTarget->DrawBitmap(cloudsImg, cloudsRect2);
// Draw level tiles
for (int i = 0; i < LEVEL_HEIGHT; i++)
{
for (int j = 0; j < LEVEL_WIDTH; j++)
{
if ((j + 1) * TILE_WIDTH >= Engine::offset && j * TILE_WIDTH < Engine::offset + RESOLUTION_X)
{
if (levelMatrix[i][j] != -1)
{
D2D1_RECT_F rectangle1 = D2D1::RectF(
j * TILE_WIDTH - Engine::offset, i * TILE_WIDTH,
(j + 1) * TILE_WIDTH - Engine::offset + 1, (i + 1) * TILE_WIDTH
);
int posY = levelMatrix[i][j] / 7;
int posX = levelMatrix[i][j] % 7;
D2D1_RECT_F rectangle2 = D2D1::RectF(
posX * TILE_WIDTH, posY * TILE_WIDTH,
posX * TILE_WIDTH + TILE_WIDTH - 1, posY * TILE_WIDTH + TILE_WIDTH - 1
);
m_pRenderTarget->DrawBitmap(tileSetImg, rectangle1, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, rectangle2);
}
}
}
}
}
CollisionDistances Level::CharacterCollides(Character* character)
{
// this returns collision details between the character and the tiles
int noCollisions = 0;
CollisionDistances collisions[9];
CollisionDistances cummulatedCollision;
cummulatedCollision.top = 0;
cummulatedCollision.bottom = 0;
cummulatedCollision.left = 0;
cummulatedCollision.right = 0;
double charTop = character->GetPosition().y - CHARACTER_HEIGHT;
double charBottom = character->GetPosition().y;
double charLeft = character->GetPosition().x - CHARACTER_WIDTH / 2 + 3;
double charRight = character->GetPosition().x + CHARACTER_WIDTH / 2 - 4;
// only check the tiles that the character is close to
int startX = (int)(charLeft / TILE_WIDTH);
if (startX < 0)
startX = 0;
int startY = (int)(charTop / TILE_WIDTH);
if (startY < 0)
startY = 0;
int endX = (int)(charRight / TILE_WIDTH);
if (endX > LEVEL_WIDTH - 1)
endX = LEVEL_WIDTH - 1;
int endY = (int)(charBottom / TILE_WIDTH);
if (endY > LEVEL_HEIGHT - 1)
endY = LEVEL_HEIGHT - 1;
for (int i = startY; i <= endY; i++)
{
for (int j = startX; j <= endX; j++)
{
if (levelSolids[i][j])
{
double tileTop = i * TILE_WIDTH;
double tileBottom = (i + 1) * TILE_WIDTH - 1;
double tileLeft = j * TILE_WIDTH;
double tileRight = (j + 1) * TILE_WIDTH - 1;
// If it's a collision
if (charTop < tileBottom && charBottom > tileTop && charRight > tileLeft && charLeft < tileRight)
{
// add this collision to the list
collisions[noCollisions].top = abs(charTop - tileBottom);
collisions[noCollisions].bottom = abs(charBottom - tileTop);
collisions[noCollisions].left = abs(charLeft - tileRight);
collisions[noCollisions].right = abs(charRight - tileLeft);
collisions[noCollisions].keepSmallest();
// add the collision details to the cummulated collision
cummulatedCollision.top += collisions[noCollisions].top;
cummulatedCollision.bottom += collisions[noCollisions].bottom;
cummulatedCollision.left += collisions[noCollisions].left;
cummulatedCollision.right += collisions[noCollisions].right;
noCollisions++;
}
}
}
}
// whichever side collides the most, that side is taken into consideration
cummulatedCollision.keepLargest();
if (cummulatedCollision.top != 0)
{
cummulatedCollision.top = 0;
for (int i = 0; i < noCollisions; i++)
{
cummulatedCollision.top = fmax(cummulatedCollision.top, collisions[i].top);
}
}
if (cummulatedCollision.bottom != 0)
{
cummulatedCollision.bottom = 0;
for (int i = 0; i < noCollisions; i++)
{
cummulatedCollision.bottom = fmax(cummulatedCollision.bottom, collisions[i].bottom);
}
}
if (cummulatedCollision.left != 0)
{
cummulatedCollision.left = 0;
for (int i = 0; i < noCollisions; i++)
{
cummulatedCollision.left = fmax(cummulatedCollision.left, collisions[i].left);
}
}
if (cummulatedCollision.right != 0)
{
cummulatedCollision.right = 0;
for (int i = 0; i < noCollisions; i++)
{
cummulatedCollision.right = fmax(cummulatedCollision.right, collisions[i].right);
}
}
return cummulatedCollision;
}
int Level::PickUpCollectibles(Character* character)
{
// this method checks for collisions with coins and returns the number
int noCollisions = 0;
double charTop = character->GetPosition().y - CHARACTER_HEIGHT;
double charBottom = character->GetPosition().y;
double charLeft = character->GetPosition().x - CHARACTER_WIDTH / 2 + 3;
double charRight = character->GetPosition().x + CHARACTER_WIDTH / 2 - 4;
int startX = (int)(charLeft / TILE_WIDTH);
if (startX < 0)
startX = 0;
int startY = (int)(charTop / TILE_WIDTH);
if (startY < 0)
startY = 0;
int endX = (int)(charRight / TILE_WIDTH);
if (endX > LEVEL_WIDTH - 1)
endX = LEVEL_WIDTH - 1;
int endY = (int)(charBottom / TILE_WIDTH);
if (endY > LEVEL_HEIGHT - 1)
endY = LEVEL_HEIGHT - 1;
for (int i = startY; i <= endY; i++)
{
for (int j = startX; j <= endX; j++)
{
if (levelCollectible[i][j])
{
double tileTop = i * TILE_WIDTH;
double tileBottom = (i + 1) * TILE_WIDTH - 1;
double tileLeft = j * TILE_WIDTH;
double tileRight = (j + 1) * TILE_WIDTH - 1;
// If it's a collision
if (charTop < tileBottom && charBottom > tileTop && charRight > tileLeft && charLeft < tileRight)
{
noCollisions++;
levelMatrix[i][j] = -1; // remove the coin by resetting the tile position
levelCollectible[i][j] = false;
}
}
}
}
return noCollisions;
}
bool Level::LevelExit(Character* character)
{
// returns true if the character collides with the level exit portal
int noCollisions = 0;
double charTop = character->GetPosition().y - CHARACTER_HEIGHT;
double charBottom = character->GetPosition().y;
double charLeft = character->GetPosition().x - CHARACTER_WIDTH / 2 + 3;
double charRight = character->GetPosition().x + CHARACTER_WIDTH / 2 - 4;
int startX = (int)(charLeft / TILE_WIDTH);
if (startX < 0)
startX = 0;
int startY = (int)(charTop / TILE_WIDTH);
if (startY < 0)
startY = 0;
int endX = (int)(charRight / TILE_WIDTH);
if (endX > LEVEL_WIDTH - 1)
endX = LEVEL_WIDTH - 1;
int endY = (int)(charBottom / TILE_WIDTH);
if (endY > LEVEL_HEIGHT - 1)
endY = LEVEL_HEIGHT - 1;
for (int i = startY; i <= endY; i++)
{
for (int j = startX; j <= endX; j++)
{
if (levelMatrix[i][j] == 11)
{
double tileTop = i * TILE_WIDTH;
double tileBottom = (i + 1) * TILE_WIDTH - 1;
double tileLeft = j * TILE_WIDTH;
double tileRight = (j + 1) * TILE_WIDTH - 1;
// If it's a collision
if (charTop < tileBottom && charBottom > tileTop&& charRight > tileLeft&& charLeft < tileRight)
{
return true;
}
}
}
}
return false;
}
bool Level::tileIsSolid(int tileNo)
{
for (int i = 0; i < 34; i++)
{
if (solids[i] == tileNo)
{
return true;
}
}
return false;
}
bool Level::tileIsCollectible(int tileNo)
{
for (int i = 0; i < 8; i++)
{
if (collectibles[i] == tileNo)
{
return true;
}
}
return false;
}