Mario: custom mario class

This commit is contained in:
2021-05-22 23:13:26 +02:00
parent 0358aa36c7
commit d9a88f2de2
4 changed files with 97 additions and 69 deletions
+13 -67
View File
@@ -15,22 +15,12 @@
#include "blocks.hpp"
#include "maploader.hpp"
#include "mario_visitor.hpp"
#define SIDE_MOVEMENT 0.8
#define FALL_MOVEMENT 1
#include "mario.hpp"
bool quit = false;
std::shared_ptr< SDLPP::RectangleRender > mario = nullptr;
std::shared_ptr< SDLPP::Texture > mario_texture = nullptr;
std::shared_ptr< Mario > mario = nullptr;
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
bool mario_facing_right = true;
void setMarioStanding() {
if ( mario->getMovement().getX() == 0 ) {
mario->pauseAnimation();
}
}
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
@@ -38,20 +28,10 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
quit = true;
break;
case SDLK_a:
mario->resumeAnimation();
mario->addMovement( -SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = false;
}
mario->walkLeft();
break;
case SDLK_d:
mario->resumeAnimation();
mario->addMovement( SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = true;
}
mario->walkRight();
break;
case SDLK_SPACE:
case SDLK_w:
@@ -69,20 +49,10 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
void handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
mario->resumeAnimation();
mario->addMovement( SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = true;
}
mario->walkRight();
break;
case SDLK_d:
mario->resumeAnimation();
mario->addMovement( -SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = false;
}
mario->walkLeft();
break;
case SDLK_w:
case SDLK_s:
@@ -152,17 +122,9 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
if ( mv.isDead() ) {
quit = true;
}
if ( !mv.isOnGround() ) {
mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT );
} else {
mario->resetMovementY();
mario->setPos(mario->getPos().getX(), mv.getGroundY() - BLOCK_SIZE);
}
if ( mv.isStopped() ||
( !mv.canGoLeft() && prevPos.getX() > mario->getPos().getX() ) ||
( !mv.canGoRight() && prevPos.getX() < mario->getPos().getX() ) ) {
mario->setPos( prevPos.getX(), mario->getPos().getY() );
}
mario->handleVisitor(mv, prevPos);
// if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x;
auto width = scene->getWidth();
auto rightBarrier = width * 0.7;
@@ -199,26 +161,10 @@ int main() {
bg->setStatic();
bg->setId( 1 );
scene->addObject( bg );
mario_texture = std::make_shared< SDLPP::Texture >(
g_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
mario = std::make_shared< SDLPP::RectangleRender >(
0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, mario_texture,
MARIO_STANDING_SRC );
mario->setAnimationFrames( MARIO_WALK_ANIM );
mario->setId( 2 );
mario->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mario->setAnimationSpeed( 12.5 );
mario->pauseAnimation();
mario->setMovement( 0, 0 );
mario->setMovementSpeed( 0.4 );
mario->addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
mario->addCollision(
SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
mario->addCollision(
SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
mario->setStatic( false );
scene->addObject( mario );
mario = std::make_shared< Mario >(renderer);
scene->addObject(mario);
auto defeat =
std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
@@ -256,7 +202,7 @@ int main() {
while ( !quit ) {
SDL_PumpEvents();
SDL_framerateDelay( &gFPS );
setMarioStanding();
mario->setStanding();
scene->renderScene();
renderer->presentRenderer();
frames++;