game/mario/blocks/turtleblock.cpp

116 lines
3.5 KiB
C++
Raw Normal View History

2023-04-08 17:51:27 +00:00
#include "turtleblock.hpp"
#include "../sprites.hpp"
#include "../global_vars.hpp"
#include "../objectids.hpp"
#include "../visitors/turtle_visitor.hpp"
TurtleBlock::TurtleBlock(int x, int y,
std::shared_ptr<SDLPP::Renderer> &renderer)
: MarioBlock(x, y-1, renderer, g_enemies_texture, TURTLE_WALK_ANIM[0],
false, false) {
setSize({BLOCK_SIZE, 2*BLOCK_SIZE});
#ifndef EDITOR
setAnimationFrames(TURTLE_WALK_ANIM);
setAnimationSpeed(12.5);
resumeAnimation();
#endif
setId(TURTLE_ID);
auto bottom_detect = SDLPP::RectColider(0.2, 1, 0.6, 0, NPC_FLOOR_DETECT);
bottom_detect.setMinHeight(1);
addCollision(bottom_detect);
addCollision(SDLPP::RectColider(0, 0.5, 0.1, 0.35, NPC_LEFT_SIDE_DETECT));
addCollision(
SDLPP::RectColider(0.9, 0.5, 0.1, 0.35, NPC_RIGHT_SIDE_DETECT));
addCollision(std::make_shared<SDLPP::RectColider>(0, 0.25, 1, 0.15,
NPC_TOP_DETECT));
setBouncable(false);
#ifndef EDITOR
setMovement(-0.19, 0);
#endif
}
void TurtleBlock::move(int ticks) {
#ifndef EDITOR
if (wasVisible()) {
MarioBlock::move(ticks);
}
#else
MarioBlock::move(ticks);
#endif
}
void TurtleBlock::custom_move(int ticks) {
#ifndef EDITOR
if (death_started) {
death_countdown -= ticks;
if (death_countdown <= 0) {
destroy();
}
} else {
gravity(ticks);
}
#endif
MarioBlock::custom_move(ticks);
}
void TurtleBlock::handleVisitor(SDLPP::Visitor &visitor) {
#ifndef EDITOR
switched_after_turtle = false;
auto &t_visitor = dynamic_cast<TurtleVisitor &>(visitor);
setOnGround(t_visitor.isOnGround());
if (isOnGround()) {
setPos(getPos().getX(), t_visitor.getGroundY() - BLOCK_SIZE*(isShell() ? 1 : 2));
}
if ((!t_visitor.canGoLeft() && getMovement().getX() < 0) ||
(!t_visitor.canGoRight() && getMovement().getX() > 0)) {
setPos(t_visitor.getValidXPos(), getPos().getY());
setMovement(-getMovement().getX(), getMovement().getY());
flipHorizontally();
}
if (t_visitor.shouldBounce()) {
setMovement(getMovement().getX(), -0.5);
}
if (t_visitor.isDead()) {
if(!isShell()) {
setShell();
// pauseAnimation();
setAnimationFrames(TURTLE_SHELL_ANIM);
setAnimationSpeed(4);
next_movement = -2 * getMovement().getX();
setMovement(0, 0);
setSize({BLOCK_SIZE, BLOCK_SIZE});
setPos(getPos().getX(), getPos().getY() + BLOCK_SIZE);
} else {
if(getMovement().getX() == 0) {
resumeAnimation();
setMovement(next_movement, 0);
} else {
pauseAnimation();
next_movement = -next_movement;
setMovement(0, 0);
setTextureSourceRect(TURTLE_SHELL_ANIM[2]);
}
}
}
if (t_visitor.instantDeath()) {
destroy();
}
if (t_visitor.switchMovement()) {
if (isShell()) {
switched_after_turtle = true;
if(getMovement().getX() == 0) {
setMovement(t_visitor.getNextMovement(), getMovement().getY());
} else {
setMovement(-getMovement().getX(), getMovement().getY());
}
setPos(t_visitor.getTurtleHitValidPos(), getPos().getY());
} else {
destroy();
}
}
#endif
}
void TurtleBlock::startDeath() {
death_started = true;
}