Prechádzať zdrojové kódy

Cell.h is Ready. Cell.cpp is partly realised

GeorgeKolog 6 rokov pred
rodič
commit
6e4f4c8086
2 zmenil súbory, kde vykonal 86 pridanie a 32 odobranie
  1. 9 2
      include/Cell.h
  2. 77 30
      source/Cell.cpp

+ 9 - 2
include/Cell.h

@@ -63,10 +63,11 @@ private:
      * Локальные методы и переменные
      */
 	bool AddedToQuery_;
+	int col_, row_;
 	void clearTable_();
 	void clearCell_();
-	void recalcAttackable_();
-	void recalcMoveable_();
+	void recalcAttackable_(Cell*, bool);
+	void recalcMoveable_(Cell*, bool);
 	/*
 	 * BFS Moveable Cells And Find Unmoveable Cells
 	 */
@@ -137,4 +138,10 @@ public:
 	 * Если пути нету, то вернется пустой список
 	 */
 	std::vector <Cell*> actualPath(Cell*);
+	/*
+	 * Отвечает за запрос, куда попадет шар, если ее направить в клетку-параметр
+	 * Выполнится только с лучае, если эта следующая клетка существует
+	 */
+	Cell* getRealShootTarget(Cell*);
+	void print();
 };

+ 77 - 30
source/Cell.cpp

@@ -5,23 +5,18 @@
 //#include "unit.h"
 #include <queue>
 #include <vector>
+#include <map>
+#include <iostream>
 
 class Unit{
+private:
 public:
     bool CanMoveForDistance(int distance){
         return true;
     }
-    /*
-    template<std::string AttackType>
-    bool CanAttackForDistance(int distance){
+    bool CanAttackForDistance(std::string AttackType, int distance){
         return true;
     }
-    bool CanAttackForDistance<"Melee">(int distance){
-        return true;
-    }
-    bool CanAttackForDistance<"Range">(int distance){
-        return true;
-    }*/
 };
 
 class EffectsForCell{
@@ -35,6 +30,7 @@ Cell::Cell(Unit* character) {
 	character_ = character;
 	clearCell_();
 	AddedToQuery_ = true;
+	col_ = row_ = 0;
 }
 
 Cell * Cell::getleftUp() {
@@ -113,7 +109,7 @@ void Cell::setisRangeAttackAble(bool isRangeAttackAble){
 }
 
 bool Cell::isEmpty() {
-	return character_ == NULL;
+	return character_ == nullptr;
 }
 
 void Cell::recalculateAllEffectsList(){
@@ -123,6 +119,8 @@ void Cell::recalculateAllEffectsList(){
     }
 }
 void Cell::add(EffectsForCell* effect){
+    if(effect == nullptr)
+        throw new int(228);
     effects_list_.push_back(effect);
 }
 void Cell::remove(std::vector<EffectsForCell*>::iterator it){
@@ -148,9 +146,9 @@ std::vector<EffectsForCell*>::iterator Cell::endIteratorEffectsList(){
 
 void Cell::RecalculateTableWithCenterThisPoint() {
     clearTable_();
-    std::queue<Cell*> qWithoutMoveable;
-    updateMoveableCells_(qWithoutMoveable);
-    updateUnMovealeCells_(qWithoutMoveable);
+    std::queue<Cell*> qWithUnMoveable;
+    updateMoveableCells_(qWithUnMoveable);
+    updateUnMovealeCells_(qWithUnMoveable);
 }
 std::vector <Cell*> Cell::actualPath(Cell* to) {
     if (!to || !to->getisMoveAble())return std::vector<Cell*>();
@@ -190,9 +188,11 @@ void Cell::clearTable_() {
 	clearCell_();
 	setdistance_through(0);
 	this->AddedToQuery_ = false;
-	auto f = [&q](Cell * t, Cell * parent) {
+	auto f = [&q](Cell * t, Cell * parent, int delta_col, int delta_row) {
 		if (t && t->AddedToQuery_ == true) {
 			q.push(t);
+			t->col_ = parent->col_ + delta_col;
+			t->row_ = parent->row_ + delta_row;
 			t->setdistance_through(
 			        parent->getdistance_through() + 1
             );
@@ -203,32 +203,35 @@ void Cell::clearTable_() {
 		Cell * Now = q.front();
 		q.pop();
 		Now->clearCell_();
-		f(Now->getleftUp(), Now);
-		f(Now->getleft(), Now);
-		f(Now->getleftDown(), Now);
-		f(Now->getrightUp(), Now);
-		f(Now->getright(), Now);
-		f(Now->getrightDown(), Now);
+		f(Now->getleftUp(), Now, -1, 0);
+		f(Now->getleft(), Now, 0, -1);
+		f(Now->getleftDown(), Now, 1, -1);
+		f(Now->getrightUp(), Now, -1, 1);
+		f(Now->getright(), Now, 0, 1);
+		f(Now->getrightDown(), Now, 1, 0);
 	}
 }
 
-void Cell::recalcAttackable_(Cell * Now){
+void Cell::recalcAttackable_(Cell * Now,bool isReached){
     if(Now == nullptr)return;
     if (!isEmpty() && !Now->isEmpty() &&
-        getCharacter()->CanAttackForDistance<"Melee">(Now->getdistance_barrier())
+        getCharacter()->CanAttackForDistance("Melee", Now->getdistance_barrier())
             ) {
-        Now->setisMeleeAttackAble(true);
+        Now->setisMeleeAttackAble(isReached);
     }
     if (!isEmpty() && !Now->isEmpty() &&
-        getCharacter()->CanAttackForDistance<"Range">(Now->getdistance_barrier())
+        getCharacter()->CanAttackForDistance("Range", Now->getdistance_barrier())
             ) {
         Now->setisRangeAttackAble(true);
     }
 }
 
-void Cell::recalcMoveable_(Cell * Now){
+void Cell::recalcMoveable_(Cell * Now, bool isReached){
     if(Now == nullptr || Now->isEmpty() || isEmpty())return;
-    getCharacter()->CanMoveForDistance()
+    if(!isEmpty() && Now->isEmpty() &&
+	   getCharacter()->CanMoveForDistance(Now->getdistance_barrier())){
+    	Now->setisMoveAble(isReached);
+    }
 }
 
 void Cell::updateMoveableCells_(std::queue<Cell*> & Q) {
@@ -250,9 +253,10 @@ void Cell::updateMoveableCells_(std::queue<Cell*> & Q) {
 	};
 	while (!q.empty()) {
 		Cell * Now = q.front();
-		Now->setisMoveAble();
-		recalcAttackable_(Now);
 		q.pop();
+		recalcMoveable_(Now, true);
+		recalcAttackable_(Now, true);
+
 		f(Now->getleftUp(), Now);
 		f(Now->getleft(), Now);
 		f(Now->getleftDown(), Now);
@@ -271,8 +275,8 @@ void Cell::updateUnMovealeCells_(std::queue<Cell*> & Q) {
 	};
 	while (!Q.empty()) {
 		Cell * Now = Q.front();
-		Now->setisMoveable(false);
-		Now->setisAttackable(false);
+		recalcMoveable_(Now, false);
+		recalcAttackable_(Now, false);
 		Q.pop();
 		f(Now->getleftUp(), Now);
 		f(Now->getleft(), Now);
@@ -282,3 +286,46 @@ void Cell::updateUnMovealeCells_(std::queue<Cell*> & Q) {
 		f(Now->getrightDown(), Now);
 	}
 }
+
+void Cell::print() {
+	std::cout << col_ << " " << row_ << std::endl;
+}
+
+Cell* Cell::getRealShootTarget(Cell* next){
+	if(next == this){
+		return next;
+	}
+	int next_col = next->col_;
+	int next_row = next->row_;
+	if(next_col - col_ == next_row - row_){
+		if(next_col > col_){
+			return getrightDown()->getright();
+		}
+		if(next_col < col_){
+			return getleftUp()->getleft();
+		}
+		throw new std::string("Don`t recalculated");
+	}
+	if(-2 * (next_col - col_) == (next_row - row_)){
+		if(next_col < col_){
+			return getrightUp()->getright();
+		}
+		if(next_col > col_){
+			return getleftDown()->getleft();
+		}
+		throw new std::string("Don`t recalculated");
+	}
+	if(next_col - col_ == -2 * (next_row - row_)){
+		if(next_col < col_){
+			return (getleftUp() != nullptr ? getleftUp()->getrightUp() : getrightUp()->getleftUp());
+		}
+		if(next_col > col_){
+			return (getleftDown() != nullptr ? getleftDown()->getrightDown() : getrightDown()->getleftDown());
+		}
+		throw new std::string("Don`t recalculated");
+	}
+	return this;
+
+
+
+}