Mario: end of level logic

This commit is contained in:
2022-09-23 16:46:50 +02:00
parent 6211e8c756
commit 3437aa6b47
8 changed files with 88 additions and 23 deletions
+25 -22
View File
@@ -70,12 +70,11 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
void handleKeyUp(SDL_Keycode key) {
switch (key) {
case SDLK_ESCAPE:
{
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.push_back(createGameMainMenuScene(renderer, false, true, true));
}
break;
case SDLK_ESCAPE: {
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.push_back(
createGameMainMenuScene(renderer, false, true, true));
} break;
case SDLK_a:
mario->walkRight();
break;
@@ -147,7 +146,7 @@ void pollEvents(SDLPP::Scene &scene) {
}
void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
if(newLoaded) {
if (newLoaded) {
auto prev_mario_pos = mario->getAbsolutePos();
scene->updateSizeAndPosition();
moveToMarioPosition(*scene, prev_mario_pos);
@@ -155,8 +154,9 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
update_count = 2;
newLoaded = false;
}
if(g_death) {
game_scenes.push_back(createGameMainMenuScene(renderer, true, false, true));
if (g_death) {
game_scenes.push_back(
createGameMainMenuScene(renderer, true, false, true));
g_death = false;
}
pollEvents(*scene);
@@ -170,12 +170,11 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
continue;
}
auto visitor = getVisitor(*moving_objects[i], *scene, g_death,
coin_count, moving_objects);
coin_count, moving_objects);
scene->visitCollisions(*moving_objects[i], *visitor);
moving_objects[i]->handleVisitor(*visitor);
auto rightmost_pos =
moving_objects[i]->getAbsolutePos().getX() +
moving_objects[i]->getDoubleRect().second.getX();
auto rightmost_pos = moving_objects[i]->getAbsolutePos().getX() +
moving_objects[i]->getDoubleRect().second.getX();
if (rightmost_pos < 0 && moving_objects[i] != mario) {
moving_objects[i]->destroy();
}
@@ -203,10 +202,13 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
scene->moveEverything((playerX > rightBarrier && rightmostX > width) *
(rightBarrier - playerX) / width,
0);
(rightBarrier - playerX) / width,
0);
update = update || (playerX > rightBarrier && rightmostX > width);
global_frames++;
if (mario->isDead()) {
g_death = true;
}
}
void doInput() {
@@ -221,7 +223,7 @@ void doInput() {
}
}
void mainGameAdditional(std::shared_ptr<SDLPP::Scene> &/*UNUSED*/) {
void mainGameAdditional(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
static auto base = SDL_GetTicks();
static int frames = 0;
mario->setStanding();
@@ -326,10 +328,10 @@ SceneStruct mainGameScene(const std::string &level_path) {
}
void loadLevel(const std::string &level) {
//std::lock_guard<std::mutex> lock(render_mutex);
// std::lock_guard<std::mutex> lock(render_mutex);
coin_count = 0;
std::lock_guard<std::mutex> lock(gamescene_mutex);
for(auto &scene : game_scenes) {
for (auto &scene : game_scenes) {
scene.scene->resetScene();
}
game_scenes.clear();
@@ -343,7 +345,7 @@ void loadLevel(const std::string &level) {
}
void loadLastLevel() {
if(last_load_level != "") {
if (last_load_level != "") {
loadLevel(last_load_level);
}
}
@@ -380,7 +382,8 @@ int main() {
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
g_text_config = std::make_shared<SDLPP::FontConfiguration>(font, "#FFFFFF",
"#000000", 0.15);
game_scenes.push_back(createGameMainMenuScene(renderer, false, false, false));
game_scenes.push_back(
createGameMainMenuScene(renderer, false, false, false));
std::thread inputThread(doInput);
SDL_PumpEvents();
@@ -394,10 +397,10 @@ int main() {
SDL_PumpEvents();
std::lock_guard<std::mutex> lock(render_mutex);
if (update) {
for(auto &scene : game_scenes) {
for (auto &scene : game_scenes) {
scene.scene->updateSizeAndPosition();
}
if(update_count > 0) {
if (update_count > 0) {
update_count--;
} else {
update = false;