From 8672830db864e033034b98d6bf56081f1f77797e Mon Sep 17 00:00:00 2001 From: zv0n Date: Sun, 23 May 2021 23:45:45 +0200 Subject: [PATCH] Mario: jumping sprite + head bump colider --- mario/mario.cpp | 14 +++++++++++--- mario/mario.hpp | 2 +- mario/objectids.hpp | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/mario/mario.cpp b/mario/mario.cpp index ccc9a67..05e478d 100644 --- a/mario/mario.cpp +++ b/mario/mario.cpp @@ -17,10 +17,13 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) ); addCollision( SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) ); + addCollision( + SDLPP::RectColider( 0.21, 0, 0.65, 0.15, MARIO_TOP_DETECT ) ); setStatic( false ); } void Mario::walkLeft() { - resumeAnimation(); + if(on_ground) + resumeAnimation(); addMovement( -side_movement, 0 ); if ( getMovement().getX() < 0 && faces_right ) { flipHorizontally(); @@ -29,7 +32,8 @@ void Mario::walkLeft() { } void Mario::walkRight() { - resumeAnimation(); + if(on_ground) + resumeAnimation(); addMovement( side_movement, 0 ); if ( getMovement().getX() > 0 && !faces_right ) { flipHorizontally(); @@ -60,6 +64,9 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D previous_p on_ground = visitor.isOnGround(); if(!jumping && on_ground) { resetMovementY(); + setTextureSourceRect(MARIO_STANDING_SRC); + if(getMovement().getX() != 0) + resumeAnimation(); // for some reason falling of the edge causes on_ground to be true, but // visitor ground_y is 0 if(visitor.getGroundY() != 0) { @@ -84,6 +91,8 @@ void Mario::jump() { slow_jump = getPos().getY() - 2*BLOCK_SIZE; addMovement( 0, -jump_movement ); ticks_till_gravity = base_gravity_ticks; + setTextureSourceRect(MARIO_JUMP_SRC); + pauseAnimation(); } void Mario::stopJump() { @@ -109,5 +118,4 @@ void Mario::custom_move( int ticks ) { } ticks_till_gravity += base_gravity_ticks; } - std::cout << getMovement().getY() << std::endl; } diff --git a/mario/mario.hpp b/mario/mario.hpp index 1e958a5..c6a0e26 100644 --- a/mario/mario.hpp +++ b/mario/mario.hpp @@ -17,7 +17,7 @@ public: virtual void custom_move( int ticks ) override; private: bool faces_right = true; - double side_movement = 0.32; + double side_movement = 0.39; double jump_movement = 1.0; bool jumping = false; bool stop_jump = false; diff --git a/mario/objectids.hpp b/mario/objectids.hpp index 81d7003..46d5151 100644 --- a/mario/objectids.hpp +++ b/mario/objectids.hpp @@ -73,6 +73,7 @@ #define MARIO_FLOOR_DETECT 0x2001 #define MARIO_LEFT_SIDE_DETECT 0x2002 #define MARIO_RIGHT_SIDE_DETECT 0x2003 +#define MARIO_TOP_DETECT 0x2004 #define EDITOR_EDIT_SQUARE 0xF001 #define EDITOR_MOUSE_ID 0xF002