Mario: mushroom interaction with Mario

This commit is contained in:
zvon 2021-08-07 21:42:51 +02:00
parent 7b2adac922
commit f04dc1e23b
4 changed files with 30 additions and 5 deletions

View File

@ -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 ) ); SDLPP::RectColider( 0.9, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT ) );
top_collision = std::make_shared<SDLPP::RectColider>( 0.5, 0, 0.2, 0.15, MARIO_TOP_DETECT ); top_collision = std::make_shared<SDLPP::RectColider>( 0.5, 0, 0.2, 0.15, MARIO_TOP_DETECT );
addCollision( top_collision ); addCollision( top_collision );
addCollision(
SDLPP::RectColider( 0, 0, 1, 1, MARIO_ID ) );
setColiderColor("#FF0000"); setColiderColor("#FF0000");
setStatic( false ); setStatic( false );
} }
@ -69,7 +71,7 @@ void Mario::handleVisitor(SDLPP::Visitor &visitor) {
on_ground = m_visitor.isOnGround(); on_ground = m_visitor.isOnGround();
if(!jumping && on_ground) { if(!jumping && on_ground) {
resetMovementY(); resetMovementY();
setTextureSourceRect(MARIO_STANDING_SRC); setBaseRect(MARIO_STANDING_SRC);
if(getMovement().getX() != 0) if(getMovement().getX() != 0)
resumeAnimation(); resumeAnimation();
// for some reason falling of the edge causes on_ground to be true, but // 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() ); setPos( objPos.getX() - BLOCK_SIZE, getPos().getY() );
} }
} }
if(m_visitor.hasMushroom()) {
setType(LandType::UNDERWORLD);
}
#endif #endif
} }
@ -115,7 +120,7 @@ void Mario::jump() {
slow_jump = getPos().getY() - 2*BLOCK_SIZE; slow_jump = getPos().getY() - 2*BLOCK_SIZE;
addMovement( 0, -jump_movement ); addMovement( 0, -jump_movement );
ticks_till_gravity = base_gravity_ticks; ticks_till_gravity = base_gravity_ticks;
setTextureSourceRect(MARIO_JUMP_SRC); setBaseRect(MARIO_JUMP_SRC);
pauseAnimation(); pauseAnimation();
} }
@ -144,8 +149,9 @@ void Mario::custom_move( int ticks ) {
} }
} }
void Mario::visit(SDLPP::Visitor &/*UNUSED*/) { void Mario::visit(SDLPP::Visitor &visitor) {
// TODO // TODO
visitor.visit(*this);
} }
void Mario::setWorldTypeSrc(LandType::Value /*UNUSED*/) { void Mario::setWorldTypeSrc(LandType::Value /*UNUSED*/) {

View File

@ -58,6 +58,9 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
newX = obj.getDoubleRect().first.getX() + newX = obj.getDoubleRect().first.getX() +
obj.getDoubleRect().second.getX(); obj.getDoubleRect().second.getX();
break; break;
case MUSHROOM_ID:
mushroom = true;
break;
default: default:
break; break;
} }

View File

@ -8,7 +8,7 @@
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, 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; void visit( const SDLPP::RenderObject &obj ) override;
bool isOnGround() const { bool isOnGround() const {
return onGround; return onGround;
@ -90,6 +90,16 @@ public:
return coin_block; return coin_block;
} }
bool hasMushroom() const {
return mushroom;
}
void setMushroomBlock(std::shared_ptr<MarioBlock> &mushroom) {
_scene.addObject(mushroom);
_scene.setZIndex(mushroom, 1);
_moving_objects.push_back(mushroom);
}
private: private:
bool onGround = false; bool onGround = false;
double groundY = 0; double groundY = 0;
@ -110,6 +120,8 @@ private:
SDLPP::Scene &_scene; SDLPP::Scene &_scene;
bool &_quit; bool &_quit;
int &_coin_count; int &_coin_count;
bool mushroom = false;
std::vector< std::shared_ptr< MarioBlock > > &_moving_objects;
}; };
#endif #endif

View File

@ -1,6 +1,7 @@
#include "visitor_generator.hpp" #include "visitor_generator.hpp"
#include "../objectids.hpp" #include "../objectids.hpp"
#include "mario_visitor.hpp" #include "mario_visitor.hpp"
#include "mushroom_visitor.hpp"
std::shared_ptr< SDLPP::Visitor > std::shared_ptr< SDLPP::Visitor >
getVisitor( const MarioBlock &block, SDLPP::Scene &scene, bool &quit, 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{}; std::shared_ptr< SDLPP::Visitor > result{};
switch(block.getId()) { switch(block.getId()) {
case MARIO_ID: case MARIO_ID:
result = std::static_pointer_cast<SDLPP::Visitor>(std::make_shared<MarioVisitor>(block.getMovement().getY() < 0, scene, quit, coin_count)); result = std::static_pointer_cast<SDLPP::Visitor>(std::make_shared<MarioVisitor>(block.getMovement().getY() < 0, scene, quit, coin_count, moving_objects));
break;
case MUSHROOM_ID:
result = std::static_pointer_cast<SDLPP::Visitor>(std::make_shared<MushroomVisitor>());
default: default:
break; break;
} }