diff options
author | tom <tom@ground-control> | 2016-05-02 21:24:02 -0500 |
---|---|---|
committer | tom <tom@ground-control> | 2016-05-02 21:24:02 -0500 |
commit | 8d9ad3eb74bc0dfc647abf03b7b99ed16f2a9115 (patch) | |
tree | eb16f853c280ed200953fbc7a500193a69ef8892 /src | |
parent | 0346965968fb5da1a52ed8f896a922c63ce180f6 (diff) |
implementing better modularity, came into bad bug. All creatures seem to have same near vector.
Diffstat (limited to 'src')
-rw-r--r-- | src/creature.cpp | 126 | ||||
-rw-r--r-- | src/list.cpp | 46 |
2 files changed, 62 insertions, 110 deletions
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(health<maxHealth) - health+=10; - } - } - return 0; + this->Action(); } 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 <Entity*>::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.y<yTarget) + if(L.y<target->getLocation().y) L.y+=speed; else L.y-=speed; } - else if(L.y==yTarget) + else if(L.y==target->getLocation().y) { - if(L.x<xTarget) + if(L.x<target->getLocation().x) L.x+=speed; else L.x-=speed; } - else if(L.x<xTarget) + else if(L.x<target->getLocation().x) { - if(L.y<yTarget) + if(L.y<target->getLocation().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.y<yTarget) + if(L.y<target->getLocation().y) { L.x-=speed; L.y+=speed; @@ -151,7 +99,5 @@ bool Creature::Action() L.x-=speed; L.y-=speed; } - } - - return false; + } } diff --git a/src/list.cpp b/src/list.cpp index ad9ab58..ff9722f 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -4,7 +4,7 @@ List::List(Window m) { int i; - for(i=0;i<10;i++) + for(i=0;i<2;i++) { Creature X(m,"img/Cbasic.png"); C.push_back(X); @@ -35,27 +35,12 @@ void List::Place() void List::Behavior() { for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++){ - it->Behavior(); - - 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); + vector<Entity*> N = getNear(*it); + it->giveN(N); - M.clear(); - N.clear(); - + it->Behavior(); + if(it->getHealth()<=0){ Location z = it->getLocation(); Resource r = Resource(main,"img/Cdead.png",z); @@ -64,3 +49,24 @@ void List::Behavior() } } } + +vector<Entity*> List::getNear(Creature nC) +{ + vector<Entity*> N; + + for(vector <Resource>::iterator it = R.begin(); it!=R.end(); it++){ + if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) ) + N.push_back(&(*it)); + } + + for(vector <Creature>::iterator it = C.begin(); it!=C.end(); it++){ + if( &nC == &(*it)) + continue; + else if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) ) + N.push_back(&(*it)); + } + + return N; +} + + |