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 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 24 deletions(-) (limited to 'src/creature.cpp') 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