diff --git a/mario/blocks.cpp b/mario/blocks.cpp index c6abfa3..e144fd7 100644 --- a/mario/blocks.cpp +++ b/mario/blocks.cpp @@ -13,6 +13,7 @@ MarioBlock::MarioBlock( int x, int y, : RectangleRender( x * BLOCK_SIZE, 1 - ( 16 - y ) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ) { _destructible = destructible; + setMovementSpeed(1); } void MarioBlock::visit( SDLPP::Visitor &visitor ) { #ifdef EDITOR @@ -25,8 +26,13 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) { destroy(); } #else - if(visitor.getFromId() == MARIO_TOP_DETECT && _destructible && dynamic_cast(visitor).canDestroy()) { - destroy(); + if(visitor.getFromId() == MARIO_TOP_DETECT && dynamic_cast(visitor).canDestroy()) { + if( _destructible ) { + destroy(); + } else { + // TODO check if anything above + bounce(); + } } #endif visitor.visit( *this ); @@ -38,6 +44,32 @@ void MarioBlock::setTerrain( bool terrain ) { _terrain = terrain; } +void MarioBlock::bounce() { + if(_bouncing) + return; + _bouncing = true; + og_pos = getPos(); + setMovement(0, -0.5); +} + +void MarioBlock::custom_move(int ticks) { + if(!_bouncing) + return; + if(getMovement().getY() < 0) { + ticks_to_bounce -= ticks; + if(ticks_to_bounce < 0) { + setMovement(0, 0.5); + ticks_to_bounce = bounce_ticks; + } + } else { + if(getPos().getY() >= og_pos.getY()) { + setMovement(0, 0); + setPos(og_pos); + _bouncing = false; + } + } +} + const std::vector< uint64_t > possibleBlocks = { FLOOR_ID, STEP_ID, diff --git a/mario/blocks.hpp b/mario/blocks.hpp index 3fdd416..9652e5b 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -11,11 +11,17 @@ public: void visit( SDLPP::Visitor &visitor ) override; void setTool( bool tool = true ); void setTerrain( bool terrain = true ); + void bounce(); + virtual void custom_move(int ticks) override; private: bool _tool = false; bool _terrain = true; bool _destructible = false; + bool _bouncing = false; + const int bounce_ticks = 100; + int ticks_to_bounce = bounce_ticks; + SDLPP::Vec2D og_pos = {}; }; extern const std::vector< uint64_t > possibleBlocks; diff --git a/mario/mario.cpp b/mario/mario.cpp index 5249f6b..fc7e1f8 100644 --- a/mario/mario.cpp +++ b/mario/mario.cpp @@ -21,8 +21,8 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan SDLPP::RectColider( 0.05, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT ) ); addCollision( SDLPP::RectColider( 0.85, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT ) ); - addCollision( - SDLPP::RectColider( 0.35, 0, 0.3, 0.15, MARIO_TOP_DETECT ) ); + top_collision = std::make_shared( 0.5, 0, 0.2, 0.15, MARIO_TOP_DETECT ); + addCollision( top_collision ); setStatic( false ); } void Mario::walkLeft() { @@ -31,6 +31,8 @@ void Mario::walkLeft() { addMovement( -side_movement, 0 ); if ( getMovement().getX() < 0 && faces_right ) { flipHorizontally(); + top_collision->setPos(0.3, 0); + updateSizeAndPosition(); faces_right = false; } } @@ -41,6 +43,8 @@ void Mario::walkRight() { addMovement( side_movement, 0 ); if ( getMovement().getX() > 0 && !faces_right ) { flipHorizontally(); + top_collision->setPos(0.5, 0); + updateSizeAndPosition(); faces_right = true; } } diff --git a/mario/mario.hpp b/mario/mario.hpp index c6a0e26..64813bf 100644 --- a/mario/mario.hpp +++ b/mario/mario.hpp @@ -30,6 +30,7 @@ private: const int base_gravity_ticks = 1000/60; const double gravity_add_jumping = jump_movement/32.0; const double gravity_add_falling = jump_movement/(64.0/7.0); + std::shared_ptr top_collision = nullptr; }; #endif