116 lines
5.3 KiB
C++
116 lines
5.3 KiB
C++
#include "blocks.hpp"
|
|
#include "global_vars.hpp"
|
|
#include "objectids.hpp"
|
|
#include "sprites.hpp"
|
|
#include <unordered_map>
|
|
|
|
MarioBlock::MarioBlock(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Texture> texture, SDL_Rect src) : RectangleRender( x * BLOCK_SIZE, 1 - (16-y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ) {}
|
|
void MarioBlock::visit( SDLPP::Visitor &visitor ) {
|
|
// TODO if character don't
|
|
if(!_tool && !_character && visitor.getVisitorType() == TOOL_VISITOR_TYPE) {
|
|
destroy();
|
|
}
|
|
visitor.visit(*this);
|
|
}
|
|
void MarioBlock::setTool(bool tool) {
|
|
_tool = tool;
|
|
}
|
|
void MarioBlock::setCharacter(bool character) {
|
|
_character = character;
|
|
}
|
|
|
|
const std::vector< uint64_t > possibleBlocks = { FLOOR_ID,
|
|
HILL_INCLINE_ID,
|
|
HILL_DECLINE_ID,
|
|
HILL_DOTS_RIGHT_ID,
|
|
HILL_DOTS_LEFT_ID,
|
|
HILL_FILL_ID,
|
|
HILL_TOP_ID,
|
|
BUSH_LEFT_ID,
|
|
BUSH_MIDDLE_ID,
|
|
BUSH_RIGHT_ID,
|
|
CLOUD_LEFT_BOTTOM_ID,
|
|
CLOUD_MIDDLE_BOTTOM_ID,
|
|
CLOUD_RIGHT_BOTTOM_ID,
|
|
CLOUD_LEFT_TOP_ID,
|
|
CLOUD_MIDDLE_TOP_ID,
|
|
CLOUD_RIGHT_TOP_ID };
|
|
|
|
const std::unordered_map<uint64_t, const SDL_Rect*> block_mapping = {
|
|
{FLOOR_ID, &FLOOR_SRC}, {HILL_INCLINE_ID, &HILL_INCLINE_SRC},
|
|
{HILL_DECLINE_ID, &HILL_DECLINE_SRC}, {HILL_DOTS_RIGHT_ID, &HILL_DOTS_RIGHT_SRC},
|
|
{HILL_DOTS_LEFT_ID, &HILL_DOTS_LEFT_SRC}, {HILL_FILL_ID, &HILL_FILL_SRC},
|
|
{HILL_TOP_ID, &HILL_TOP_SRC}, {BUSH_LEFT_ID, &BUSH_LEFT_SRC}, {BUSH_MIDDLE_ID, &BUSH_MIDDLE_SRC},
|
|
{BUSH_RIGHT_ID, &BUSH_RIGHT_SRC}, {CLOUD_LEFT_BOTTOM_ID, &CLOUD_LEFT_BOTTOM_SRC},
|
|
{CLOUD_MIDDLE_BOTTOM_ID, &CLOUD_MIDDLE_BOTTOM_SRC}, {CLOUD_RIGHT_BOTTOM_ID, &CLOUD_RIGHT_BOTTOM_SRC},
|
|
{CLOUD_LEFT_TOP_ID, &CLOUD_LEFT_TOP_SRC}, {CLOUD_MIDDLE_TOP_ID, &CLOUD_MIDDLE_TOP_SRC},
|
|
{CLOUD_RIGHT_TOP_ID, &CLOUD_RIGHT_TOP_SRC}
|
|
};
|
|
|
|
std::shared_ptr< SDLPP::RectangleRender >
|
|
createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
|
|
std::shared_ptr< SDLPP::Texture > &texture, const SDL_Rect &src,
|
|
uint64_t id, bool collision = false ) {
|
|
auto block = std::make_shared<MarioBlock>(x, y, renderer, texture, src);
|
|
block->setId( id );
|
|
block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
block->setStatic();
|
|
if ( collision )
|
|
block->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
|
|
return block;
|
|
}
|
|
|
|
SDL_Rect getSourceRectByID( uint64_t id, BlockType type ) {
|
|
if(block_mapping.find(id) == block_mapping.end())
|
|
return {};
|
|
SDL_Rect ret_src = *block_mapping.at(id);
|
|
switch ( type ) {
|
|
case OVERWORLD:
|
|
ret_src.x += OVERWORLD_SHIFT.getX();
|
|
ret_src.y += OVERWORLD_SHIFT.getY();
|
|
break;
|
|
case UNDERWORLD:
|
|
ret_src.x += UNDERWORLD_SHIFT.getX();
|
|
ret_src.y += UNDERWORLD_SHIFT.getY();
|
|
break;
|
|
case WATER:
|
|
ret_src.x += WATER_SHIFT.getX();
|
|
ret_src.y += WATER_SHIFT.getY();
|
|
break;
|
|
case BOWSER:
|
|
ret_src.x += BOWSER_SHIFT.getX();
|
|
ret_src.y += BOWSER_SHIFT.getY();
|
|
break;
|
|
}
|
|
return ret_src;
|
|
}
|
|
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y, std::shared_ptr<SDLPP::Texture> texture, bool collision ) {
|
|
return createBlock( renderer, x, y, texture,
|
|
getSourceRectByID( block_id, type ), block_id,
|
|
collision );
|
|
}
|
|
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, std::shared_ptr<SDLPP::Texture> texture, bool collision ) {
|
|
return createTerrainBlock( block_id, type, renderer, 0, 0, texture, collision );
|
|
}
|
|
|
|
std::shared_ptr< SDLPP::RectangleRender >
|
|
createTerrainBlock( uint64_t block_id, BlockType type,
|
|
std::shared_ptr< SDLPP::Renderer > &renderer, int x,
|
|
int y, bool collision ) {
|
|
return createTerrainBlock(block_id, type, renderer, x, y, g_terrain_texture, collision);
|
|
}
|
|
|
|
std::shared_ptr< SDLPP::RectangleRender >
|
|
createTerrainBlock( uint64_t block_id, BlockType type,
|
|
std::shared_ptr< SDLPP::Renderer > &renderer,
|
|
bool collision ) {
|
|
return createTerrainBlock(block_id, type, renderer, g_terrain_texture, collision);
|
|
}
|
|
|
|
std::shared_ptr<SDLPP::RectangleRender> createMario( BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y ) {
|
|
//TODO add type additions
|
|
auto mario = createBlock( renderer, x, y, g_mario_texture, MARIO_STANDING_SRC, MARIO_ID, true );
|
|
dynamic_cast<MarioBlock&>(*mario).setCharacter();
|
|
return mario;
|
|
}
|