Mario: don't let Mario go left
This commit is contained in:
parent
dd6f37264c
commit
b0b84101a2
129
mario/main.cpp
129
mario/main.cpp
@ -20,13 +20,14 @@
|
||||
#define FALL_MOVEMENT 1
|
||||
|
||||
bool quit = false;
|
||||
std::shared_ptr<SDLPP::RectangleRender> mario = nullptr;
|
||||
std::shared_ptr<SDLPP::Texture> mario_texture = nullptr;
|
||||
std::shared_ptr< SDLPP::RectangleRender > mario = 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() {
|
||||
if(mario->getMovement().getX() == 0) {
|
||||
if ( mario->getMovement().getX() == 0 ) {
|
||||
mario->pauseAnimation();
|
||||
// mario->setTexture(mario_texture, MARIO_STANDING_SRC);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,8 +89,38 @@ void pollEvents( SDLPP::Scene &scene ) {
|
||||
handleKeyUp( event.key.keysym.sym );
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED )
|
||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||
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:
|
||||
break;
|
||||
}
|
||||
@ -103,17 +134,21 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
||||
while ( true ) {
|
||||
SDL_framerateDelay( &gFPS );
|
||||
pollEvents( *scene );
|
||||
auto prevPos = mario->getDoubleRect().first;
|
||||
scene->updateScene();
|
||||
MarioVisitor mv{};
|
||||
scene->visitCollisions(*mario, mv);
|
||||
if(mv.isDead()) {
|
||||
scene->visitCollisions( *mario, mv );
|
||||
if ( mv.isDead() ) {
|
||||
quit = true;
|
||||
}
|
||||
if(!mv.isOnGround()) {
|
||||
mario->setMovement(mario->getMovement().getX(), FALL_MOVEMENT);
|
||||
if ( !mv.isOnGround() ) {
|
||||
mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT );
|
||||
} else {
|
||||
mario->resetMovementY();
|
||||
}
|
||||
if ( mv.isStopped() ) {
|
||||
mario->setPos( prevPos.getX(), mario->getPos().getY() );
|
||||
}
|
||||
auto playerX = mario->getRect().x;
|
||||
auto width = scene->getWidth();
|
||||
auto rightBarrier = width * 0.7;
|
||||
@ -125,9 +160,6 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
||||
( playerX > rightBarrier && rightmostX > width ) *
|
||||
( rightBarrier - playerX ) / width,
|
||||
0 );
|
||||
scene->moveEverything( ( playerX < leftBarrier && leftmostX < 0 ) *
|
||||
( leftBarrier - playerX ) / width,
|
||||
0 );
|
||||
}
|
||||
}
|
||||
|
||||
@ -141,43 +173,63 @@ int main() {
|
||||
SDLPP::Window w( "Mario clone!" );
|
||||
w.setResizable( true );
|
||||
|
||||
auto renderer = std::make_shared< SDLPP::Renderer >( w );
|
||||
renderer = std::make_shared< SDLPP::Renderer >( w );
|
||||
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
|
||||
|
||||
// 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 bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,renderer, MARIO_OVERWORLD_COLORKEY, true);
|
||||
bg->setPermanent();
|
||||
bg->setId(1);
|
||||
scene->addObject(bg);
|
||||
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_OVERWORLD_STANDING_SRC);
|
||||
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
|
||||
bg->setStatic();
|
||||
bg->setId( 1 );
|
||||
scene->addObject( bg );
|
||||
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_OVERWORLD_STANDING_SRC );
|
||||
mario->setAnimationFrames( MARIO_OVERWORLD_WALK_ANIM );
|
||||
mario->setId(2);
|
||||
mario->setId( 2 );
|
||||
mario->centerX();
|
||||
mario->setAnimationSpeed(12.5);
|
||||
mario->setAnimationSpeed( 12.5 );
|
||||
mario->pauseAnimation();
|
||||
// mario->setTexture(mario_texture, MARIO_STANDING_SRC);
|
||||
mario->setMovement(0, FALL_MOVEMENT);
|
||||
mario->setMovementSpeed(0.4);
|
||||
mario->addCollision(SDLPP::RectColider(0.11,0,0.8,1));
|
||||
scene->addObject(mario);
|
||||
mario->setMovement( 0, 0 );
|
||||
mario->setMovementSpeed( 0.4 );
|
||||
mario->addCollision(
|
||||
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) );
|
||||
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>(
|
||||
0,1.01,0,0,renderer);
|
||||
defeat->setId(DEATH_ID);
|
||||
auto defeat =
|
||||
std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
|
||||
defeat->setId( DEATH_ID );
|
||||
defeat->centerX();
|
||||
defeat->setStatic();
|
||||
auto defeatCol = SDLPP::RectColider(-1, 0, -1, -1);
|
||||
defeat->setPermanent();
|
||||
auto defeatCol = SDLPP::RectColider( -1, 0, -1, -1 );
|
||||
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;
|
||||
SDL_initFramerate( &gFPS );
|
||||
@ -187,9 +239,10 @@ int main() {
|
||||
int frames = 0;
|
||||
std::thread inputThread( doInput, scene );
|
||||
inputThread.detach();
|
||||
scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 );
|
||||
while ( !quit ) {
|
||||
SDL_PumpEvents();
|
||||
SDL_framerateDelay(&gFPS);
|
||||
SDL_framerateDelay( &gFPS );
|
||||
setMarioStanding();
|
||||
scene->renderScene();
|
||||
renderer->presentRenderer();
|
||||
|
@ -4,13 +4,22 @@
|
||||
|
||||
void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
|
||||
auto id = obj.getId();
|
||||
switch(id) {
|
||||
std::cout << from << std::endl;
|
||||
switch ( id ) {
|
||||
case FLOOR_OVERWORLD_ID:
|
||||
if ( from == MARIO_FLOOR_DETECT )
|
||||
onGround = true;
|
||||
else if ( from == MARIO_SIDE_DETECT )
|
||||
stop = true;
|
||||
break;
|
||||
case DEATH_ID:
|
||||
death = true;
|
||||
break;
|
||||
case STOP_MOVEMENT:
|
||||
stop = true;
|
||||
newX = obj.getDoubleRect().first.getX() +
|
||||
obj.getDoubleRect().second.getX();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -2,20 +2,35 @@
|
||||
#define MARIO_VISITOR_H
|
||||
|
||||
#include "../sdlpp/sdlpp_visitor.hpp"
|
||||
#include "../sdlpp/sdlpp_geometry.hpp"
|
||||
#include <bits/stdint-uintn.h>
|
||||
|
||||
class MarioVisitor : public SDLPP::Visitor {
|
||||
public:
|
||||
MarioVisitor() {}
|
||||
virtual void visit( const SDLPP::RenderObject &obj );
|
||||
virtual void visit( const SDLPP::RenderObject &obj ) override;
|
||||
bool isOnGround() {
|
||||
return onGround;
|
||||
}
|
||||
bool isDead() {
|
||||
return death;
|
||||
}
|
||||
bool isStopped() {
|
||||
return stop;
|
||||
}
|
||||
double newXPos() {
|
||||
return newX;
|
||||
}
|
||||
virtual void fromId( uint64_t id ) override {
|
||||
from = id;
|
||||
}
|
||||
|
||||
private:
|
||||
bool onGround = false;
|
||||
bool death = false;
|
||||
bool stop = false;
|
||||
double newX;
|
||||
uint64_t from = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -19,5 +19,9 @@
|
||||
#define CLOUD_OVERWORLD_RIGHT_TOP_ID 0x70000010
|
||||
|
||||
#define DEATH_ID 0x10000001
|
||||
#define STOP_MOVEMENT 0x2000
|
||||
|
||||
#define MARIO_FLOOR_DETECT 0x20000001
|
||||
#define MARIO_SIDE_DETECT 0x20000002
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user