123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- #include "abstractfactory.h"
- #include "units/unit.h"
- #include "Cell.h"
- #include <iostream>
- #include <algorithm>
- #include <cassert>
- #include <string>
- #include <QFile>
- #include <QString>
- #include <QTextStream>
- Unit::Unit(QString parameters) {
- QStringList params = parameters.split("|");
- assert(params.size() >= 2);
- race_id_ = params[0];
- unit_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);
- }
- void Unit::loadUnitName(QString unit_folder) {
- QFile file(unit_folder + "unitname.txt");
- file.open(QIODevice::ReadOnly);
- QTextStream in(&file);
- in.setCodec("UTF-8");
- unit_name_ = in.readLine();
- }
- void Unit::loadUnitDescr(QString unit_folder) {
- QFile file(unit_folder + "descr.txt");
- file.open(QIODevice::ReadOnly);
- QTextStream in(&file);
- in.setCodec("UTF-8");
- unit_descr_ = in.readAll();
- }
- void Unit::loadUnitBaseClass(QString unit_folder) {
- QFile file(unit_folder + "baseclass.txt");
- file.open(QIODevice::ReadOnly);
- QTextStream in(&file);
- in.setCodec("UTF-8");
- 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();
- }
- 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");
- file.open(QIODevice::ReadOnly);
- QTextStream in(&file);
- in.setCodec("UTF-8");
- QString line = in.readLine();
- while (!in.atEnd()) {
- parent_specs_.push_back(line);
- line = in.readLine();
- }
- parent_specs_.push_back(line);
- line = in.readLine();
- }
- void Unit::loadUnitUpgradeSpecs(QString unit_folder) {
- QFile file(unit_folder + "nextgrades.txt");
- file.open(QIODevice::ReadOnly);
- QTextStream in(&file);
- in.setCodec("UTF-8");
- QString line = in.readLine();
- while (!in.atEnd()) {
- upgrade_specs_.push_back(line);
- line = in.readLine();
- }
- upgrade_specs_.push_back(line);
- line = in.readLine();
- }
- 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_;
- }
- int Unit::getLevel() {
- return level_;
- }
- int Unit::getHealthPoints() {
- return health_points_;
- }
- int Unit::getAttackRange() {
- return attack_range_;
- }
- int Unit::getActivityPoints(){
- return activity_points_;
- }
- int Unit::getStartingActivityPoints() {
- return starting_activity_points_;
- }
- Cell* Unit::getLocation() {
- return location_;
- }
- void Unit::setLocation(Cell* to) {
- location_ = to;
- }
- int Unit::getMovementSpeed() {
- return movement_speed_;
- }
- int Unit::getAttackCost(){
- return attack_cost_;
- }
- int Unit::getInitiative() {
- return initiative_;
- }
- int Unit::getIntelligence() {
- return intelligence_;
- }
- int Unit::getStrength() {
- return strength_;
- }
- int Unit::getAgility() {
- return agility_;
- }
- int Unit::getAttackPoints(){
- return attack_cost_;
- }
- int Unit::getMagicDefence() {
- return magic_defence_;
- }
- int Unit::getPhysicDefence() {
- return physic_defence_;
- }
- double Unit::getRealX() {
- return real_x_;
- }
- void Unit::setRealX(double x) {
- real_x_ = x;
- }
- double Unit::getRealY() {
- return real_y_;
- }
- void Unit::setRealY(double y) {
- real_y_ = y;
- }
- 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;
- }
- 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 (damage - 2.5 * double(damage) * double(physic_defence_) / 100.0);
- }
- else if (damageType[0] == 'm' || damageType[0] == 'M') {
- return (damage - 2.5 * double(damage) * double(magic_defence_) / 100.0);
- }
- }
- int Unit::lenOfActualPath(Cell* destination) {
- return getLocation()->actualPath(destination).size() - 1;
- }
- bool Unit::canMoveForDistance(int distance) {
- return (activity_points_ * movement_speed_ >= distance);
- }
- bool Unit::canMoveToCell(Cell* destination) {
- return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
- }
- void Unit::moveToCell(Cell* destination) {
- if (!canMoveToCell(destination))
- return; //here could be a gui-message about failed move (x-mark, for example)
- else {
- activity_points_ -= lenOfActualPath(destination)/movement_speed_;
- if (lenOfActualPath(destination) % movement_speed_)
- activity_points_ -= 1;
- setLocation(destination);
- }
- }
- QString Unit::getUnitId() const {
- return unit_id_;
- }
- QString Unit::getUnitName() const {
- return unit_name_;
- }
- QString Unit::getUnitDescr() const {
- return unit_descr_;
- }
- QString Unit::getUnitBaseClassId() const {
- return base_class_id_;
- }
- std::vector<QString> Unit::getUnitTraits() const {
- return {
- QString::number(health_points_),
- QString::number(attack_range_),
- QString::number(activity_points_),
- QString::number(initiative_)
- };
- }
- QImage Unit::getUnitIcon() const {
- return unit_icon_;
- }
|