game/mario/mario.cpp

125 lines
3.9 KiB
C++
Raw Normal View History

2021-05-22 21:13:26 +00:00
#include "mario.hpp"
#include "global_vars.hpp"
#include "objectids.hpp"
#include "sprites.hpp"
Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::RectangleRender(0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, g_mario_texture, MARIO_STANDING_SRC) {
setAnimationFrames( MARIO_WALK_ANIM );
setId( MARIO_ID );
setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
setAnimationSpeed( 12.5 );
pauseAnimation();
setMovement( 0, 0 );
2021-05-23 21:32:15 +00:00
setMovementSpeed( 1 );
2021-05-22 21:13:26 +00:00
addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
addCollision(
SDLPP::RectColider( 0.05, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
2021-05-22 21:13:26 +00:00
addCollision(
SDLPP::RectColider( 0.85, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
addCollision(
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 ) );
2021-05-22 21:13:26 +00:00
setStatic( false );
}
void Mario::walkLeft() {
if(on_ground)
resumeAnimation();
2021-05-22 21:13:26 +00:00
addMovement( -side_movement, 0 );
if ( getMovement().getX() < 0 && faces_right ) {
flipHorizontally();
faces_right = false;
}
}
void Mario::walkRight() {
if(on_ground)
resumeAnimation();
2021-05-22 21:13:26 +00:00
addMovement( side_movement, 0 );
if ( getMovement().getX() > 0 && !faces_right ) {
flipHorizontally();
faces_right = true;
}
}
void Mario::setStanding() {
if ( getMovement().getX() == 0 ) {
pauseAnimation();
}
}
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
2021-05-22 21:13:26 +00:00
// handle gravity
2021-05-22 21:54:01 +00:00
on_ground = visitor.isOnGround();
2021-05-23 21:32:15 +00:00
if(!jumping && on_ground) {
2021-05-22 21:13:26 +00:00
resetMovementY();
setTextureSourceRect(MARIO_STANDING_SRC);
if(getMovement().getX() != 0)
resumeAnimation();
2021-05-23 21:32:15 +00:00
// for some reason falling of the edge causes on_ground to be true, but
// visitor ground_y is 0
if(visitor.getGroundY() != 0) {
setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE);
}
2021-05-22 21:13:26 +00:00
}
2021-05-23 21:57:29 +00:00
if(visitor.topBlock() && getMovement().getY() < 0) {
resetMovementY();
stop_jump = true;
2021-05-23 21:57:29 +00:00
}
2021-05-22 21:13:26 +00:00
// make sure Mario isn't stuck inside a wall
// TODO more readable function names
if ( visitor.isStopped() || !visitor.canGoLeft() || !visitor.canGoRight() ) {
2021-05-22 21:13:26 +00:00
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() );
}
2021-05-22 21:13:26 +00:00
}
}
2021-05-22 21:54:01 +00:00
void Mario::jump() {
if(!on_ground)
return;
jumping = true;
stop_jump = false;
max_jump = getPos().getY() - 3*BLOCK_SIZE;
min_jump = getPos().getY() - 1*BLOCK_SIZE;
slow_jump = getPos().getY() - 2*BLOCK_SIZE;
2021-05-23 21:32:15 +00:00
addMovement( 0, -jump_movement );
ticks_till_gravity = base_gravity_ticks;
setTextureSourceRect(MARIO_JUMP_SRC);
pauseAnimation();
2021-05-22 21:54:01 +00:00
}
void Mario::stopJump() {
stop_jump = true;
}
2021-05-23 21:32:15 +00:00
void Mario::custom_move( int ticks ) {
if(!jumping && on_ground)
return;
if(getMovement().getY() >= 1.0625 * jump_movement)
return;
ticks_till_gravity -= ticks;
if(ticks_till_gravity < 0) {
if(getMovement().getY() > 0) {
jumping = false;
addMovement(0, gravity_add_jumping);
} else {
if(stop_jump) {
addMovement(0, gravity_add_falling);
} else {
addMovement(0, gravity_add_jumping);
}
}
ticks_till_gravity += base_gravity_ticks;
}
}