diff --git a/mario/blocks.cpp b/mario/blocks.cpp index e942c76..2e6a984 100644 --- a/mario/blocks.cpp +++ b/mario/blocks.cpp @@ -80,6 +80,13 @@ void MarioBlock::custom_move(int ticks) { } } +void MarioBlock::setType(LandType::Value type) { + _type = type; +} +LandType::Value MarioBlock::getType() const { + return _type; +} + const std::vector< uint64_t > possibleBlocks = { FLOOR_ID, STEP_ID, @@ -149,6 +156,13 @@ const std::vector< uint64_t > possibleCharacters = { MARIO_ID, }; +const std::vector< LandType::Value > possibleLands = { + LandType::OVERWORLD, + LandType::UNDERWORLD, + LandType::WATER, + LandType::BOWSER +}; + const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = { { FLOOR_ID, &FLOOR_SRC }, { HILL_INCLINE_ID, &HILL_INCLINE_SRC }, diff --git a/mario/blocks.hpp b/mario/blocks.hpp index 422ff87..6602a41 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -4,6 +4,10 @@ #include "../sdlpp/sdlpp_rectrenderer.hpp" #include +struct LandType { + enum Value { OVERWORLD = 0, UNDERWORLD = 1, WATER = 2, BOWSER = 4 }; +}; + class MarioBlock : public SDLPP::RectangleRender { public: MarioBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer, @@ -13,6 +17,8 @@ public: void setTerrain( bool terrain = true ); void bounce(); virtual void custom_move(int ticks) override; + void setType(LandType::Value type); + LandType::Value getType() const; private: bool _tool = false; @@ -22,15 +28,13 @@ private: const int bounce_ticks = 100; int ticks_to_bounce = bounce_ticks; SDLPP::Vec2D og_pos = {}; + LandType::Value _type; }; extern const std::vector< uint64_t > possibleBlocks; extern const std::vector< uint64_t > possibleMods; extern const std::vector< uint64_t > possibleCharacters; - -struct LandType { - enum Value { OVERWORLD = 0, UNDERWORLD = 1, WATER = 2, BOWSER = 4 }; -}; +extern const std::vector< LandType::Value > possibleLands; struct BlockRole { enum Value { diff --git a/mario/editor.cpp b/mario/editor.cpp index 361282f..7e4dd2a 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -36,14 +36,16 @@ #define CHARACTER_RIGHT_ENABLED_FLAG 0x00000200 #define TOOLS_WIDTH 4 -#define CHARACTER_WIDTH 3 -#define MOD_WIDTH 3 +#define CHARACTER_WIDTH 2 +#define MOD_WIDTH 2 +#define OVERWORLD_WIDTH 2 struct ToolType { enum Value { BLOCK, MOD, CHARACTER, + WORLD, }; }; @@ -83,6 +85,7 @@ struct GlobalVars { std::vector< std::shared_ptr< SDLPP::RenderObject > > tool_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > mod_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > character_boxes; + enum LandType::Value current_world_type; std::shared_ptr< SDLPP::RenderObject > current_tool; std::shared_ptr< SDLPP::Texture > translucent_terrain_texture; std::shared_ptr< SDLPP::Texture > translucent_mario_texture; @@ -93,7 +96,6 @@ struct GlobalVars { GlobalVars global_vars; std::mutex destruction_mutex; -enum LandType::Value g_current_world_type = LandType::OVERWORLD; void updateTool() { auto tool_index = global_vars.tool.index; @@ -126,7 +128,7 @@ void updateTool() { break; } global_vars.current_tool->setTexture( - target_texture, getSourceRectByID( tool_id, g_current_world_type ) ); + target_texture, getSourceRectByID( tool_id, global_vars.current_world_type ) ); global_vars.current_tool->setId( tool_id ); global_vars.current_tool->getCollisions()[0]->setId( tool_id ); } @@ -159,7 +161,9 @@ void setToolColor( const std::string &color ) { case ToolType::CHARACTER: multiplier = CHARACTER_WIDTH; store = &global_vars.character_boxes; + break; default: + store = nullptr; break; } auto index = global_vars.tool.index % ( 2 * multiplier ); @@ -390,6 +394,19 @@ bool getFlag( uint64_t flag ) { return global_vars.flags & flag; } +void updateWorld() { + for(auto &block : global_vars.tools) { + block->setTextureSourceRect(getSourceRectByID(block->getId(), global_vars.current_world_type)); + dynamic_cast(block.get())->setType(global_vars.current_world_type); + } + for(auto &block : global_vars.characters) { + block->setTextureSourceRect(getSourceRectByID(block->getId(), global_vars.current_world_type)); + dynamic_cast(block.get())->setType(global_vars.current_world_type); + } + global_vars.current_tool->setTextureSourceRect(getSourceRectByID(global_vars.current_tool->getId(), global_vars.current_world_type)); + dynamic_cast(global_vars.current_tool.get())->setType(global_vars.current_world_type); +} + // TODO add mouse wheel control for modifiers void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) { switch ( key ) { @@ -408,6 +425,22 @@ void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) { case SDLK_s: selectLowerTool(); break; + case SDLK_1: + global_vars.current_world_type = possibleLands[0]; + updateWorld(); + break; + case SDLK_2: + global_vars.current_world_type = possibleLands[1]; + updateWorld(); + break; + case SDLK_3: + global_vars.current_world_type = possibleLands[2]; + updateWorld(); + break; + case SDLK_4: + global_vars.current_world_type = possibleLands[3]; + updateWorld(); + break; case SDLK_r: scene.getRenderer().setRenderColiders( !scene.getRenderer().getRenderColiders() ); @@ -506,6 +539,7 @@ void placeTool( SDLPP::Scene &scene ) { std::lock_guard< std::mutex > lock( destruction_mutex ); ToolVisitor visitor; + visitor.setSourceType(global_vars.current_world_type); auto tool_type = getBlockRole( global_vars.current_tool->getId() ); switch ( tool_type ) { case BlockRole::TERRAIN: @@ -521,7 +555,7 @@ void placeTool( SDLPP::Scene &scene ) { auto &obj = getSelectedObject(); switch ( visitor.getVisitorType() ) { case VisitorType::Terrain: - std::get< MapObject::TERRAIN_TYPE >( obj ) = LandType::OVERWORLD; + std::get< MapObject::TERRAIN_TYPE >( obj ) = global_vars.current_world_type; std::get< MapObject::TERRAIN_ID >( obj ) = 0; break; case VisitorType::Modifier: @@ -540,12 +574,12 @@ void placeTool( SDLPP::Scene &scene ) { std::shared_ptr< SDLPP::RenderObject > new_obj = nullptr; switch ( visitor.getVisitorType() ) { case VisitorType::Terrain: - std::get< MapObject::TERRAIN_TYPE >( obj ) = LandType::OVERWORLD; + std::get< MapObject::TERRAIN_TYPE >( obj ) = global_vars.current_world_type; std::get< MapObject::TERRAIN_ID >( obj ) = global_vars.current_tool->getId(); // TODO why 1 +? new_obj = createTerrainBlock( - global_vars.current_tool->getId(), LandType::OVERWORLD, + global_vars.current_tool->getId(), global_vars.current_world_type, renderer, 1 + global_vars.mouse.edit_box.getX(), global_vars.mouse.edit_box.getY(), true ); new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); @@ -553,11 +587,11 @@ void placeTool( SDLPP::Scene &scene ) { case VisitorType::Modifier: if ( tool_type == BlockRole::MARIO ) { std::get< MapObject::CHARACTER_TYPE >( obj ) = - LandType::OVERWORLD; + global_vars.current_world_type; std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID; std::get< MapObject::MODIFIER_TYPE >( obj ) = 0; std::get< MapObject::MODIFIER_DATA >( obj ) = 0; - new_obj = createMario( LandType::OVERWORLD, renderer, + new_obj = createMario( global_vars.current_world_type, renderer, 1 + global_vars.mouse.edit_box.getX(), global_vars.mouse.edit_box.getY() ); // remove mario if exists @@ -573,7 +607,7 @@ void placeTool( SDLPP::Scene &scene ) { global_vars.current_tool->getId(); std::get< MapObject::MODIFIER_DATA >( obj ) = 0; new_obj = createTerrainBlock( - global_vars.current_tool->getId(), LandType::OVERWORLD, + global_vars.current_tool->getId(), global_vars.current_world_type, renderer, 1 + global_vars.mouse.edit_box.getX(), global_vars.mouse.edit_box.getY(), global_vars.translucent_terrain_texture, true ); @@ -637,12 +671,18 @@ void pollEvents( SDLPP::Scene &scene ) { multiplier = 2 * CHARACTER_WIDTH; max_index = global_vars.characters.size(); cur_page = global_vars.tool.cur_page_characters; + break; + case ToolType::WORLD: + multiplier = 2 * OVERWORLD_WIDTH; default: break; } size_t index = tool_box.getY() * ( multiplier / 2 ) + tool_box.getX(); - if ( index < max_index ) { + if(global_vars.mouse.tool_type == ToolType::WORLD) { + global_vars.current_world_type = possibleLands[index]; + updateWorld(); + } else if ( index < max_index ) { global_vars.tool.type = global_vars.mouse.tool_type; updateToolIndex( cur_page * multiplier + index ); } @@ -738,7 +778,7 @@ void populateToolGrid( ToolType::Value type, const std::vector< uint64_t > &blocks, std::vector< std::shared_ptr< SDLPP::RenderObject > > &tool_store, std::vector< std::shared_ptr< SDLPP::RenderObject > > &tool_box_store, - std::shared_ptr< SDLPP::Scene > &scene ) { + std::shared_ptr< SDLPP::Scene > &scene, const std::string &title, const std::shared_ptr &font_config ) { auto renderer = scene->getRendererShared(); for ( int j = 0; j < count_y; j++ ) { for ( int i = 0; i < count_x; i++ ) { @@ -753,12 +793,14 @@ void populateToolGrid( case ToolType::CHARACTER: if ( block == MARIO_ID ) tool_store.push_back( - createMario( LandType::OVERWORLD, renderer, 0, 0 ) ); + createMario( global_vars.current_world_type, renderer, 0, 0 ) ); else case ToolType::BLOCK: case ToolType::MOD: tool_store.push_back( createTerrainBlock( - block, LandType::OVERWORLD, renderer, false ) ); + block, global_vars.current_world_type, renderer, false ) ); + default: + break; } tool_store.back()->setHidden( true ); tool_store.back()->setPermanent(); @@ -779,6 +821,40 @@ void populateToolGrid( tool_box_store.push_back( tool_box ); } } + auto tool_text = std::make_shared(start_x - BLOCK_SIZE, start_y - BLOCK_SIZE, (count_x + 2) * BLOCK_SIZE, BLOCK_SIZE, renderer, title, font_config, SDLPP_TEXT_CENTER); + tool_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); + tool_text->setPermanent(); + scene->addObject(tool_text); +} + +void populateWorldType( + int count_x, int count_y, float start_x, float start_y, + std::shared_ptr< SDLPP::Scene > &scene, const std::string &title, const std::shared_ptr &font_config ) { + auto renderer = scene->getRendererShared(); + for ( int j = 0; j < count_y; j++ ) { + for ( int i = 0; i < count_x; i++ ) { + auto tool_box = std::make_shared< ToolBox >( i, j, start_x, start_y, + renderer ); + tool_box->setType(ToolType::WORLD); + scene->addObject( tool_box ); + } + } + int tool_index = 0; + for ( auto &type : possibleLands ) { + auto land = createTerrainBlock( BRICK_ID, type, renderer, false ); + auto x = tool_index % count_x; + auto y = tool_index / count_x; + land->setId(EDITOR_WORLD_CHANGE_ID); + land->setPermanent(); + land->setPos( start_x + x * BLOCK_SIZE, + start_y + y * BLOCK_SIZE ); + scene->addObject( land ); + tool_index = ( tool_index + 1 ) % ( 2 * count_x ); + } + auto tool_text = std::make_shared(start_x, start_y - BLOCK_SIZE, count_x * BLOCK_SIZE, BLOCK_SIZE, renderer, title, font_config, SDLPP_TEXT_CENTER); + tool_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); + tool_text->setPermanent(); + scene->addObject(tool_text); } void checkArrowsEnabled( uint64_t cur_page, uint64_t max_page, @@ -846,6 +922,8 @@ int main() { bg->setId( 1 ); scene->addObject( bg ); + global_vars.current_world_type = LandType::OVERWORLD; + // TODO file name loadMap( scene, global_vars.mario, "test_binary.bin", renderer, global_vars.objects ); @@ -875,7 +953,7 @@ int main() { // tools populateToolGrid( TOOLS_WIDTH, 2, ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, ToolType::BLOCK, possibleBlocks, - global_vars.tools, global_vars.tool_boxes, scene ); + global_vars.tools, global_vars.tool_boxes, scene, "TERRAIN", font_config ); arrows = createArrowControls( ( MAP_WIDTH - TOOLS_WIDTH - 1 ) * BLOCK_SIZE, MAP_WIDTH * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_TOOL_ID, @@ -885,30 +963,34 @@ int main() { createGrid( ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, TOOLS_WIDTH, 2, scene ); // mods - populateToolGrid( MOD_WIDTH, 2, 2 * BLOCK_SIZE, BLOCK_SIZE, ToolType::MOD, + populateToolGrid( MOD_WIDTH, 2, 4 * BLOCK_SIZE, BLOCK_SIZE, ToolType::MOD, possibleMods, global_vars.mods, global_vars.mod_boxes, - scene ); + scene, "MODS", font_config ); arrows = createArrowControls( - BLOCK_SIZE, ( MOD_WIDTH + 2 ) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, + 3 * BLOCK_SIZE, ( MOD_WIDTH + 4 ) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_MOD_ID, EDITOR_RIGHT_MOD_ID, scene, font_config ); auto left_mod_arrow = arrows.first; auto right_mod_arrow = arrows.second; - createGrid( 2 * BLOCK_SIZE, BLOCK_SIZE, MOD_WIDTH, 2, scene ); + createGrid( 4 * BLOCK_SIZE, BLOCK_SIZE, MOD_WIDTH, 2, scene ); // characters - populateToolGrid( CHARACTER_WIDTH, 2, ( MOD_WIDTH + 5 ) * BLOCK_SIZE, + populateToolGrid( CHARACTER_WIDTH, 2, ( MOD_WIDTH + 7 ) * BLOCK_SIZE, BLOCK_SIZE, ToolType::CHARACTER, possibleCharacters, global_vars.characters, global_vars.character_boxes, - scene ); + scene, "CHARACTERS", font_config ); arrows = createArrowControls( - ( MOD_WIDTH + 4 ) * BLOCK_SIZE, - ( MOD_WIDTH + 5 + CHARACTER_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, + ( MOD_WIDTH + 6 ) * BLOCK_SIZE, + ( MOD_WIDTH + 7 + CHARACTER_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_CHARACTER_ID, EDITOR_RIGHT_CHARACTER_ID, scene, font_config ); auto left_char_arrow = arrows.first; auto right_char_arrow = arrows.second; - createGrid( ( MOD_WIDTH + 5 ) * BLOCK_SIZE, BLOCK_SIZE, CHARACTER_WIDTH, 2, + createGrid( ( MOD_WIDTH + 7 ) * BLOCK_SIZE, BLOCK_SIZE, CHARACTER_WIDTH, 2, scene ); + // world type + populateWorldType(OVERWORLD_WIDTH, 2, 0, BLOCK_SIZE, scene, "WORLD", font_config); + createGrid( 0, BLOCK_SIZE, OVERWORLD_WIDTH, 2, scene ); + global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH; global_vars.tool.max_page_tools = @@ -936,7 +1018,7 @@ int main() { renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); global_vars.translucent_mario_texture->setAlpha( 100 ); global_vars.current_tool = createTerrainBlock( - possibleBlocks[global_vars.tool.index], LandType::OVERWORLD, renderer, + possibleBlocks[global_vars.tool.index], global_vars.current_world_type, renderer, global_vars.translucent_terrain_texture, false ); global_vars.current_tool->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) ); diff --git a/mario/editor_visitor.cpp b/mario/editor_visitor.cpp index b9a9375..7e68774 100644 --- a/mario/editor_visitor.cpp +++ b/mario/editor_visitor.cpp @@ -1,5 +1,6 @@ #include "editor_visitor.hpp" #include "../sdlpp/sdlpp_renderobject.hpp" +#include "blocks.hpp" #include "objectids.hpp" #include "edit_box.hpp" #include "tool_box.hpp" @@ -91,18 +92,25 @@ bool MouseVisitor::moveCharactersRight( uint64_t flags ) { void ToolVisitor::visit( const SDLPP::RenderObject &obj ) { auto id = obj.getCollisions()[0]->getId(); switch ( id ) { - case EDITOR_TERRAIN_ID: + case EDITOR_TERRAIN_ID: { + const MarioBlock &m_obj = dynamic_cast(obj); remove_block = true; if ( obj.getId() == source_id && + m_obj.getType() == source_type && getVisitorType() == VisitorType::Terrain ) { add_block = false; } - case EDITOR_CHARACTER_ID: + } + break; + case EDITOR_CHARACTER_ID: { + const MarioBlock &m_obj = dynamic_cast(obj); remove_block = true; if ( obj.getId() == source_id && + m_obj.getType() == source_type && getVisitorType() == VisitorType::Modifier ) { add_block = false; } + } default: break; } diff --git a/mario/editor_visitor.hpp b/mario/editor_visitor.hpp index 41a7378..2254823 100644 --- a/mario/editor_visitor.hpp +++ b/mario/editor_visitor.hpp @@ -3,6 +3,7 @@ #include "../sdlpp/sdlpp_visitor.hpp" #include "../sdlpp/sdlpp_geometry.hpp" +#include "blocks.hpp" #include struct VisitorType { @@ -80,6 +81,9 @@ public: virtual uint64_t getVisitorType() override { return _type; } + void setSourceType(LandType::Value type) { + source_type = type; + } bool addBlock(); bool removeBlock(); @@ -88,6 +92,7 @@ private: bool add_block = true; uint64_t source_id = 0; uint64_t _type = 0; + LandType::Value source_type; }; #endif diff --git a/mario/objectids.hpp b/mario/objectids.hpp index 51da459..f61d9f8 100644 --- a/mario/objectids.hpp +++ b/mario/objectids.hpp @@ -91,6 +91,7 @@ #define EDITOR_RIGHT_MOD_ID 0xF00B #define EDITOR_LEFT_CHARACTER_ID 0xF00C #define EDITOR_RIGHT_CHARACTER_ID 0xF00D +#define EDITOR_WORLD_CHANGE_ID 0xF00E #define MOUSE_VISITOR_TYPE 0xE003 #define MARIO_VISITOR_TYPE 0xE004