diff --git a/mario/mario.cpp b/mario/mario.cpp index 700e10f..fa52e76 100644 --- a/mario/mario.cpp +++ b/mario/mario.cpp @@ -27,6 +27,8 @@ Mario::Mario(int x, int y, const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::RectColider( 0.9, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT ) ); top_collision = std::make_shared( 0.5, 0, 0.2, 0.15, MARIO_TOP_DETECT ); addCollision( top_collision ); + addCollision( + SDLPP::RectColider( 0, 0, 1, 1, MARIO_ID ) ); setColiderColor("#FF0000"); setStatic( false ); } @@ -69,7 +71,7 @@ void Mario::handleVisitor(SDLPP::Visitor &visitor) { on_ground = m_visitor.isOnGround(); if(!jumping && on_ground) { resetMovementY(); - setTextureSourceRect(MARIO_STANDING_SRC); + setBaseRect(MARIO_STANDING_SRC); if(getMovement().getX() != 0) resumeAnimation(); // for some reason falling of the edge causes on_ground to be true, but @@ -102,6 +104,9 @@ void Mario::handleVisitor(SDLPP::Visitor &visitor) { setPos( objPos.getX() - BLOCK_SIZE, getPos().getY() ); } } + if(m_visitor.hasMushroom()) { + setType(LandType::UNDERWORLD); + } #endif } @@ -115,7 +120,7 @@ void Mario::jump() { slow_jump = getPos().getY() - 2*BLOCK_SIZE; addMovement( 0, -jump_movement ); ticks_till_gravity = base_gravity_ticks; - setTextureSourceRect(MARIO_JUMP_SRC); + setBaseRect(MARIO_JUMP_SRC); pauseAnimation(); } @@ -144,8 +149,9 @@ void Mario::custom_move( int ticks ) { } } -void Mario::visit(SDLPP::Visitor &/*UNUSED*/) { +void Mario::visit(SDLPP::Visitor &visitor) { // TODO + visitor.visit(*this); } void Mario::setWorldTypeSrc(LandType::Value /*UNUSED*/) { diff --git a/mario/visitors/mario_visitor.cpp b/mario/visitors/mario_visitor.cpp index 148e032..ab78703 100644 --- a/mario/visitors/mario_visitor.cpp +++ b/mario/visitors/mario_visitor.cpp @@ -58,6 +58,9 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) { newX = obj.getDoubleRect().first.getX() + obj.getDoubleRect().second.getX(); break; + case MUSHROOM_ID: + mushroom = true; + break; default: break; } diff --git a/mario/visitors/mario_visitor.hpp b/mario/visitors/mario_visitor.hpp index d6e227e..9917c6d 100644 --- a/mario/visitors/mario_visitor.hpp +++ b/mario/visitors/mario_visitor.hpp @@ -8,7 +8,7 @@ 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) {} + MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &quit, int &coin_count, std::vector< std::shared_ptr< MarioBlock > > &moving_objects) : jumping(is_jumping), _scene(scene), _quit(quit), _coin_count(coin_count), _moving_objects(moving_objects) {} void visit( const SDLPP::RenderObject &obj ) override; bool isOnGround() const { return onGround; @@ -90,6 +90,16 @@ public: return coin_block; } + bool hasMushroom() const { + return mushroom; + } + + void setMushroomBlock(std::shared_ptr &mushroom) { + _scene.addObject(mushroom); + _scene.setZIndex(mushroom, 1); + _moving_objects.push_back(mushroom); + } + private: bool onGround = false; double groundY = 0; @@ -110,6 +120,8 @@ private: SDLPP::Scene &_scene; bool &_quit; int &_coin_count; + bool mushroom = false; + std::vector< std::shared_ptr< MarioBlock > > &_moving_objects; }; #endif diff --git a/mario/visitors/visitor_generator.cpp b/mario/visitors/visitor_generator.cpp index f759ec2..3419210 100644 --- a/mario/visitors/visitor_generator.cpp +++ b/mario/visitors/visitor_generator.cpp @@ -1,6 +1,7 @@ #include "visitor_generator.hpp" #include "../objectids.hpp" #include "mario_visitor.hpp" +#include "mushroom_visitor.hpp" std::shared_ptr< SDLPP::Visitor > getVisitor( const MarioBlock &block, SDLPP::Scene &scene, bool &quit, @@ -9,7 +10,10 @@ getVisitor( const MarioBlock &block, SDLPP::Scene &scene, bool &quit, std::shared_ptr< SDLPP::Visitor > result{}; switch(block.getId()) { case MARIO_ID: - result = std::static_pointer_cast(std::make_shared(block.getMovement().getY() < 0, scene, quit, coin_count)); + result = std::static_pointer_cast(std::make_shared(block.getMovement().getY() < 0, scene, quit, coin_count, moving_objects)); + break; + case MUSHROOM_ID: + result = std::static_pointer_cast(std::make_shared()); default: break; }