๐ฏ
Goal for this step Define levels as data structs and swap between them.
1 A struct Level holds: platforms, enemies (as spawn data), spikes, checkpoints, door, width.
2 A makeLevel(int n) function returns the struct for level n, pure data, no logic.
3 Loading = copying the structโs contents into the live vectors and resetting the player.
4 Reaching the door loads n+1; after the last, the champion screen. Your Ep 4 game is now a multi-level product.
struct Level {
std::vector<sf::FloatRect> platforms, spikes, checkpoints;
std::vector<Enemy> enemies;
sf::FloatRect door;
float width;
};
Level makeLevel(int n) {
if (n == 0) {
Level L;
L.width = 2400;
L.platforms = { {0,440,2400,40}, {150,340,140,16} /* ... */ };
L.enemies.emplace_back(390, 236, 380, 500);
L.spikes = { {760, 424, 40, 16} };
L.checkpoints = { {1000, 380, 12, 60} };
L.door = {2320, 380, 30, 60};
return L;
}
// n == 1 ...
}
Separating level DATA from game LOGIC meansโฆ
A Levels run faster
B New levels need zero new logic, and eventually data can come from files or a level editor
C The compiler optimises structs
Check Answer
โ
Correct! Next Step โ