Mario: add bouncing

This commit is contained in:
zv0n 2021-05-25 22:05:50 +02:00
parent 0c4f2482c7
commit 6fe283bb34
4 changed files with 47 additions and 4 deletions

View File

@ -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<MarioVisitor&>(visitor).canDestroy()) {
destroy();
if(visitor.getFromId() == MARIO_TOP_DETECT && dynamic_cast<MarioVisitor&>(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,

View File

@ -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<double> og_pos = {};
};
extern const std::vector< uint64_t > possibleBlocks;

View File

@ -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<SDLPP::RectColider>( 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;
}
}

View File

@ -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<SDLPP::RectColider> top_collision = nullptr;
};
#endif