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
|
#include "Level.h"
#include <iostream>
#include <fstream>
#include "Monster.h"
#include "Pickup.h"
#include "Hazard.h"
#include "Platform.h"
Level::Level() {
startX = -1;
startY = -1;
}
void Level::changeLevel(int levelNo) {
// Load background image
if (!(background = loadImage("space.png")))
std::cout << "Level::changeLevel: Error loading background image.\n";
// Make player into a Marcus
player.setImage("marcussheet.png");
player.setClip(ORIENT_FRONT, MARCUS_WIDTH * 0, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_BACK, MARCUS_WIDTH * 1, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_RIGHT, MARCUS_WIDTH * 2, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
player.setClip(ORIENT_LEFT, MARCUS_WIDTH * 3, 0, MARCUS_WIDTH, MARCUS_HEIGHT);
// Read the map file (all level/player init should be done here, not above)
loadMap(levelNo);
// Cope for no defined start position
if (startX < 0) {
startX = 0;
startY = 0;
}
player.setX(startX);
player.setY(startY);
cameraX = 0;
}
void Level::move() {
// Construct a rectangle for player, for collision detection
SDL_Rect rect;
rect.w = player.getClip()->w;
rect.h = player.getClip()->h;
// Only record y-coord for now
rect.y = player.getY();
// Apply gravity
player.incYVel(GRAVITY);
// Player x-axis movement
rect.x = player.getX() + player.getXVel();
// If there's a collision in the x-axis ...
while (checkCollision(rect)) {
// ... fit snugly to edge
if (player.getXVel() > 0) {
rect.x--;
} else {
rect.x++;
}
}
if (rect.x >= 0)
player.setX(rect.x);
// Player y-axis movement
rect.y = player.getY() + player.getYVel();
// If there's a collision in the y-axis ...
if (checkCollision(rect)) {
while (checkCollision(rect)) {
// ... fit snugly to edge
if (player.getYVel() > 0)
rect.y--;
else
rect.y++;
}
player.setYVel(0);
}
player.setY(rect.y);
// Kill the player if they fall off the screen
if (player.getY() > SCREEN_HEIGHT) {
player.kill();
player.setX(startX);
player.setY(startY);
cameraX = 0;
}
player.orient();
}
void Level::draw(Screen *screen) {
int x = cameraX
* (SCREEN_WIDTH - background->w)
/ (SCREEN_WIDTH - MAP_X*50);
int y = SCREEN_HEIGHT - background->h;
// Blit background
screen->blit(x, y, background);
// If player is in the middle of the screen ...
if (player.getX() + cameraX > SCREEN_WIDTH / 2 - player.getClip()->w / 2 + player.getXVel()
&& player.getX() + cameraX < SCREEN_WIDTH / 2 + player.getClip()->w / 2 + player.getXVel()) {
// ... and is either moving left, or moving right but not past the end of the map ...
if (player.getXVel() < 0 || (player.getXVel() > 0 && -cameraX + SCREEN_WIDTH < MAP_X * TILE_SIZE)) {
// ... then scroll the level
cameraX -= player.getXVel();
}
}
// Don't let the camera move to before the start of the level
if (cameraX > 0)
cameraX = 0;
// Blit player
screen->blit(player.getX() + cameraX, player.getY(), player.image, player.getClip());
// Draw each platform...
for (int i = 0; i < numPlatforms; i++) {
// ...if it is visible
if (platform[i].getX() + TILE_SIZE + cameraX >= 0 && platform[i].getX()
+ cameraX <= SCREEN_WIDTH) {
screen->blit(platform[i].getX() + cameraX, platform[i].getY(),
platform[0].image, platform[i].getClip());
}
}
}
void Level::loadMap(int LevelNo) {
numPlatforms = 0;
// TODO: Let each Platform have a reference a single tiles image
platform[0].setImage("tiles.png");
// Open map file
std::ifstream map("level01.map");
if (!map)
std::cout << "map is NULL.\n";
// Read each tile...
for (int i = 0; i < MAP_X * MAP_Y; i++) {
int type = -1;
map >> type;
if (map.fail()) {
std::cout << "map.fail().\n";
}
// ...and register it as a platform if it's a platform
if (type == 1 || type == 2) {
// Set x/y-coordinates
platform[numPlatforms].setX((i % MAP_X) * TILE_SIZE);
platform[numPlatforms].setY((i / MAP_X) * TILE_SIZE);
// Set the correct sprite sheet clip
platform[numPlatforms].setClip(0, TILE_SIZE * (type - 1), 0, TILE_SIZE, TILE_SIZE);
numPlatforms++;
} else if (type == 'P') {
// Set the player starting position
startX = (i % MAP_X) * TILE_SIZE;
startY = (i / MAP_X) * TILE_SIZE - player.getClip()->h;
}
}
// Close map file
map.close();
}
bool Level::checkCollision(SDL_Rect r) {
// Loop through each platform
for (int i = 0; i < numPlatforms; i++) {
// Check for collisions in the x-axis ...
if (r.x > platform[i].getX() - r.w && r.x < platform[i].getX() + TILE_SIZE)
// ... if found, check for collisions in the y-axis
if (r.y > platform[i].getY() - r.h && r.y < platform[i].getY() + TILE_SIZE)
return true;
}
return false;
}
|