Mario: detect left/right collision

This commit is contained in:
zvon 2021-04-29 12:33:31 +02:00
parent e8e9e12b58
commit b611e2479a
5 changed files with 19 additions and 11 deletions

View File

@ -92,7 +92,6 @@ void pollEvents( SDLPP::Scene &scene ) {
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
scene.updateSizeAndPosition(); scene.updateSizeAndPosition();
if ( leftStop ) { if ( leftStop ) {
auto rendDimsInt = renderer->getDimensions();
auto rendDims = renderer->getDoubleDimensions(); auto rendDims = renderer->getDoubleDimensions();
auto left = rendDims.getX() < 2.0 auto left = rendDims.getX() < 2.0
? -( rendDims.getX() - 1 ) / 2.0 - 0.1 ? -( rendDims.getX() - 1 ) / 2.0 - 0.1
@ -141,16 +140,14 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
} else { } else {
mario->resetMovementY(); mario->resetMovementY();
} }
if ( mv.isStopped() ) { 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->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;
auto leftBarrier = width * 0.3;
auto rightmostX = auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w; scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
auto leftmostX = scene->leftmost()->getRect().x;
scene->moveEverything( scene->moveEverything(
( playerX > rightBarrier && rightmostX > width ) * ( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width, ( rightBarrier - playerX ) / width,
@ -196,9 +193,9 @@ int main() {
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) ); SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) );
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0, 0, 0.1, 0.9, MARIO_SIDE_DETECT ) ); SDLPP::RectColider( 0, 0, 0.1, 0.9, MARIO_LEFT_SIDE_DETECT ) );
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0.9, 0, 0.1, 0.9, MARIO_SIDE_DETECT ) ); SDLPP::RectColider( 0.9, 0, 0.1, 0.9, MARIO_RIGHT_SIDE_DETECT ) );
mario->setStatic( false ); mario->setStatic( false );
scene->addObject( mario ); scene->addObject( mario );

View File

@ -56,10 +56,10 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::Rectan
std::getline(mapFile, buffer); std::getline(mapFile, buffer);
auto mario_y = std::stoi(buffer); auto mario_y = std::stoi(buffer);
auto cur_y = 1 - rows * BLOCK_SIZE; auto cur_y = 1 - rows * BLOCK_SIZE;
for(size_t i = 0; i < rows; i++) { for(int i = 0; i < rows; i++) {
std::getline(mapFile, buffer); std::getline(mapFile, buffer);
auto cur_x = -BLOCK_SIZE; auto cur_x = -BLOCK_SIZE;
for(size_t j = 0; j < cols; j++) { for(int j = 0; j < cols; j++) {
cur_x += BLOCK_SIZE; cur_x += BLOCK_SIZE;
if(buffer[j] == ' ') if(buffer[j] == ' ')
continue; continue;

View File

@ -8,8 +8,10 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
case FLOOR_OVERWORLD_ID: case FLOOR_OVERWORLD_ID:
if ( from == MARIO_FLOOR_DETECT ) if ( from == MARIO_FLOOR_DETECT )
onGround = true; onGround = true;
else if ( from == MARIO_SIDE_DETECT ) else if ( from == MARIO_LEFT_SIDE_DETECT )
stop = true; left = true;
else if (from == MARIO_RIGHT_SIDE_DETECT )
right = true;
break; break;
case DEATH_ID: case DEATH_ID:
death = true; death = true;

View File

@ -23,6 +23,12 @@ public:
virtual void fromId( uint64_t id ) override { virtual void fromId( uint64_t id ) override {
from = id; from = id;
} }
bool canGoLeft() {
return !left;
}
bool canGoRight() {
return !right;
}
private: private:
bool onGround = false; bool onGround = false;
@ -30,6 +36,8 @@ private:
bool stop = false; bool stop = false;
double newX; double newX;
uint64_t from = -1; uint64_t from = -1;
bool left = false;
bool right = false;
}; };
#endif #endif

View File

@ -22,6 +22,7 @@
#define STOP_MOVEMENT 0x2000 #define STOP_MOVEMENT 0x2000
#define MARIO_FLOOR_DETECT 0x20000001 #define MARIO_FLOOR_DETECT 0x20000001
#define MARIO_SIDE_DETECT 0x20000002 #define MARIO_LEFT_SIDE_DETECT 0x20000002
#define MARIO_RIGHT_SIDE_DETECT 0x20000003
#endif #endif