From 6b967f3bd7a3e7203b57bd9e4edb0db82bc9ed1c Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 26 May 2015 14:04:04 -0500 Subject: revamped the list and creatures two bugs i can see right now -somehow when a resource has less than 0 amount it still sticks around (somewhat rare) -some creatures just bounce back and forth until they die not getting to their target --- src/creature.cpp | 80 +++++++++++++++++++++++++++++++++++++++----------------- src/list.cpp | 79 ++++++++++++++----------------------------------------- src/main.cpp | 2 +- src/resource.cpp | 5 ++++ 4 files changed, 81 insertions(+), 85 deletions(-) (limited to 'src') diff --git a/src/creature.cpp b/src/creature.cpp index c7b36ca..9590dde 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -10,74 +10,99 @@ Creature::Creature(Window m, std::string s) //Constructor //initializes random start coordinates for creature, target position is equivalent to it's position yPosition=yTarget=rand()%800; xPosition=xTarget=rand()%1200; + hasTarget = false; + n=0; } int Creature::Behavior() { health-=1; //Decrements health each time a behavior is executed - this->Priority(); //Checks which action has priority (doesn't really do this right now) + + this->Priority(); if(this->Action()) { - health+=10; - return 2; + if(nR.size()) + { + nR[n]->eat(); + if(health<500) + health+=10; + } } + return 0; } void Creature::Priority() { - //Traverses location vector, if object at [i] is resource (2), then creature's target coordinates are set - int i; - for(i=0;igetLocation(),nR[0]->getLocation()); + + if(d>Distance(this->getLocation(),nR[i]->getLocation())) { - xTarget = location[i].x; - yTarget = location[i].y; + d=Distance(this->getLocation(),nR[i]->getLocation()); + n=i; } } + + if(nR.size()) + { + xTarget = nR[n]->getLocation().x; + yTarget = nR[n]->getLocation().y; + hasTarget = true; + } + else + hasTarget = false; } bool Creature::Action() { //If the distance is close, will return an bool - //if(xPosition == xTarget && yPosition == yTarget) // return false; - if(sqrt(pow(xPosition - xTarget, 2) + pow(yPosition - yTarget, 2)) < 2) - return true; + if(nR.size()) + if(5 > Distance(this->getLocation(),nR[n]->getLocation())) + { + if(hasTarget) + return true; + else + return false; + } //Makes moves towards target coordinates if(xPosition==xTarget) { if(yPosition::iterator it = C.begin(); it!=C.end(); it++) + it->Place(); //places all resources - for(i = 0; i < R.size(); i++) + for(int j = 0; j Z; - - for(i = 0; i < C.size(); i++) + for(int i = 0; i(Distance(C[i].getLocation(),L[j]))) - Z.push_back(L[j]); + vector N; - C[i].giveKnown(Z); //sets creature's target location? - Z.clear(); //clear vector Z for next creature + for(int j = 0; j < R.size(); j++) + if(250>Distance(C[i].getLocation(),R[j].getLocation())) + N.push_back(&R[j]); + + C[i].give(N); + N.clear(); // This kills the creature if(C[i].getHealth()<=0) diff --git a/src/main.cpp b/src/main.cpp index 4290386..ed41895 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ int main() L.Behavior(); main.Render(); - SDL_Delay(10); + //SDL_Delay(10); } main.Destroy(); diff --git a/src/resource.cpp b/src/resource.cpp index 4f12e2d..71a13e3 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -28,3 +28,8 @@ Location Resource::getLocation() //Returns resource object Location L(xPosition,yPosition,2); return L; } + +void Resource::eat() +{ + amount-=10; +} -- cgit v1.2.3