Mario: add basic big mario logic

This commit is contained in:
2022-09-25 19:44:28 +02:00
parent 09cb13195c
commit 4109cfe1af
12 changed files with 156 additions and 57 deletions
+6 -2
View File
@@ -2,6 +2,7 @@
#include "../../sdlpp/sdlpp_renderobject.hpp"
#include "../objectids.hpp"
#include "../sprites.hpp"
#include "../mario.hpp"
void GoombaVisitor::visit(const SDLPP::RenderObject &obj) {
auto id = obj.getId();
@@ -49,8 +50,11 @@ void GoombaVisitor::visit(const SDLPP::RenderObject &obj) {
death = true;
break;
case MARIO_ID:
if (from == NPC_TOP_DETECT) {
death = true;
{
auto &mario = dynamic_cast<const Mario &>(obj);
if (from == NPC_TOP_DETECT && obj.getPos().getY() <= goomba_pos.getY() - BLOCK_SIZE && !mario.isJumping()) {
death = true;
}
}
break;
default:
+3 -1
View File
@@ -8,7 +8,8 @@
class GoombaVisitor : public SDLPP::Visitor {
public:
GoombaVisitor() = default;
GoombaVisitor() = delete;
GoombaVisitor(const SDLPP::Vec2D<double> &pos) : goomba_pos(pos) {}
void visit(const SDLPP::RenderObject &obj) override;
bool isOnGround() const {
return onGround;
@@ -59,6 +60,7 @@ private:
bool top_hit = false;
SDLPP::Vec2D<double> movement_blockage;
double validXPos = 0;
const SDLPP::Vec2D<double> goomba_pos;
};
#endif
+1 -1
View File
@@ -55,7 +55,7 @@ void MarioVisitor::visit(const SDLPP::RenderObject &obj) {
case GOOMBA_ID:
if (from != MARIO_FLOOR_DETECT && from != MARIO_ENEMY_DETECT) {
_death = true;
} else {
} else if (from == MARIO_FLOOR_DETECT || from == MARIO_ENEMY_DETECT) {
_bounce = true;
}
break;
+17 -5
View File
@@ -8,11 +8,13 @@
class MarioVisitor : public SDLPP::Visitor {
public:
MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &death,
MarioVisitor(bool is_jumping, SDLPP::Scene &scene,
int &coin_count,
std::vector<std::shared_ptr<MarioBlock>> &moving_objects)
: jumping(is_jumping), _scene(scene), _death(death),
_coin_count(coin_count), _moving_objects(moving_objects) {}
std::vector<std::shared_ptr<MarioBlock>> &moving_objects,
bool is_big, bool has_star)
: jumping(is_jumping), _scene(scene),
_coin_count(coin_count), _moving_objects(moving_objects),
_is_big(is_big), _has_star(has_star) {}
void visit(const SDLPP::RenderObject &obj) override;
bool isOnGround() const {
return onGround;
@@ -116,6 +118,14 @@ public:
return endPos;
}
bool isBig() const {
return _is_big;
}
bool hasStar() const {
return _has_star;
}
private:
bool onGround = false;
double groundY = 0;
@@ -133,13 +143,15 @@ private:
SDLPP::Vec2D<double> movement_blockage;
std::shared_ptr<MarioBlock> coin_block = nullptr;
SDLPP::Scene &_scene;
bool &_death;
int &_coin_count;
bool mushroom = false;
std::vector<std::shared_ptr<MarioBlock>> &_moving_objects;
bool _bounce = false;
bool _end = false;
SDLPP::Vec2D<double> endPos;
bool _is_big = false;
bool _has_star = false;
bool _death = false;
};
#endif
+10 -6
View File
@@ -3,18 +3,22 @@
#include "mario_visitor.hpp"
#include "mushroom_visitor.hpp"
#include "goomba_visitor.hpp"
#include "../mario.hpp"
std::shared_ptr<SDLPP::Visitor>
getVisitor(const MarioBlock &block, SDLPP::Scene &scene, bool &death,
getVisitor(const MarioBlock &block, SDLPP::Scene &scene,
int &coin_count,
std::vector<std::shared_ptr<MarioBlock>> &moving_objects) {
std::shared_ptr<SDLPP::Visitor> result{};
switch (block.getId()) {
case MARIO_ID:
result = std::static_pointer_cast<SDLPP::Visitor>(
std::make_shared<MarioVisitor>(block.getMovement().getY() < 0,
scene, death, coin_count,
moving_objects));
{
auto &mario = dynamic_cast<const Mario &>(block);
result = std::static_pointer_cast<SDLPP::Visitor>(
std::make_shared<MarioVisitor>(block.getMovement().getY() < 0,
scene, coin_count,
moving_objects, mario.isBig(), mario.hasStar()));
}
break;
case MUSHROOM_ID:
result = std::static_pointer_cast<SDLPP::Visitor>(
@@ -22,7 +26,7 @@ getVisitor(const MarioBlock &block, SDLPP::Scene &scene, bool &death,
break;
case GOOMBA_ID:
result = std::static_pointer_cast<SDLPP::Visitor>(
std::make_shared<GoombaVisitor>());
std::make_shared<GoombaVisitor>(block.getPos()));
break;
default:
break;
+1 -1
View File
@@ -4,7 +4,7 @@
#include "../blocks.hpp"
std::shared_ptr<SDLPP::Visitor>
getVisitor(const MarioBlock &block, SDLPP::Scene &scene, bool &quit,
getVisitor(const MarioBlock &block, SDLPP::Scene &scene,
int &coin_count,
std::vector<std::shared_ptr<MarioBlock>> &moving_objects);