Mario: merge mapLoader functions into one

This commit is contained in:
zvon 2021-05-31 18:54:43 +02:00
parent 0b3689b0ae
commit af3954cb61
2 changed files with 42 additions and 72 deletions

View File

@ -9,63 +9,12 @@
// TODO move to one function // TODO move to one function
void loadMap( std::shared_ptr< SDLPP::Scene > &scene, void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
std::shared_ptr< SDLPP::RectangleRender > mario, std::shared_ptr< SDLPP::RenderObject > mario,
const std::string &file, const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer ) { std::shared_ptr< SDLPP::Renderer > &renderer ) {
std::ifstream map_file; std::vector< mapColumnType > tmp = {};
map_file.open( file, std::ios::in | std::ios::binary ); loadMap(scene, mario, file, renderer, tmp, false);
uint16_t cols; scene->moveZTop(mario);
map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
for ( uint16_t i = 0; i < cols; i++ ) {
for ( int j = 0; j < 16; j++ ) {
uint16_t input_number;
uint8_t additional_data = 0;
uint8_t character_type = 0, character = 0, modifier_type = 0,
modifier_data = 0;
map_file.read( ( char * )&input_number,
sizeof( uint16_t ) / sizeof( char ) );
uint8_t type = ( input_number & 0xF000 ) >> 12;
uint16_t id = ( input_number & 0x0FFF ) | BLOCK_PREFIX;
if ( type & 0x8 ) {
map_file.read( ( char * )&additional_data,
sizeof( uint8_t ) / sizeof( char ) );
type &= ~0x8;
if ( additional_data & 0x80 ) {
// modifier
additional_data &= ~0x80;
modifier_type = ( additional_data & 0xF0 ) >> 4;
modifier_data = additional_data & 0x0F;
} else {
// character
character_type = ( additional_data & 0xF0 ) >> 4;
character = additional_data & 0x0F;
}
}
bool collision = false;
bool destructible = false;
if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) {
collision = true;
}
// TODO definitely make this somehow more streamlined, probably
// flags
if ( modifier_type == DESTRUCTIBLE_ID ) {
destructible = true;
}
// TODO add modifiers to createTerrainBlock
auto obj =
createTerrainBlock( id, static_cast< LandType::Value >( type ),
renderer, i, j, collision, destructible );
if ( obj != nullptr )
scene->addObject( obj );
if ( character ) {
if ( character == MARIO_ID ) {
mario->setPos( i * BLOCK_SIZE,
1 - ( 16 - j ) * BLOCK_SIZE );
}
}
}
}
scene->moveZTop( mario );
} }
// editor loader // editor loader
@ -73,14 +22,20 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
std::shared_ptr< SDLPP::RenderObject > &mario, std::shared_ptr< SDLPP::RenderObject > &mario,
const std::string &file, const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
std::vector< mapColumnType > &objects ) { std::vector< mapColumnType > &objects, bool editor ) {
std::ifstream map_file; std::ifstream map_file;
map_file.open( file, std::ios::in | std::ios::binary ); map_file.open( file, std::ios::in | std::ios::binary );
uint16_t cols; uint16_t cols;
map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) ); map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
objects.resize( cols ); if(editor) {
objects.resize( cols );
}
mapColumnType *col = nullptr;
for ( uint16_t i = 0; i < cols; i++ ) { for ( uint16_t i = 0; i < cols; i++ ) {
auto &col = objects[i]; if(editor) {
col = &objects[i];
}
for ( int j = 0; j < 16; j++ ) { for ( int j = 0; j < 16; j++ ) {
uint16_t input_number; uint16_t input_number;
uint8_t additional_data = 0; uint8_t additional_data = 0;
@ -105,33 +60,48 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
character = additional_data & 0x0F; character = additional_data & 0x0F;
} }
} }
col[j] = { type, id, character_type, character, if(editor) {
modifier_type, modifier_data }; col->at(j) = { type, id, character_type, character,
modifier_type, modifier_data };
}
bool destructible = false;
if(!editor && modifier_type == DESTRUCTIBLE_ID) {
destructible = true;
}
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock
auto obj = auto obj =
createTerrainBlock( id, static_cast< LandType::Value >( type ), createTerrainBlock( id, static_cast< LandType::Value >( type ),
renderer, i, j, true ); renderer, i, j, destructible, editor );
obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); if(obj != nullptr) {
scene->addObject( obj ); if(editor) {
obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
}
scene->addObject( obj );
}
if ( character ) { if ( character ) {
if ( character == MARIO_ID ) { if ( character == MARIO_ID ) {
scene->addObject( createMario( if(editor) {
static_cast< LandType::Value >( character_type ), scene->addObject( createMario(
renderer, i, j ) ); static_cast< LandType::Value >( character_type ),
mario = scene->getObject(scene->getObjects().size() - 1); renderer, i, j ) );
mario = scene->getObject(scene->getObjects().size() - 1);
} else {
mario->setPos( i * BLOCK_SIZE,
1 - ( 16 - j ) * BLOCK_SIZE );
}
} }
} }
if ( modifier_type ) { if ( editor && modifier_type ) {
auto mod = createTerrainBlock( auto mod = createTerrainBlock(
modifier_type, LandType::OVERWORLD, renderer, i, j, modifier_type, LandType::OVERWORLD, renderer, i, j,
g_translucent_terrain_texture, true ); g_translucent_terrain_texture, false, editor );
mod->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); mod->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
dynamic_cast< MarioBlock * >( mod.get() )->setTerrain( false ); dynamic_cast< MarioBlock * >( mod.get() )->setTerrain( false );
scene->addObject( mod ); scene->addObject( mod );
} }
} }
} }
if ( objects.size() < 18 ) { if ( editor && objects.size() < 18 ) {
objects.resize( 18 ); objects.resize( 18 );
} }
} }

View File

@ -20,14 +20,14 @@ typedef std::tuple< uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t >
typedef std::array< mapObjectType, 16 > mapColumnType; typedef std::array< mapObjectType, 16 > mapColumnType;
void loadMap( std::shared_ptr< SDLPP::Scene > &scene, void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
std::shared_ptr< SDLPP::RectangleRender > mario, std::shared_ptr< SDLPP::RenderObject > mario,
const std::string &file, const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer ); std::shared_ptr< SDLPP::Renderer > &renderer );
void loadMap( std::shared_ptr< SDLPP::Scene > &scene, void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
std::shared_ptr< SDLPP::RenderObject > &mario, std::shared_ptr< SDLPP::RenderObject > &mario,
const std::string &file, const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
std::vector< mapColumnType > &objects ); std::vector< mapColumnType > &objects, bool editor = true );
void saveMap( const std::string &file, std::vector< mapColumnType > &objects ); void saveMap( const std::string &file, std::vector< mapColumnType > &objects );
#endif #endif