game/mario/blocks/mushroomblock.cpp

65 lines
2.1 KiB
C++
Raw Normal View History

2021-08-07 19:41:15 +00:00
#include "mushroomblock.hpp"
#include "../sprites.hpp"
#include "../global_vars.hpp"
#include "../objectids.hpp"
#include "../visitors/mushroom_visitor.hpp"
2021-10-18 08:10:43 +00:00
MushroomBlock::MushroomBlock(int x, int y,
std::shared_ptr<SDLPP::Renderer> &renderer)
: MarioBlock(x, y, renderer, g_terrain_texture, MUSHROOM_SRC, true,
true) {
2021-08-07 19:41:15 +00:00
setHidden(true);
ensureCollision();
setId(MUSHROOM_ID);
2021-10-18 08:10:43 +00:00
auto bottom_detect = SDLPP::RectColider(0.2, 1, 0.6, 0, NPC_FLOOR_DETECT);
2021-08-07 19:41:15 +00:00
bottom_detect.setColor("#FF0000");
bottom_detect.setOutlineColor("#FF0000");
bottom_detect.setMinHeight(1);
addCollision(bottom_detect);
2021-10-18 08:10:43 +00:00
addCollision(SDLPP::RectColider(0, 0.25, 0.1, 0.6, NPC_LEFT_SIDE_DETECT));
2021-08-07 19:41:15 +00:00
addCollision(
2021-10-18 08:10:43 +00:00
SDLPP::RectColider(0.9, 0.25, 0.1, 0.6, NPC_RIGHT_SIDE_DETECT));
2021-08-07 19:41:15 +00:00
}
void MushroomBlock::custom_move(int ticks) {
2021-10-18 08:10:43 +00:00
if (_parent != nullptr && !_parent->isBouncing() && !isTraveling()) {
2021-08-07 19:41:15 +00:00
setHidden(false);
travelToPos(_parent->getPos() - SDLPP::Vec2D<double>(0, BLOCK_SIZE));
_parent = nullptr;
2021-10-18 08:10:43 +00:00
} else if (_parent == nullptr && !isTraveling() && !_started_movement) {
2021-08-07 19:41:15 +00:00
_started_movement = true;
2022-11-11 18:47:35 +00:00
if(!_fire_flower) {
setMovement(movementSpeed / 4, 0);
}
2021-08-07 19:41:15 +00:00
}
gravity(ticks);
MarioBlock::custom_move(ticks);
}
void MushroomBlock::setParent(MarioBlock *parent) {
_parent = parent;
}
void MushroomBlock::handleVisitor(SDLPP::Visitor &visitor) {
2022-11-19 21:53:24 +00:00
auto &m_visitor = dynamic_cast<MushroomVisitor &>(visitor);
if (m_visitor.getDeath()) {
destroy();
}
2021-10-18 08:10:43 +00:00
if (!_started_movement) {
2021-08-07 19:41:15 +00:00
return;
}
setOnGround(m_visitor.isOnGround());
2021-10-18 08:10:43 +00:00
if (isOnGround()) {
2021-08-07 20:17:27 +00:00
setPos(getPos().getX(), m_visitor.getGroundY() - BLOCK_SIZE);
}
2021-10-18 08:10:43 +00:00
if (!m_visitor.canGoLeft() || !m_visitor.canGoRight()) {
2021-08-07 20:17:27 +00:00
setPos(m_visitor.getValidXPos(), getPos().getY());
2021-08-07 19:41:15 +00:00
setMovement(-getMovement().getX(), getMovement().getY());
}
}
2022-11-11 18:47:35 +00:00
void MushroomBlock::setFireFlower(bool fire_flower) {
setTextureSourceRect(fire_flower ? FIRE_FLOWER_SRC : MUSHROOM_SRC);
_fire_flower = fire_flower;
}