From f7002f0f387c7202ba57e0052be07498723552c7 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 7 Aug 2021 22:17:27 +0200 Subject: [PATCH] Mario: better mushroom movement --- mario/blocks/mushroomblock.cpp | 4 ++++ mario/main.cpp | 2 +- mario/visitors/mushroom_visitor.cpp | 2 ++ mario/visitors/mushroom_visitor.hpp | 4 ++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mario/blocks/mushroomblock.cpp b/mario/blocks/mushroomblock.cpp index d7d8348..2b30ac5 100644 --- a/mario/blocks/mushroomblock.cpp +++ b/mario/blocks/mushroomblock.cpp @@ -42,7 +42,11 @@ void MushroomBlock::handleVisitor(SDLPP::Visitor &visitor) { } auto &m_visitor = dynamic_cast(visitor); setOnGround(m_visitor.isOnGround()); + if(isOnGround()) { + setPos(getPos().getX(), m_visitor.getGroundY() - BLOCK_SIZE); + } if(!m_visitor.canGoLeft() || !m_visitor.canGoRight()) { + setPos(m_visitor.getValidXPos(), getPos().getY()); setMovement(-getMovement().getX(), getMovement().getY()); } if(m_visitor.getDeath()) { diff --git a/mario/main.cpp b/mario/main.cpp index ac1785c..6b044b8 100644 --- a/mario/main.cpp +++ b/mario/main.cpp @@ -142,6 +142,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { std::lock_guard< std::mutex > lock( render_mutex ); scene->updateScene(); auto prev_coin_count = coin_count; + // TODO only start doing visits once the object has been on screen at least once for(size_t i = 0; i < moving_objects.size(); i++) { auto visitor = getVisitor( *moving_objects[i], *scene, quit, coin_count, moving_objects ); @@ -151,7 +152,6 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { moving_objects[i]->getDoubleRect().second.getX(); if(rightmost_pos < 0) { moving_objects[i]->destroy(); - std::cout << "DESTRUCTION!!!!" << std::endl; } } std::vector killed_indices{}; diff --git a/mario/visitors/mushroom_visitor.cpp b/mario/visitors/mushroom_visitor.cpp index 2f59419..858a262 100644 --- a/mario/visitors/mushroom_visitor.cpp +++ b/mario/visitors/mushroom_visitor.cpp @@ -34,11 +34,13 @@ void MushroomVisitor::visit( const SDLPP::RenderObject &obj ) { } else if ( from == MARIO_LEFT_SIDE_DETECT ) { if(!left && !right) { movement_blockage = obj.getPos(); + validXPos = movement_blockage.getX() + BLOCK_SIZE; } left = true; } else if (from == MARIO_RIGHT_SIDE_DETECT ) { if(!left && !right) { movement_blockage = obj.getPos(); + validXPos = movement_blockage.getX() - BLOCK_SIZE; } right = true; } diff --git a/mario/visitors/mushroom_visitor.hpp b/mario/visitors/mushroom_visitor.hpp index 12bb600..8b6c9b3 100644 --- a/mario/visitors/mushroom_visitor.hpp +++ b/mario/visitors/mushroom_visitor.hpp @@ -40,10 +40,14 @@ public: bool getDeath() { return death; } + double getValidXPos() { + return validXPos; + } private: bool onGround = false; double groundY = 0; + double validXPos = 0; bool stop = false; bool left = false; bool right = false;