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
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)
COMMON_OBJECTS += libsdlpp.a
endif
@ -71,6 +71,8 @@ editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp edit
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
coineditorblock.${OBJEXT}: blocks/coineditorblock.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/coineditorblock.hpp
$(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
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
mapobject.${OBJEXT}: mapobject.cpp ../sdlpp/sdlpp.hpp objectids.hpp

View File

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

View File

@ -34,6 +34,11 @@ public:
void setCoinCount( int coins );
void setDestructible( bool destructible = true );
void ensureCollision();
bool isBouncing();
protected:
double bounce_speed = 0.5;
int bounce_ticks = 100;
private:
bool _tool = false;
@ -43,8 +48,8 @@ private:
bool _bouncing = false;
int _coins = 0;
bool _mushroom = false;
const int bounce_ticks = 100;
int ticks_to_bounce = bounce_ticks;
bool _release_coin = false;
int ticks_to_bounce = 0;
SDLPP::Vec2D< double > og_pos = {};
LandType::Value _type;
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 ) {
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,
std::shared_ptr< SDLPP::Renderer > renderer ) : MarioBlock (x, y, renderer, g_mod_texture, MOD_MUSHROOM_SRC, false ) {
setId( MUSHROOM_MODIFIER_ID );

View File

@ -290,11 +290,6 @@ public:
BackgroundModifierBlock( int x, int y,
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 {
public:
MushroomModifierBlock( int x, int y,

View File

@ -142,6 +142,11 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
coin_count++;
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
auto playerX = mario->getRect().x;

View File

@ -3,6 +3,7 @@
#include "../sdlpp/sdlpp_visitor.hpp"
#include "../sdlpp/sdlpp_geometry.hpp"
#include "blocks.hpp"
class MarioVisitor : public SDLPP::Visitor {
public:
@ -71,6 +72,18 @@ public:
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:
bool onGround = false;
double groundY = 0;
@ -87,6 +100,7 @@ private:
bool jumping;
bool coin = false;
SDLPP::Vec2D<double> movement_blockage;
std::shared_ptr<MarioBlock> coin_block = nullptr;
};
class BounceVisitor : public SDLPP::Visitor {

View File

@ -61,6 +61,7 @@
#define CANNON_TOWER_ID 0x7038
#define CANNON_PEDESTAL_ID 0x7039
#define CANNON_ID 0x703A
#define COIN_ID 0x703B
// modifiers
#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_PEDESTAL_SRC = {256, 29, 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_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_PEDESTAL_SRC;
extern const SDL_Rect CANNON_SRC;
extern const SDL_Rect COIN_SRC;
//------------------ MODIFIERS ----------------------
extern const SDL_Rect MOD_DESTRUCTIBLE_SRC;
extern const SDL_Rect MOD_BACKGROUND_SRC;