Mario: Add restart and background color for menus
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
zv0n 2022-09-22 20:16:46 +02:00
parent 8b9ef16929
commit a1afaf427a
10 changed files with 95 additions and 41 deletions

View File

@ -11,3 +11,4 @@ std::shared_ptr<SDLPP::Texture> g_translucent_enemies_texture;
std::shared_ptr<SDLPP::Scene> g_playground{};
std::shared_ptr<SDLPP::FontConfiguration> g_text_config{};
bool g_quit = false;
bool g_death = false;

View File

@ -13,5 +13,6 @@ extern std::shared_ptr<SDLPP::Texture> g_translucent_enemies_texture;
extern std::shared_ptr<SDLPP::Scene> g_playground;
extern std::shared_ptr<SDLPP::FontConfiguration> g_text_config;
extern bool g_quit;
extern bool g_death;
#endif

View File

@ -24,6 +24,7 @@
#include "scenes/game_scenes.hpp"
bool update = false;
int update_count = 0;
bool newLoaded = false;
std::shared_ptr<Mario> mario = nullptr;
std::shared_ptr<SDLPP::RectangleRender> leftStop = nullptr;
@ -32,6 +33,7 @@ std::shared_ptr<SDLPP::TextRenderer> fps = nullptr;
std::shared_ptr<SDLPP::TextRenderer> coins = nullptr;
int coin_count = 0;
int global_frames = 0;
std::string last_load_level = "";
std::vector<std::shared_ptr<MarioBlock>> moving_objects = {};
std::vector<SceneStruct> game_scenes{};
@ -71,7 +73,7 @@ void handleKeyUp(SDL_Keycode key) {
case SDLK_ESCAPE:
{
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.push_back(createGameMainMenuScene(renderer));
game_scenes.push_back(createGameMainMenuScene(renderer, false, true, true));
}
break;
case SDLK_a:
@ -136,6 +138,7 @@ void pollEvents(SDLPP::Scene &scene) {
scene.updateSizeAndPosition();
moveToMarioPosition(scene, prev_mario_pos);
update = true;
update_count = 2;
}
default:
break;
@ -144,6 +147,18 @@ void pollEvents(SDLPP::Scene &scene) {
}
void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
if(newLoaded) {
auto prev_mario_pos = mario->getAbsolutePos();
scene->updateSizeAndPosition();
moveToMarioPosition(*scene, prev_mario_pos);
update = true;
update_count = 2;
newLoaded = false;
}
if(g_death) {
game_scenes.push_back(createGameMainMenuScene(renderer, true, false, true));
g_death = false;
}
pollEvents(*scene);
std::lock_guard<std::mutex> lock(render_mutex);
scene->updateScene();
@ -154,7 +169,7 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
if (!moving_objects[i]->wasVisible()) {
continue;
}
auto visitor = getVisitor(*moving_objects[i], *scene, g_quit,
auto visitor = getVisitor(*moving_objects[i], *scene, g_death,
coin_count, moving_objects);
scene->visitCollisions(*moving_objects[i], *visitor);
moving_objects[i]->handleVisitor(*visitor);
@ -178,6 +193,8 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
if (coin_count != prev_coin_count) {
coins->changeText(std::to_string(coin_count) + " COINS");
update = true;
update_count = 2;
}
// if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x;
@ -230,6 +247,7 @@ SceneStruct mainGameScene(const std::string &level_path) {
bg->setId(1);
scene->addObject(bg);
mario.reset();
mario = std::make_shared<Mario>(renderer);
scene->addObject(mario);
@ -311,11 +329,23 @@ void loadLevel(const std::string &level) {
//std::lock_guard<std::mutex> lock(render_mutex);
coin_count = 0;
std::lock_guard<std::mutex> lock(gamescene_mutex);
for(auto &scene : game_scenes) {
scene.scene->resetScene();
}
game_scenes.clear();
game_scenes.push_back(mainGameScene("levels/" + level));
game_scenes.back().scene->updateSizeAndPosition();
update = true;
newLoaded = true;
update_count = 2;
last_load_level = level;
g_death = false;
}
void loadLastLevel() {
if(last_load_level != "") {
loadLevel(last_load_level);
}
}
#ifdef _WIN32
@ -350,15 +380,10 @@ 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));
game_scenes.push_back(createGameMainMenuScene(renderer, false, false, false));
std::thread inputThread(doInput);
SDL_PumpEvents();
// TODO main menu
/* game_scenes.push_back(mainGameScene("levels/test.marmap"));
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
renderer->presentRenderer();*/
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
renderer->presentRenderer();
@ -368,12 +393,12 @@ int main() {
SDL_framerateDelay(&gFPS);
SDL_PumpEvents();
std::lock_guard<std::mutex> lock(render_mutex);
if (update || newLoaded) {
if (update) {
for(auto &scene : game_scenes) {
scene.scene->updateSizeAndPosition();
}
if(newLoaded) {
newLoaded = false;
if(update_count > 0) {
update_count--;
} else {
update = false;
}

View File

@ -5,16 +5,20 @@
#include "../objectids.hpp"
#include "../editor_visitor.hpp"
#include <functional>
#include "../sprites.hpp"
bool __update_scenes_main_menu = false;
bool __quit_scenes_main_menu = false;
bool __restart_scenes_main_menu = false;
bool __started_main_menu = false;
bool __include_resume = false;
uint64_t __cur_button_index_main_menu = -1;
uint64_t __cur_button_index_main_menu_down = -1;
std::vector<std::shared_ptr<Button>> __buttons_main_menu{};
std::shared_ptr<SDLPP::RectangleRender> __mouse_main_menu{};
extern void loadLevel(const std::string &level);
extern void loadLastLevel();
void quitMainMenu() {
g_quit = true;
@ -24,6 +28,11 @@ void resumeMainMenu() {
__quit_scenes_main_menu = true;
}
void restartMainMenu() {
__quit_scenes_main_menu = true;
__restart_scenes_main_menu = true;
}
void quitMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
quitMainMenu();
}
@ -32,9 +41,14 @@ void resumeMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
resumeMainMenu();
}
void restartMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
restartMainMenu();
}
void resetGlobals() {
__update_scenes_main_menu = false;
__quit_scenes_main_menu = false;
__restart_scenes_main_menu = false;
__started_main_menu = false;
__cur_button_index_main_menu_down = -1;
__mouse_main_menu.reset();
@ -42,7 +56,7 @@ void resetGlobals() {
}
void showLoadMenu(void */*UNUSED*/, Button */*UNUSED*/) {
auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), "levels", loadLevel, false);
auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), "levels", loadLevel, false, false);
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.pop_back();
resetGlobals();
@ -50,11 +64,10 @@ void showLoadMenu(void */*UNUSED*/, Button */*UNUSED*/) {
}
void __updateSelectedButton_MainMenu(uint64_t new_index) {
if (new_index != __cur_button_index_main_menu &&
new_index != (uint64_t)-1) {
if (new_index != (uint64_t)-1) {
__buttons_main_menu[new_index]->setHighlight();
if (__cur_button_index_main_menu != (uint64_t)-1) {
if (__cur_button_index_main_menu != (uint64_t)-1 && new_index != __cur_button_index_main_menu) {
__buttons_main_menu[__cur_button_index_main_menu]
->unsetHighlight();
}
@ -65,7 +78,11 @@ void __updateSelectedButton_MainMenu(uint64_t new_index) {
void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
switch (key) {
case SDLK_ESCAPE:
if(__include_resume) {
resumeMainMenu();
} else {
quitMainMenu();
}
break;
case SDLK_DOWN:
case SDLK_s:
@ -95,10 +112,10 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
}
std::shared_ptr<SDLPP::Scene>
createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_resume) {
createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_restart, bool include_resume, bool transparent_bg) {
auto scene = std::make_shared<SDLPP::Scene>(renderer);
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
"#00000088", true);
transparent_bg ? "#00000088" : MARIO_OVERWORLD_COLORKEY, true);
bg->setPermanent();
bg->setId(1);
scene->addObject(bg);
@ -133,15 +150,24 @@ createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_res
__buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
}
if(include_restart || include_resume) {
__buttons_main_menu.emplace_back(std::make_shared<Button>(
0.2, 0.4, 0.6, 0.1, renderer, "LOAD", default_button_theme,
0.2, 0.4, 0.6, 0.1, renderer, "RESTART", default_button_theme,
restartMainMenuCallback, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
}
__buttons_main_menu.emplace_back(std::make_shared<Button>(
0.2, 0.55, 0.6, 0.1, renderer, "LOAD", default_button_theme,
showLoadMenu, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
__buttons_main_menu.emplace_back(std::make_shared<Button>(
0.2, 0.7, 0.6, 0.1, renderer, "QUIT", default_button_theme,
0.2, 0.85, 0.6, 0.1, renderer, "QUIT", default_button_theme,
quitMainMenuCallback, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
@ -166,6 +192,9 @@ void additionalRenderMainMenu(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
}
if (__quit_scenes_main_menu) {
game_scenes.pop_back();
if(__restart_scenes_main_menu) {
loadLastLevel();
}
resetGlobals();
}
}
@ -218,9 +247,10 @@ void pollEventsMainMenu(std::shared_ptr<SDLPP::Scene> &scene) {
}
SceneStruct
createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_resume) {
createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_restart, bool include_resume, bool transparent_background) {
__include_resume = include_resume;
SceneStruct ret{};
ret.scene = createSceneMainMenu(renderer, include_resume);
ret.scene = createSceneMainMenu(renderer, include_restart, include_resume, transparent_background);
ret.additionalRender = additionalRenderMainMenu;
ret.doInput = pollEventsMainMenu;
__update_scenes_main_menu = true;

View File

@ -9,7 +9,7 @@
extern std::mutex render_mutex;
extern std::vector<SceneStruct> game_scenes;
SceneStruct createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_resume = true);
SceneStruct createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_restart = true, bool include_resume = true, bool transparent_background = true);
SceneStruct createGameRetryScene(std::shared_ptr<SDLPP::Renderer> &renderer);
#endif

View File

@ -8,6 +8,7 @@
#include "../objectids.hpp"
#include "../editor_visitor.hpp"
#include "../filesystem.hpp"
#include "../sprites.hpp"
bool __update_scenes_load_dialog = false;
bool __quit_scenes_load_dialog = false;
@ -123,10 +124,10 @@ void addLevelButton(const std::string &path, const std::string &file,
std::shared_ptr<SDLPP::Scene>
createSceneLoadDialog(std::shared_ptr<SDLPP::Renderer> &renderer,
const std::string &path) {
const std::string &path, bool transparent_bg) {
auto scene = std::make_shared<SDLPP::Scene>(renderer);
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
"#000000BB", true);
transparent_bg ? "#000000BB" : MARIO_OVERWORLD_COLORKEY, true);
bg->setPermanent();
bg->setId(1);
scene->addObject(bg);
@ -161,7 +162,7 @@ createSceneLoadDialog(std::shared_ptr<SDLPP::Renderer> &renderer,
scene->addObject(button);
}
auto font_config = std::make_shared<SDLPP::FontConfiguration>(
g_text_config->getFont(), "#000000", "#282828", 0.05);
g_text_config->getFont(), "#000000", "#FFFFFFAA", 0.05);
auto dialog_text = std::make_shared<SDLPP::TextRenderer>(
0.1, 0.15, 0.8, 0.3, renderer, "LEVELS", font_config,
SDLPP_TEXT_CENTER);
@ -263,11 +264,11 @@ SceneStruct
createLoadScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &path,
std::function<void(const std::string &)> finalizer,
bool pop_at_finish) {
bool pop_at_finish, bool transparent_bg) {
__load_dialog_finalizer = std::move(finalizer);
__pop_at_finish_load_dialog = pop_at_finish;
SceneStruct ret{};
ret.scene = createSceneLoadDialog(renderer, path);
ret.scene = createSceneLoadDialog(renderer, path, transparent_bg);
ret.additionalRender = __additionalRender_LoadDialog;
ret.doInput = __pollEvents_LoadDialog;
__update_scenes_load_dialog = true;

View File

@ -19,6 +19,6 @@ SceneStruct createOkScene(std::shared_ptr<SDLPP::Renderer> renderer,
SceneStruct createLoadScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &path,
std::function<void(const std::string &)> finalizer,
bool pop_at_finish = true);
bool pop_at_finish = true, bool transparent_bg = true);
#endif

View File

@ -50,14 +50,11 @@ void MarioVisitor::visit(const SDLPP::RenderObject &obj) {
}
break;
case DEATH_ID:
// TODO remove death?
death = true;
_quit = true;
_death = true;
break;
case GOOMBA_ID:
if (from != MARIO_FLOOR_DETECT && from != MARIO_ENEMY_DETECT) {
death = true;
_quit = true;
_death = true;
} else {
_bounce = true;
}

View File

@ -8,17 +8,17 @@
class MarioVisitor : public SDLPP::Visitor {
public:
MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &quit,
MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &death,
int &coin_count,
std::vector<std::shared_ptr<MarioBlock>> &moving_objects)
: jumping(is_jumping), _scene(scene), _quit(quit),
: jumping(is_jumping), _scene(scene), _death(death),
_coin_count(coin_count), _moving_objects(moving_objects) {}
void visit(const SDLPP::RenderObject &obj) override;
bool isOnGround() const {
return onGround;
}
bool isDead() const {
return death;
return _death;
}
bool isStopped() const {
return stop;
@ -111,7 +111,6 @@ public:
private:
bool onGround = false;
double groundY = 0;
bool death = false;
bool stop = false;
double newX{};
uint64_t from = -1;
@ -126,7 +125,7 @@ private:
SDLPP::Vec2D<double> movement_blockage;
std::shared_ptr<MarioBlock> coin_block = nullptr;
SDLPP::Scene &_scene;
bool &_quit;
bool &_death;
int &_coin_count;
bool mushroom = false;
std::vector<std::shared_ptr<MarioBlock>> &_moving_objects;

View File

@ -5,7 +5,7 @@
#include "goomba_visitor.hpp"
std::shared_ptr<SDLPP::Visitor>
getVisitor(const MarioBlock &block, SDLPP::Scene &scene, bool &quit,
getVisitor(const MarioBlock &block, SDLPP::Scene &scene, bool &death,
int &coin_count,
std::vector<std::shared_ptr<MarioBlock>> &moving_objects) {
std::shared_ptr<SDLPP::Visitor> result{};
@ -13,7 +13,7 @@ getVisitor(const MarioBlock &block, SDLPP::Scene &scene, bool &quit,
case MARIO_ID:
result = std::static_pointer_cast<SDLPP::Visitor>(
std::make_shared<MarioVisitor>(block.getMovement().getY() < 0,
scene, quit, coin_count,
scene, death, coin_count,
moving_objects));
break;
case MUSHROOM_ID: