diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 293a0e4..297afc6 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -199,6 +199,7 @@ struct Game::GameImpl { std::array number_labels; //Label: 0,1...10 size_t slot_selected = 0; std::array slot_spell; + SpellHolder spell_holder; std::shared_ptr console; std::shared_ptr spellSelect; bool consoleActive = false; @@ -272,47 +273,17 @@ Game::Game() { data->number_labels.at(i).SetFontSize(20); data->number_labels.at(i).SetText(std::to_string(i)); } - Spell& slot0 = data->slot_spell.at(0); - slot0.icon = "effect_fireball"; - slot0.name = "spell_fireball"; - Spell& slot1 = data->slot_spell.at(1); - slot1.icon = "item_weapon_long_knife"; - slot1.name = "weapon_slash_long_knife"; - Spell& slot2 = data->slot_spell.at(2); - slot2.icon = ""; - slot2.name = "spell_create_block"; - slot2.tile = 607; - slot2.type = SpellCursorType::tile; - Spell& slot3 = data->slot_spell.at(3); - slot3.icon = ""; - slot3.name = "spell_create_block"; - slot3.tile = 28; - slot3.type = SpellCursorType::tile; - Spell& slot4 = data->slot_spell.at(4); - slot4.icon = ""; - slot4.name = "spell_create_block"; - slot4.tile = 16; - slot4.type = SpellCursorType::tile; - Spell& slot5 = data->slot_spell.at(5); - slot5.icon = "item_food_potato"; - slot5.name = "spell_spawn_item"; - slot5.item_name = "food_potato"; - Spell& slot6 = data->slot_spell.at(6); - slot6.icon = "item_barrel"; - slot6.name = "spell_spawn_item"; - slot6.item_name = "barrel"; - Spell& slot7 = data->slot_spell.at(7); - slot7.icon = "tree_palm"; //Quite big. As this has no icon - slot7.name = "spell_spawn_item"; - slot7.item_name = "tree_palm"; - Spell& slot8 = data->slot_spell.at(8); - slot8.icon = "cactus_one"; - slot8.name = "spell_spawn_item"; - slot8.item_name = "cactus_full"; - Spell& slot9 = data->slot_spell.at(9); - slot9.icon = "icon_trash_can"; - slot9.name = "spell_clear_block"; - slot9.type = SpellCursorType::tile; + data->spell_holder.init(); + data->slot_spell.at(0) = data->spell_holder.get_spell_by_name("spell_fireball"); + data->slot_spell.at(1) = data->spell_holder.get_spell_by_name("weapon_slash_long_knife"); + data->slot_spell.at(2) = data->spell_holder.get_spell_by_name("spell_create_block:607"); + data->slot_spell.at(3) = data->spell_holder.get_spell_by_name("spell_create_block:28"); + data->slot_spell.at(4) = data->spell_holder.get_spell_by_name("spell_create_block:16"); + data->slot_spell.at(5) = data->spell_holder.get_spell_by_name("spell_spawn_item:food_potato"); + data->slot_spell.at(6) = data->spell_holder.get_spell_by_name("spell_spawn_item:barrel"); + data->slot_spell.at(7) = data->spell_holder.get_spell_by_name("spell_spawn_item:tree_palm"); + data->slot_spell.at(8) = data->spell_holder.get_spell_by_name("spell_spawn_item:cactus_full"); + data->slot_spell.at(9) = data->spell_holder.get_spell_by_name("spell_clear_block"); } Game::~Game() { diff --git a/src/saland/model/spells.cpp b/src/saland/model/spells.cpp index e69de29..8370b1d 100644 --- a/src/saland/model/spells.cpp +++ b/src/saland/model/spells.cpp @@ -0,0 +1,118 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2020 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + +#include "spells.hpp" + +void SpellHolder::add_spell(const Spell &spell) +{ + spells.push_back(spell); + std::string name = spell.name; + if (spell.item_name[0]) + { + name = name + ":" + spell.item_name; + } + if (spell.tile) + { + name = name + ":" + std::to_string(spell.tile); + } + spellIndex[name] = spells.size() - 1; +} + +void SpellHolder::init() +{ + Spell slot0; + slot0.icon = "effect_fireball"; + slot0.name = "spell_fireball"; + Spell slot1; + slot1.icon = "item_weapon_long_knife"; + slot1.name = "weapon_slash_long_knife"; + Spell slot2; + slot2.icon = ""; + slot2.name = "spell_create_block"; + slot2.tile = 607; + slot2.type = SpellCursorType::tile; + Spell slot3; + slot3.icon = ""; + slot3.name = "spell_create_block"; + slot3.tile = 28; + slot3.type = SpellCursorType::tile; + Spell slot4; + slot4.icon = ""; + slot4.name = "spell_create_block"; + slot4.tile = 16; + slot4.type = SpellCursorType::tile; + Spell slot5; + slot5.icon = "item_food_potato"; + slot5.name = "spell_spawn_item"; + slot5.item_name = "food_potato"; + Spell slot6; + slot6.icon = "item_barrel"; + slot6.name = "spell_spawn_item"; + slot6.item_name = "barrel"; + Spell slot7; + slot7.icon = "tree_palm"; // Quite big. As this has no icon + slot7.name = "spell_spawn_item"; + slot7.item_name = "tree_palm"; + Spell slot8; + slot8.icon = "cactus_one"; + slot8.name = "spell_spawn_item"; + slot8.item_name = "cactus_full"; + Spell slot9; + slot9.icon = "icon_trash_can"; + slot9.name = "spell_clear_block"; + slot9.type = SpellCursorType::tile; + add_spell(slot0); + add_spell(slot1); + add_spell(slot2); + add_spell(slot3); + add_spell(slot4); + add_spell(slot5); + add_spell(slot6); + add_spell(slot7); + add_spell(slot8); + add_spell(slot9); +} + +size_t SpellHolder::get_spell_count() const +{ + return spells.size(); +} + +const Spell &SpellHolder::get_spell(size_t index) const +{ + if (index >= spells.size()) + { + return blankSpell; + } + return spells.at(index); +} + +const Spell &SpellHolder::get_spell_by_name(const std::string &spell_name) const +{ + auto it = spellIndex.find(spell_name); + if (it == spellIndex.end()) + { + return blankSpell; + } + return get_spell(it->second); +} \ No newline at end of file diff --git a/src/saland/model/spells.hpp b/src/saland/model/spells.hpp index 4357c12..c7573a2 100644 --- a/src/saland/model/spells.hpp +++ b/src/saland/model/spells.hpp @@ -25,6 +25,8 @@ https://github.com/sago007/saland #define MODEL_SPELLS_HPP #include +#include +#include enum class SpellCursorType { dot = 0, tile=1, shpere = 2 }; @@ -38,9 +40,14 @@ struct Spell { class SpellHolder { std::vector spells; - public: + std::map spellIndex; + Spell blankSpell; + bool initialized = false; +public: + void init(); + void add_spell(const Spell& spell); size_t get_spell_count() const; - const Spell& get_spell(int index) const; + const Spell& get_spell(size_t index) const; const Spell& get_spell_by_name(const std::string&) const; };