Mario: destroy blocks 1 at a time, if jumping and would hit only with small side, move Mario a bit so the jump is successful

This commit is contained in:
zv0n 2021-05-25 12:26:04 +02:00
parent 7390f684f5
commit 7ed8b0f4e1
6 changed files with 40 additions and 21 deletions

View File

@ -4,6 +4,7 @@
#include "sprites.hpp" #include "sprites.hpp"
#include "editor_visitor.hpp" #include "editor_visitor.hpp"
#include <unordered_map> #include <unordered_map>
#include "mario_visitor.hpp"
MarioBlock::MarioBlock( int x, int y, MarioBlock::MarioBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer, std::shared_ptr< SDLPP::Renderer > renderer,
@ -22,7 +23,7 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) {
visitor.getVisitorType() == VisitorType::Modifier ) { visitor.getVisitorType() == VisitorType::Modifier ) {
destroy(); destroy();
} }
if(visitor.getFromId() == MARIO_TOP_DETECT && _destructible) { if(visitor.getFromId() == MARIO_TOP_DETECT && _destructible && dynamic_cast<MarioVisitor&>(visitor).canDestroy()) {
destroy(); destroy();
} }
visitor.visit( *this ); visitor.visit( *this );

View File

@ -119,7 +119,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
pollEvents( *scene ); pollEvents( *scene );
auto prevPos = mario->getDoubleRect().first; auto prevPos = mario->getDoubleRect().first;
scene->updateScene(); scene->updateScene();
MarioVisitor mv{}; MarioVisitor mv(mario->getMovement().getY() < 0);
scene->visitCollisions( *mario, mv ); scene->visitCollisions( *mario, mv );
if ( mv.isDead() ) { if ( mv.isDead() ) {
quit = true; quit = true;

View File

@ -14,11 +14,15 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan
addCollision( addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) ); SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
addCollision( addCollision(
SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) ); SDLPP::RectColider( 0.05, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
addCollision( addCollision(
SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) ); SDLPP::RectColider( 0.85, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
addCollision( addCollision(
SDLPP::RectColider( 0.45, 0, 0.1, 0.15, MARIO_TOP_DETECT ) ); 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 ) );
setStatic( false ); setStatic( false );
} }
void Mario::walkLeft() { void Mario::walkLeft() {
@ -48,19 +52,8 @@ void Mario::setStanding() {
} }
void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) { void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) {
// TODO - https://web.archive.org/web/20130807122227/http://i276.photobucket.com/albums/kk21/jdaster64/smb_playerphysics.png
// handle gravity // handle gravity
// TODO - https://haydnscarlett.wordpress.com/2020/07/30/marios-jump/
/* if (jumping) {
if(stop_jump && getPos().getY() - min_jump < 0.05) {
jumping = false;
resetMovementY();
} else if( getPos().getY() - max_jump < 0.05 ) {
jumping = false;
resetMovementY();
} else if ( getPos().getY() - slow_jump < 0.05 && getMovement().getY() < -1*jump_movement/5 ) {
addMovement(0, jump_movement/50);
}
}*/
on_ground = visitor.isOnGround(); on_ground = visitor.isOnGround();
if(!jumping && on_ground) { if(!jumping && on_ground) {
resetMovementY(); resetMovementY();
@ -75,12 +68,19 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_p
} }
if(visitor.topBlock() && getMovement().getY() < 0) { if(visitor.topBlock() && getMovement().getY() < 0) {
resetMovementY(); resetMovementY();
stop_jump = true;
} }
// make sure Mario isn't stuck inside a wall // make sure Mario isn't stuck inside a wall
if ( visitor.isStopped() || // TODO more readable function names
( !visitor.canGoLeft() && previous_position.getX() > getPos().getX() ) || if ( visitor.isStopped() || !visitor.canGoLeft() || !visitor.canGoRight() ) {
( !visitor.canGoRight() && previous_position.getX() < getPos().getX() ) ) {
setPos( previous_position.getX(), getPos().getY() ); setPos( previous_position.getX(), getPos().getY() );
} else if (visitor.moveTop() && (jumping && !stop_jump)) {
auto objPos = visitor.getRightLeftPos();
if(objPos.getX() < getPos().getX()) {
setPos( objPos.getX() + BLOCK_SIZE, getPos().getY() );
} else {
setPos( objPos.getX() - BLOCK_SIZE, getPos().getY() );
}
} }
} }

View File

@ -17,6 +17,9 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
right = true; right = true;
} else if (from == MARIO_TOP_DETECT) { } else if (from == MARIO_TOP_DETECT) {
top_hit = true; top_hit = true;
} else if (from == MARIO_TOP_LEFT_DETECT || from == MARIO_TOP_RIGHT_DETECT) {
rightleftpos = obj.getPos();
top_left_right = true;
} }
break; break;
case DEATH_ID: case DEATH_ID:

View File

@ -6,7 +6,7 @@
class MarioVisitor : public SDLPP::Visitor { class MarioVisitor : public SDLPP::Visitor {
public: public:
MarioVisitor() {} MarioVisitor(bool is_jumping) : jumping(is_jumping) {}
virtual void visit( const SDLPP::RenderObject &obj ) override; virtual void visit( const SDLPP::RenderObject &obj ) override;
bool isOnGround() { bool isOnGround() {
return onGround; return onGround;
@ -44,6 +44,16 @@ public:
bool topBlock() { bool topBlock() {
return top_hit; return top_hit;
} }
bool moveTop() {
return top_left_right;
}
const SDLPP::Vec2D<double> &getRightLeftPos() {
return rightleftpos;
}
bool canDestroy() {
return jumping && !top_hit;
}
private: private:
bool onGround = false; bool onGround = false;
@ -56,6 +66,9 @@ private:
bool right = false; bool right = false;
uint64_t _type = 0; uint64_t _type = 0;
bool top_hit = false; bool top_hit = false;
SDLPP::Vec2D<double> rightleftpos;
bool top_left_right = false;
bool jumping;
}; };
#endif #endif

View File

@ -74,6 +74,8 @@
#define MARIO_LEFT_SIDE_DETECT 0x2002 #define MARIO_LEFT_SIDE_DETECT 0x2002
#define MARIO_RIGHT_SIDE_DETECT 0x2003 #define MARIO_RIGHT_SIDE_DETECT 0x2003
#define MARIO_TOP_DETECT 0x2004 #define MARIO_TOP_DETECT 0x2004
#define MARIO_TOP_LEFT_DETECT 0x2005
#define MARIO_TOP_RIGHT_DETECT 0x2006
#define EDITOR_EDIT_SQUARE 0xF001 #define EDITOR_EDIT_SQUARE 0xF001
#define EDITOR_MOUSE_ID 0xF002 #define EDITOR_MOUSE_ID 0xF002