Teleportation in editor
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Some checks reported errors
continuous-integration/drone/push Build encountered an error
This commit is contained in:
parent
6ac2726a94
commit
9402f2afa2
@ -100,6 +100,7 @@ target_sources(editor
|
||||
PRIVATE scenes/yes_no_scene.cpp
|
||||
PRIVATE scenes/ok_scene.cpp
|
||||
PRIVATE scenes/load_scene.cpp
|
||||
PRIVATE scenes/text_scene.cpp
|
||||
)
|
||||
|
||||
target_compile_definitions(editor PUBLIC EDITOR)
|
||||
|
@ -41,10 +41,13 @@ void MushroomBlock::setParent(MarioBlock *parent) {
|
||||
}
|
||||
|
||||
void MushroomBlock::handleVisitor(SDLPP::Visitor &visitor) {
|
||||
auto &m_visitor = dynamic_cast<MushroomVisitor &>(visitor);
|
||||
if (m_visitor.getDeath()) {
|
||||
destroy();
|
||||
}
|
||||
if (!_started_movement) {
|
||||
return;
|
||||
}
|
||||
auto &m_visitor = dynamic_cast<MushroomVisitor &>(visitor);
|
||||
setOnGround(m_visitor.isOnGround());
|
||||
if (isOnGround()) {
|
||||
setPos(getPos().getX(), m_visitor.getGroundY() - BLOCK_SIZE);
|
||||
@ -53,9 +56,6 @@ void MushroomBlock::handleVisitor(SDLPP::Visitor &visitor) {
|
||||
setPos(m_visitor.getValidXPos(), getPos().getY());
|
||||
setMovement(-getMovement().getX(), getMovement().getY());
|
||||
}
|
||||
if (m_visitor.getDeath()) {
|
||||
destroy();
|
||||
}
|
||||
}
|
||||
|
||||
void MushroomBlock::setFireFlower(bool fire_flower) {
|
||||
|
@ -65,8 +65,6 @@ bool FSLib::isDirectory(const string &path) {
|
||||
}
|
||||
|
||||
bool FSLib::rename(const string &file_a, const string &file_b) {
|
||||
// TODO log
|
||||
std::cout << file_a << " -> " << file_b << std::endl;
|
||||
return ::rename(file_a.c_str(), file_b.c_str()) == 0;
|
||||
}
|
||||
|
||||
|
@ -385,7 +385,6 @@ SceneStruct mainGameScene(const std::string &level_path) {
|
||||
}
|
||||
|
||||
void setTeleportLevelMain(const std::string &level) {
|
||||
std::cout << "Setting: " << level << std::endl;
|
||||
_teleport_level = level;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "../sdlpp/sdlpp_rectrenderer.hpp"
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include "mario.hpp"
|
||||
#include "sprites.hpp"
|
||||
#include "blocks.hpp"
|
||||
|
@ -132,10 +132,10 @@ void Mario::handleVisitor(SDLPP::Visitor &visitor) {
|
||||
if (m_visitor.hasMushroom()) {
|
||||
setBig();
|
||||
}
|
||||
if (m_visitor.hasTeleport() && isCrouching()) {
|
||||
std::cout << "TELEPORTING" << std::endl;
|
||||
if (m_visitor.hasTeleport() && (
|
||||
(m_visitor.teleportBottom() && isCrouching()) || (!m_visitor.teleportBottom() && walkingRight())
|
||||
)) {
|
||||
setTeleportLevelMain(m_visitor.getTeleportLevel());
|
||||
std::cout << "Setted" << std::endl;
|
||||
}
|
||||
if (m_visitor.levelEnd() && controllable) {
|
||||
if (std::abs(getPos().getX() - m_visitor.getEndPos().getX()) <
|
||||
@ -261,6 +261,7 @@ void Mario::setBig() {
|
||||
} else {
|
||||
setBigFlag();
|
||||
setSize({ BLOCK_SIZE, 2 * BLOCK_SIZE });
|
||||
setPos(getPos() - SDLPP::Vec2D<double>(0, BLOCK_SIZE));
|
||||
}
|
||||
setBaseRect(isJumping()
|
||||
? (hasFire() ? MARIO_JUMP_FIRE_SRC : MARIO_JUMP_BIG_SRC)
|
||||
@ -272,7 +273,6 @@ void Mario::setBig() {
|
||||
change_dir_src = hasFire() ? &MARIO_CHANGE_DIR_FIRE_SRC : &MARIO_CHANGE_DIR_BIG_SRC;
|
||||
jump_src = hasFire() ? &MARIO_JUMP_FIRE_SRC : &MARIO_JUMP_BIG_SRC;
|
||||
walk_anim = hasFire() ? &MARIO_WALK_FIRE_ANIM : &MARIO_WALK_BIG_ANIM;
|
||||
setPos(getPos() - SDLPP::Vec2D<double>(0, BLOCK_SIZE));
|
||||
}
|
||||
|
||||
void Mario::unsetBig() {
|
||||
|
@ -52,6 +52,8 @@ bool quit_editor = false;
|
||||
|
||||
const std::string levelsDir = "levels";
|
||||
bool map_changed = false;
|
||||
MapObject *current_obj = nullptr;
|
||||
std::shared_ptr<MarioBlock> current_modifier = nullptr;
|
||||
|
||||
struct ToolType {
|
||||
enum Value {
|
||||
@ -148,14 +150,12 @@ bool getFlag(uint64_t flag) {
|
||||
}
|
||||
|
||||
void saveMapCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
||||
std::cout << "SAVING" << std::endl;
|
||||
saveMap(levelsDir + FSLib::dir_divisor + level_name_text + ".marmap",
|
||||
global_vars.objects);
|
||||
map_changed = false;
|
||||
game_scenes.pop_back();
|
||||
}
|
||||
void loadMapDialogCallback(const std::string &level_name) {
|
||||
std::cout << "LOADING" << std::endl;
|
||||
map_changed = false;
|
||||
level_name_text = level_name.substr(0, level_name.length() - 7);
|
||||
setFlag(LOAD_MAP_FLAG);
|
||||
@ -511,7 +511,6 @@ void updateWorld() {
|
||||
void handleKeyUp(SDL_Keycode key, SDLPP::Scene &scene) {
|
||||
switch (key) {
|
||||
case SDLK_ESCAPE:
|
||||
std::cout << "Eskape" << std::endl;
|
||||
game_scenes.push_back(mainMenuScene);
|
||||
break;
|
||||
case SDLK_a:
|
||||
@ -681,6 +680,17 @@ MapObject &getSelectedObject() {
|
||||
return global_vars.objects[pos.getX()][pos.getY()];
|
||||
}
|
||||
|
||||
void setObjTeleport(const std::string &level) {
|
||||
if(current_obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
if(level.empty()) {
|
||||
current_modifier->destroy();
|
||||
return;
|
||||
}
|
||||
current_obj->setModifier(TELEPORT_MODIFIER_ID, level);
|
||||
}
|
||||
|
||||
void placeTool(SDLPP::Scene &scene) {
|
||||
std::lock_guard<std::mutex> lock(render_mutex);
|
||||
map_changed = true;
|
||||
@ -767,8 +777,9 @@ void placeTool(SDLPP::Scene &scene) {
|
||||
new_obj->setData(global_vars.current_tool->getData());
|
||||
if(new_obj->getId() == TELEPORT_MODIFIER_ID) {
|
||||
// TODO input
|
||||
new_obj->setTeleportLevel("test2.marmap");
|
||||
obj.setModifier(global_vars.current_tool->getId(), "test2.marmap");
|
||||
current_obj = &obj;
|
||||
current_modifier = new_obj;
|
||||
game_scenes.push_back(createTextScene(scene.getRendererShared(), setObjTeleport));
|
||||
}
|
||||
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
|
||||
// TODO createModifierBlock
|
||||
@ -783,6 +794,7 @@ void placeTool(SDLPP::Scene &scene) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO move to a functions file, used in text_scene.cpp as well
|
||||
bool controlOrCommandPressed() {
|
||||
return SDL_GetModState() & KMOD_CTRL || SDL_GetModState() & KMOD_GUI;
|
||||
}
|
||||
|
@ -19,4 +19,8 @@ createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer,
|
||||
SceneStruct
|
||||
createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer);
|
||||
|
||||
SceneStruct
|
||||
createTextScene(std::shared_ptr<SDLPP::Renderer> renderer,
|
||||
std::function<void(const std::string &)> finalizer);
|
||||
|
||||
#endif
|
216
mario/scenes/text_scene.cpp
Normal file
216
mario/scenes/text_scene.cpp
Normal file
@ -0,0 +1,216 @@
|
||||
#include "SDL2/SDL_keycode.h"
|
||||
#include "editor_scenes.hpp"
|
||||
#include "../../sdlpp/sdlpp.hpp"
|
||||
#include "../../sdlpp/sdlpp_mouse.hpp"
|
||||
#include "../global_vars.hpp"
|
||||
#include "../objectids.hpp"
|
||||
#include "../editor_visitor.hpp"
|
||||
|
||||
bool __update_scenes_text_dialog = false;
|
||||
bool __quit_scenes_text_dialog = false;
|
||||
bool __started_text_dialog = false;
|
||||
bool __update_text = false;
|
||||
uint64_t __cur_button_index_text_dialog = -1;
|
||||
uint64_t __cur_button_index_text_dialog_down = -1;
|
||||
std::vector<std::shared_ptr<Button>> __buttons_text_dialog{};
|
||||
std::shared_ptr<SDLPP::RectangleRender> __mouse_text_dialog{};
|
||||
std::shared_ptr<SDLPP::TextRenderer> __level_name_object{};
|
||||
std::string __level_name_text = "LEVEL NAME";
|
||||
|
||||
std::function<void(const std::string &)> __text_dialog_finalizer;
|
||||
|
||||
void __quitTextDialog() {
|
||||
__quit_scenes_text_dialog = true;
|
||||
}
|
||||
|
||||
void __quitCallback_TextDialog(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
||||
__quitTextDialog();
|
||||
}
|
||||
|
||||
void __handleKeyUp_TextDialog(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
|
||||
switch (key) {
|
||||
case SDLK_ESCAPE:
|
||||
__level_name_text = "";
|
||||
// fallthrough
|
||||
case SDLK_RETURN:
|
||||
__quitTextDialog();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<SDLPP::Scene>
|
||||
createSceneTextDialog(std::shared_ptr<SDLPP::Renderer> &renderer) {
|
||||
auto scene = std::make_shared<SDLPP::Scene>(renderer);
|
||||
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
|
||||
"#00000088", true);
|
||||
bg->setPermanent();
|
||||
bg->setId(1);
|
||||
scene->addObject(bg);
|
||||
__mouse_text_dialog =
|
||||
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
|
||||
__mouse_text_dialog->setMinWidth(1);
|
||||
__mouse_text_dialog->setMinHeight(1);
|
||||
__mouse_text_dialog->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
__mouse_text_dialog->setId(EDITOR_MOUSE_ID);
|
||||
__mouse_text_dialog->setColiderColor("#00FF00");
|
||||
__mouse_text_dialog->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
|
||||
scene->addObject(__mouse_text_dialog);
|
||||
|
||||
ButtonConfig default_button_theme{};
|
||||
default_button_theme.bg_color = "#FFFFFF88";
|
||||
default_button_theme.bg_color_highlight = "#FFFFFFBB";
|
||||
default_button_theme.bg_color_disabled = "#AAAAAA88";
|
||||
default_button_theme.font_color = "#000000";
|
||||
default_button_theme.font_color_highlight = "#000000";
|
||||
default_button_theme.font_color_disabled = "#555555";
|
||||
default_button_theme.font_outline_color = "#FFFFFF88";
|
||||
default_button_theme.font_outline_color_highlight = "#FFFFFFAA";
|
||||
default_button_theme.font_outline_color_disabled = "#787878";
|
||||
default_button_theme.outline = 0.1;
|
||||
// buttons
|
||||
__buttons_text_dialog.emplace_back(std::make_shared<Button>(
|
||||
0.3, 0.7, 0.4, 0.2, renderer, "OK", default_button_theme,
|
||||
__quitCallback_TextDialog, nullptr));
|
||||
__buttons_text_dialog.back()->setAlignment(SDLPP::OBJ_CENTER,
|
||||
SDLPP::OBJ_CENTER);
|
||||
__buttons_text_dialog.back()->setPermanent();
|
||||
__buttons_text_dialog.back()->setButtonIndex(__buttons_text_dialog.size() - 1);
|
||||
__buttons_text_dialog.back()->setHighlight();
|
||||
for (auto &button : __buttons_text_dialog) {
|
||||
scene->addObject(button);
|
||||
}
|
||||
auto font_config = std::make_shared<SDLPP::FontConfiguration>(
|
||||
g_text_config->getFont(), "#000000", "#282828", 0.05);
|
||||
// level name
|
||||
__level_name_object = std::make_shared<SDLPP::TextRenderer>(
|
||||
0.1, 0.4, 0.8, 0.2, renderer, __level_name_text,
|
||||
font_config, SDLPP_TEXT_LEFT);
|
||||
__level_name_object->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
__level_name_object->setPermanent();
|
||||
__level_name_object->setColor("#FFFFFF88");
|
||||
scene->addObject(__level_name_object);
|
||||
return scene;
|
||||
}
|
||||
|
||||
void __resetGlobals_TextDialog() {
|
||||
__update_scenes_text_dialog = false;
|
||||
__quit_scenes_text_dialog = false;
|
||||
__started_text_dialog = false;
|
||||
__cur_button_index_text_dialog = -1;
|
||||
__cur_button_index_text_dialog_down = -1;
|
||||
__buttons_text_dialog.clear();
|
||||
__mouse_text_dialog.reset();
|
||||
__level_name_object.reset();
|
||||
__level_name_text = "LEVEL NAME";
|
||||
}
|
||||
|
||||
void __additionalRender_TextDialog(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
|
||||
if (__update_text) {
|
||||
__level_name_object->changeText(__level_name_text);
|
||||
__update_text = false;
|
||||
__update_scenes_text_dialog = true;
|
||||
}
|
||||
if (__update_scenes_text_dialog) {
|
||||
for (auto &_scene : game_scenes) {
|
||||
_scene.scene->updateSizeAndPosition();
|
||||
}
|
||||
if (__started_text_dialog) {
|
||||
__update_scenes_text_dialog = false;
|
||||
} else {
|
||||
__started_text_dialog = true;
|
||||
}
|
||||
}
|
||||
if (__quit_scenes_text_dialog) {
|
||||
SDL_StopTextInput();
|
||||
if(!__level_name_text.empty() &&
|
||||
(__level_name_text.size() < 7 || __level_name_text.substr(__level_name_text.size() - 7) != ".marmap")) {
|
||||
__level_name_text += ".marmap";
|
||||
}
|
||||
__text_dialog_finalizer(__level_name_text);
|
||||
game_scenes.pop_back();
|
||||
__resetGlobals_TextDialog();
|
||||
}
|
||||
}
|
||||
|
||||
void __getMousePositionFlags_TextDialog(SDLPP::Scene &scene) {
|
||||
auto mouse = scene.getObjects({ EDITOR_MOUSE_ID })[0];
|
||||
// move mouse colider to mouse position
|
||||
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(
|
||||
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
|
||||
|
||||
MouseVisitor visitor;
|
||||
scene.visitCollisions(*mouse, visitor);
|
||||
__cur_button_index_text_dialog = visitor.getCurButton();
|
||||
}
|
||||
|
||||
bool controlOrCommandPressed();
|
||||
|
||||
void __pollEvents_TextDialog(std::shared_ptr<SDLPP::Scene> &scene) {
|
||||
SDL_Event event;
|
||||
while (SDLPP::getSDLEvent(event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
__quitTextDialog();
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
__handleKeyUp_TextDialog(event.key.keysym.sym, *scene);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.sym == SDLK_BACKSPACE &&
|
||||
!__level_name_text.empty()) {
|
||||
__level_name_text.pop_back();
|
||||
__update_text = true;
|
||||
} else if (event.key.keysym.sym == SDLK_c &&
|
||||
controlOrCommandPressed()) {
|
||||
// handle copy
|
||||
SDL_SetClipboardText(__level_name_text.c_str());
|
||||
} else if (event.key.keysym.sym == SDLK_v &&
|
||||
controlOrCommandPressed()) {
|
||||
// handle paste
|
||||
__level_name_text += SDL_GetClipboardText();
|
||||
__update_text = true;
|
||||
}
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||
__update_scenes_text_dialog = true;
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
__getMousePositionFlags_TextDialog(*scene);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (__cur_button_index_text_dialog_down ==
|
||||
__cur_button_index_text_dialog &&
|
||||
__cur_button_index_text_dialog != (uint64_t)-1) {
|
||||
__buttons_text_dialog[__cur_button_index_text_dialog]
|
||||
->performFunction();
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
// store current mouse flags in previous mouse flags
|
||||
__cur_button_index_text_dialog_down = __cur_button_index_text_dialog;
|
||||
break;
|
||||
case SDL_TEXTINPUT:
|
||||
__level_name_text += event.text.text;
|
||||
__update_text = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SceneStruct createTextScene(std::shared_ptr<SDLPP::Renderer> renderer,
|
||||
std::function<void(const std::string &)> finalizer) {
|
||||
__text_dialog_finalizer = finalizer;
|
||||
SceneStruct ret{};
|
||||
ret.scene = createSceneTextDialog(renderer);
|
||||
ret.additionalRender = __additionalRender_TextDialog;
|
||||
ret.doInput = __pollEvents_TextDialog;
|
||||
__update_scenes_text_dialog = true;
|
||||
SDL_StartTextInput();
|
||||
return ret;
|
||||
}
|
@ -6,10 +6,12 @@
|
||||
void MarioVisitor::visit(const SDLPP::RenderObject &obj) {
|
||||
auto id = obj.getId();
|
||||
switch (id) {
|
||||
case PIPE_LEFT_BOTTOM_ID:
|
||||
case PIPE_RIGHT_BOTTOM_ID:
|
||||
case PIPE_LEFT_TOP_ID:
|
||||
case PIPE_RIGHT_TOP_ID:
|
||||
teleport_bottom = true;
|
||||
// fallthrough
|
||||
case PIPE_LEFT_BOTTOM_ID:
|
||||
case PIPE_RIGHT_BOTTOM_ID:
|
||||
{
|
||||
auto m_obj = dynamic_cast<const MarioBlock &>(obj);
|
||||
if(m_obj.hasTeleport()) {
|
||||
|
@ -136,6 +136,9 @@ public:
|
||||
void setTeleportLevel(const std::string &level) {
|
||||
teleport_level = level;
|
||||
}
|
||||
bool teleportBottom() {
|
||||
return teleport_bottom;
|
||||
}
|
||||
|
||||
private:
|
||||
bool onGround = false;
|
||||
@ -165,6 +168,7 @@ private:
|
||||
bool _death = false;
|
||||
bool _instant_death = false;
|
||||
std::string teleport_level = "";
|
||||
bool teleport_bottom = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -46,7 +46,6 @@ void ProjectileVisitor::visit(const SDLPP::RenderObject &obj) {
|
||||
}
|
||||
break;
|
||||
case GOOMBA_ID:
|
||||
std::cout << "DEATH" << std::endl;
|
||||
death = true;
|
||||
default:
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user