diff options
-rw-r--r-- | inc/list.hpp | 2 | ||||
-rw-r--r-- | src/list.cpp | 63 |
2 files changed, 27 insertions, 38 deletions
diff --git a/inc/list.hpp b/inc/list.hpp index 05e9fcc..14a6a9a 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -12,7 +12,7 @@ class List List(Window m); void Behavior(); void Place(); - double Distance(Location A, Location B); + double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; private: //vectors containing objects of each type diff --git a/src/list.cpp b/src/list.cpp index e2724f3..ad9ab58 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -34,44 +34,33 @@ void List::Place() void List::Behavior() { - for(int i = 0; i<C.size(); i++) - { - C[i].Behavior(); - - vector<Resource*> N; - - for(int j = 0; j < R.size(); j++) - if(C[i].getBestSense()>Distance(C[i].getLocation(),R[j].getLocation())) - N.push_back(&R[j]); - - C[i].giveR(N); - N.clear(); - - vector<Creature*> M; - for(int j = 0; j < C.size(); j++) - { - if(j==i) - continue; - else if(C[i].getBestSense()>Distance(C[i].getLocation(),C[j].getLocation())) - M.push_back(&C[j]); - } + for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++){ + it->Behavior(); - C[i].giveC(M); - M.clear(); + vector<Resource*> N; + for(vector <Resource>::iterator jt = R.begin(); jt!=R.end(); jt++){ + if( it->getBestSense() > Distance(it->getLocation(),jt->getLocation()) ) + N.push_back(&(*jt)); + } + it->giveR(N); + + vector<Creature*> M; + for(vector <Creature>::iterator jt = C.begin(); jt!=C.end(); jt++){ + if( jt == it) + continue; + else if( it->getBestSense() > Distance(it->getLocation(),jt->getLocation()) ) + M.push_back(&(*jt)); + } + it->giveC(M); + + M.clear(); + N.clear(); - // This kills the creature - if(C[i].getHealth()<=0) - { - Location z = C[i].getLocation(); - Resource r = Resource(main,"img/Cdead.png",z); - R.push_back(r); - C.erase(C.begin()+i); + if(it->getHealth()<=0){ + Location z = it->getLocation(); + Resource r = Resource(main,"img/Cdead.png",z); + R.push_back(r); + C.erase(it--); + } } - } -} - -double List::Distance(Location A, Location B) -{ - //computes distance between two points - return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); } |