Mario: add travelToPos function to blocks

This commit is contained in:
2021-08-07 21:39:15 +02:00
parent 7cb3f95e44
commit 4742f3dd02
2 changed files with 123 additions and 15 deletions
+95 -13
View File
@@ -10,6 +10,7 @@
#include "blocks/simpleblocks.hpp"
#include "blocks/coineditorblock.hpp"
#include "blocks/coinblock.hpp"
#include "blocks/mushroomblock.hpp"
#include "mario.hpp"
#define CAN_BE_DESTROYED_FLAG 0x0000000000000001
@@ -66,6 +67,13 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) {
dynamic_cast< MarioVisitor & >( visitor ).setCoin();
dynamic_cast< MarioVisitor & >( visitor ).setCoinBlock(coin);
}
if ( hasMushroom() ) {
removeMushroom();
auto mushroom = createTerrainBlock(MUSHROOM_ID, LandType::OVERWORLD, renderer);
mushroom->setPos(getPos());
std::dynamic_pointer_cast<MushroomBlock>(mushroom)->setParent(this);
dynamic_cast< MarioVisitor & >( visitor ).setMushroomBlock(mushroom);
}
}
#endif
visitor.visit( *this );
@@ -78,32 +86,94 @@ void MarioBlock::setTerrain( bool terrain ) {
}
void MarioBlock::bounce() {
if ( _bouncing )
if ( _bouncing ) {
return;
}
_bouncing = true;
og_pos = getPos();
ticks_to_bounce = bounce_ticks;
setMovement( 0, -bounce_speed );
}
bool MarioBlock::isBouncing() {
void MarioBlock::travelToPos(const SDLPP::Vec2D<double> &target) {
if(_traveling) {
return;
}
_traveling = true;
_target = target;
auto movement = (_target - getPos());
auto abs_mov_x = movement.getX();
if(abs_mov_x < 0) {
abs_mov_x *= -1;
}
auto abs_mov_y = movement.getY();
if(abs_mov_y < 0) {
abs_mov_y *= -1;
}
movement = movement / (abs_mov_x > abs_mov_y ? abs_mov_x : abs_mov_y);
movement = movement * travel_speed;
setMovement(movement.getX(), movement.getY());
}
void MarioBlock::gravity(int ticks) {
if(_on_ground) {
return;
}
_ticks_till_gravity -= ticks;
if(_ticks_till_gravity < 0) {
addMovement(0, _gravity_acceleration);
_ticks_till_gravity = _base_gravity_ticks;
}
}
bool MarioBlock::isBouncing() const {
return _bouncing;
}
bool MarioBlock::isTraveling() const {
return _traveling;
}
void MarioBlock::custom_move( int ticks ) {
if ( !_bouncing )
if ( !_bouncing && !_traveling ) {
return;
if ( getMovement().getY() < 0 ) {
ticks_to_bounce -= ticks;
if ( ticks_to_bounce < 0 ) {
setMovement( 0, bounce_speed );
ticks_to_bounce = bounce_ticks;
}
if( _bouncing ) {
if ( getMovement().getY() < 0 ) {
ticks_to_bounce -= ticks;
if ( ticks_to_bounce < 0 ) {
setMovement( 0, bounce_speed );
ticks_to_bounce = bounce_ticks;
}
} else {
if ( getPos().getY() >= og_pos.getY() ) {
setMovement( 0, 0 );
setPos( getPos().getX(), og_pos.getY() );
_bouncing = false;
}
}
} else {
if ( getPos().getY() >= og_pos.getY() ) {
setMovement( 0, 0 );
setPos( getPos().getX(), og_pos.getY() );
_bouncing = false;
}
if(_traveling) {
bool overshot_x = (getMovement().getX() < 0 &&
getPos().getX() <= _target.getX()) ||
(getMovement().getX() > 0 &&
getPos().getX() >= _target.getX());
bool overshot_y = (getMovement().getY() < 0 &&
getPos().getY() <= _target.getY()) ||
(getMovement().getY() > 0 &&
getPos().getY() >= _target.getY());
if(overshot_x) {
setPos(_target.getX(), getPos().getY());
setMovement(0, getMovement().getY());
}
if(overshot_y) {
setPos(getPos().getX(), _target.getY());
setMovement(getMovement().getX(), 0);
}
if(getMovement() == SDLPP::Vec2D<double>(0,0)) {
_traveling = false;
}
}
}
@@ -128,6 +198,9 @@ void MarioBlock::removeCoin() {
void MarioBlock::removeMushroom() {
_mushroom = false;
}
void MarioBlock::addMushroom() {
_mushroom = true;
}
void MarioBlock::setCoinCount( int coins ) {
_coins = coins;
}
@@ -510,6 +583,10 @@ createBlockById( uint64_t id, int x, int y,
result = std::static_pointer_cast< MarioBlock >(
std::make_shared< CoinBlock >( x, y, renderer ) );
break;
case MUSHROOM_ID:
result = std::static_pointer_cast< MarioBlock >(
std::make_shared< MushroomBlock >( x, y, renderer ) );
break;
#endif
}
return result;
@@ -574,3 +651,8 @@ enum BlockRole::Value getBlockRole( uint64_t id ) {
// TODO modifier/character
return BlockRole::MODIFIER;
}
void MarioBlock::setBaseRect(SDL_Rect rect) {
_base_src = rect;
setType(getType());
}