Mario: custom mario class
This commit is contained in:
parent
0358aa36c7
commit
d9a88f2de2
@ -17,7 +17,7 @@ OUTPUTFLAG = -o
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
COMMON_OBJECTS = blocks.${OBJEXT} global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT}
|
COMMON_OBJECTS = blocks.${OBJEXT} global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT}
|
||||||
MARIO_OBJECTS = mario.${OBJEXT} mario_visitor.${OBJEXT} ${COMMON_OBJECTS}
|
MARIO_OBJECTS = main.${OBJEXT} mario_visitor.${OBJEXT} mario.${OBJEXT} ${COMMON_OBJECTS}
|
||||||
EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} tool_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS}
|
EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} tool_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS}
|
||||||
|
|
||||||
ifeq ($(UNAME_S),Linux)
|
ifeq ($(UNAME_S),Linux)
|
||||||
@ -47,7 +47,7 @@ editor: ${EDITOR_OBJECTS}
|
|||||||
$(CXX) $(CXXFLAGS) -o $@ $^ ${LDFLAGS} -L $(shell pwd) -lsdlpp
|
$(CXX) $(CXXFLAGS) -o $@ $^ ${LDFLAGS} -L $(shell pwd) -lsdlpp
|
||||||
endif
|
endif
|
||||||
|
|
||||||
mario.${OBJEXT}: main.cpp ../sdlpp/sdlpp.hpp sprites.hpp
|
main.${OBJEXT}: main.cpp ../sdlpp/sdlpp.hpp sprites.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
||||||
blocks.${OBJEXT}: blocks.cpp ../sdlpp/sdlpp.hpp blocks.hpp sprites.hpp
|
blocks.${OBJEXT}: blocks.cpp ../sdlpp/sdlpp.hpp blocks.hpp sprites.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
||||||
@ -67,6 +67,8 @@ tool_box.${OBJEXT}: tool_box.cpp ../sdlpp/sdlpp.hpp sprites.hpp tool_box.hpp
|
|||||||
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
||||||
editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp editor_visitor.hpp
|
editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp editor_visitor.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
||||||
|
mario.${OBJEXT}: mario.cpp ../sdlpp/sdlpp.hpp mario.hpp global_vars.hpp objectids.hpp sprites.hpp ../sdlpp/sdlpp_rectrenderer.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
|
||||||
libsdlpp.so: ../sdlpp
|
libsdlpp.so: ../sdlpp
|
||||||
$(MAKE) clean -C ../sdlpp
|
$(MAKE) clean -C ../sdlpp
|
||||||
$(MAKE) -C ../sdlpp
|
$(MAKE) -C ../sdlpp
|
||||||
|
@ -15,22 +15,12 @@
|
|||||||
#include "blocks.hpp"
|
#include "blocks.hpp"
|
||||||
#include "maploader.hpp"
|
#include "maploader.hpp"
|
||||||
#include "mario_visitor.hpp"
|
#include "mario_visitor.hpp"
|
||||||
|
#include "mario.hpp"
|
||||||
#define SIDE_MOVEMENT 0.8
|
|
||||||
#define FALL_MOVEMENT 1
|
|
||||||
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
std::shared_ptr< SDLPP::RectangleRender > mario = nullptr;
|
std::shared_ptr< Mario > mario = nullptr;
|
||||||
std::shared_ptr< SDLPP::Texture > mario_texture = nullptr;
|
|
||||||
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
|
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
|
||||||
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
|
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
|
||||||
bool mario_facing_right = true;
|
|
||||||
|
|
||||||
void setMarioStanding() {
|
|
||||||
if ( mario->getMovement().getX() == 0 ) {
|
|
||||||
mario->pauseAnimation();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
@ -38,20 +28,10 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
quit = true;
|
quit = true;
|
||||||
break;
|
break;
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
mario->resumeAnimation();
|
mario->walkLeft();
|
||||||
mario->addMovement( -SIDE_MOVEMENT, 0 );
|
|
||||||
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
|
|
||||||
mario->flipHorizontally();
|
|
||||||
mario_facing_right = false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
mario->resumeAnimation();
|
mario->walkRight();
|
||||||
mario->addMovement( SIDE_MOVEMENT, 0 );
|
|
||||||
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
|
|
||||||
mario->flipHorizontally();
|
|
||||||
mario_facing_right = true;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
@ -69,20 +49,10 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
void handleKeyUp( SDL_Keycode key ) {
|
void handleKeyUp( SDL_Keycode key ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
mario->resumeAnimation();
|
mario->walkRight();
|
||||||
mario->addMovement( SIDE_MOVEMENT, 0 );
|
|
||||||
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
|
|
||||||
mario->flipHorizontally();
|
|
||||||
mario_facing_right = true;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
mario->resumeAnimation();
|
mario->walkLeft();
|
||||||
mario->addMovement( -SIDE_MOVEMENT, 0 );
|
|
||||||
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
|
|
||||||
mario->flipHorizontally();
|
|
||||||
mario_facing_right = false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
@ -152,17 +122,9 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
|||||||
if ( mv.isDead() ) {
|
if ( mv.isDead() ) {
|
||||||
quit = true;
|
quit = true;
|
||||||
}
|
}
|
||||||
if ( !mv.isOnGround() ) {
|
mario->handleVisitor(mv, prevPos);
|
||||||
mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT );
|
|
||||||
} else {
|
// if player is > 0.7 of playground, move everything left
|
||||||
mario->resetMovementY();
|
|
||||||
mario->setPos(mario->getPos().getX(), mv.getGroundY() - BLOCK_SIZE);
|
|
||||||
}
|
|
||||||
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 playerX = mario->getRect().x;
|
||||||
auto width = scene->getWidth();
|
auto width = scene->getWidth();
|
||||||
auto rightBarrier = width * 0.7;
|
auto rightBarrier = width * 0.7;
|
||||||
@ -199,25 +161,9 @@ int main() {
|
|||||||
bg->setStatic();
|
bg->setStatic();
|
||||||
bg->setId( 1 );
|
bg->setId( 1 );
|
||||||
scene->addObject( bg );
|
scene->addObject( bg );
|
||||||
mario_texture = std::make_shared< SDLPP::Texture >(
|
g_mario_texture = std::make_shared< SDLPP::Texture >(
|
||||||
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
|
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
|
||||||
mario = std::make_shared< SDLPP::RectangleRender >(
|
mario = std::make_shared< Mario >(renderer);
|
||||||
0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, mario_texture,
|
|
||||||
MARIO_STANDING_SRC );
|
|
||||||
mario->setAnimationFrames( MARIO_WALK_ANIM );
|
|
||||||
mario->setId( 2 );
|
|
||||||
mario->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
||||||
mario->setAnimationSpeed( 12.5 );
|
|
||||||
mario->pauseAnimation();
|
|
||||||
mario->setMovement( 0, 0 );
|
|
||||||
mario->setMovementSpeed( 0.4 );
|
|
||||||
mario->addCollision(
|
|
||||||
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
|
|
||||||
mario->addCollision(
|
|
||||||
SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
|
|
||||||
mario->addCollision(
|
|
||||||
SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
|
|
||||||
mario->setStatic( false );
|
|
||||||
scene->addObject(mario);
|
scene->addObject(mario);
|
||||||
|
|
||||||
auto defeat =
|
auto defeat =
|
||||||
@ -256,7 +202,7 @@ int main() {
|
|||||||
while ( !quit ) {
|
while ( !quit ) {
|
||||||
SDL_PumpEvents();
|
SDL_PumpEvents();
|
||||||
SDL_framerateDelay( &gFPS );
|
SDL_framerateDelay( &gFPS );
|
||||||
setMarioStanding();
|
mario->setStanding();
|
||||||
scene->renderScene();
|
scene->renderScene();
|
||||||
renderer->presentRenderer();
|
renderer->presentRenderer();
|
||||||
frames++;
|
frames++;
|
||||||
|
60
mario/mario.cpp
Normal file
60
mario/mario.cpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include "mario.hpp"
|
||||||
|
#include "global_vars.hpp"
|
||||||
|
#include "objectids.hpp"
|
||||||
|
#include "sprites.hpp"
|
||||||
|
|
||||||
|
Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::RectangleRender(0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, g_mario_texture, MARIO_STANDING_SRC) {
|
||||||
|
setAnimationFrames( MARIO_WALK_ANIM );
|
||||||
|
setId( MARIO_ID );
|
||||||
|
setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||||
|
setAnimationSpeed( 12.5 );
|
||||||
|
pauseAnimation();
|
||||||
|
setMovement( 0, 0 );
|
||||||
|
setMovementSpeed( 0.4 );
|
||||||
|
addCollision(
|
||||||
|
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
|
||||||
|
addCollision(
|
||||||
|
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 ) );
|
||||||
|
setStatic( false );
|
||||||
|
}
|
||||||
|
void Mario::walkLeft() {
|
||||||
|
resumeAnimation();
|
||||||
|
addMovement( -side_movement, 0 );
|
||||||
|
if ( getMovement().getX() < 0 && faces_right ) {
|
||||||
|
flipHorizontally();
|
||||||
|
faces_right = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mario::walkRight() {
|
||||||
|
resumeAnimation();
|
||||||
|
addMovement( side_movement, 0 );
|
||||||
|
if ( getMovement().getX() > 0 && !faces_right ) {
|
||||||
|
flipHorizontally();
|
||||||
|
faces_right = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mario::setStanding() {
|
||||||
|
if ( getMovement().getX() == 0 ) {
|
||||||
|
pauseAnimation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) {
|
||||||
|
// handle gravity
|
||||||
|
if ( !visitor.isOnGround() ) {
|
||||||
|
setMovement( getMovement().getX(), fall_movement );
|
||||||
|
} else {
|
||||||
|
resetMovementY();
|
||||||
|
setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE);
|
||||||
|
}
|
||||||
|
// make sure Mario isn't stuck inside a wall
|
||||||
|
if ( visitor.isStopped() ||
|
||||||
|
( !visitor.canGoLeft() && previous_position.getX() > getPos().getX() ) ||
|
||||||
|
( !visitor.canGoRight() && previous_position.getX() < getPos().getX() ) ) {
|
||||||
|
setPos( previous_position.getX(), getPos().getY() );
|
||||||
|
}
|
||||||
|
}
|
20
mario/mario.hpp
Normal file
20
mario/mario.hpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef MARIO_H
|
||||||
|
#define MARIO_H
|
||||||
|
|
||||||
|
#include "../sdlpp/sdlpp_rectrenderer.hpp"
|
||||||
|
#include "mario_visitor.hpp"
|
||||||
|
|
||||||
|
class Mario : public SDLPP::RectangleRender {
|
||||||
|
public:
|
||||||
|
Mario(const std::shared_ptr< SDLPP::Renderer > &renderer);
|
||||||
|
void walkLeft();
|
||||||
|
void walkRight();
|
||||||
|
void setStanding();
|
||||||
|
void handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position);
|
||||||
|
private:
|
||||||
|
bool faces_right = true;
|
||||||
|
double side_movement = 0.8;
|
||||||
|
double fall_movement = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user