Mario: added destructible modifier to editor

This commit is contained in:
zv0n 2021-05-26 00:46:19 +02:00
parent 6fe283bb34
commit c2fb78b0a6
12 changed files with 100 additions and 33 deletions

View File

@ -119,7 +119,6 @@ const std::vector< uint64_t > possibleBlocks = {
MUSHROOM_PLATFORM_TOP_LEFT_ID, MUSHROOM_PLATFORM_TOP_LEFT_ID,
MUSHROOM_PLATFORM_TOP_MIDDLE_ID, MUSHROOM_PLATFORM_TOP_MIDDLE_ID,
MUSHROOM_PLATFORM_TOP_RIGHT_ID, MUSHROOM_PLATFORM_TOP_RIGHT_ID,
TREE_PLATFORM_BARK_ID, TREE_PLATFORM_BARK_ID,
MUSHROOM_PLATFORM_BARK_TOP_ID, MUSHROOM_PLATFORM_BARK_TOP_ID,
TREE_LEAVES_TOP_ID, TREE_LEAVES_TOP_ID,
@ -131,6 +130,7 @@ const std::vector< uint64_t > possibleBlocks = {
CANNON_PEDESTAL_ID, CANNON_PEDESTAL_ID,
CANNON_ID, CANNON_ID,
MARIO_ID, MARIO_ID,
DESTRUCTIBLE_ID,
}; };
const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = { const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = {
@ -192,7 +192,8 @@ const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = {
{ CANNON_TOWER_ID, &CANNON_TOWER_SRC }, { CANNON_TOWER_ID, &CANNON_TOWER_SRC },
{ CANNON_PEDESTAL_ID, &CANNON_PEDESTAL_SRC }, { CANNON_PEDESTAL_ID, &CANNON_PEDESTAL_SRC },
{ CANNON_ID, &CANNON_SRC }, { CANNON_ID, &CANNON_SRC },
{ MARIO_ID, &MARIO_STANDING_SRC } { MARIO_ID, &MARIO_STANDING_SRC },
{ DESTRUCTIBLE_ID, &DESTRUCTIBLE_SRC },
}; };
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
@ -281,6 +282,8 @@ enum BlockRole::Value getBlockRole( uint64_t id ) {
return BlockRole::TERRAIN; return BlockRole::TERRAIN;
if ( id == MARIO_ID ) if ( id == MARIO_ID )
return BlockRole::MARIO; return BlockRole::MARIO;
if ( id < MARIO_ID )
return BlockRole::MODIFIER;
// TODO modifier/character // TODO modifier/character
return BlockRole::MODIFIER; return BlockRole::MODIFIER;
} }

View File

@ -80,6 +80,7 @@ void updateTool() {
target_texture = global_vars.translucent_mario_texture; target_texture = global_vars.translucent_mario_texture;
break; break;
case BlockRole::MODIFIER: case BlockRole::MODIFIER:
target_texture = global_vars.translucent_terrain_texture;
break; break;
case BlockRole::CHARACTER: case BlockRole::CHARACTER:
break; break;
@ -290,7 +291,7 @@ void placeTool( SDLPP::Scene &scene ) {
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
break; break;
case VisitorType::Modifier: case VisitorType::Modifier:
// TODO check if modifier or character if(tool_type == BlockRole::CHARACTER) {
std::get< MapObject::CHARACTER_TYPE >( obj ) = LandType::OVERWORLD; std::get< MapObject::CHARACTER_TYPE >( obj ) = LandType::OVERWORLD;
// TODO character ID // TODO character ID
std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID; std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID;
@ -305,6 +306,21 @@ void placeTool( SDLPP::Scene &scene ) {
global_vars.mario_pos = getSelectedObjectPosition(); global_vars.mario_pos = getSelectedObjectPosition();
new_obj->getCollisions()[0]->setId( EDITOR_CHARACTER_ID ); new_obj->getCollisions()[0]->setId( EDITOR_CHARACTER_ID );
z_index = scene.getObjects().size() - 1; z_index = scene.getObjects().size() - 1;
} else {
// TODO data
std::cout << "Want to set: " << (int)std::get< MapObject::MODIFIER_TYPE >( obj ) << std::endl;
std::get< MapObject::MODIFIER_TYPE >( obj ) = global_vars.current_tool->getId();
std::get< MapObject::MODIFIER_DATA >( obj ) = 0;
std::cout << "SET MODIFIER_TYPE: " << (int)std::get< MapObject::MODIFIER_TYPE >( obj ) << std::endl;
new_obj = createTerrainBlock(
global_vars.current_tool->getId(), LandType::OVERWORLD,
renderer, 1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), global_vars.translucent_terrain_texture, true );
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
// TODO createModifierBlock
dynamic_cast<MarioBlock*>(new_obj.get())->setTerrain(false);
z_index = scene.getObjects().size() - 1;
}
break; break;
default: default:
break; break;
@ -389,6 +405,10 @@ int main() {
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_mario_texture = std::make_shared< SDLPP::Texture >( g_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture =
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",
MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture->setAlpha( 100 );
auto scene = std::make_shared< SDLPP::Scene >( renderer ); auto scene = std::make_shared< SDLPP::Scene >( renderer );
auto bg = std::make_shared< SDLPP::RectangleRender >( auto bg = std::make_shared< SDLPP::RectangleRender >(

View File

@ -3,3 +3,4 @@
std::shared_ptr< SDLPP::Texture > g_terrain_texture{}; std::shared_ptr< SDLPP::Texture > g_terrain_texture{};
std::shared_ptr< SDLPP::Texture > g_mario_texture{}; std::shared_ptr< SDLPP::Texture > g_mario_texture{};
std::shared_ptr< SDLPP::Texture > g_translucent_terrain_texture{};

View File

@ -5,5 +5,6 @@
extern std::shared_ptr< SDLPP::Texture > g_terrain_texture; extern std::shared_ptr< SDLPP::Texture > g_terrain_texture;
extern std::shared_ptr< SDLPP::Texture > g_mario_texture; extern std::shared_ptr< SDLPP::Texture > g_mario_texture;
extern std::shared_ptr< SDLPP::Texture > g_translucent_terrain_texture;
#endif #endif

View File

@ -174,6 +174,10 @@ int main() {
scene->addObject( bg ); scene->addObject( bg );
g_mario_texture = std::make_shared< SDLPP::Texture >( g_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture =
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",
MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture->setAlpha( 100 );
mario = std::make_shared< Mario >(renderer); mario = std::make_shared< Mario >(renderer);
scene->addObject(mario); scene->addObject(mario);

View File

@ -5,6 +5,7 @@
#include "sprites.hpp" #include "sprites.hpp"
#include "blocks.hpp" #include "blocks.hpp"
#include "objectids.hpp" #include "objectids.hpp"
#include "global_vars.hpp"
// 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,
@ -45,6 +46,10 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) { if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) {
collision = true; collision = true;
} }
// TODO definitely make this somehow more streamlined, probably flags
if(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 ),
@ -112,6 +117,12 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene, const std::string &file,
renderer, i, j ) ); renderer, i, j ) );
} }
} }
if ( modifier_type ) {
auto mod = createTerrainBlock( modifier_type, LandType::OVERWORLD, renderer, i, j, g_translucent_terrain_texture, true );
mod->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
dynamic_cast<MarioBlock*>(mod.get())->setTerrain(false);
scene->addObject(mod);
}
} }
} }
if ( objects.size() < 18 ) { if ( objects.size() < 18 ) {
@ -129,24 +140,24 @@ void saveMap( const std::string &file, std::vector< mapColumnType > &objects ) {
for ( auto &col : objects ) { for ( auto &col : objects ) {
for ( int i = 0; i < 16; i++ ) { for ( int i = 0; i < 16; i++ ) {
auto &obj = col[i]; auto &obj = col[i];
uint16_t wide_type = std::get< 0 >( obj ); uint16_t wide_type = std::get< MapObject::TERRAIN_TYPE >( obj );
wide_type = wide_type << 12; wide_type = wide_type << 12;
uint16_t write_num = ( 0x0FFF & std::get< 1 >( obj ) ) | wide_type; uint16_t write_num = ( 0x0FFF & std::get< MapObject::TERRAIN_ID >( obj ) ) | wide_type;
// 3 becuase character type can be 0 (overworld), 4 because modifier // character type can be 0 (overworld), modifier data can be 0
// data can be 0 (breakable) if ( std::get< MapObject::CHARACTER_ID >( obj ) || std::get< MapObject::MODIFIER_TYPE >( obj ) ) {
if ( std::get< 3 >( obj ) || std::get< 4 >( obj ) ) {
write_num |= 0x8000; write_num |= 0x8000;
} }
output_file.write( ( char * )&write_num, output_file.write( ( char * )&write_num,
sizeof( uint16_t ) / sizeof( char ) ); sizeof( uint16_t ) / sizeof( char ) );
uint8_t additional_data = 0; uint8_t additional_data = 0;
if ( std::get< 3 >( obj ) ) { if ( std::get< MapObject::CHARACTER_ID >( obj ) ) {
additional_data |= std::get< 2 >( obj ) << 4; additional_data |= std::get< MapObject::CHARACTER_TYPE >( obj ) << 4;
additional_data |= std::get< 3 >( obj ); additional_data |= std::get< MapObject::CHARACTER_ID >( obj );
} else if ( std::get< 4 >( obj ) ) { } else if ( std::get< MapObject::MODIFIER_TYPE >( obj ) ) {
additional_data |= std::get< 4 >( obj ) << 4; std::cout << "SAVING MODIFIERS!" << std::endl;
additional_data |= std::get< MapObject::MODIFIER_TYPE >( obj ) << 4;
additional_data |= 0x80; additional_data |= 0x80;
additional_data |= std::get< 5 >( obj ); additional_data |= std::get< MapObject::MODIFIER_DATA >( obj );
} }
if ( additional_data ) { if ( additional_data ) {
output_file.write( ( char * )&additional_data, output_file.write( ( char * )&additional_data,

View File

@ -11,12 +11,13 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan
pauseAnimation(); pauseAnimation();
setMovement( 0, 0 ); setMovement( 0, 0 );
setMovementSpeed( 1 ); setMovementSpeed( 1 );
auto bottom_detect = SDLPP::RectColider( 0.3, 1, 0.4, 0, MARIO_FLOOR_DETECT );
bottom_detect.setMinHeight(1);
addCollision(bottom_detect);
addCollision( addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) ); SDLPP::RectColider( 0.05, 0.1, 0.1, 0.89, MARIO_LEFT_SIDE_DETECT ) );
addCollision( addCollision(
SDLPP::RectColider( 0.05, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) ); SDLPP::RectColider( 0.85, 0.1, 0.1, 0.89, MARIO_RIGHT_SIDE_DETECT ) );
addCollision(
SDLPP::RectColider( 0.85, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
addCollision( addCollision(
SDLPP::RectColider( 0.05, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT ) ); SDLPP::RectColider( 0.05, 0, 0.1, 0.1, MARIO_TOP_LEFT_DETECT ) );
addCollision( addCollision(
@ -76,8 +77,16 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_p
} }
// make sure Mario isn't stuck inside a wall // make sure Mario isn't stuck inside a wall
// TODO more readable function names // TODO more readable function names
if ( visitor.isStopped() || !visitor.canGoLeft() || !visitor.canGoRight() ) { if ( visitor.isStopped() ) {
if(visitor.getStopX() > previous_position.getX())
previous_position = {visitor.getStopX(), previous_position.getY()};
setPos( previous_position.getX(), getPos().getY() ); setPos( previous_position.getX(), getPos().getY() );
} else if ( visitor.canGoLeft() != visitor.canGoRight() ) {
SDLPP::Vec2D<double> next_pos = { visitor.getMovementBlockage().getX() + (visitor.canGoLeft() * -1 + visitor.canGoRight() * 1) * BLOCK_SIZE, getPos().getY() };
if((visitor.canGoLeft() && next_pos.getX() > previous_position.getX()) || (visitor.canGoRight() && next_pos.getX() < previous_position.getX())) {
next_pos = previous_position;
}
setPos(next_pos);
} else if (visitor.moveTop() && (jumping && !stop_jump)) { } else if (visitor.moveTop() && (jumping && !stop_jump)) {
auto objPos = visitor.getRightLeftPos(); auto objPos = visitor.getRightLeftPos();
if(objPos.getX() < getPos().getX()) { if(objPos.getX() < getPos().getX()) {

View File

@ -12,8 +12,12 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
onGround = true; onGround = true;
groundY = obj.getPos().getY(); groundY = obj.getPos().getY();
} else if ( from == MARIO_LEFT_SIDE_DETECT ) { } else if ( from == MARIO_LEFT_SIDE_DETECT ) {
if(!left && !right)
movement_blockage = obj.getPos();
left = true; left = true;
} else if (from == MARIO_RIGHT_SIDE_DETECT ) { } else if (from == MARIO_RIGHT_SIDE_DETECT ) {
if(!left && !right)
movement_blockage = obj.getPos();
right = true; right = true;
} else if (from == MARIO_TOP_DETECT) { } else if (from == MARIO_TOP_DETECT) {
top_hit = true; top_hit = true;

View File

@ -55,6 +55,14 @@ public:
return jumping && !top_hit; return jumping && !top_hit;
} }
const SDLPP::Vec2D<double> &getMovementBlockage() {
return movement_blockage;
}
double getStopX() {
return newX;
}
private: private:
bool onGround = false; bool onGround = false;
double groundY = 0; double groundY = 0;
@ -69,6 +77,7 @@ private:
SDLPP::Vec2D<double> rightleftpos; SDLPP::Vec2D<double> rightleftpos;
bool top_left_right = false; bool top_left_right = false;
bool jumping; bool jumping;
SDLPP::Vec2D<double> movement_blockage;
}; };
#endif #endif

View File

@ -63,6 +63,7 @@
#define CANNON_ID 0x703A #define CANNON_ID 0x703A
// modifiers // modifiers
#define DESTRUCTIBLE_ID 0x01
// character IDs // character IDs
#define MARIO_ID 0x0F #define MARIO_ID 0x0F

View File

@ -79,6 +79,8 @@ const SDL_Rect CANNON_TOWER_SRC = {256, 46, 16, 16};
const SDL_Rect CANNON_PEDESTAL_SRC = {256, 29, 16, 16}; const SDL_Rect CANNON_PEDESTAL_SRC = {256, 29, 16, 16};
const SDL_Rect CANNON_SRC = {256, 12, 16, 16}; const SDL_Rect CANNON_SRC = {256, 12, 16, 16};
extern const SDL_Rect DESTRUCTIBLE_SRC = {0, 0, 16, 16};
const SDLPP::Vec2D<uint64_t> OVERWORLD_SHIFT = {0, 0}; const SDLPP::Vec2D<uint64_t> OVERWORLD_SHIFT = {0, 0};
const SDLPP::Vec2D<uint64_t> UNDERWORLD_SHIFT = {274, 0}; const SDLPP::Vec2D<uint64_t> UNDERWORLD_SHIFT = {274, 0};
const SDLPP::Vec2D<uint64_t> WATER_SHIFT = {548, 0}; const SDLPP::Vec2D<uint64_t> WATER_SHIFT = {548, 0};

View File

@ -82,6 +82,8 @@ extern const SDL_Rect TREE_LEAVES_BOTTOM_SRC;
extern const SDL_Rect CANNON_TOWER_SRC; extern const SDL_Rect CANNON_TOWER_SRC;
extern const SDL_Rect CANNON_PEDESTAL_SRC; extern const SDL_Rect CANNON_PEDESTAL_SRC;
extern const SDL_Rect CANNON_SRC; extern const SDL_Rect CANNON_SRC;
//------------------ MODIFIERS ----------------------
extern const SDL_Rect DESTRUCTIBLE_SRC;
extern const SDLPP::Vec2D<uint64_t> OVERWORLD_SHIFT; extern const SDLPP::Vec2D<uint64_t> OVERWORLD_SHIFT;
extern const SDLPP::Vec2D<uint64_t> UNDERWORLD_SHIFT; extern const SDLPP::Vec2D<uint64_t> UNDERWORLD_SHIFT;