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 ) {
scene.updateSizeAndPosition();
if ( leftStop ) {
auto rendDimsInt = renderer->getDimensions();
auto rendDims = renderer->getDoubleDimensions();
auto left = rendDims.getX() < 2.0
? -( rendDims.getX() - 1 ) / 2.0 - 0.1
@ -141,16 +140,14 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
} else {
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() );
}
auto playerX = mario->getRect().x;
auto width = scene->getWidth();
auto rightBarrier = width * 0.7;
auto leftBarrier = width * 0.3;
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
auto leftmostX = scene->leftmost()->getRect().x;
scene->moveEverything(
( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width,
@ -196,9 +193,9 @@ int main() {
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 ) );
SDLPP::RectColider( 0, 0, 0.1, 0.9, MARIO_LEFT_SIDE_DETECT ) );
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 );
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);
auto mario_y = std::stoi(buffer);
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);
auto cur_x = -BLOCK_SIZE;
for(size_t j = 0; j < cols; j++) {
for(int j = 0; j < cols; j++) {
cur_x += BLOCK_SIZE;
if(buffer[j] == ' ')
continue;

View File

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

View File

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

View File

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