From 8d9ad3eb74bc0dfc647abf03b7b99ed16f2a9115 Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 2 May 2016 21:24:02 -0500 Subject: implementing better modularity, came into bad bug. All creatures seem to have same near vector. --- src/creature.cpp | 126 ++++++++++++++++--------------------------------------- 1 file changed, 36 insertions(+), 90 deletions(-) (limited to 'src/creature.cpp') diff --git a/src/creature.cpp b/src/creature.cpp index f2b85e5..24508bf 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -8,8 +8,8 @@ Creature::Creature(Window m, std::string s) maxHealth = 1000; hunger = 0; - L.y=yTarget=rand()%800; - L.x=xTarget=rand()%1200; + L.y=rand()%800; + L.x=rand()%1200; type = 1; hasTarget = false; @@ -18,114 +18,62 @@ Creature::Creature(Window m, std::string s) n=0; } -int Creature::Behavior() +void Creature::Behavior() { - health-=1; + //health-=1; this->Priority(); - if(this->Action()){ - if(nR.size()){ - nR[n]->eat(); - if(healthAction(); } void Creature::Priority() -{ - double d; - - // Gets location for closest resource - for(int i = 0; i < nR.size(); i++) - { - if(i==0) - { - d = Distance(this->getLocation(),nR[0]->getLocation()); - n = 0; - continue; - } - - if(d>Distance(this->getLocation(),nR[i]->getLocation())) - { - d=Distance(this->getLocation(),nR[i]->getLocation()); - n=i; - } - } - - if(nR.size()==0) - hasTarget=false; - - // If there is available targets and the unit doesnt have a target, assign the closest one. - //cout << "size: " << nR.size() << endl; - //cout << "hastarget: "<< hasTarget << endl; - if(nR.size()>0&&!hasTarget) - { - xTarget = nR[n]->getLocation().x; - yTarget = nR[n]->getLocation().y; - hasTarget = true; - wandering = false; - } - // If there is not available targets and doesnt have a target, set a random location as a target - else if(nR.size()==0&&!hasTarget) - { - if(!wandering) - { - xTarget = rand()%1200; - yTarget = rand()%800; - wandering = true; - hasTarget = false; - } - else - { - Location L(xTarget,yTarget); - if(Distance(this->getLocation(),L)<5) - wandering = false; - hasTarget = false; - } - } +{ + for(vector ::iterator it = N.begin(); it!=N.end(); it++){ + if((*it)->getType() == 2){ + if(!hasTarget){ + target = *it; + wandering = false; + hasTarget = true; + } + else + break; + } + } } -bool Creature::Action() -{ - //If the distance is close, will return an bool - //if(xPosition == xTarget && yPosition == yTarget) - // return false; +void Creature::Action() +{ - if(nR.size()) - if(5 > Distance(this->getLocation(),nR[n]->getLocation())) - { - if(hasTarget) - { - hasTarget = false; - return true; - } - else - return false; - } + if(hasTarget){ + if(Distance(L,target->getLocation())<5){ + target->eat(); + health+=1000; + } + if(target->getAmount()==0) + hasTarget = false; + } //Makes moves towards target coordinates - if(L.x==xTarget) + if(L.x==target->getLocation().x) { - if(L.ygetLocation().y) L.y+=speed; else L.y-=speed; } - else if(L.y==yTarget) + else if(L.y==target->getLocation().y) { - if(L.xgetLocation().x) L.x+=speed; else L.x-=speed; } - else if(L.xgetLocation().x) { - if(L.ygetLocation().y) { L.x+=speed; L.y+=speed; @@ -138,9 +86,9 @@ bool Creature::Action() } } - else if (L.x>xTarget) + else if (L.x>target->getLocation().x) { - if(L.ygetLocation().y) { L.x-=speed; L.y+=speed; @@ -151,7 +99,5 @@ bool Creature::Action() L.x-=speed; L.y-=speed; } - } - - return false; + } } -- cgit v1.2.3