Bläddra i källkod

Refactored Unit.h in order to use with gui. Added inheritor c

Ivan Arkhipov 6 år sedan
förälder
incheckning
7fc915e8be

+ 1 - 0
assets/units/races.txt

@@ -0,0 +1 @@
+warcraft

+ 8 - 4
client.pro

@@ -30,20 +30,22 @@ SOURCES += \
     source/gui/uniticon.cpp \
     source/gui/gui.cpp \
     source/gui/recruitmentscene.cpp \
-    source/unit.cpp \
+    source/units/unit.cpp \
     source/player.cpp \
     source/gui/mainmenu.cpp \
     source/gui/guiscenemanager.cpp \
     source/gui/detatchedwidget.cpp \
     source/gui/playervsplayerintro.cpp \
     source/race.cpp \
-    source/racemanager.cpp
+    source/racemanager.cpp \
+    source/units/mage.cpp \
+    source/units/warrior.cpp
 
 HEADERS += \
     include/gui/uniticon.h \
     include/gui/gui.h \
     include/gui/recruitmentscene.h \
-    include/unit.h \
+    include/units/unit.h \
     include/abstractfactory.h \
     include/player.h \
     include/gui/mainmenu.h \
@@ -51,7 +53,9 @@ HEADERS += \
     include/gui/detatchedwidget.h \
     include/gui/playervsplayerintro.h \
     include/race.h \
-    include/racemanager.h
+    include/racemanager.h \
+    include/units/mage.h \
+    include/units/warrior.h
 
 FORMS += \
     include/gui/gui.ui \

+ 5 - 3
include/race.h

@@ -14,14 +14,16 @@ class Race : public QObject
 {
     Q_OBJECT
 public:
-    explicit Race(QObject *parent = nullptr);
+    explicit Race(QString race_name, QObject *parent = nullptr);
 
-    Unit* createUnit(QString race_name);
+    Unit* createUnit(QString unit_name);
 
-    std::vector<QString> getAvailableUnitsList();
+    const std::vector<QString> &getAvailableUnitsList();
 
 private:
     ObjectFactory<Unit, QString> units_factory_;
+    std::vector<QString> available_units_list_;
+
 signals:
 
 public slots:

+ 6 - 1
include/racemanager.h

@@ -19,7 +19,12 @@ public:
     }
 
     Race* getRace(QString race_name);
-    std::vector<QString> getAvailableRacesList();
+    const std::vector<QString>& getAvailableRacesList();
+
+private:
+    std::vector<QString> available_races_;
+    std::map<QString, Race*> races_;
+
 signals:
 
 public slots:

+ 0 - 151
include/unit.h

@@ -1,151 +0,0 @@
-#pragma once
-#include <iostream>
-#include <vector>
-#include "AbstractFactory.h"
-
-class Spell {
-public:
-    int a;
-};
-
-class Cell {
-	//waiting for a realisation
-public:
-	//must be in cell.h
-	bool isEmpty() { 
-		return true;
-	}
-	std::vector <Cell*> actualPath(Cell* destination) { //the shortest existing path from (*this) to (*destination)
-		std::vector <Cell*> path;
-		return path;
-	}
-};
-
-class Unit {
-
-protected:
-	std::vector <Spell> skills_;
-
-private:
-	//personal information
-	int cost_;
-	std::string parent_spec_;
-	std::vector<std::string> upgrade_specs_;
-	double experience_;
-	double level_;
-	std::string race_; //lower case
-
-	//actions and events
-	double initiative_;
-	int activity_points_;
-
-	//movement
-	Cell* location_;
-	int movement_speed_;	//how many cells can move for one activity point
-	double real_x_;
-	double real_y_;
-
-	//attack action
-	double agility_;
-	double attack_range_;
-	double damage_per_hit_;
-	double intelligence_;
-	double strength_;
-	int attack_cost_;     //how many activity points does attack cost
-
-	//durability
-	double health_points_;
-	double magic_defence_;  //less or equal 40
-	double physic_defence_; //less or equal 40
-
-public:
-	Unit() = delete;
-	Unit(std::string path) {
-
-	}
-	virtual ~Unit() = delete;
-
-	int getCost();
-	void setCost(int value);
-
-	std::string getParentSpec();
-	void setParentSpec(std::string specId);
-
-	std::vector<std::string> getUpgradeSpecs();
-	void setUpgradeSpecs(std::vector <std::string> specs);
-
-	double getExperience();
-	void setExperience(double value);
-
-	double getLevel();
-	void setLevel(double value);
-
-	double getHealthPoints();
-	void setHealthPoints(double value);
-
-	double getAttackRange();
-	void setAttackRange(double value);
-
-	int getActivityPoints();
-	void setActivityPoints(int value);
-
-	Cell* getLocation();
-	void setLocation(Cell* to);
-
-	int getMovementSpeed();
-	void setMovementSpeed(int value);
-
-	int getAttackCost();
-	void setAttackCost(int value);
-
-	double getInitiative();
-	void setInitiative(double value);
-
-	double getDamagePerHit();
-	void setDamagePerHit(double value);
-
-	double getIntelligence();
-	void setIntelligence(double value);
-
-	double getStrength();
-	void setStrength(double value);
-
-	double getAgility();
-	void setAgility(double value);
-
-	int getAttackPoints();
-	void setAttackPoints(int value);
-
-	double getMagicDefence();
-	void setMagicDefence(double value);
-
-	double getPhysicDefence();
-	void setPhysicDefence(double value);
-
-	std::string getRace();
-	void setRace(std::string new_race);
-
-	double getRealX();
-	void setRealX(double x);
-
-	double getRealY();
-	void setRealY(double y);
-
-	virtual void calculateDamagePerHit();
-
-	virtual double reduceIncomingDamage(std::string damageType, int value);
-
-	int lenOfActualPath(Cell* destination);
-
-	virtual bool canMoveForDistance(int distance);
-
-	virtual bool canMoveToCell(Cell* destination);
-
-	virtual void moveToCell(Cell* destination);
-
-	virtual	bool canAttackForDistance(int distance) = 0;
-
-	virtual bool canAttackToCell(Cell* destination) = 0;
-
-	virtual bool canAttackUnit(Unit* target) = 0;
-};

+ 14 - 0
include/units/mage.h

@@ -0,0 +1,14 @@
+#ifndef UNITS_MAGE_H
+#define UNITS_MAGE_H
+
+#include "units/unit.h"
+
+#include <QObject>
+
+class Mage : public Unit
+{
+public:
+    explicit Mage(QString unit_name);
+};
+
+#endif // UNITS_MAGE_H

+ 142 - 0
include/units/unit.h

@@ -0,0 +1,142 @@
+#pragma once
+
+#include "AbstractFactory.h"
+
+#include <QObject>
+#include <QString>
+
+#include <iostream>
+#include <vector>
+
+class Spell {
+public:
+    int a;
+};
+
+class Cell {
+	//waiting for a realisation
+public:
+	//must be in cell.h
+	bool isEmpty() { 
+		return true;
+	}
+	std::vector <Cell*> actualPath(Cell* destination) { //the shortest existing path from (*this) to (*destination)
+		std::vector <Cell*> path;
+		return path;
+	}
+};
+
+class Unit : public QObject {
+    Q_OBJECT
+
+public:
+    explicit Unit(QString unit_name);
+
+    virtual ~Unit() {}
+
+    //---------------------------------------------//
+    //---------Basic traits getters section--------//
+    //---------------------------------------------//
+
+	double getExperience();
+	double getLevel();
+    int    getMovementSpeed();
+	double getInitiative();
+	double getDamagePerHit();
+
+	double getIntelligence();
+	double getStrength();
+	double getAgility();
+
+    int    getActivityPoints();
+    int    getAttackPoints();
+    int    getAttackCost();
+    double getAttackRange();
+
+    double getHealthPoints();
+
+    double getMagicDefence();
+	double getPhysicDefence();
+
+    int getCost();
+    void setCost(int value);
+
+    std::string              getParentSpec();
+    std::vector<std::string> getUpgradeSpecs();
+
+    //---------------------------------------------//
+    //------------Unit location section------------//
+    //---------------------------------------------//
+
+    Cell* getLocation();
+    void setLocation(Cell* to);
+
+	double getRealX();
+	void setRealX(double x);
+
+	double getRealY();
+	void setRealY(double y);
+
+
+    //---------------------------------------------//
+    //--------Damage checkers & calculators--------//
+    //---------------------------------------------//
+
+    virtual void calculateDamagePerHit();
+
+    virtual double reduceIncomingDamage(std::string damageType, int value);
+
+    virtual	bool canAttackForDistance(int distance) {}
+
+    virtual bool canAttackToCell(Cell* destination) {}
+
+    virtual bool canAttackUnit(Unit* target) {}
+
+    //---------------------------------------------//
+    //-------Movement checkers & calculators-------//
+    //---------------------------------------------//
+
+
+    int lenOfActualPath(Cell* destination);
+
+    virtual bool canMoveForDistance(int distance);
+
+    virtual bool canMoveToCell(Cell* destination);
+
+    virtual void moveToCell(Cell* destination);
+
+protected:
+    std::vector <Spell> skills_;
+
+    //personal information
+    int cost_;
+    std::string parent_spec_;
+    std::vector<std::string> upgrade_specs_;
+    double experience_;
+    double level_;
+    std::string race_; //lower case
+
+    //actions and events
+    double initiative_;
+    int activity_points_;
+
+    //movement
+    Cell* location_;
+    int movement_speed_;	//how many cells can move for one activity point
+    double real_x_;
+    double real_y_;
+
+    //attack action
+    double agility_;
+    double attack_range_;
+    double damage_per_hit_;
+    double intelligence_;
+    double strength_;
+    int attack_cost_;     //how many activity points does attack cost
+
+    //durability
+    double health_points_;
+    double magic_defence_;  //less or equal 40
+    double physic_defence_; //less or equal 40
+
+};

+ 15 - 0
include/units/warrior.h

@@ -0,0 +1,15 @@
+#ifndef UNITS_WARRIOR_H
+#define UNITS_WARRIOR_H
+
+#include "units/unit.h"
+
+#include <QObject>
+#include <QString>
+
+class Warrior : public Unit
+{
+public:
+    explicit Warrior(QString unit_name);
+};
+
+#endif // UNITS_WARRIOR_H

+ 20 - 1
source/race.cpp

@@ -1,6 +1,25 @@
 #include "race.h"
+#include "units/unit.h"
 
-Race::Race(QObject *parent) : QObject(parent)
+#include <QFile>
+#include <QTextStream>
+#include <QString>
+
+Race::Race(QString race_name, QObject *parent) : QObject(parent)
 {
+    QFile file(":/assets/units/" + race_name + "/units.txt");
+    QTextStream in(&file);
+    QString unit_name = in.readLine();
+
+    //units_factory_.registerClass();
 
+    while (!in.atEnd()) {
+        available_units_list_.push_back(unit_name);
+        race_name = in.readLine();
+    }
 }
+
+
+Unit* createUnit(QString unit_name);
+
+std::vector<QString> getAvailableUnitsList();

+ 31 - 0
source/racemanager.cpp

@@ -1,6 +1,37 @@
 #include "racemanager.h"
+#include "race.h"
+
+#include <QFile>
+#include <QTextStream>
+#include <QDebug>
 
 RaceManager::RaceManager(QObject *parent) : QObject(parent)
 {
+    QFile file(":/assets/units/raceslist.txt");
+    QTextStream in(&file);
+    QString race_name = in.readLine();
+
+    while(!in.atEnd()) {
+        if (races_.count(race_name) != 0) {
+            qWarning() << "Warning! found double occurences of races in raceslist.txt";
+            continue;
+        }
+
+        available_races_.push_back(race_name);
+        races_[race_name] = new Race(race_name);
+        race_name = in.readLine();
+    }
+}
+
+Race* RaceManager::getRace(QString race_name) {
+    if (races_.count(race_name) == 0) {
+        qWarning() << "Error! Not found race with name " << race_name;
+        return nullptr;
+    }
+
+    return races_[race_name];
+}
 
+const std::vector<QString>& RaceManager::getAvailableRacesList() {
+    return available_races_;
 }

+ 7 - 0
source/units/mage.cpp

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

+ 23 - 63
source/unit.cpp → source/units/unit.cpp

@@ -2,67 +2,55 @@
 #include <algorithm>
 #include <cassert>
 #include <string>
-#include "unit.h"
+
 #include "AbstractFactory.h"
+#include "units/unit.h"
+
+Unit::Unit(QString unit_name) {
+
+}
 
 int Unit::getCost(){
 	return cost_;
 }
 
-void Unit::setCost(int value){
-	cost_ = value;
-}
 
 std::string Unit::getParentSpec(){
 	return parent_spec_;
 }
 
-void Unit::setParentSpec(std::string specId){
-	parent_spec_ = specId;
-}
+
 
 std::vector<std::string> Unit::getUpgradeSpecs(){
 	return upgrade_specs_;
 }
 
-void Unit::setUpgradeSpecs(std::vector<std::string> specs){
-	upgrade_specs_ = specs;
-}
+
 
 double Unit::getExperience() {
 	return experience_;
 }
-void Unit::setExperience(double value) {
-	experience_ = value;
-}
+
 
 double Unit::getLevel() {
 	return level_;
-};
-void Unit::setLevel(double value) {
-	level_ = value;
 }
 
+
 double Unit::getHealthPoints() {
 	return health_points_;
-};
-void Unit::setHealthPoints(double value) {
-	health_points_ = value;
 }
 
+
 double Unit::getAttackRange() {
 	return attack_range_;
 }
-void Unit::setAttackRange(double value) {
-	attack_range_ = value;
-}
+
 
 int Unit::getActivityPoints(){
 	return activity_points_;
 }
-void Unit::setActivityPoints(int value){
-	activity_points_ = value;
-}
+
 
 Cell* Unit::getLocation() {
 	return location_;
@@ -74,79 +62,52 @@ void Unit::setLocation(Cell* to) {
 int Unit::getMovementSpeed() {
 	return movement_speed_;
 }
-void Unit::setMovementSpeed(int value) {
-	movement_speed_ = value;
-}
+
 
 int Unit::getAttackCost(){
 	return attack_cost_;
 }
-void Unit::setAttackCost(int value){
-	attack_cost_ = value;
-}
+
 
 double Unit::getInitiative() {
 	return initiative_;
 }
-void Unit::setInitiative(double value) {
-	initiative_ = value;
-}
+
 
 double Unit::getDamagePerHit() {
 	return damage_per_hit_;
 }
-void Unit::setDamagePerHit(double value) {
-	damage_per_hit_ = value;
-}
+
 
 double Unit::getIntelligence() {
 	return intelligence_;
 }
-void Unit::setIntelligence(double value) {
-	intelligence_ = value;
-}
+
 
 double Unit::getStrength() {
 	return strength_;
 }
-void Unit::setStrength(double value) {
-	strength_ = value;
-}
+
 
 double Unit::getAgility() {
 	return agility_;
 }
-void Unit::setAgility(double value) {
-	agility_ = value;
-}
+
 
 int Unit::getAttackPoints(){
 	return attack_cost_;
 }
-void Unit::setAttackPoints(int value){
-	attack_cost_ = value;
-}
+
 
 double Unit::getMagicDefence() {
 	return magic_defence_;
 }
-void Unit::setMagicDefence(double value) {
-	magic_defence_ = value;
-}
+
 
 double Unit::getPhysicDefence() {
 	return physic_defence_;
 }
-void Unit::setPhysicDefence(double value) {
-	physic_defence_ = value;
-}
 
-std::string Unit::getRace() {
-	return race_;
-}
-void Unit::setRace(std::string new_race) {
-	race_ = new_race;
-}
 
 double Unit::getRealX() {
 	return real_x_;
@@ -195,8 +156,7 @@ void Unit::moveToCell(Cell* destination) {
 	if (!canMoveToCell(destination))
 		return;	//here could be a gui-message about failed move (x-mark, for example)
 	else {
-		int decreasedValue = getMovementSpeed() - lenOfActualPath(destination);
-		setMovementSpeed(decreasedValue);
+        movement_speed_ -= lenOfActualPath(destination);
 		setLocation(destination);
 	}
 }

+ 7 - 0
source/units/warrior.cpp

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