Mario: don't let Mario go left

This commit is contained in:
zvon 2021-04-26 21:59:21 +02:00
parent dd6f37264c
commit b0b84101a2
4 changed files with 129 additions and 48 deletions

View File

@ -20,13 +20,14 @@
#define FALL_MOVEMENT 1 #define FALL_MOVEMENT 1
bool quit = false; bool quit = false;
std::shared_ptr<SDLPP::RectangleRender> mario = nullptr; std::shared_ptr< SDLPP::RectangleRender > mario = nullptr;
std::shared_ptr<SDLPP::Texture> mario_texture = nullptr; std::shared_ptr< SDLPP::Texture > mario_texture = nullptr;
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
void setMarioStanding() { void setMarioStanding() {
if(mario->getMovement().getX() == 0) { if ( mario->getMovement().getX() == 0 ) {
mario->pauseAnimation(); mario->pauseAnimation();
// mario->setTexture(mario_texture, MARIO_STANDING_SRC);
} }
} }
@ -88,8 +89,38 @@ void pollEvents( SDLPP::Scene &scene ) {
handleKeyUp( event.key.keysym.sym ); handleKeyUp( event.key.keysym.sym );
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
scene.updateSizeAndPosition(); scene.updateSizeAndPosition();
if ( leftStop ) {
auto rendDimsInt = renderer->getDimensions();
std::cout << "RENDER DIMENSIONS: " << rendDimsInt.getX()
<< ", " << rendDimsInt.getY() << std::endl;
auto rendDims = renderer->getDoubleDimensions();
std::cout << "RENDER DIMENSIONS: " << rendDims.getX()
<< ", " << rendDims.getY() << std::endl;
auto left = rendDims.getX() < 2.0
? -( rendDims.getX() - 1 ) / 2.0 - 0.1
: -0.5;
std::cout << left << std::endl;
leftStop->setPos( left, 0 );
}
auto dimensions = renderer->getDoubleDimensions();
auto leftmost = -( dimensions.getX() - 1 ) / 2.0;
auto rightmost = 1 - leftmost;
auto marioPos = mario->getPos();
if ( marioPos.getX() < leftmost ) {
scene.moveEverything( leftmost - marioPos.getX(), 0 );
} else if ( marioPos.getX() > rightmost ) {
scene.moveEverything( marioPos.getX() - rightmost, 0 );
}
if ( mario->getPos().getX() <
leftStop->getDoubleRect().first.getX() +
leftStop->getDoubleRect().second.getX() ) {
mario->setPos( leftStop->getDoubleRect().first.getX() +
leftStop->getDoubleRect().second.getX(),
mario->getPos().getY() );
}
}
default: default:
break; break;
} }
@ -103,17 +134,21 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
while ( true ) { while ( true ) {
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
pollEvents( *scene ); pollEvents( *scene );
auto prevPos = mario->getDoubleRect().first;
scene->updateScene(); scene->updateScene();
MarioVisitor mv{}; MarioVisitor mv{};
scene->visitCollisions(*mario, mv); scene->visitCollisions( *mario, mv );
if(mv.isDead()) { if ( mv.isDead() ) {
quit = true; quit = true;
} }
if(!mv.isOnGround()) { if ( !mv.isOnGround() ) {
mario->setMovement(mario->getMovement().getX(), FALL_MOVEMENT); mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT );
} else { } else {
mario->resetMovementY(); mario->resetMovementY();
} }
if ( mv.isStopped() ) {
mario->setPos( prevPos.getX(), mario->getPos().getY() );
}
auto playerX = mario->getRect().x; auto playerX = mario->getRect().x;
auto width = scene->getWidth(); auto width = scene->getWidth();
auto rightBarrier = width * 0.7; auto rightBarrier = width * 0.7;
@ -125,9 +160,6 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
( playerX > rightBarrier && rightmostX > width ) * ( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width, ( rightBarrier - playerX ) / width,
0 ); 0 );
scene->moveEverything( ( playerX < leftBarrier && leftmostX < 0 ) *
( leftBarrier - playerX ) / width,
0 );
} }
} }
@ -141,43 +173,63 @@ int main() {
SDLPP::Window w( "Mario clone!" ); SDLPP::Window w( "Mario clone!" );
w.setResizable( true ); w.setResizable( true );
auto renderer = std::make_shared< SDLPP::Renderer >( w ); renderer = std::make_shared< SDLPP::Renderer >( w );
renderer->setBlendMode( SDL_BLENDMODE_BLEND ); renderer->setBlendMode( SDL_BLENDMODE_BLEND );
// prepare global vars // prepare global vars
g_terrain_texture = std::make_shared<SDLPP::Texture>(renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY); g_terrain_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
auto scene = std::make_shared<SDLPP::Scene>(renderer); auto scene = std::make_shared< SDLPP::Scene >( renderer );
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,renderer, MARIO_OVERWORLD_COLORKEY, true); auto bg = std::make_shared< SDLPP::RectangleRender >(
bg->setPermanent(); 0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
bg->setId(1); bg->setStatic();
scene->addObject(bg); bg->setId( 1 );
mario_texture = std::make_shared<SDLPP::Texture>(renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY); scene->addObject( bg );
mario = std::make_shared<SDLPP::RectangleRender>( mario_texture = std::make_shared< SDLPP::Texture >(
0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, mario_texture, MARIO_OVERWORLD_STANDING_SRC); renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
mario = std::make_shared< SDLPP::RectangleRender >(
0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, mario_texture,
MARIO_OVERWORLD_STANDING_SRC );
mario->setAnimationFrames( MARIO_OVERWORLD_WALK_ANIM ); mario->setAnimationFrames( MARIO_OVERWORLD_WALK_ANIM );
mario->setId(2); mario->setId( 2 );
mario->centerX(); mario->centerX();
mario->setAnimationSpeed(12.5); mario->setAnimationSpeed( 12.5 );
mario->pauseAnimation(); mario->pauseAnimation();
// mario->setTexture(mario_texture, MARIO_STANDING_SRC); mario->setMovement( 0, 0 );
mario->setMovement(0, FALL_MOVEMENT); mario->setMovementSpeed( 0.4 );
mario->setMovementSpeed(0.4); mario->addCollision(
mario->addCollision(SDLPP::RectColider(0.11,0,0.8,1)); SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) );
scene->addObject(mario); mario->addCollision(
SDLPP::RectColider( 0, 0, 0.1, 0.9, MARIO_SIDE_DETECT ) );
mario->addCollision(
SDLPP::RectColider( 0.9, 0, 0.1, 0.9, MARIO_SIDE_DETECT ) );
mario->setStatic( false );
scene->addObject( mario );
auto defeat = std::make_shared<SDLPP::RectangleRender>( auto defeat =
0,1.01,0,0,renderer); std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
defeat->setId(DEATH_ID); defeat->setId( DEATH_ID );
defeat->centerX(); defeat->centerX();
defeat->setStatic(); defeat->setPermanent();
auto defeatCol = SDLPP::RectColider(-1, 0, -1, -1); auto defeatCol = SDLPP::RectColider( -1, 0, -1, -1 );
defeatCol.setInfinite(); defeatCol.setInfinite();
defeat->addCollision(defeatCol); defeat->addCollision( defeatCol );
scene->addObject(defeat); scene->addObject( defeat );
loadMap(scene, mario, "testmap.txt", renderer); leftStop = std::make_shared< SDLPP::RectangleRender >( -0.1, 0, 0.11, 0,
renderer );
leftStop->setId( STOP_MOVEMENT );
leftStop->centerX();
leftStop->setPermanent();
auto leftStopCol = SDLPP::RectColider( 0, -1, 1, -1 );
leftStopCol.setInfinite();
leftStop->addCollision( leftStopCol );
leftStop->setColiderColor( "#FF00FF" );
scene->addObject( leftStop );
loadMap( scene, mario, "testmap.txt", renderer );
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate( &gFPS ); SDL_initFramerate( &gFPS );
@ -187,9 +239,10 @@ int main() {
int frames = 0; int frames = 0;
std::thread inputThread( doInput, scene ); std::thread inputThread( doInput, scene );
inputThread.detach(); inputThread.detach();
scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 );
while ( !quit ) { while ( !quit ) {
SDL_PumpEvents(); SDL_PumpEvents();
SDL_framerateDelay(&gFPS); SDL_framerateDelay( &gFPS );
setMarioStanding(); setMarioStanding();
scene->renderScene(); scene->renderScene();
renderer->presentRenderer(); renderer->presentRenderer();

View File

@ -4,13 +4,22 @@
void MarioVisitor::visit( const SDLPP::RenderObject &obj ) { void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId(); auto id = obj.getId();
switch(id) { std::cout << from << std::endl;
switch ( id ) {
case FLOOR_OVERWORLD_ID: case FLOOR_OVERWORLD_ID:
if ( from == MARIO_FLOOR_DETECT )
onGround = true; onGround = true;
else if ( from == MARIO_SIDE_DETECT )
stop = true;
break; break;
case DEATH_ID: case DEATH_ID:
death = true; death = true;
break; break;
case STOP_MOVEMENT:
stop = true;
newX = obj.getDoubleRect().first.getX() +
obj.getDoubleRect().second.getX();
break;
default: default:
break; break;
} }

View File

@ -2,20 +2,35 @@
#define MARIO_VISITOR_H #define MARIO_VISITOR_H
#include "../sdlpp/sdlpp_visitor.hpp" #include "../sdlpp/sdlpp_visitor.hpp"
#include "../sdlpp/sdlpp_geometry.hpp"
#include <bits/stdint-uintn.h>
class MarioVisitor : public SDLPP::Visitor { class MarioVisitor : public SDLPP::Visitor {
public: public:
MarioVisitor() {} MarioVisitor() {}
virtual void visit( const SDLPP::RenderObject &obj ); virtual void visit( const SDLPP::RenderObject &obj ) override;
bool isOnGround() { bool isOnGround() {
return onGround; return onGround;
} }
bool isDead() { bool isDead() {
return death; return death;
} }
bool isStopped() {
return stop;
}
double newXPos() {
return newX;
}
virtual void fromId( uint64_t id ) override {
from = id;
}
private: private:
bool onGround = false; bool onGround = false;
bool death = false; bool death = false;
bool stop = false;
double newX;
uint64_t from = -1;
}; };
#endif #endif

View File

@ -19,5 +19,9 @@
#define CLOUD_OVERWORLD_RIGHT_TOP_ID 0x70000010 #define CLOUD_OVERWORLD_RIGHT_TOP_ID 0x70000010
#define DEATH_ID 0x10000001 #define DEATH_ID 0x10000001
#define STOP_MOVEMENT 0x2000
#define MARIO_FLOOR_DETECT 0x20000001
#define MARIO_SIDE_DETECT 0x20000002
#endif #endif