game/mario/mario.cpp

214 lines
6.7 KiB
C++
Raw Normal View History

2021-05-22 21:13:26 +00:00
#include "mario.hpp"
2022-09-23 14:46:50 +00:00
#include "blocks.hpp"
2021-05-22 21:13:26 +00:00
#include "global_vars.hpp"
#include "objectids.hpp"
#include "sprites.hpp"
#include "visitors/mario_visitor.hpp"
2021-05-22 21:13:26 +00:00
2021-10-18 07:08:35 +00:00
Mario::Mario(int x, int y, const std::shared_ptr<SDLPP::Renderer> &renderer)
: MarioBlock(x, y, renderer, g_mario_texture, MARIO_STANDING_SRC) {
setAnimationFrames(MARIO_WALK_ANIM);
setId(MARIO_ID);
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
setAnimationSpeed(12.5);
2021-05-22 21:13:26 +00:00
pauseAnimation();
2021-10-18 07:08:35 +00:00
setMovement(0, 0);
setMovementSpeed(1);
auto bottom_detect = SDLPP::RectColider(0.2, 1, 0.6, 0, MARIO_FLOOR_DETECT);
2021-05-27 14:33:00 +00:00
bottom_detect.setColor("#FF0000");
bottom_detect.setOutlineColor("#FF0000");
bottom_detect.setMinHeight(1);
addCollision(bottom_detect);
2021-10-18 07:08:35 +00:00
addCollision(SDLPP::RectColider(0, 0.25, 0.1, 0.6, MARIO_LEFT_SIDE_DETECT));
2021-05-22 21:13:26 +00:00
addCollision(
2021-10-18 07:08:35 +00:00
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));
2021-05-27 14:33:00 +00:00
setColiderColor("#FF0000");
2021-10-18 07:08:35 +00:00
setStatic(false);
bounce_speed *= 4;
2021-05-22 21:13:26 +00:00
}
2021-10-18 07:08:35 +00:00
Mario::Mario(const std::shared_ptr<SDLPP::Renderer> &renderer)
: Mario(0, 0, renderer) {}
2021-05-22 21:13:26 +00:00
void Mario::walkLeft() {
2022-09-23 14:46:50 +00:00
if (!controllable) {
return;
}
2021-10-18 07:08:35 +00:00
if (on_ground)
resumeAnimation();
2021-10-18 07:08:35 +00:00
addMovement(-side_movement, 0);
if (getMovement().getX() < 0 && faces_right) {
2021-05-22 21:13:26 +00:00
flipHorizontally();
2021-05-25 20:05:50 +00:00
top_collision->setPos(0.3, 0);
updateSizeAndPosition();
2021-05-22 21:13:26 +00:00
faces_right = false;
}
}
void Mario::walkRight() {
2022-09-23 14:46:50 +00:00
if (!controllable) {
return;
}
2021-10-18 07:08:35 +00:00
if (on_ground)
resumeAnimation();
2021-10-18 07:08:35 +00:00
addMovement(side_movement, 0);
if (getMovement().getX() > 0 && !faces_right) {
2021-05-22 21:13:26 +00:00
flipHorizontally();
2021-05-25 20:05:50 +00:00
top_collision->setPos(0.5, 0);
updateSizeAndPosition();
2021-05-22 21:13:26 +00:00
faces_right = true;
}
}
void Mario::setStanding() {
2021-10-18 07:08:35 +00:00
if (getMovement().getX() == 0) {
2021-05-22 21:13:26 +00:00
pauseAnimation();
}
}
void Mario::handleVisitor(SDLPP::Visitor &visitor) {
#ifndef EDITOR
2021-10-18 07:08:35 +00:00
// TODO -
// https://web.archive.org/web/20130807122227/http://i276.photobucket.com/albums/kk21/jdaster64/smb_playerphysics.png
auto &m_visitor = dynamic_cast<MarioVisitor &>(visitor);
2021-05-22 21:13:26 +00:00
// handle gravity
on_ground = m_visitor.isOnGround();
2021-10-18 07:08:35 +00:00
if (!jumping && on_ground) {
2021-05-22 21:13:26 +00:00
resetMovementY();
2021-08-07 19:42:51 +00:00
setBaseRect(MARIO_STANDING_SRC);
2022-09-23 14:46:50 +00:00
if (!controllable) {
setDeath();
}
2021-10-18 07:08:35 +00:00
if (getMovement().getX() != 0)
resumeAnimation();
2021-05-23 21:32:15 +00:00
// for some reason falling of the edge causes on_ground to be true, but
// visitor ground_y is 0
2021-10-18 07:08:35 +00:00
if (m_visitor.getGroundY() != 0) {
setPos(getPos().getX(), m_visitor.getGroundY() - BLOCK_SIZE);
2021-05-23 21:32:15 +00:00
}
2021-05-22 21:13:26 +00:00
}
2021-05-27 14:33:00 +00:00
// if we just left ground gravity didn't work in custom_move
2021-10-18 07:08:35 +00:00
if (!on_ground && !jumping && getMovement().getY() == 0) {
addMovement(0, 2 * gravity_add_falling);
2021-05-27 14:33:00 +00:00
}
2021-10-18 07:08:35 +00:00
if (m_visitor.topBlock() && getMovement().getY() < 0) {
2021-05-23 21:57:29 +00:00
resetMovementY();
stop_jump = true;
2021-05-23 21:57:29 +00:00
}
2022-09-25 17:44:28 +00:00
if (m_visitor.shouldBounce() && getMovement().getY() > 0) {
addMovement(0, -bounce_speed);
}
2021-05-22 21:13:26 +00:00
// make sure Mario isn't stuck inside a wall
// TODO more readable function names
2021-10-18 07:08:35 +00:00
if (m_visitor.isStopped()) {
setPos(m_visitor.getStopX(), getPos().getY());
} else if (m_visitor.canGoLeft() != m_visitor.canGoRight() &&
!(on_ground && m_visitor.getMovementBlockage().getY() >
getPos().getY() + BLOCK_SIZE / 2)) {
// 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();
2021-10-18 07:08:35 +00:00
if (objPos.getX() < getPos().getX()) {
setPos(objPos.getX() + BLOCK_SIZE, getPos().getY());
} else {
2021-10-18 07:08:35 +00:00
setPos(objPos.getX() - BLOCK_SIZE, getPos().getY());
}
2021-05-22 21:13:26 +00:00
}
2021-10-18 07:08:35 +00:00
if (m_visitor.hasMushroom()) {
2022-09-25 17:44:28 +00:00
setBig();
2021-08-07 19:42:51 +00:00
}
2022-09-23 14:46:50 +00:00
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();
}
}
2022-09-25 17:44:28 +00:00
if (m_visitor.isDead()) {
handleDeath();
}
#endif
2021-05-22 21:13:26 +00:00
}
2022-09-25 17:44:28 +00:00
void Mario::handleDeath() {
if (isBig() && ticks_till_vulnurable <= 0) {
unsetBig();
ticks_till_vulnurable = base_vulnurable_ticks;
} else if (ticks_till_vulnurable <= 0) {
setDeath();
}
}
2021-05-22 21:54:01 +00:00
void Mario::jump() {
2022-09-23 14:46:50 +00:00
if (!controllable) {
return;
}
2021-10-18 07:08:35 +00:00
if (!on_ground)
2021-05-22 21:54:01 +00:00
return;
jumping = true;
stop_jump = false;
2021-10-18 07:08:35 +00:00
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);
2021-05-23 21:32:15 +00:00
ticks_till_gravity = base_gravity_ticks;
2021-08-07 19:42:51 +00:00
setBaseRect(MARIO_JUMP_SRC);
pauseAnimation();
2021-05-22 21:54:01 +00:00
}
void Mario::stopJump() {
stop_jump = true;
}
2021-05-23 21:32:15 +00:00
2021-10-18 07:08:35 +00:00
void Mario::custom_move(int ticks) {
2022-09-25 17:44:28 +00:00
if (ticks_till_vulnurable > 0) {
ticks_till_vulnurable -= ticks;
}
MarioBlock::custom_move(ticks);
2021-10-18 07:08:35 +00:00
if (!jumping && on_ground)
2021-05-23 21:32:15 +00:00
return;
2021-10-18 07:08:35 +00:00
if (getMovement().getY() >= 1.0625 * jump_movement)
2021-05-23 21:32:15 +00:00
return;
ticks_till_gravity -= ticks;
2021-10-18 07:08:35 +00:00
if (ticks_till_gravity < 0) {
if (getMovement().getY() > 0) {
2021-05-23 21:32:15 +00:00
jumping = false;
addMovement(0, gravity_add_jumping);
} else {
2021-10-18 07:08:35 +00:00
if (stop_jump) {
2021-05-23 21:32:15 +00:00
addMovement(0, gravity_add_falling);
} else {
addMovement(0, gravity_add_jumping);
}
}
ticks_till_gravity += base_gravity_ticks;
}
}
2021-08-07 19:42:51 +00:00
void Mario::visit(SDLPP::Visitor &visitor) {
// TODO
2021-08-07 19:42:51 +00:00
visitor.visit(*this);
}
void Mario::setWorldTypeSrc(LandType::Value /*UNUSED*/) {
MarioBlock::setWorldTypeSrc(LandType::OVERWORLD);
// TODO
}
2022-09-23 14:46:50 +00:00
void Mario::stopMovement() {
controllable = false;
setMovement(0, getMovement().getY());
}