Mario: fix modifier deletion

This commit is contained in:
zvon 2021-08-07 12:13:23 +02:00
parent 7b1ef25f37
commit 6238022ed2
8 changed files with 88 additions and 77 deletions

View File

@ -23,7 +23,7 @@ public:
LandType::Value getType() const; LandType::Value getType() const;
virtual void onScrollUp() {} virtual void onScrollUp() {}
virtual void onScrollDown() {} virtual void onScrollDown() {}
virtual uint8_t getData() { virtual uint8_t getData() const {
return 0; return 0;
} }
virtual void setData( uint8_t /*UNUSED*/ ) {} virtual void setData( uint8_t /*UNUSED*/ ) {}

View File

@ -66,7 +66,7 @@ void CoinEditorBlock::onScrollDown() {
subtractOne(); subtractOne();
} }
uint8_t CoinEditorBlock::getData() { uint8_t CoinEditorBlock::getData() const {
return _amount; return _amount;
} }

View File

@ -17,7 +17,7 @@ public:
void setAmount( int amount ); void setAmount( int amount );
void onScrollUp() override; void onScrollUp() override;
void onScrollDown() override; void onScrollDown() override;
uint8_t getData() override; uint8_t getData() const override;
void setData( uint8_t data ) override; void setData( uint8_t data ) override;
private: private:

View File

@ -51,11 +51,11 @@ struct ToolType {
}; };
struct MouseInfo { struct MouseInfo {
uint64_t cur_flags; uint64_t cur_flags{};
uint64_t prev_flags; uint64_t prev_flags{};
SDLPP::Vec2D< int > edit_box; SDLPP::Vec2D< int > edit_box;
SDLPP::Vec2D< int > tool_box; SDLPP::Vec2D< int > tool_box;
ToolType::Value tool_type; ToolType::Value tool_type{};
}; };
struct MapInfo { struct MapInfo {
@ -76,10 +76,10 @@ struct ToolInfo {
}; };
struct GlobalVars { struct GlobalVars {
MouseInfo mouse; MouseInfo mouse{};
MapInfo map; MapInfo map{};
ToolInfo tool; ToolInfo tool{};
uint64_t flags; uint64_t flags{};
std::vector< mapColumnType > objects; std::vector< mapColumnType > objects;
std::vector< std::shared_ptr< MarioBlock > > tools; std::vector< std::shared_ptr< MarioBlock > > tools;
std::vector< std::shared_ptr< MarioBlock > > mods; 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 > > tool_boxes;
std::vector< std::shared_ptr< SDLPP::RenderObject > > mod_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > mod_boxes;
std::vector< std::shared_ptr< SDLPP::RenderObject > > character_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 > coin_tool;
std::shared_ptr< MarioBlock > generic_tool; std::shared_ptr< MarioBlock > generic_tool;
std::shared_ptr< MarioBlock > current_tool; std::shared_ptr< MarioBlock > current_tool;
@ -151,8 +151,9 @@ void updateTool() {
} }
void removeMario() { void removeMario() {
if ( !global_vars.mario ) if ( !global_vars.mario ) {
return; return;
}
global_vars global_vars
.objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()] .objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()]
.unsetCharacter(); .unsetCharacter();
@ -160,7 +161,7 @@ void removeMario() {
} }
void setToolColor( const std::string &color ) { 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; int multiplier = 0;
switch ( global_vars.tool.type ) { switch ( global_vars.tool.type ) {
case ToolType::BLOCK: case ToolType::BLOCK:
@ -174,13 +175,12 @@ void setToolColor( const std::string &color ) {
case ToolType::CHARACTER: case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH; multiplier = CHARACTER_WIDTH;
store = &global_vars.character_boxes; store = &global_vars.character_boxes;
break;
default: default:
store = nullptr;
break; break;
} }
if ( store == nullptr ) if ( store == nullptr ) {
return; return;
}
auto index = global_vars.tool.index % ( 2 * multiplier ); auto index = global_vars.tool.index % ( 2 * multiplier );
store->at( index )->setColor( color ); store->at( index )->setColor( color );
} }
@ -216,8 +216,9 @@ void updateToolSelection( int prev_index, ToolType::Value type ) {
default: default:
break; break;
} }
if ( tool_vec == nullptr ) if ( tool_vec == nullptr ) {
return; return;
}
auto cur = cur_page * multiplier; auto cur = cur_page * multiplier;
size_t prev = prev_index * multiplier; size_t prev = prev_index * multiplier;
for ( size_t i = prev; for ( size_t i = prev;
@ -369,8 +370,9 @@ void selectPrevTool() {
break; break;
} }
int subtraction = 1; int subtraction = 1;
if ( global_vars.tool.index % multiplier == 0 ) if ( global_vars.tool.index % multiplier == 0 ) {
subtraction = multiplier + 1; subtraction = multiplier + 1;
}
if ( global_vars.tool.index == 0 || if ( global_vars.tool.index == 0 ||
global_vars.tool.index - subtraction > global_vars.tool.index - subtraction >
static_cast< uint64_t >( -multiplier ) ) { static_cast< uint64_t >( -multiplier ) ) {
@ -399,12 +401,15 @@ void selectNextTool() {
} }
int addition = 1; int addition = 1;
if ( global_vars.tool.index % multiplier == if ( global_vars.tool.index % multiplier ==
static_cast< uint64_t >( multiplier - 1 ) ) static_cast< uint64_t >( multiplier - 1 ) ) {
addition = multiplier + 1; addition = multiplier + 1;
if ( global_vars.tool.index == max_index ) }
if ( global_vars.tool.index == max_index ) {
return; return;
if ( global_vars.tool.index + addition > max_index ) }
if ( global_vars.tool.index + addition > max_index ) {
addition = 1; addition = 1;
}
updateToolIndex( global_vars.tool.index + addition ); updateToolIndex( global_vars.tool.index + addition );
} }
@ -566,6 +571,7 @@ void placeTool( SDLPP::Scene &scene ) {
ToolVisitor visitor; ToolVisitor visitor;
visitor.setSourceType( global_vars.current_world_type ); visitor.setSourceType( global_vars.current_world_type );
visitor.setSourceData( global_vars.current_tool->getData() );
auto tool_type = getBlockRole( global_vars.current_tool->getId() ); auto tool_type = getBlockRole( global_vars.current_tool->getId() );
switch ( tool_type ) { switch ( tool_type ) {
case BlockRole::TERRAIN: case BlockRole::TERRAIN:
@ -578,6 +584,7 @@ void placeTool( SDLPP::Scene &scene ) {
scene.visitCollisions( *global_vars.current_tool, visitor ); scene.visitCollisions( *global_vars.current_tool, visitor );
auto &obj = getSelectedObject(); auto &obj = getSelectedObject();
std::cout << "Remove? " << (visitor.removeBlock() ? "YES" : "NO") << ", Add?" << (visitor.addBlock() ? "YES" : "NO") << std::endl;
if ( visitor.removeBlock() && !visitor.addBlock() ) { if ( visitor.removeBlock() && !visitor.addBlock() ) {
switch ( visitor.getVisitorType() ) { switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain: case VisitorType::Terrain:
@ -591,7 +598,7 @@ void placeTool( SDLPP::Scene &scene ) {
} }
} else if ( visitor.addBlock() ) { } else if ( visitor.addBlock() ) {
auto renderer = scene.getRendererShared(); auto renderer = scene.getRendererShared();
int z_index = 1; uint64_t z_index = 1;
std::shared_ptr< MarioBlock > new_obj = nullptr; std::shared_ptr< MarioBlock > new_obj = nullptr;
switch ( visitor.getVisitorType() ) { switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain: case VisitorType::Terrain:

View File

@ -93,17 +93,17 @@ void ToolVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getCollisions()[0]->getId(); auto id = obj.getCollisions()[0]->getId();
switch ( id ) { switch ( id ) {
case EDITOR_TERRAIN_ID: { case EDITOR_TERRAIN_ID: {
const MarioBlock &m_obj = dynamic_cast<const MarioBlock&>(obj); const auto &m_obj = dynamic_cast<const MarioBlock&>(obj);
remove_block = true; remove_block = true;
if ( obj.getId() == source_id && if ( obj.getId() == source_id &&
m_obj.getType() == source_type && ((m_obj.getType() == source_type &&
getVisitorType() == VisitorType::Terrain ) { getVisitorType() == VisitorType::Terrain) || ( m_obj.getData() == _data && getVisitorType() == VisitorType::Modifier))) {
add_block = false; add_block = false;
} }
} }
break; break;
case EDITOR_CHARACTER_ID: { case EDITOR_CHARACTER_ID: {
const MarioBlock &m_obj = dynamic_cast<const MarioBlock&>(obj); const auto &m_obj = dynamic_cast<const MarioBlock&>(obj);
remove_block = true; remove_block = true;
if ( obj.getId() == source_id && if ( obj.getId() == source_id &&
m_obj.getType() == source_type && m_obj.getType() == source_type &&
@ -115,9 +115,9 @@ void ToolVisitor::visit( const SDLPP::RenderObject &obj ) {
break; break;
} }
} }
bool ToolVisitor::addBlock() { bool ToolVisitor::addBlock() const {
return add_block; return add_block;
} }
bool ToolVisitor::removeBlock() { bool ToolVisitor::removeBlock() const {
return remove_block; return remove_block;
} }

View File

@ -15,34 +15,34 @@ struct VisitorType {
class MouseVisitor : public SDLPP::Visitor { class MouseVisitor : public SDLPP::Visitor {
public: public:
MouseVisitor() {} MouseVisitor() = default;
virtual void visit( const SDLPP::RenderObject &obj ) override; void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t /*UNUSED*/ ) override {} void setFromId( uint64_t /*UNUSED*/ ) override {}
virtual uint64_t getFromId() override { uint64_t getFromId() const override {
return 0; return 0;
} }
uint64_t getFlags() { uint64_t getFlags() const {
return select_flags; return select_flags;
} }
bool foundEditBox() { bool foundEditBox() const {
return edit_box; return edit_box;
} }
const SDLPP::Vec2D< int > &getEditBoxIndexes() { const SDLPP::Vec2D< int > &getEditBoxIndexes() const {
return edit_box_location; return edit_box_location;
} }
bool foundToolBox() { bool foundToolBox() const {
return tool_box; return tool_box;
} }
const SDLPP::Vec2D< int > &getToolBoxIndexes() { const SDLPP::Vec2D< int > &getToolBoxIndexes() const {
return tool_box_location; return tool_box_location;
} }
virtual void setVisitorType( uint64_t type ) override { void setVisitorType( uint64_t type ) override {
_type = type; _type = type;
} }
virtual uint64_t getVisitorType() override { uint64_t getVisitorType() const override {
return _type; return _type;
} }
uint64_t getToolType() { uint64_t getToolType() const {
return tool_box_type; return tool_box_type;
} }
@ -61,38 +61,42 @@ private:
bool tool_box = false; bool tool_box = false;
SDLPP::Vec2D< int > edit_box_location = { -1, -1 }; SDLPP::Vec2D< int > edit_box_location = { -1, -1 };
SDLPP::Vec2D< int > tool_box_location = { -1, -1 }; SDLPP::Vec2D< int > tool_box_location = { -1, -1 };
uint64_t _type; uint64_t _type{};
uint64_t tool_box_type = 0; uint64_t tool_box_type = 0;
}; };
class ToolVisitor : public SDLPP::Visitor { class ToolVisitor : public SDLPP::Visitor {
public: public:
ToolVisitor(){}; ToolVisitor() = default;
virtual void visit( const SDLPP::RenderObject &obj ) override; void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t id ) override { void setFromId( uint64_t id ) override {
source_id = id; source_id = id;
} }
virtual uint64_t getFromId() override { uint64_t getFromId() const override {
return source_id; return source_id;
} }
virtual void setVisitorType( uint64_t type ) override { void setVisitorType( uint64_t type ) override {
_type = type; _type = type;
} }
virtual uint64_t getVisitorType() override { uint64_t getVisitorType() const override {
return _type; return _type;
} }
void setSourceType(LandType::Value type) { void setSourceType(LandType::Value type) {
source_type = type; source_type = type;
} }
bool addBlock(); void setSourceData(const uint64_t &data) {
bool removeBlock(); _data = data;
}
bool addBlock() const;
bool removeBlock() const;
private: private:
bool remove_block = false; bool remove_block = false;
bool add_block = true; bool add_block = true;
uint64_t source_id = 0; uint64_t source_id = 0;
uint64_t _type = 0; uint64_t _type = 0;
LandType::Value source_type; uint64_t _data = 0;
LandType::Value source_type{};
}; };
#endif #endif

View File

@ -6,29 +6,29 @@
class BounceVisitor : public SDLPP::Visitor { class BounceVisitor : public SDLPP::Visitor {
public: public:
BounceVisitor() {} BounceVisitor() = default;
virtual void visit( const SDLPP::RenderObject &obj ) override; void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t id ) override { void setFromId( uint64_t id ) override {
from = id; from = id;
} }
virtual uint64_t getFromId() override { uint64_t getFromId() const override {
return from; return from;
} }
virtual void setVisitorType( uint64_t type ) override { void setVisitorType( uint64_t type ) override {
_type = type; _type = type;
} }
virtual uint64_t getVisitorType() override { uint64_t getVisitorType() const override {
return _type; return _type;
} }
bool canBounce() { bool canBounce() const {
return hits < 2; return hits < 2;
} }
private: private:
int hits = 0; int hits = 0;
uint64_t from; uint64_t from{};
uint64_t _type; uint64_t _type{};
}; };
#endif #endif

View File

@ -9,51 +9,51 @@
class MarioVisitor : public SDLPP::Visitor { class MarioVisitor : public SDLPP::Visitor {
public: public:
MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &quit, int &coin_count) : jumping(is_jumping), _scene(scene), _quit(quit), _coin_count(coin_count) {} 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; void visit( const SDLPP::RenderObject &obj ) override;
bool isOnGround() { bool isOnGround() const {
return onGround; return onGround;
} }
bool isDead() { bool isDead() const {
return death; return death;
} }
bool isStopped() { bool isStopped() const {
return stop; return stop;
} }
double newXPos() { double newXPos() const {
return newX; return newX;
} }
virtual void setFromId( uint64_t id ) override { void setFromId( uint64_t id ) override {
from = id; from = id;
} }
virtual uint64_t getFromId() override { uint64_t getFromId() const override {
return from; return from;
} }
virtual void setVisitorType( uint64_t type ) override { void setVisitorType( uint64_t type ) override {
_type = type; _type = type;
} }
virtual uint64_t getVisitorType() override { uint64_t getVisitorType() const override {
return _type; return _type;
} }
bool canGoLeft() { bool canGoLeft() const {
return !left; return !left;
} }
bool canGoRight() { bool canGoRight() const {
return !right; return !right;
} }
double getGroundY() { double getGroundY() const {
return groundY; return groundY;
} }
bool topBlock() { bool topBlock() const {
return top_hit; return top_hit;
} }
bool moveTop() { bool moveTop() const {
return top_left_right && !top_hit; return top_left_right && !top_hit;
} }
const SDLPP::Vec2D<double> &getRightLeftPos() { const SDLPP::Vec2D<double> &getRightLeftPos() {
return rightleftpos; return rightleftpos;
} }
bool canDestroy() { bool canDestroy() const {
return jumping && !top_hit; return jumping && !top_hit;
} }
@ -61,7 +61,7 @@ public:
return movement_blockage; return movement_blockage;
} }
double getStopX() { double getStopX() const {
return newX; return newX;
} }
@ -71,7 +71,7 @@ public:
_coin_count++; _coin_count++;
} }
bool hasCoin() { bool hasCoin() const {
return coin; return coin;
} }
@ -95,7 +95,7 @@ private:
double groundY = 0; double groundY = 0;
bool death = false; bool death = false;
bool stop = false; bool stop = false;
double newX; double newX{};
uint64_t from = -1; uint64_t from = -1;
bool left = false; bool left = false;
bool right = false; bool right = false;