Mario: prepartations for multiple moving objects

This commit is contained in:
2021-08-05 00:32:17 +02:00
parent 37f7bab63e
commit bfe658618e
19 changed files with 321 additions and 249 deletions
+17
View File
@@ -0,0 +1,17 @@
#include "bounce_visitor.hpp"
#include "../../sdlpp/sdlpp_renderobject.hpp"
#include "../objectids.hpp"
void BounceVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId();
switch ( id ) {
case FLOOR_ID:
case BRICK_ID:
case BRICK_TOP_ID:
if(from == BOUNCE_COLLISION) {
hits += 1;
}
default:
break;
}
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef BOUNCE_VISITOR_HPP
#define BOUNCE_VISITOR_HPP
#include "../../sdlpp/sdlpp_visitor.hpp"
#include "../../sdlpp/sdlpp_geometry.hpp"
class BounceVisitor : public SDLPP::Visitor {
public:
BounceVisitor() {}
virtual void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t id ) override {
from = id;
}
virtual uint64_t getFromId() override {
return from;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
virtual uint64_t getVisitorType() override {
return _type;
}
bool canBounce() {
return hits < 2;
}
private:
int hits = 0;
uint64_t from;
uint64_t _type;
};
#endif
+64
View File
@@ -0,0 +1,64 @@
#include "mario_visitor.hpp"
#include "../../sdlpp/sdlpp_renderobject.hpp"
#include "../objectids.hpp"
#include "../sprites.hpp"
void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId();
switch ( id ) {
case FLOOR_ID:
case BRICK_ID:
case BRICK_TOP_ID:
case PIPE_LEFT_BOTTOM_ID:
case PIPE_RIGHT_BOTTOM_ID:
case PIPE_LEFT_TOP_ID:
case PIPE_RIGHT_TOP_ID:
case STEP_ID:
case SIDEWAY_PIPE_END_TOP_ID:
case SIDEWAY_PIPE_END_BOTTOM_ID:
case SIDEWAY_PIPE_MIDDLE_BOTTOM_ID:
case SIDEWAY_PIPE_MIDDLE_TOP_ID:
case SIDEWAY_PIPE_CONNECTOR_BOTTOM_ID:
case SIDEWAY_PIPE_CONNECTOR_TOP_ID:
case TREE_PLATFORM_TOP_LEFT_ID:
case TREE_PLATFORM_TOP_RIGHT_ID:
case MUSHROOM_PLATFORM_TOP_MIDDLE_ID:
case MUSHROOM_PLATFORM_TOP_LEFT_ID:
case MUSHROOM_PLATFORM_TOP_RIGHT_ID:
case CANNON_TOWER_ID:
case CANNON_PEDESTAL_ID:
case CANNON_ID:
if ( from == MARIO_FLOOR_DETECT ) {
onGround = true;
groundY = obj.getPos().getY();
} else if ( from == MARIO_LEFT_SIDE_DETECT ) {
if(!left && !right) {
movement_blockage = obj.getPos();
}
left = true;
} else if (from == MARIO_RIGHT_SIDE_DETECT ) {
if(!left && !right) {
movement_blockage = obj.getPos();
}
right = true;
} else if (from == MARIO_TOP_DETECT) {
top_hit = true;
} else if (from == MARIO_TOP_LEFT_DETECT || from == MARIO_TOP_RIGHT_DETECT) {
rightleftpos = obj.getPos();
top_left_right = true;
}
break;
case DEATH_ID:
// TODO remove death?
death = true;
_quit = true;
break;
case STOP_MOVEMENT:
stop = true;
newX = obj.getDoubleRect().first.getX() +
obj.getDoubleRect().second.getX();
break;
default:
break;
}
}
+115
View File
@@ -0,0 +1,115 @@
#ifndef MARIO_VISITOR_H
#define MARIO_VISITOR_H
#include "../../sdlpp/sdlpp_visitor.hpp"
#include "../../sdlpp/sdlpp_geometry.hpp"
#include "../../sdlpp/sdlpp_scene.hpp"
#include "../blocks.hpp"
class MarioVisitor : public SDLPP::Visitor {
public:
MarioVisitor(bool is_jumping, SDLPP::Scene &scene, bool &quit, int &coin_count) : jumping(is_jumping), _scene(scene), _quit(quit), _coin_count(coin_count) {}
virtual void visit( const SDLPP::RenderObject &obj ) override;
bool isOnGround() {
return onGround;
}
bool isDead() {
return death;
}
bool isStopped() {
return stop;
}
double newXPos() {
return newX;
}
virtual void setFromId( uint64_t id ) override {
from = id;
}
virtual uint64_t getFromId() override {
return from;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
virtual uint64_t getVisitorType() override {
return _type;
}
bool canGoLeft() {
return !left;
}
bool canGoRight() {
return !right;
}
double getGroundY() {
return groundY;
}
bool topBlock() {
return top_hit;
}
bool moveTop() {
return top_left_right && !top_hit;
}
const SDLPP::Vec2D<double> &getRightLeftPos() {
return rightleftpos;
}
bool canDestroy() {
return jumping && !top_hit;
}
const SDLPP::Vec2D<double> &getMovementBlockage() {
return movement_blockage;
}
double getStopX() {
return newX;
}
void setCoin() {
// TODO remove coin?
coin = true;
_coin_count++;
}
bool hasCoin() {
return coin;
}
void setCoinBlock(std::shared_ptr<MarioBlock> &coin) {
// TODO remove coin_block?
coin_block = coin;
_scene.addObject(coin);
_scene.setZIndex(coin, 1);
}
bool hasCoinBlock() {
return coin_block != nullptr;
}
std::shared_ptr<MarioBlock> &getCoinBlock() {
return coin_block;
}
private:
bool onGround = false;
double groundY = 0;
bool death = false;
bool stop = false;
double newX;
uint64_t from = -1;
bool left = false;
bool right = false;
uint64_t _type = 0;
bool top_hit = false;
SDLPP::Vec2D<double> rightleftpos;
bool top_left_right = false;
bool jumping;
bool coin = false;
SDLPP::Vec2D<double> movement_blockage;
std::shared_ptr<MarioBlock> coin_block = nullptr;
SDLPP::Scene &_scene;
bool &_quit;
int &_coin_count;
};
#endif
+17
View File
@@ -0,0 +1,17 @@
#include "visitor_generator.hpp"
#include "../objectids.hpp"
#include "mario_visitor.hpp"
std::shared_ptr< SDLPP::Visitor >
getVisitor( const MarioBlock &block, SDLPP::Scene &scene, bool &quit,
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, quit, coin_count));
default:
break;
}
return result;
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef MARIO_VISITOR_GENERATOR_HPP
#define MARIO_VISITOR_GENERATOR_HPP
#include "../blocks.hpp"
std::shared_ptr< SDLPP::Visitor >
getVisitor( const MarioBlock &block, SDLPP::Scene &scene, bool &quit,
int &coin_count,
std::vector< std::shared_ptr< MarioBlock > > &moving_objects );
#endif