Refactor to use objects for terrain/mod creation, added coin modifier

This commit is contained in:
2021-07-23 00:08:05 +02:00
parent 79d9f266b4
commit 3be728843a
15 changed files with 1384 additions and 127 deletions
+61 -25
View File
@@ -20,6 +20,7 @@
#include "edit_box.hpp"
#include "editor_visitor.hpp"
#include "tool_box.hpp"
#include "blocks/coinblock.hpp"
#define MAP_WIDTH 24
#define MAP_HEIGHT 16
@@ -86,7 +87,9 @@ struct GlobalVars {
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< MarioBlock > coin_tool;
std::shared_ptr< MarioBlock > generic_tool;
std::shared_ptr< MarioBlock > current_tool;
std::shared_ptr< SDLPP::Texture > translucent_terrain_texture;
std::shared_ptr< SDLPP::Texture > translucent_mario_texture;
std::shared_ptr< SDLPP::Texture > translucent_mod_texture;
@@ -127,11 +130,20 @@ void updateTool() {
case BlockRole::CHARACTER:
break;
}
global_vars.current_tool->setTexture(
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 );
global_vars.current_tool->setHidden(true);
if(tool_id == COIN_MODIFIER_ID) {
global_vars.coin_tool->setPos(global_vars.current_tool->getPos());
global_vars.current_tool = global_vars.coin_tool;
} else {
global_vars.generic_tool->setPos(global_vars.current_tool->getPos());
global_vars.current_tool = global_vars.generic_tool;
global_vars.current_tool->setTexture(
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 );
}
global_vars.current_tool->setHidden(false);
}
void removeMario() {
@@ -415,10 +427,11 @@ void updateWorld() {
dynamic_cast< MarioBlock * >( 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< MarioBlock * >( global_vars.current_tool.get() )
->setType( global_vars.current_world_type );
if(getBlockRole(global_vars.current_tool->getId()) != BlockRole::MODIFIER) {
global_vars.current_tool->setTextureSourceRect( getSourceRectByID(
global_vars.current_tool->getId(), global_vars.current_world_type ) );
global_vars.current_tool->setType( global_vars.current_world_type );
}
}
// TODO add mouse wheel control for modifiers
@@ -569,6 +582,7 @@ void placeTool( SDLPP::Scene &scene ) {
scene.visitCollisions( *global_vars.current_tool, visitor );
auto &obj = getSelectedObject();
if ( visitor.removeBlock() && !visitor.addBlock() ) {
std::cout << "DO NOT ADD" << std::endl;
switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain:
obj.unsetTerrain();
@@ -580,9 +594,10 @@ void placeTool( SDLPP::Scene &scene ) {
break;
}
} else if ( visitor.addBlock() ) {
std::cout << "ADD" << std::endl;
auto renderer = scene.getRendererShared();
int z_index = 1;
std::shared_ptr< SDLPP::RenderObject > new_obj = nullptr;
std::shared_ptr< MarioBlock > new_obj = nullptr;
switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain:
obj.setTerrain( global_vars.current_tool->getId(),
@@ -599,7 +614,7 @@ void placeTool( SDLPP::Scene &scene ) {
obj.setCharacter(MARIO_ID, global_vars.current_world_type);
new_obj = createMario( global_vars.current_world_type, renderer,
global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY() );
global_vars.mouse.edit_box.getY(), true );
// remove mario if exists
removeMario();
global_vars.mario = new_obj;
@@ -608,14 +623,15 @@ void placeTool( SDLPP::Scene &scene ) {
z_index = scene.getObjects().size() - 1;
// TODO BlockRole::Character
} else {
// TODO data
obj.setModifier(global_vars.current_tool->getId(), 0);
obj.setModifier(global_vars.current_tool->getId(), global_vars.current_tool->getData());
new_obj = createTerrainBlock(
obj.getModifierId(),
LandType::OVERWORLD, renderer,
global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(),
global_vars.translucent_mod_texture, false, true );
false, true );
new_obj->setTextureKeepSRC(global_vars.translucent_mod_texture);
new_obj->setData(global_vars.current_tool->getData());
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
// TODO createModifierBlock
dynamic_cast< MarioBlock * >( new_obj.get() )
@@ -697,6 +713,13 @@ void pollEvents( SDLPP::Scene &scene ) {
// store current mouse flags in previous mouse flags
global_vars.mouse.prev_flags = global_vars.mouse.cur_flags;
break;
case SDL_MOUSEWHEEL:
if(event.wheel.y > 0) {
global_vars.current_tool->onScrollUp();
} else if (event.wheel.y < 0) {
global_vars.current_tool->onScrollDown();
}
break;
default:
break;
}
@@ -799,14 +822,14 @@ void populateToolGrid(
case ToolType::CHARACTER:
if ( block == MARIO_ID ) {
tool_store.push_back( createMario(
global_vars.current_world_type, renderer, 0, 0 ) );
global_vars.current_world_type, renderer, 0, 0, true ) );
break;
}
// fall through
case ToolType::MOD:
tool_store.push_back(
createTerrainBlock( block, global_vars.current_world_type,
renderer, g_mod_texture, false, true ) );
renderer, false, true ) );
break;
case ToolType::BLOCK:
tool_store.push_back(
@@ -921,6 +944,12 @@ int main() {
w.setResizable( true );
BLOCK_SIZE = 1.0 / 26;
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
auto font_config = std::make_shared< SDLPP::FontConfiguration >(
font, "#000000", "#282828", 0.05 );
g_text_config = std::make_shared< SDLPP::FontConfiguration >(
font, "#FFFFFF", "#000000", 0.15 );
auto renderer = std::make_shared< SDLPP::Renderer >( w );
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
@@ -951,10 +980,6 @@ int main() {
loadMap( scene, global_vars.mario, "test_binary.bin",
global_vars.objects, true, MAP_WIDTH );
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
auto font_config = std::make_shared< SDLPP::FontConfiguration >(
font, "#000000", "#282828", 0.05 );
// create grids and arrow controls
// map
auto arrows = createArrowControls(
@@ -1051,16 +1076,27 @@ int main() {
global_vars.translucent_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
global_vars.translucent_mario_texture->setAlpha( 100 );
global_vars.current_tool = createTerrainBlock(
global_vars.generic_tool = createTerrainBlock(
possibleBlocks[global_vars.tool.index], global_vars.current_world_type,
renderer, global_vars.translucent_terrain_texture, false );
global_vars.current_tool->removeCollisions();
global_vars.current_tool->addCollision(
renderer, false );
global_vars.generic_tool->setTextureKeepSRC(global_vars.translucent_terrain_texture);
global_vars.generic_tool->removeCollisions();
global_vars.generic_tool->addCollision(
SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
global_vars.current_tool = global_vars.generic_tool;
global_vars.coin_tool = createTerrainBlock(COIN_MODIFIER_ID, LandType::OVERWORLD, renderer, 0, 0, false, true );
global_vars.coin_tool->setData(1);
global_vars.coin_tool->removeCollisions();
global_vars.coin_tool->addCollision(
SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
global_vars.translucent_mod_texture = g_translucent_mod_texture;
dynamic_cast< MarioBlock & >( *global_vars.current_tool ).setTool();
scene->addObject( global_vars.current_tool );
scene->addObject( global_vars.coin_tool );
global_vars.coin_tool->setHidden(true);
scene->moveZTop( global_vars.current_tool );
scene->moveZTop( global_vars.coin_tool );
scene->moveEverything( BLOCK_SIZE, 0 );
FPSmanager gFPS;