GeorgeKolog před 6 roky
rodič
revize
8c4de10e8d

+ 2 - 1
client.pro

@@ -120,7 +120,8 @@ SOURCES +=                                      \
     ui/dialog_form/dialogform.cpp \
     ui/about_us/aboutus.cpp \
     source/effects/selfmove.cpp \
-    source/spells/selfmovespell.cpp
+    source/spells/selfmovespell.cpp \
+    source/spellsmanager.cpp
 
 HEADERS +=                                      \
     include/abstractfactory.h                   \

+ 1 - 0
include/effects/effect.h

@@ -26,6 +26,7 @@ public:
         AfterAction = 2,
         AfterEndTurn = 3
     };
+    Effect() = default;
     explicit Effect(QString parameters);
 
     virtual ~Effect() {}

+ 3 - 1
include/effects/meleedamage.h

@@ -4,11 +4,13 @@
 
 #ifndef GAME_CLIENT_MELEEDAMAGE_H
 #define GAME_CLIENT_MELEEDAMAGE_H
-#pragma once
 #include <cassert>
 #include "effects\effect.h"
 
 class MeleeDamage : public Effect {
+public:
+    MeleeDamage() = default;
+
     void OperateOnCell(Cell* cell);
     void OperateOnUnit(Unit* unit);
     void OperateOnUnitToCell(Unit* who, Cell* where);

+ 3 - 1
include/effects/selfheal.h

@@ -4,11 +4,13 @@
 
 #ifndef GAME_CLIENT_SELFHEAL_H
 #define GAME_CLIENT_SELFHEAL_H
-#pragma once
 #include <cassert>
 #include "effects\effect.h"
 
 class SelfHeal : public Effect {
+public:
+    SelfHeal() = default;
+
     void OperateOnCell(Cell* cell);
     void OperateOnUnit(Unit* unit);
     void OperateOnUnitToCell(Unit* who, Cell* where);

+ 7 - 4
include/effects/selfmove.h

@@ -4,10 +4,13 @@
 #include "effects\effect.h"
 
 class SelfMove : public Effect {
-    void OperateOnUnitAndCell(Unit* who, Cell* where);
-    void OperateOnCell(Cell* cell);
-    void OperateOnUnit(Unit* unit);
-    void Execute(Cell* from, Cell* where, Unit* who, Unit* whom, TypeOfTrigger Type);
+public:
+    explicit SelfMove(QString param) : Effect(param) {}
+
+    void OperateOnUnitToCell(Unit* who, Cell* where) override;
+    void OperateOnCell(Cell* cell) override;
+    void OperateOnUnit(Unit* unit) override;
+    void Execute(Cell* from, Cell* where, Unit* who, Unit* whom, TypeOfTrigger Type) override;
 };
 
 

+ 6 - 2
include/spells/meleedamagespell.h

@@ -4,12 +4,16 @@
 
 #ifndef GAME_CLIENT_MELEEDAMAGE_H
 #define GAME_CLIENT_MELEEDAMAGE_H
-#pragma once
+
 #include <cassert>
 #include "spells\spell.h"
 
 class MeleeDamageSpell : public Spell {
-    void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom);
+public:
+    MeleeDamageSpell(QString param) : Spell(param) {}
+
+    void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom) override;
+    bool canCastToCell(Cell* destination, Cell* from) override;
 };
 
 #endif //GAME_CLIENT_MELEEDAMAGE_H

+ 5 - 1
include/spells/selfhealspell.h

@@ -8,7 +8,11 @@
 #include "spells\spell.h"
 
 class SelfHealSpell : public Spell {
-    void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom);
+public:
+    SelfHealSpell(QString param) : Spell(param) {}
+
+    void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom) override;
+    bool canCastToCell(Cell* destination, Cell* from) override;
 };
 
 #endif //GAME_CLIENT_SELFHEAL_H

+ 5 - 1
include/spells/selfmovespell.h

@@ -3,7 +3,11 @@
 #include "spells\spell.h"
 
 class SelfMoveSpell : public Spell {
-    void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom);
+public:
+    SelfMoveSpell(QString param) : Spell(param) {}
+
+    void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom) override;
+    bool canCastToCell(Cell* destination, Cell* from) override;
 };
 
 #endif // SELFMOVESPELL_H

+ 3 - 2
include/spells/spell.h

@@ -13,12 +13,13 @@
 #include "units/unit.h"
 //#include "AbstractFactory.h"
 #include "effects/effect.h"
+#include <memory>
 
 class Spell : public QObject {
     Q_OBJECT
 
 protected:
-    std :: vector <Effect*> effects_;
+    std :: vector <std::shared_ptr<Effect> > effects_;
 private:
     int distance_;
     bool forCell_;
@@ -33,7 +34,7 @@ public:
     bool getForCell();
     void setForCell(bool value);
 
-    virtual bool canCastToCell(Cell* destination, Cell* from);
+    virtual bool canCastToCell(Cell* destination, Cell* from) = 0;
     bool isNeirbor(Cell* destination, Cell* from);
     virtual void CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom) = 0;
 

+ 12 - 13
include/spellsmanager.h

@@ -1,6 +1,6 @@
 #ifndef SPELLSMANAGER_H
 #define SPELLSMANAGER_H
-/*#include <QObject>
+#include <QObject>
 #include <QString>
 #include <QImage>
 
@@ -14,20 +14,19 @@ class SpellManager : public QObject
 {
     Q_OBJECT
 public:
-    //explicit Race(QString race_name, QObject *parent = nullptr);
-
     std::shared_ptr<Spell> createSpell(QString spell_name);
-
-    const std::vector<QString> &getAvailableUnitsList();
-    const std::vector<std::shared_ptr<Unit>> &getAllUnitsList();
-
+    std::shared_ptr<Effect> createEffect(QString effect_name);
+    static SpellManager& getInstance(){
+        static SpellManager instance;
+        return instance;
+    }
 private:
-    QString race_id_;
-    QString race_name_;
-    QImage race_icon_;
-    QString race_descr_;
-    ObjectFactory<Unit, QString> spells_factory_;
+    SpellManager();
+    SpellManager(const SpellManager&) = delete;
+    SpellManager &operator=(SpellManager&) = delete;
+    ObjectFactory<Spell, QString> spells_factory_;
+    ObjectFactory<Effect, QString> effects_factory_;
     std::vector<QString> available_spells_list_;
     std::vector<std::shared_ptr<Unit>> all_spells_list_;
-}*/
+};
 #endif // SPELLSMANAGER_H

+ 2 - 2
source/effects/selfmove.cpp

@@ -12,11 +12,11 @@ void SelfMove::OperateOnUnit(Unit* unit){
     throw std :: string("CAN'T TOUCH THIS");
 }
 
-void SelfMove::OperateOnUnitAndCell(Unit* unit, Cell* cell){
+void SelfMove::OperateOnUnitToCell(Unit* unit, Cell* cell){
     unit->setLocation(cell);
 }
 
 void SelfMove::Execute(Cell* from, Cell* where, Unit* who, Unit* whom, TypeOfTrigger Type){
     if(Type == 1)
-        OperateOnUnitAndCell(who, where);
+        OperateOnUnitToCell(who, where);
 }

+ 1 - 2
source/spells/meleedamagespell.cpp

@@ -1,7 +1,6 @@
 //
 // Created by IgorBat on 23.04.2018.
 //
-#pragma once
 #include <cassert>
 #include "spells/meleedamagespell.h"
 #include "units/unit.h"
@@ -13,5 +12,5 @@ void MeleeDamageSpell::CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom)
 }
 
 bool MeleeDamageSpell::canCastToCell(Cell* destination, Cell* from){
-    return isNeirbor(Cell* destination, Cell* from);
+    return isNeirbor(destination, from);
 }

+ 1 - 1
source/spells/selfhealspell.cpp

@@ -14,5 +14,5 @@ void SelfHealSpell::CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom){
 }
 
 bool SelfHealSpell::canCastToCell(Cell* destination, Cell* from){
-    return isNeirbor(Cell* destination, Cell* from);
+    return isNeirbor(destination, from);
 }

+ 1 - 1
source/spells/selfmovespell.cpp

@@ -10,5 +10,5 @@ void SelfMoveSpell::CastSpell(Cell* from, Cell* where, Unit* who, Unit* whom){
 }
 
 bool SelfMoveSpell::canCastToCell(Cell* destination, Cell* from){
-    return isNeirbor(Cell* destination, Cell* from);
+    return isNeirbor(destination, from);
 }

+ 5 - 2
source/spells/spell.cpp

@@ -13,8 +13,9 @@
 #include <QFile>
 #include <QString>
 #include <QTextStream>
+#include <spellsmanager.h>
 
-Spell::Spell(QString parameters) {
+Spell::Spell(QString parameters) : QObject(nullptr) {
     QStringList params = parameters.split("\n");
     assert(params.size() >= 1);
     spell_name_ = params[0];
@@ -47,7 +48,9 @@ void Spell::loadSpellTraits(QString spell_folder) {
     assert(params.size() >= 1);
     size_t countEffects = params[0].toInt();
     for (size_t i = 1; i <= countEffects; ++i){
-        effects_.push_back(new Effect(params[i]));
+        auto list = params[i].split('|');
+        auto object_ptr =SpellManager::getInstance().createEffect(list[0]);
+        effects_.push_back(object_ptr);
     }
 }
 

+ 28 - 0
source/spellsmanager.cpp

@@ -0,0 +1,28 @@
+#include <spellsmanager.h>
+#include <spells/meleedamagespell.h>
+#include <spells/selfhealspell.h>
+#include <spells/selfmovespell.h>
+
+#include <effects/effect.h>
+#include <effects/meleedamage.h>
+#include <effects/selfheal.h>
+#include <effects/selfmove.h>
+
+SpellManager::SpellManager(){
+    spells_factory_.addClass<MeleeDamageSpell, QString>("MeleeDamageSpell");
+    spells_factory_.addClass<SelfHealSpell, QString>("SelfHealSpell");
+    spells_factory_.addClass<SelfMoveSpell, QString>("SelfMoveSpell");
+
+    effects_factory_.addClass<SelfMove, QString>("SelfMove");
+    //effects_factory_.addClass<MeleeDamage, QString>("MeleeDamage");
+    //effects_factory_.addClass<SelfHeal, QString>("SelfHeal");
+
+
+}
+std::shared_ptr<Spell> SpellManager::createSpell(QString spell_name){
+    return spells_factory_.createObject(spell_name, QString());
+}
+
+std::shared_ptr<Effect> SpellManager::createEffect(QString effect_name){
+    return effects_factory_.createObject(effect_name, QString());
+}