瀏覽代碼

Completed base realisation of Race and RaceManager classes.

Implemented unit initialisation part.
Needs testing.
Ivan Arkhipov 6 年之前
父節點
當前提交
72d7779280

+ 14 - 14
include/abstractfactory.h

@@ -3,20 +3,20 @@
 #include <string>
 #include <map>
 
-template <class Base, typename IdType>
+template <class Base, typename CreatorParam>
 class AbstractCreator {
 public:
 	virtual ~AbstractCreator() {}
-	virtual Base* create(const IdType &id) const = 0;
+    virtual Base* create(const CreatorParam &id) const = 0;
 };
 
-template <class ObjectClass, class Base, typename IdType>
-class Creator : public AbstractCreator <Base, IdType> {
+template <class ObjectClass, class Base, typename CreatorParam>
+class Creator : public AbstractCreator<Base, CreatorParam> {
 public:
-	Creator(IdType id) {}
+    Creator() {}
 	virtual ~Creator() {}
-	Base* create(const IdType &id) const {
-		return dynamic_cast<Base*>(new ObjectClass(id));
+    Base* create(const CreatorParam &params) const {
+        return dynamic_cast<Base*>(new ObjectClass(params));
 	}
 };
 
@@ -34,14 +34,14 @@ public:
 			delete item.second;
 	}
 
-	template <class C, typename idCreator>
-	void add(const IdType &id) {
-		registerClass(id, new Creator<C, Base, idCreator>(id));
+    template <class C, typename CreatorParam>
+    void addClass(const IdType &id) {
+        registerClass(id, new Creator<C, Base, CreatorParam>());
 	}
 
-	template <typename idCreator>
-	Base* create(const IdType &idClass, const idCreator &id) {
-		return factory_[idClass]->create(id);
+    template <typename CreatorParam>
+    Base* createObject(const IdType &idClass, const CreatorParam &parameters) {
+        return factory_[idClass]->create(parameters);
 	}
 
 protected:
@@ -52,4 +52,4 @@ protected:
 		else
 			delete ConcreteFabric;
 	}
-};
+};

+ 1 - 0
include/race.h

@@ -21,6 +21,7 @@ public:
     const std::vector<QString> &getAvailableUnitsList();
 
 private:
+    QString race_name_;
     ObjectFactory<Unit, QString> units_factory_;
     std::vector<QString> available_units_list_;
 

+ 1 - 1
include/units/mage.h

@@ -8,7 +8,7 @@
 class Mage : public Unit
 {
 public:
-    explicit Mage(QString unit_name);
+    explicit Mage(QString parameters);
 };
 
 #endif // UNITS_MAGE_H

+ 39 - 5
include/units/unit.h

@@ -4,6 +4,7 @@
 
 #include <QObject>
 #include <QString>
+#include <QImage>
 
 #include <iostream>
 #include <vector>
@@ -30,7 +31,7 @@ class Unit : public QObject {
     Q_OBJECT
 
 public:
-    explicit Unit(QString unit_name);
+    explicit Unit(QString parameters);
 
     virtual ~Unit() {}
 
@@ -61,8 +62,8 @@ public:
     int getCost();
     void setCost(int value);
 
-    std::string              getParentSpec();
-    std::vector<std::string> getUpgradeSpecs();
+    std::vector<QString> getParentSpecs();
+    std::vector<QString> getUpgradeSpecs();
 
     //---------------------------------------------//
     //------------Unit location section------------//
@@ -105,13 +106,38 @@ public:
 
     virtual void moveToCell(Cell* destination);
 
+    //---------------------------------------------//
+    //----------------GUI section------------------//
+    //---------------------------------------------//
+
+    QString getUnitName();
+    QString getUnitDescr();
+    QString getUnitBaseClassId();
+    std::vector<QString> getUnitTraits();
+    QImage getUnitIcon();
+
+    //---------------------------------------------//
+    //-----------Parameters load section-----------//
+    //---------------------------------------------//
+
+private:
+    void loadUnitName(QString unit_folder);
+    void loadUnitDescr(QString unit_folder);
+    void loadUnitBaseClass(QString unit_folder);
+    void loadUnitTraits(QString unit_folder);
+    void loadUnitIcon(QString unit_folder);
+    void loadUnitPrevSpecs(QString unit_folder);
+    void loadUnitUpgradeSpecs(QString unit_folder);
+
 protected:
     std::vector <Spell> skills_;
 
     //personal information
     int cost_;
-    std::string parent_spec_;
-    std::vector<std::string> upgrade_specs_;
+
+    std::vector<QString> parent_specs_;
+    std::vector<QString> upgrade_specs_;
+
     double experience_;
     double level_;
     std::string race_; //lower case
@@ -139,4 +165,12 @@ protected:
     double magic_defence_;  //less or equal 40
     double physic_defence_; //less or equal 40
 
+    // GUI values
+    QString race_id_;
+    QString unit_id_;
+
+    QString unit_name_;
+    QString unit_descr_;
+    QString base_class_id_;
+    QImage unit_icon_;
 };

+ 1 - 1
include/units/warrior.h

@@ -9,7 +9,7 @@
 class Warrior : public Unit
 {
 public:
-    explicit Warrior(QString unit_name);
+    explicit Warrior(QString parameters);
 };
 
 #endif // UNITS_WARRIOR_H

+ 1 - 6
source/gui/guiscenemanager.cpp

@@ -16,12 +16,7 @@ GuiSceneManager::GuiSceneManager(QObject *parent) : QObject(parent) {
     changeScene("main_menu");
 }
 
-GuiSceneManager::~GuiSceneManager() {
-    delete gui_;
-    for (auto scene : scenes_) {
-        delete scene.second;
-    }
-}
+GuiSceneManager::~GuiSceneManager() {}
 
 
 bool GuiSceneManager::registerScene(QString scene_name, QWidget* name) {

+ 2 - 1
source/main.cpp

@@ -8,6 +8,7 @@ int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
 
-    GuiSceneManager::getInstance().changeScene("main_menu");
+    GuiSceneManager::getInstance();
+
     return a.exec();
 }

+ 22 - 3
source/race.cpp

@@ -1,17 +1,26 @@
 #include "race.h"
 #include "units/unit.h"
+#include "units/warrior.h"
+#include "units/mage.h"
 
 #include <QFile>
 #include <QTextStream>
 #include <QString>
 
+#include <cassert>
+#include <vector>
+#include <algorithm>
+
 Race::Race(QString race_name, QObject *parent) : QObject(parent)
 {
+    race_name_ = race_name;
+
     QFile file(":/assets/units/" + race_name + "/units.txt");
     QTextStream in(&file);
     QString unit_name = in.readLine();
 
-    //units_factory_.registerClass();
+    units_factory_.addClass<Warrior, QString>("Warrior");
+    units_factory_.addClass<Warrior, QString>("Mage");
 
     while (!in.atEnd()) {
         available_units_list_.push_back(unit_name);
@@ -20,6 +29,16 @@ Race::Race(QString race_name, QObject *parent) : QObject(parent)
 }
 
 
-Unit* createUnit(QString unit_name);
+Unit* Race::createUnit(QString unit_name) {
+    assert(std::count(available_units_list_.begin(), available_units_list_.end(), unit_name) != 0);
+
+    QFile file(":/asserts/units/" + race_name_ + "/" + unit_name + "/baseclass.txt");
+    QTextStream in(&file);
+    QString class_id = in.readLine();
 
-std::vector<QString> getAvailableUnitsList();
+    return units_factory_.createObject<QString>(class_id, race_name_ + "|" + unit_name);
+}
+
+const std::vector<QString>& Race::getAvailableUnitsList() {
+    return available_units_list_;
+}

+ 1 - 1
source/units/mage.cpp

@@ -1,7 +1,7 @@
 #include "units/mage.h"
 #include "units/unit.h"
 
-Mage::Mage(QString unit_name) : Unit(unit_name)
+Mage::Mage(QString parameters) : Unit(parameters)
 {
 
 }

+ 101 - 24
source/units/unit.cpp

@@ -1,60 +1,120 @@
+#include "AbstractFactory.h"
+#include "units/unit.h"
+
 #include <iostream>
 #include <algorithm>
 #include <cassert>
 #include <string>
 
-#include "AbstractFactory.h"
-#include "units/unit.h"
+#include <QFile>
+#include <QString>
+#include <QTextStream>
+
+Unit::Unit(QString parameters) {
+    QStringList params = parameters.split("|");
+
+    assert(params.size() >= 2);
 
-Unit::Unit(QString unit_name) {
+    unit_id_ = params[0];
+    race_id_ = params[1];
 
+    QString unit_folder = ":/assets/units/" + race_id_ + "/" + unit_id_ + "/";
+
+    loadUnitName(unit_folder);
+    loadUnitDescr(unit_folder);
+    loadUnitBaseClass(unit_folder);
+    loadUnitTraits(unit_folder);
+    loadUnitIcon(unit_folder);
+    loadUnitPrevSpecs(unit_folder);
+    loadUnitUpgradeSpecs(unit_folder);
 }
 
-int Unit::getCost(){
-	return cost_;
+void Unit::loadUnitName(QString unit_folder) {
+    QFile file(unit_folder + "unitname.txt");
+    QTextStream in(&file);
+    unit_name_ = in.readLine();
+}
+
+void Unit::loadUnitDescr(QString unit_folder) {
+    QFile file(unit_folder + "descr.txt");
+    QTextStream in(&file);
+    unit_descr_ = in.readAll();
+}
+
+void Unit::loadUnitBaseClass(QString unit_folder) {
+    QFile file(unit_folder + "baseclass.txt");
+    QTextStream in(&file);
+    base_class_id_ = in.readLine();
 }
 
+void Unit::loadUnitTraits(QString unit_folder) {
+   // TO BE DONE!!!
+    health_points_ = rand();
+    activity_points_ = rand();
+    attack_cost_ = rand();
+    attack_range_ = rand();
+}
 
-std::string Unit::getParentSpec(){
-	return parent_spec_;
+void Unit::loadUnitIcon(QString unit_folder) {
+    unit_icon_.load(unit_folder + "icon.png");
 }
 
+void Unit::loadUnitPrevSpecs(QString unit_folder) {
+    QFile file(unit_folder + "prevgrades.txt");
+    QTextStream in(&file);
+    QString line = in.readLine();
+    while (in.atEnd()) {
+        parent_specs_.push_back(line);
+        line = in.readLine();
+    }
+}
 
+void Unit::loadUnitUpgradeSpecs(QString unit_folder) {
+    QFile file(unit_folder + "nextgrades.txt");
+    QTextStream in(&file);
+    QString line = in.readLine();
+    while (in.atEnd()) {
+        upgrade_specs_.push_back(line);
+        line = in.readLine();
+    }
+}
 
-std::vector<std::string> Unit::getUpgradeSpecs(){
-	return upgrade_specs_;
+int Unit::getCost(){
+	return cost_;
 }
 
+std::vector<QString> Unit::getParentSpecs(){
+    return parent_specs_;
+}
 
+std::vector<QString> Unit::getUpgradeSpecs(){
+	return upgrade_specs_;
+}
 
 double Unit::getExperience() {
 	return experience_;
 }
 
-
 double Unit::getLevel() {
 	return level_;
 }
 
-
 double Unit::getHealthPoints() {
 	return health_points_;
 }
 
-
 double Unit::getAttackRange() {
 	return attack_range_;
 }
 
-
 int Unit::getActivityPoints(){
 	return activity_points_;
 }
 
-
 Cell* Unit::getLocation() {
 	return location_;
 }
+
 void Unit::setLocation(Cell* to) {
 	location_ = to;
 }
@@ -63,55 +123,46 @@ int Unit::getMovementSpeed() {
 	return movement_speed_;
 }
 
-
 int Unit::getAttackCost(){
 	return attack_cost_;
 }
 
-
 double Unit::getInitiative() {
 	return initiative_;
 }
 
-
 double Unit::getDamagePerHit() {
 	return damage_per_hit_;
 }
 
-
 double Unit::getIntelligence() {
 	return intelligence_;
 }
 
-
 double Unit::getStrength() {
 	return strength_;
 }
 
-
 double Unit::getAgility() {
 	return agility_;
 }
 
-
 int Unit::getAttackPoints(){
 	return attack_cost_;
 }
 
-
 double Unit::getMagicDefence() {
 	return magic_defence_;
 }
 
-
 double Unit::getPhysicDefence() {
 	return physic_defence_;
 }
 
-
 double Unit::getRealX() {
 	return real_x_;
 }
+
 void Unit::setRealX(double x) {
 	real_x_ = x;
 }
@@ -119,6 +170,7 @@ void Unit::setRealX(double x) {
 double Unit::getRealY() {
 	return real_y_;
 }
+
 void Unit::setRealY(double y) {
 	real_y_ = y;
 }
@@ -160,3 +212,28 @@ void Unit::moveToCell(Cell* destination) {
 		setLocation(destination);
 	}
 }
+
+QString Unit::getUnitName() {
+    return unit_name_;
+}
+
+QString Unit::getUnitDescr() {
+    return unit_descr_;
+}
+
+QString Unit::getUnitBaseClassId() {
+    return base_class_id_;
+}
+
+std::vector<QString> Unit::getUnitTraits() {
+    return {
+            QString::number(health_points_),
+            QString::number(attack_range_),
+            QString::number(activity_points_),
+            QString::number(initiative_)
+    };
+}
+
+QImage Unit::getUnitIcon() {
+    return unit_icon_;
+}

+ 1 - 1
source/units/warrior.cpp

@@ -1,7 +1,7 @@
 #include "units/warrior.h"
 #include "units/unit.h"
 
-Warrior::Warrior(QString unit_name) : Unit(unit_name)
+Warrior::Warrior(QString parameters) : Unit(parameters)
 {
 
 }