From 6238022ed2e6d15494d91418c3f70be29358dc06 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 7 Aug 2021 12:13:23 +0200 Subject: [PATCH] Mario: fix modifier deletion --- mario/blocks.hpp | 2 +- mario/blocks/coineditorblock.cpp | 2 +- mario/blocks/coineditorblock.hpp | 2 +- mario/editor.cpp | 45 +++++++++++++++++------------ mario/editor_visitor.cpp | 12 ++++---- mario/editor_visitor.hpp | 48 +++++++++++++++++-------------- mario/visitors/bounce_visitor.hpp | 18 ++++++------ mario/visitors/mario_visitor.hpp | 36 +++++++++++------------ 8 files changed, 88 insertions(+), 77 deletions(-) diff --git a/mario/blocks.hpp b/mario/blocks.hpp index 91c7039..fc47965 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -23,7 +23,7 @@ public: LandType::Value getType() const; virtual void onScrollUp() {} virtual void onScrollDown() {} - virtual uint8_t getData() { + virtual uint8_t getData() const { return 0; } virtual void setData( uint8_t /*UNUSED*/ ) {} diff --git a/mario/blocks/coineditorblock.cpp b/mario/blocks/coineditorblock.cpp index d159bdf..2c611f0 100644 --- a/mario/blocks/coineditorblock.cpp +++ b/mario/blocks/coineditorblock.cpp @@ -66,7 +66,7 @@ void CoinEditorBlock::onScrollDown() { subtractOne(); } -uint8_t CoinEditorBlock::getData() { +uint8_t CoinEditorBlock::getData() const { return _amount; } diff --git a/mario/blocks/coineditorblock.hpp b/mario/blocks/coineditorblock.hpp index 11e3685..a68b532 100644 --- a/mario/blocks/coineditorblock.hpp +++ b/mario/blocks/coineditorblock.hpp @@ -17,7 +17,7 @@ public: void setAmount( int amount ); void onScrollUp() override; void onScrollDown() override; - uint8_t getData() override; + uint8_t getData() const override; void setData( uint8_t data ) override; private: diff --git a/mario/editor.cpp b/mario/editor.cpp index 8055fa3..c28024f 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -51,11 +51,11 @@ struct ToolType { }; struct MouseInfo { - uint64_t cur_flags; - uint64_t prev_flags; + uint64_t cur_flags{}; + uint64_t prev_flags{}; SDLPP::Vec2D< int > edit_box; SDLPP::Vec2D< int > tool_box; - ToolType::Value tool_type; + ToolType::Value tool_type{}; }; struct MapInfo { @@ -76,10 +76,10 @@ struct ToolInfo { }; struct GlobalVars { - MouseInfo mouse; - MapInfo map; - ToolInfo tool; - uint64_t flags; + MouseInfo mouse{}; + MapInfo map{}; + ToolInfo tool{}; + uint64_t flags{}; std::vector< mapColumnType > objects; std::vector< std::shared_ptr< MarioBlock > > tools; std::vector< std::shared_ptr< MarioBlock > > mods; @@ -87,7 +87,7 @@ struct GlobalVars { std::vector< std::shared_ptr< SDLPP::RenderObject > > tool_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > mod_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > character_boxes; - enum LandType::Value current_world_type; + enum LandType::Value current_world_type{}; std::shared_ptr< MarioBlock > coin_tool; std::shared_ptr< MarioBlock > generic_tool; std::shared_ptr< MarioBlock > current_tool; @@ -151,8 +151,9 @@ void updateTool() { } void removeMario() { - if ( !global_vars.mario ) + if ( !global_vars.mario ) { return; + } global_vars .objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()] .unsetCharacter(); @@ -160,7 +161,7 @@ void removeMario() { } void setToolColor( const std::string &color ) { - std::vector< std::shared_ptr< SDLPP::RenderObject > > *store; + std::vector< std::shared_ptr< SDLPP::RenderObject > > *store = nullptr; int multiplier = 0; switch ( global_vars.tool.type ) { case ToolType::BLOCK: @@ -174,13 +175,12 @@ void setToolColor( const std::string &color ) { case ToolType::CHARACTER: multiplier = CHARACTER_WIDTH; store = &global_vars.character_boxes; - break; default: - store = nullptr; break; } - if ( store == nullptr ) + if ( store == nullptr ) { return; + } auto index = global_vars.tool.index % ( 2 * multiplier ); store->at( index )->setColor( color ); } @@ -216,8 +216,9 @@ void updateToolSelection( int prev_index, ToolType::Value type ) { default: break; } - if ( tool_vec == nullptr ) + if ( tool_vec == nullptr ) { return; + } auto cur = cur_page * multiplier; size_t prev = prev_index * multiplier; for ( size_t i = prev; @@ -369,8 +370,9 @@ void selectPrevTool() { break; } int subtraction = 1; - if ( global_vars.tool.index % multiplier == 0 ) + if ( global_vars.tool.index % multiplier == 0 ) { subtraction = multiplier + 1; + } if ( global_vars.tool.index == 0 || global_vars.tool.index - subtraction > static_cast< uint64_t >( -multiplier ) ) { @@ -399,12 +401,15 @@ void selectNextTool() { } int addition = 1; if ( global_vars.tool.index % multiplier == - static_cast< uint64_t >( multiplier - 1 ) ) + static_cast< uint64_t >( multiplier - 1 ) ) { addition = multiplier + 1; - if ( global_vars.tool.index == max_index ) + } + if ( global_vars.tool.index == max_index ) { return; - if ( global_vars.tool.index + addition > max_index ) + } + if ( global_vars.tool.index + addition > max_index ) { addition = 1; + } updateToolIndex( global_vars.tool.index + addition ); } @@ -566,6 +571,7 @@ void placeTool( SDLPP::Scene &scene ) { ToolVisitor visitor; visitor.setSourceType( global_vars.current_world_type ); + visitor.setSourceData( global_vars.current_tool->getData() ); auto tool_type = getBlockRole( global_vars.current_tool->getId() ); switch ( tool_type ) { case BlockRole::TERRAIN: @@ -578,6 +584,7 @@ void placeTool( SDLPP::Scene &scene ) { scene.visitCollisions( *global_vars.current_tool, visitor ); auto &obj = getSelectedObject(); + std::cout << "Remove? " << (visitor.removeBlock() ? "YES" : "NO") << ", Add?" << (visitor.addBlock() ? "YES" : "NO") << std::endl; if ( visitor.removeBlock() && !visitor.addBlock() ) { switch ( visitor.getVisitorType() ) { case VisitorType::Terrain: @@ -591,7 +598,7 @@ void placeTool( SDLPP::Scene &scene ) { } } else if ( visitor.addBlock() ) { auto renderer = scene.getRendererShared(); - int z_index = 1; + uint64_t z_index = 1; std::shared_ptr< MarioBlock > new_obj = nullptr; switch ( visitor.getVisitorType() ) { case VisitorType::Terrain: diff --git a/mario/editor_visitor.cpp b/mario/editor_visitor.cpp index 7e68774..9f57ab0 100644 --- a/mario/editor_visitor.cpp +++ b/mario/editor_visitor.cpp @@ -93,17 +93,17 @@ void ToolVisitor::visit( const SDLPP::RenderObject &obj ) { auto id = obj.getCollisions()[0]->getId(); switch ( id ) { case EDITOR_TERRAIN_ID: { - const MarioBlock &m_obj = dynamic_cast(obj); + const auto &m_obj = dynamic_cast(obj); remove_block = true; if ( obj.getId() == source_id && - m_obj.getType() == source_type && - getVisitorType() == VisitorType::Terrain ) { + ((m_obj.getType() == source_type && + getVisitorType() == VisitorType::Terrain) || ( m_obj.getData() == _data && getVisitorType() == VisitorType::Modifier))) { add_block = false; } } break; case EDITOR_CHARACTER_ID: { - const MarioBlock &m_obj = dynamic_cast(obj); + const auto &m_obj = dynamic_cast(obj); remove_block = true; if ( obj.getId() == source_id && m_obj.getType() == source_type && @@ -115,9 +115,9 @@ void ToolVisitor::visit( const SDLPP::RenderObject &obj ) { break; } } -bool ToolVisitor::addBlock() { +bool ToolVisitor::addBlock() const { return add_block; } -bool ToolVisitor::removeBlock() { +bool ToolVisitor::removeBlock() const { return remove_block; } diff --git a/mario/editor_visitor.hpp b/mario/editor_visitor.hpp index 2254823..f2ec758 100644 --- a/mario/editor_visitor.hpp +++ b/mario/editor_visitor.hpp @@ -15,34 +15,34 @@ struct VisitorType { class MouseVisitor : public SDLPP::Visitor { public: - MouseVisitor() {} - virtual void visit( const SDLPP::RenderObject &obj ) override; - virtual void setFromId( uint64_t /*UNUSED*/ ) override {} - virtual uint64_t getFromId() override { + MouseVisitor() = default; + void visit( const SDLPP::RenderObject &obj ) override; + void setFromId( uint64_t /*UNUSED*/ ) override {} + uint64_t getFromId() const override { return 0; } - uint64_t getFlags() { + uint64_t getFlags() const { return select_flags; } - bool foundEditBox() { + bool foundEditBox() const { return edit_box; } - const SDLPP::Vec2D< int > &getEditBoxIndexes() { + const SDLPP::Vec2D< int > &getEditBoxIndexes() const { return edit_box_location; } - bool foundToolBox() { + bool foundToolBox() const { return tool_box; } - const SDLPP::Vec2D< int > &getToolBoxIndexes() { + const SDLPP::Vec2D< int > &getToolBoxIndexes() const { return tool_box_location; } - virtual void setVisitorType( uint64_t type ) override { + void setVisitorType( uint64_t type ) override { _type = type; } - virtual uint64_t getVisitorType() override { + uint64_t getVisitorType() const override { return _type; } - uint64_t getToolType() { + uint64_t getToolType() const { return tool_box_type; } @@ -61,38 +61,42 @@ private: bool tool_box = false; SDLPP::Vec2D< int > edit_box_location = { -1, -1 }; SDLPP::Vec2D< int > tool_box_location = { -1, -1 }; - uint64_t _type; + uint64_t _type{}; uint64_t tool_box_type = 0; }; class ToolVisitor : public SDLPP::Visitor { public: - ToolVisitor(){}; - virtual void visit( const SDLPP::RenderObject &obj ) override; - virtual void setFromId( uint64_t id ) override { + ToolVisitor() = default; + void visit( const SDLPP::RenderObject &obj ) override; + void setFromId( uint64_t id ) override { source_id = id; } - virtual uint64_t getFromId() override { + uint64_t getFromId() const override { return source_id; } - virtual void setVisitorType( uint64_t type ) override { + void setVisitorType( uint64_t type ) override { _type = type; } - virtual uint64_t getVisitorType() override { + uint64_t getVisitorType() const override { return _type; } void setSourceType(LandType::Value type) { source_type = type; } - bool addBlock(); - bool removeBlock(); + void setSourceData(const uint64_t &data) { + _data = data; + } + bool addBlock() const; + bool removeBlock() const; private: bool remove_block = false; bool add_block = true; uint64_t source_id = 0; uint64_t _type = 0; - LandType::Value source_type; + uint64_t _data = 0; + LandType::Value source_type{}; }; #endif diff --git a/mario/visitors/bounce_visitor.hpp b/mario/visitors/bounce_visitor.hpp index 1f5f94e..5a037cb 100644 --- a/mario/visitors/bounce_visitor.hpp +++ b/mario/visitors/bounce_visitor.hpp @@ -6,29 +6,29 @@ class BounceVisitor : public SDLPP::Visitor { public: - BounceVisitor() {} - virtual void visit( const SDLPP::RenderObject &obj ) override; - virtual void setFromId( uint64_t id ) override { + BounceVisitor() = default; + void visit( const SDLPP::RenderObject &obj ) override; + void setFromId( uint64_t id ) override { from = id; } - virtual uint64_t getFromId() override { + uint64_t getFromId() const override { return from; } - virtual void setVisitorType( uint64_t type ) override { + void setVisitorType( uint64_t type ) override { _type = type; } - virtual uint64_t getVisitorType() override { + uint64_t getVisitorType() const override { return _type; } - bool canBounce() { + bool canBounce() const { return hits < 2; } private: int hits = 0; - uint64_t from; - uint64_t _type; + uint64_t from{}; + uint64_t _type{}; }; #endif diff --git a/mario/visitors/mario_visitor.hpp b/mario/visitors/mario_visitor.hpp index 2affcd4..d6e227e 100644 --- a/mario/visitors/mario_visitor.hpp +++ b/mario/visitors/mario_visitor.hpp @@ -9,51 +9,51 @@ class MarioVisitor : public SDLPP::Visitor { public: MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &quit, int &coin_count) : jumping(is_jumping), _scene(scene), _quit(quit), _coin_count(coin_count) {} - virtual void visit( const SDLPP::RenderObject &obj ) override; - bool isOnGround() { + void visit( const SDLPP::RenderObject &obj ) override; + bool isOnGround() const { return onGround; } - bool isDead() { + bool isDead() const { return death; } - bool isStopped() { + bool isStopped() const { return stop; } - double newXPos() { + double newXPos() const { return newX; } - virtual void setFromId( uint64_t id ) override { + void setFromId( uint64_t id ) override { from = id; } - virtual uint64_t getFromId() override { + uint64_t getFromId() const override { return from; } - virtual void setVisitorType( uint64_t type ) override { + void setVisitorType( uint64_t type ) override { _type = type; } - virtual uint64_t getVisitorType() override { + uint64_t getVisitorType() const override { return _type; } - bool canGoLeft() { + bool canGoLeft() const { return !left; } - bool canGoRight() { + bool canGoRight() const { return !right; } - double getGroundY() { + double getGroundY() const { return groundY; } - bool topBlock() { + bool topBlock() const { return top_hit; } - bool moveTop() { + bool moveTop() const { return top_left_right && !top_hit; } const SDLPP::Vec2D &getRightLeftPos() { return rightleftpos; } - bool canDestroy() { + bool canDestroy() const { return jumping && !top_hit; } @@ -61,7 +61,7 @@ public: return movement_blockage; } - double getStopX() { + double getStopX() const { return newX; } @@ -71,7 +71,7 @@ public: _coin_count++; } - bool hasCoin() { + bool hasCoin() const { return coin; } @@ -95,7 +95,7 @@ private: double groundY = 0; bool death = false; bool stop = false; - double newX; + double newX{}; uint64_t from = -1; bool left = false; bool right = false;