game/mario/mario.cpp
zv0n 9402f2afa2
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Teleportation in editor
2022-11-19 22:53:24 +01:00

292 lines
9.4 KiB
C++

#include "mario.hpp"
#include "blocks.hpp"
#include "global_vars.hpp"
#include "objectids.hpp"
#include "sprites.hpp"
#include "visitors/mario_visitor.hpp"
#include "blocks/fireball.hpp"
Mario::Mario(int x, int y, const std::shared_ptr<SDLPP::Renderer> &renderer, std::function<void(std::shared_ptr<MarioBlock>&, bool)> addObject)
: MarioBlock(x, y, renderer, g_mario_texture, MARIO_STANDING_SRC), _addObject(addObject) {
setAnimationFrames(*walk_anim);
setId(MARIO_ID);
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
setAnimationSpeed(12.5);
pauseAnimation();
setMovement(0, 0);
setMovementSpeed(1);
auto bottom_detect = SDLPP::RectColider(0.2, 1, 0.6, 0, MARIO_FLOOR_DETECT);
bottom_detect.setColor("#FF0000");
bottom_detect.setOutlineColor("#FF0000");
bottom_detect.setMinHeight(1);
addCollision(bottom_detect);
addCollision(SDLPP::RectColider(0, 0.25, 0.1, 0.6, MARIO_LEFT_SIDE_DETECT));
addCollision(
SDLPP::RectColider(0.9, 0.25, 0.1, 0.6, MARIO_RIGHT_SIDE_DETECT));
addCollision(SDLPP::RectColider(0, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT));
addCollision(SDLPP::RectColider(0.9, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT));
top_collision = std::make_shared<SDLPP::RectColider>(0.5, 0, 0.2, 0.15,
MARIO_TOP_DETECT);
addCollision(top_collision);
addCollision(SDLPP::RectColider(0, 1, 1, 0.2, MARIO_ENEMY_DETECT));
setColiderColor("#FF0000");
setStatic(false);
bounce_speed *= 4;
}
Mario::Mario(const std::shared_ptr<SDLPP::Renderer> &renderer, std::function<void(std::shared_ptr<MarioBlock>&, bool)> addObject)
: Mario(0, 0, renderer, addObject) {}
void Mario::walkLeft() {
if (!controllable || isCrouching()) {
return;
}
if (on_ground)
resumeAnimation();
addMovement(-side_movement, 0);
if (getMovement().getX() < 0 && faces_right) {
flipHorizontally();
top_collision->setPos(0.3, 0);
updateSizeAndPosition();
faces_right = false;
}
}
void Mario::walkRight() {
if (!controllable || isCrouching()) {
return;
}
if (on_ground)
resumeAnimation();
addMovement(side_movement, 0);
if (getMovement().getX() > 0 && !faces_right) {
flipHorizontally();
top_collision->setPos(0.5, 0);
updateSizeAndPosition();
faces_right = true;
}
}
void Mario::setStanding() {
if (getMovement().getX() == 0) {
pauseAnimation();
}
}
void setTeleportLevelMain(const std::string &level);
void Mario::handleVisitor(SDLPP::Visitor &visitor) {
#ifndef EDITOR
// TODO -
// https://web.archive.org/web/20130807122227/http://i276.photobucket.com/albums/kk21/jdaster64/smb_playerphysics.png
auto &m_visitor = dynamic_cast<MarioVisitor &>(visitor);
// handle gravity
on_ground = m_visitor.isOnGround();
if (!jumping && on_ground) {
resetMovementY();
setBaseRect(*standing_src);
if (!controllable) {
setDeath();
}
if (getMovement().getX() != 0)
resumeAnimation();
// for some reason falling of the edge causes on_ground to be true, but
// visitor ground_y is 0
if (m_visitor.getGroundY() != 0) {
setPos(getPos().getX(),
m_visitor.getGroundY() - getDoubleRect().second.getY());
}
}
// if we just left ground gravity didn't work in custom_move
if (!on_ground && !jumping && getMovement().getY() == 0) {
addMovement(0, 2 * gravity_add_falling);
}
if (m_visitor.topBlock() && getMovement().getY() < 0) {
resetMovementY();
stop_jump = true;
}
if (m_visitor.shouldBounce() && getMovement().getY() > 0) {
addMovement(0, -bounce_speed);
}
// make sure Mario isn't stuck inside a wall
// TODO more readable function names
if (m_visitor.isStopped()) {
setPos(m_visitor.getStopX(), getPos().getY());
} else if (m_visitor.canGoLeft() != m_visitor.canGoRight()) {
// only stop mario on ground if the block obstructs at least half of him
// (important for bug when mario lands from a high jump and is
// teleported if visitor is fired at wrong moment)
SDLPP::Vec2D<double> next_pos = {
m_visitor.getMovementBlockage().getX() +
(m_visitor.canGoLeft() * -1 + m_visitor.canGoRight() * 1) *
BLOCK_SIZE,
getPos().getY()
};
setPos(next_pos);
} else if (m_visitor.moveTop() && jumping && !stop_jump) {
auto objPos = m_visitor.getRightLeftPos();
if (objPos.getX() < getPos().getX()) {
setPos(objPos.getX() + BLOCK_SIZE, getPos().getY());
} else {
setPos(objPos.getX() - BLOCK_SIZE, getPos().getY());
}
}
if (m_visitor.hasMushroom()) {
setBig();
}
if (m_visitor.hasTeleport() && (
(m_visitor.teleportBottom() && isCrouching()) || (!m_visitor.teleportBottom() && walkingRight())
)) {
setTeleportLevelMain(m_visitor.getTeleportLevel());
}
if (m_visitor.levelEnd() && controllable) {
if (std::abs(getPos().getX() - m_visitor.getEndPos().getX()) <
BLOCK_SIZE / 8) {
setPos(m_visitor.getEndPos().getX(), getPos().getY());
stopMovement();
}
}
if (m_visitor.isDead()) {
handleDeath();
}
if (m_visitor.isInstantDead()) {
setDeath();
}
#endif
}
void Mario::handleDeath() {
if (isBig() && ticks_till_vulnurable <= 0) {
unsetBig();
ticks_till_vulnurable = base_vulnurable_ticks;
} else if (ticks_till_vulnurable <= 0) {
setDeath();
}
}
void Mario::jump() {
if (!controllable || isCrouching()) {
return;
}
if (!on_ground)
return;
jumping = true;
stop_jump = false;
max_jump = getPos().getY() - 3 * BLOCK_SIZE;
min_jump = getPos().getY() - 1 * BLOCK_SIZE;
slow_jump = getPos().getY() - 2 * BLOCK_SIZE;
addMovement(0, -jump_movement);
ticks_till_gravity = base_gravity_ticks;
setBaseRect(*jump_src);
pauseAnimation();
}
void Mario::crouch() {
if(walkingLeft()) {
walkRight();
} else if(walkingRight()) {
walkLeft();
}
_crouching = true;
}
void Mario::uncrouch() {
_crouching = false;
}
#ifndef EDITOR
void Mario::fire() {
if (!hasFire()) {
return;
}
auto fireball = std::make_shared<Fireball>(0, 0, renderer);
fireball->setPos(getPos() + SDLPP::Vec2D<double>((faces_right ? 1 : -1) * BLOCK_SIZE, BLOCK_SIZE));
fireball->setMovementDir(!faces_right);
auto fireball_m = std::static_pointer_cast<MarioBlock>(fireball);
fireball->setHidden(false);
_addObject(fireball_m, true);
}
void Mario::setAddObjFunc(std::function<void(std::shared_ptr<MarioBlock>&, bool)> func) {
_addObject = func;
}
std::function<void(std::shared_ptr<MarioBlock>&, bool)> Mario::getAddObjFunc() {
return _addObject;
}
#endif
void Mario::stopJump() {
stop_jump = true;
}
void Mario::custom_move(int ticks) {
if (ticks_till_vulnurable > 0) {
ticks_till_vulnurable -= ticks;
}
MarioBlock::custom_move(ticks);
if (!jumping && on_ground)
return;
if (getMovement().getY() >= 1.0625 * jump_movement)
return;
ticks_till_gravity -= ticks;
if (ticks_till_gravity < 0) {
if (getMovement().getY() > 0) {
jumping = false;
addMovement(0, gravity_add_jumping);
} else {
if (stop_jump) {
addMovement(0, gravity_add_falling);
} else {
addMovement(0, gravity_add_jumping);
}
}
ticks_till_gravity += base_gravity_ticks;
}
}
void Mario::visit(SDLPP::Visitor &visitor) {
// TODO
visitor.visit(*this);
}
void Mario::setWorldTypeSrc(LandType::Value /*UNUSED*/) {
MarioBlock::setWorldTypeSrc(LandType::OVERWORLD);
// TODO
}
void Mario::stopMovement() {
controllable = false;
setMovement(0, getMovement().getY());
}
void Mario::setBig() {
if (isBig()) {
setFireFlag();
} else {
setBigFlag();
setSize({ BLOCK_SIZE, 2 * BLOCK_SIZE });
setPos(getPos() - SDLPP::Vec2D<double>(0, BLOCK_SIZE));
}
setBaseRect(isJumping()
? (hasFire() ? MARIO_JUMP_FIRE_SRC : MARIO_JUMP_BIG_SRC)
: (hasFire() ? MARIO_STANDING_FIRE_SRC
: MARIO_STANDING_BIG_SRC));
setAnimationFrames(hasFire() ? MARIO_WALK_FIRE_ANIM : MARIO_WALK_BIG_ANIM);
standing_src = hasFire() ? &MARIO_STANDING_FIRE_SRC : &MARIO_STANDING_BIG_SRC;
death_src = hasFire() ? &MARIO_DEATH_FIRE_SRC : &MARIO_DEATH_BIG_SRC;
change_dir_src = hasFire() ? &MARIO_CHANGE_DIR_FIRE_SRC : &MARIO_CHANGE_DIR_BIG_SRC;
jump_src = hasFire() ? &MARIO_JUMP_FIRE_SRC : &MARIO_JUMP_BIG_SRC;
walk_anim = hasFire() ? &MARIO_WALK_FIRE_ANIM : &MARIO_WALK_BIG_ANIM;
}
void Mario::unsetBig() {
if (hasFire()) {
unsetFireFlag();
} else {
unsetBigFlag();
setSize({ BLOCK_SIZE, BLOCK_SIZE });
setBaseRect(isJumping() ? MARIO_JUMP_SRC : MARIO_STANDING_SRC);
setAnimationFrames(MARIO_WALK_ANIM);
standing_src = &MARIO_STANDING_SRC;
death_src = &MARIO_DEATH_SRC;
change_dir_src = &MARIO_CHANGE_DIR_SRC;
jump_src = &MARIO_JUMP_SRC;
walk_anim = &MARIO_WALK_ANIM;
}
}