#38 [UNIT] => [DEV]

Merged
Endevir merged 1 commits from GooseHouse/unit into GooseHouse/dev 6 years ago
7 changed files with 120 additions and 53 deletions
  1. 4 2
      client.pro
  2. 1 1
      include/Cell.h
  3. 29 27
      include/units/unit.h
  4. 1 0
      include/units/warrior.h
  5. 4 3
      source/Cell.cpp
  6. 42 20
      source/units/unit.cpp
  7. 39 0
      source/units/warrior.cpp

+ 4 - 2
client.pro

@@ -43,7 +43,8 @@ SOURCES += \
     source/gui/scene.cpp \
     source/gui/raceicon.cpp \
     source/playermanager.cpp \
-    source/gui/prebattlescene.cpp
+    source/gui/prebattlescene.cpp \
+    source/Cell.cpp
 
 HEADERS += \
     include/gui/uniticon.h \
@@ -63,7 +64,8 @@ HEADERS += \
     include/gui/scene.h \
     include/gui/raceicon.h \
     include/playermanager.h \
-    include/gui/prebattlescene.h
+    include/gui/prebattlescene.h \
+    include/Cell.h
 
 FORMS += \
     include/gui/gui.ui \

+ 1 - 1
include/Cell.h

@@ -7,7 +7,7 @@
  * Melee - без учёта препятствий
  * Range - с учётом препятствий
  * Шаблон - строка. При такой реализации можно добавить новый вид атаки
- * Необходимо в Unit добавить метод bool isCharacter() - является ли данный юнит персонажем
+ * OK! - Необходимо в Unit добавить метод bool isCharacter() - является ли данный юнит персонажем
  */
 class Unit;
 /*

+ 29 - 27
include/units/unit.h

@@ -12,6 +12,8 @@ public:
     int a;
 };
 
+class Cell;
+
 class Unit : public QObject {
     Q_OBJECT
 
@@ -25,28 +27,28 @@ public:
     //---------------------------------------------//
 
 	double getExperience();
-	double getLevel();
-    int    getMovementSpeed();
-	double getInitiative();
-	double getDamagePerHit();
+    int getLevel();
+    int getMovementSpeed();
+    int getInitiative();
 
-	double getIntelligence();
-	double getStrength();
-	double getAgility();
+    int getIntelligence();
+    int getStrength();
+    int getAgility();
 
-    int    getActivityPoints();
-    int    getAttackPoints();
-    int    getAttackCost();
-    double getAttackRange();
-    int    getStartingActivityPoints();
+    int getActivityPoints();
+    int getAttackPoints();
+    int getAttackCost();
+    int getAttackRange();
+    int getStartingActivityPoints();
 
-    double getHealthPoints();
+    int getHealthPoints();
 
-    double getMagicDefence();
-	double getPhysicDefence();
+    int getMagicDefence();
+    int getPhysicDefence();
 
     int getCost();
     void setCost(int value);
+    virtual bool isCharacter();
 
     std::vector<QString> getParentSpecs();
     std::vector<QString> getUpgradeSpecs();
@@ -69,9 +71,7 @@ public:
     //--------Damage checkers & calculators--------//
     //---------------------------------------------//
 
-    virtual void calculateDamagePerHit();
-
-    virtual double reduceIncomingDamage(std::string damageType, int value);
+    virtual int reduceIncomingDamage(std::string damageType, int value);
 
     virtual	bool canAttackForDistance(std::string, int) {return false;}
 
@@ -79,6 +79,8 @@ public:
 
     virtual bool canAttackUnit(Unit* ) {return false;}
 
+
+
     //---------------------------------------------//
     //-------Movement checkers & calculators-------//
     //---------------------------------------------//
@@ -92,6 +94,7 @@ public:
 
     virtual void moveToCell(Cell* destination);
 
+    virtual int theSameNear();
 
     //---------------------------------------------//
     //----------------GUI section------------------//
@@ -134,7 +137,7 @@ protected:
     std::vector<QString> upgrade_specs_;
 
     double experience_;
-    double level_;
+    int level_;
     std::string race_; //lower case
 
     //actions and events
@@ -148,18 +151,17 @@ protected:
     double real_y_;
 
     //attack action
-    double agility_;
-    double attack_range_;
-    double damage_per_hit_;
-    double intelligence_;
-    double strength_;
+    int agility_;
+    int attack_range_;
+    int intelligence_;
+    int strength_;
     int attack_cost_;     //how many activity points does attack cost
     int starting_activity_points_;
 
     //durability
-    double health_points_;
-    double magic_defence_;  //less or equal 40
-    double physic_defence_; //less or equal 40
+    int health_points_;
+    int magic_defence_;  //less or equal 40
+    int physic_defence_; //less or equal 40
 
     // GUI values
     QString race_id_;

+ 1 - 0
include/units/warrior.h

@@ -10,6 +10,7 @@ class Warrior : public Unit
 {
 public:
     explicit Warrior(QString parameters);
+    int reduceIncomingDamage(std::string damageType, int damage) override;
 };
 
 #endif // UNITS_WARRIOR_H

+ 4 - 3
source/Cell.cpp

@@ -2,7 +2,7 @@
  * I love Qt
  */
 #include "Cell.h"
-#include "unit.h"
+#include "units/unit.h"
 #include <string>
 #include <queue>
 #include <vector>
@@ -203,7 +203,7 @@ void Cell::clearTable_() {
 }
 
 void Cell::recalcAttackable_(Cell * Now,bool isReached){
-    if(Now == nullptr)return;
+   /* if(Now == nullptr)return;
     if (!isEmpty() && !Now->isEmpty() &&
         getCharacter()->canAttackForDistance("Melee", Now->getdistance_barrier())
             ) {
@@ -213,7 +213,8 @@ void Cell::recalcAttackable_(Cell * Now,bool isReached){
         getCharacter()->canAttackForDistance("Range", Now->getdistance_barrier())
             ) {
         Now->setisRangeAttackAble(true);
-    }
+    }*/
+    return;
 }
 
 void Cell::recalcMoveable_(Cell * Now, bool isReached){

+ 42 - 20
source/units/unit.cpp

@@ -111,15 +111,15 @@ double Unit::getExperience() {
 	return experience_;
 }
 
-double Unit::getLevel() {
+int Unit::getLevel() {
 	return level_;
 }
 
-double Unit::getHealthPoints() {
+int Unit::getHealthPoints() {
 	return health_points_;
 }
 
-double Unit::getAttackRange() {
+int Unit::getAttackRange() {
 	return attack_range_;
 }
 
@@ -147,23 +147,19 @@ int Unit::getAttackCost(){
 	return attack_cost_;
 }
 
-double Unit::getInitiative() {
+int Unit::getInitiative() {
 	return initiative_;
 }
 
-double Unit::getDamagePerHit() {
-	return damage_per_hit_;
-}
-
-double Unit::getIntelligence() {
+int Unit::getIntelligence() {
 	return intelligence_;
 }
 
-double Unit::getStrength() {
+int Unit::getStrength() {
 	return strength_;
 }
 
-double Unit::getAgility() {
+int Unit::getAgility() {
 	return agility_;
 }
 
@@ -171,11 +167,11 @@ int Unit::getAttackPoints(){
 	return attack_cost_;
 }
 
-double Unit::getMagicDefence() {
+int Unit::getMagicDefence() {
 	return magic_defence_;
 }
 
-double Unit::getPhysicDefence() {
+int Unit::getPhysicDefence() {
 	return physic_defence_;
 }
 
@@ -195,25 +191,49 @@ void Unit::setRealY(double y) {
 	real_y_ = y;
 }
 
-void Unit::calculateDamagePerHit() {
-	damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
+bool Unit::isCharacter(){
+    return true;
+}
+
+int Unit::theSameNear(){
+    short near_qnt = 0;
+    if (this->location_->getleftUp()->getCharacter()->base_class_id_ == this->base_class_id_){
+        near_qnt++;
+    }
+    if (this->location_->getleft()->getCharacter()->base_class_id_ == this->base_class_id_){
+        near_qnt++;
+    }
+    if (this->location_->getleftDown()->getCharacter()->base_class_id_ == this->base_class_id_){
+        near_qnt++;
+    }
+    if (this->location_->getrightUp()->getCharacter()->base_class_id_ == this->base_class_id_){
+        near_qnt++;
+    }
+    if (this->location_->getright()->getCharacter()->base_class_id_ == this->base_class_id_){
+        near_qnt++;
+    }
+    if (this->location_->getrightDown()->getCharacter()->base_class_id_ == this->base_class_id_){
+        near_qnt++;
+    }
+
+    return near_qnt;
 }
 
-double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
+int Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
 	assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
         (damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M'));
 	assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
 	assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
 	if (damageType[0] == 'p' || damageType[0] == 'P') {
-		return (1 - 2.5 * physic_defence_ / 100) * damage;
+        return (damage - 2.5 * double(damage) * double(physic_defence_) / 100.0);
 	}
 	else if (damageType[0] == 'm' || damageType[0] == 'M') {
-		return (1 - 2.5 * magic_defence_ / 100) * damage;
+        return (damage - 2.5 * double(damage) * double(magic_defence_) / 100.0);
 	}
 }
 
 int Unit::lenOfActualPath(Cell* destination) {
-	return getLocation()->actualPath(destination).size();
+    return getLocation()->actualPath(destination).size() - 1;
 }
 
 bool Unit::canMoveForDistance(int distance) {
@@ -228,7 +248,9 @@ void Unit::moveToCell(Cell* destination) {
 	if (!canMoveToCell(destination))
 		return;	//here could be a gui-message about failed move (x-mark, for example)
 	else {
-        movement_speed_ -= lenOfActualPath(destination);
+        activity_points_ -= lenOfActualPath(destination)/movement_speed_;
+        if (lenOfActualPath(destination) % movement_speed_)
+            activity_points_ -= 1;
 		setLocation(destination);
 	}
 }

+ 39 - 0
source/units/warrior.cpp

@@ -1,7 +1,46 @@
 #include "units/warrior.h"
 #include "units/unit.h"
+#include <cassert>
 
 Warrior::Warrior(QString parameters) : Unit(parameters)
 {
 
 }
+
+int Warrior::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
+    assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
+        (damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M'));
+    assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
+    assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
+    int near_same = this->theSameNear();
+
+
+    int active_defence;
+    if (damageType[0] == 'p' || damageType[0] == 'P') {
+        active_defence = physic_defence_;
+    }
+    else if (damageType[0] == 'm' || damageType[0] == 'M') {
+        active_defence = magic_defence_;
+    }
+
+    //return (damage - 2.5 * damage * active_defence / 100);
+    int reduced_damage;
+    switch (near_same) {
+    case 0:
+        reduced_damage = damage - 2.5 * double(damage) * double(active_defence) / 100.0;
+        break;
+    case 1:
+        reduced_damage = damage - 2.9 * double(damage) * double(active_defence) / 100.0;
+        break;
+    case 2:
+        reduced_damage = damage - 3.3 * double(damage) * double(active_defence) / 100.0;
+        break;
+    case 3:
+        reduced_damage = damage - 3.7 * double(damage) * double(active_defence) / 100.0;
+        break;
+    default:
+        reduced_damage = damage - 4.1 * double(damage) * double(active_defence) / 100.0;
+        break;
+    }
+    return reduced_damage;
+}