Add coin animation

This commit is contained in:
zvon 2021-07-24 20:50:24 +02:00
parent 130a01feda
commit e67cf508a2
12 changed files with 89 additions and 14 deletions

View File

@ -18,7 +18,7 @@ LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
OUTPUTFLAG = -o OUTPUTFLAG = -o
endif endif
COMMON_OBJECTS = global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT} mapobject.${OBJEXT} coineditorblock.${OBJEXT} simpleblocks.${OBJEXT} mario.${OBJEXT} COMMON_OBJECTS = global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT} mapobject.${OBJEXT} coineditorblock.${OBJEXT} coinblock.${OBJEXT} simpleblocks.${OBJEXT} mario.${OBJEXT}
ifneq ($(UNAME_S),Windows) ifneq ($(UNAME_S),Windows)
COMMON_OBJECTS += libsdlpp.a COMMON_OBJECTS += libsdlpp.a
endif endif
@ -71,6 +71,8 @@ editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp edit
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
coineditorblock.${OBJEXT}: blocks/coineditorblock.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/coineditorblock.hpp coineditorblock.${OBJEXT}: blocks/coineditorblock.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/coineditorblock.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
coinblock.${OBJEXT}: blocks/coinblock.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/coinblock.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
simpleblocks.${OBJEXT}: blocks/simpleblocks.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/simpleblocks.hpp simpleblocks.${OBJEXT}: blocks/simpleblocks.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/simpleblocks.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
mapobject.${OBJEXT}: mapobject.cpp ../sdlpp/sdlpp.hpp objectids.hpp mapobject.${OBJEXT}: mapobject.cpp ../sdlpp/sdlpp.hpp objectids.hpp

View File

@ -3,10 +3,12 @@
#include "objectids.hpp" #include "objectids.hpp"
#include "sprites.hpp" #include "sprites.hpp"
#include "editor_visitor.hpp" #include "editor_visitor.hpp"
#include <memory>
#include <unordered_map> #include <unordered_map>
#include "mario_visitor.hpp" #include "mario_visitor.hpp"
#include "blocks/simpleblocks.hpp" #include "blocks/simpleblocks.hpp"
#include "blocks/coineditorblock.hpp" #include "blocks/coineditorblock.hpp"
#include "blocks/coinblock.hpp"
#include "mario.hpp" #include "mario.hpp"
#define CAN_BE_DESTROYED_FLAG 0x0000000000000001 #define CAN_BE_DESTROYED_FLAG 0x0000000000000001
@ -57,7 +59,11 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) {
} }
if ( hasCoin() ) { if ( hasCoin() ) {
removeCoin(); removeCoin();
auto coin = createTerrainBlock(COIN_ID, LandType::OVERWORLD, renderer);
coin->setPos(getPos());
std::dynamic_pointer_cast<CoinBlock>(coin)->setParent(this);
dynamic_cast< MarioVisitor & >( visitor ).setCoin(); dynamic_cast< MarioVisitor & >( visitor ).setCoin();
dynamic_cast< MarioVisitor & >( visitor ).setCoinBlock(coin);
} }
} }
#endif #endif
@ -75,7 +81,12 @@ void MarioBlock::bounce() {
return; return;
_bouncing = true; _bouncing = true;
og_pos = getPos(); og_pos = getPos();
setMovement( 0, -0.5 ); ticks_to_bounce = bounce_ticks;
setMovement( 0, -bounce_speed );
}
bool MarioBlock::isBouncing() {
return _bouncing;
} }
void MarioBlock::custom_move( int ticks ) { void MarioBlock::custom_move( int ticks ) {
@ -84,7 +95,7 @@ void MarioBlock::custom_move( int ticks ) {
if ( getMovement().getY() < 0 ) { if ( getMovement().getY() < 0 ) {
ticks_to_bounce -= ticks; ticks_to_bounce -= ticks;
if ( ticks_to_bounce < 0 ) { if ( ticks_to_bounce < 0 ) {
setMovement( 0, 0.5 ); setMovement( 0, bounce_speed );
ticks_to_bounce = bounce_ticks; ticks_to_bounce = bounce_ticks;
} }
} else { } else {
@ -491,6 +502,10 @@ createBlockById( uint64_t id, int x, int y,
result = std::static_pointer_cast< MarioBlock >( result = std::static_pointer_cast< MarioBlock >(
std::make_shared< MushroomModifierBlock >( x, y, renderer ) ); std::make_shared< MushroomModifierBlock >( x, y, renderer ) );
break; break;
case COIN_ID:
result = std::static_pointer_cast< MarioBlock >(
std::make_shared< CoinBlock >( x, y, renderer ) );
break;
} }
return result; return result;
} }

View File

@ -34,6 +34,11 @@ public:
void setCoinCount( int coins ); void setCoinCount( int coins );
void setDestructible( bool destructible = true ); void setDestructible( bool destructible = true );
void ensureCollision(); void ensureCollision();
bool isBouncing();
protected:
double bounce_speed = 0.5;
int bounce_ticks = 100;
private: private:
bool _tool = false; bool _tool = false;
@ -43,8 +48,8 @@ private:
bool _bouncing = false; bool _bouncing = false;
int _coins = 0; int _coins = 0;
bool _mushroom = false; bool _mushroom = false;
const int bounce_ticks = 100; bool _release_coin = false;
int ticks_to_bounce = bounce_ticks; int ticks_to_bounce = 0;
SDLPP::Vec2D< double > og_pos = {}; SDLPP::Vec2D< double > og_pos = {};
LandType::Value _type; LandType::Value _type;
SDL_Rect _base_src; SDL_Rect _base_src;

View File

@ -0,0 +1,25 @@
#include "coinblock.hpp"
#include "../sprites.hpp"
#include "../global_vars.hpp"
CoinBlock::CoinBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock(x, y, renderer, g_terrain_texture, COIN_SRC, true, true) {
setHidden(true);
bounce_speed = 0.75;
bounce_ticks = 150;
}
void CoinBlock::custom_move(int ticks) {
if(_parent != nullptr && !_parent->isBouncing() && !isBouncing()) {
setHidden(false);
bounce();
_parent = nullptr;
} else if (_parent == nullptr && !isBouncing()) {
setHidden(true);
destroy();
}
MarioBlock::custom_move(ticks);
}
void CoinBlock::setParent(MarioBlock *parent) {
_parent = parent;
}

View File

@ -0,0 +1,15 @@
#ifndef COIN_BLOCK_HPP
#define COIN_BLOCK_HPP
#include "../blocks.hpp"
class CoinBlock : public MarioBlock {
public:
CoinBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer );
virtual void custom_move(int ticks) override;
void setParent(MarioBlock *parent);
private:
MarioBlock *_parent;
};
#endif

View File

@ -444,10 +444,6 @@ BackgroundModifierBlock::BackgroundModifierBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock (x, y, renderer, g_mod_texture, MOD_BACKGROUND_SRC, false ) { std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock (x, y, renderer, g_mod_texture, MOD_BACKGROUND_SRC, false ) {
setId( BACKGROUND_MODIFIER_ID ); setId( BACKGROUND_MODIFIER_ID );
} }
CoinModifierBlock::CoinModifierBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock (x, y, renderer, g_mod_texture, MOD_COIN_SRC, false ) {
setId( COIN_MODIFIER_ID );
}
MushroomModifierBlock::MushroomModifierBlock( int x, int y, MushroomModifierBlock::MushroomModifierBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock (x, y, renderer, g_mod_texture, MOD_MUSHROOM_SRC, false ) { std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock (x, y, renderer, g_mod_texture, MOD_MUSHROOM_SRC, false ) {
setId( MUSHROOM_MODIFIER_ID ); setId( MUSHROOM_MODIFIER_ID );

View File

@ -290,11 +290,6 @@ public:
BackgroundModifierBlock( int x, int y, BackgroundModifierBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer ); std::shared_ptr< SDLPP::Renderer > renderer );
}; };
class CoinModifierBlock : public MarioBlock {
public:
CoinModifierBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer );
};
class MushroomModifierBlock : public MarioBlock { class MushroomModifierBlock : public MarioBlock {
public: public:
MushroomModifierBlock( int x, int y, MushroomModifierBlock( int x, int y,

View File

@ -142,6 +142,11 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
coin_count++; coin_count++;
coins->changeText(std::to_string(coin_count) + " COINS"); coins->changeText(std::to_string(coin_count) + " COINS");
} }
if ( mv.hasCoinBlock() ) {
auto coin = mv.getCoinBlock();
scene->addObject(coin);
scene->setZIndex(coin, 1);
}
// if player is > 0.7 of playground, move everything left // if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x; auto playerX = mario->getRect().x;

View File

@ -3,6 +3,7 @@
#include "../sdlpp/sdlpp_visitor.hpp" #include "../sdlpp/sdlpp_visitor.hpp"
#include "../sdlpp/sdlpp_geometry.hpp" #include "../sdlpp/sdlpp_geometry.hpp"
#include "blocks.hpp"
class MarioVisitor : public SDLPP::Visitor { class MarioVisitor : public SDLPP::Visitor {
public: public:
@ -71,6 +72,18 @@ public:
return coin; return coin;
} }
void setCoinBlock(std::shared_ptr<MarioBlock> &coin) {
coin_block = coin;
}
bool hasCoinBlock() {
return coin_block != nullptr;
}
std::shared_ptr<MarioBlock> &getCoinBlock() {
return coin_block;
}
private: private:
bool onGround = false; bool onGround = false;
double groundY = 0; double groundY = 0;
@ -87,6 +100,7 @@ private:
bool jumping; bool jumping;
bool coin = false; bool coin = false;
SDLPP::Vec2D<double> movement_blockage; SDLPP::Vec2D<double> movement_blockage;
std::shared_ptr<MarioBlock> coin_block = nullptr;
}; };
class BounceVisitor : public SDLPP::Visitor { class BounceVisitor : public SDLPP::Visitor {

View File

@ -61,6 +61,7 @@
#define CANNON_TOWER_ID 0x7038 #define CANNON_TOWER_ID 0x7038
#define CANNON_PEDESTAL_ID 0x7039 #define CANNON_PEDESTAL_ID 0x7039
#define CANNON_ID 0x703A #define CANNON_ID 0x703A
#define COIN_ID 0x703B
// modifiers // modifiers
#define DESTRUCTIBLE_MODIFIER_ID 0x01 #define DESTRUCTIBLE_MODIFIER_ID 0x01

View File

@ -78,6 +78,7 @@ const SDL_Rect TREE_LEAVES_BOTTOM_SRC = {239, 29, 16, 16};
const SDL_Rect CANNON_TOWER_SRC = {256, 46, 16, 16}; const SDL_Rect CANNON_TOWER_SRC = {256, 46, 16, 16};
const SDL_Rect CANNON_PEDESTAL_SRC = {256, 29, 16, 16}; const SDL_Rect CANNON_PEDESTAL_SRC = {256, 29, 16, 16};
const SDL_Rect CANNON_SRC = {256, 12, 16, 16}; const SDL_Rect CANNON_SRC = {256, 12, 16, 16};
const SDL_Rect COIN_SRC = {549, 202, 16, 16};
extern const SDL_Rect MOD_DESTRUCTIBLE_SRC = {0, 0, 16, 16}; extern const SDL_Rect MOD_DESTRUCTIBLE_SRC = {0, 0, 16, 16};
extern const SDL_Rect MOD_BACKGROUND_SRC = {16, 0, 16, 16}; extern const SDL_Rect MOD_BACKGROUND_SRC = {16, 0, 16, 16};

View File

@ -86,6 +86,7 @@ extern const SDL_Rect TREE_LEAVES_BOTTOM_SRC;
extern const SDL_Rect CANNON_TOWER_SRC; extern const SDL_Rect CANNON_TOWER_SRC;
extern const SDL_Rect CANNON_PEDESTAL_SRC; extern const SDL_Rect CANNON_PEDESTAL_SRC;
extern const SDL_Rect CANNON_SRC; extern const SDL_Rect CANNON_SRC;
extern const SDL_Rect COIN_SRC;
//------------------ MODIFIERS ---------------------- //------------------ MODIFIERS ----------------------
extern const SDL_Rect MOD_DESTRUCTIBLE_SRC; extern const SDL_Rect MOD_DESTRUCTIBLE_SRC;
extern const SDL_Rect MOD_BACKGROUND_SRC; extern const SDL_Rect MOD_BACKGROUND_SRC;