diff options
author | Tom Barrett <tombarrett@cornell.engr.siu.edu> | 2015-05-08 10:14:42 -0500 |
---|---|---|
committer | Tom Barrett <tombarrett@cornell.engr.siu.edu> | 2015-05-08 10:14:42 -0500 |
commit | 39d7fe95633baafe4539f604186b151f30401a39 (patch) | |
tree | 242d91eafe56a15db046a1068d8e970624c6abfd /src/creature.cpp | |
parent | 76e10e651906b756c4482274cdf1d8ccb686b915 (diff) | |
parent | 8c5121486a0e5d94ae5e5ee67370a31530cf8b6f (diff) |
Merge branch 'master' of http://github.com/majortom6/natures
Diffstat (limited to 'src/creature.cpp')
-rw-r--r-- | src/creature.cpp | 114 |
1 files changed, 52 insertions, 62 deletions
diff --git a/src/creature.cpp b/src/creature.cpp index 9d50291..5c3bb8e 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -1,105 +1,95 @@ #include "creature.hpp" -Creature::Creature(Window m, std::string s) +Creature::Creature(Window m, std::string s) //Constructor { texture = loadTexture(s, m); renderer = m.getRenderer(); - hp = 100; - hu = 0; + health = 100; + hunger = 0; - int zy = rand()%800; - int zx = rand()%1200; - y=zy; - x=zx; - //std::cout << x << ' ' << y << std::endl; - - //For the test resource - xT=yT=300; + //initializes random start coordinates for creature, target position is equivalent to it's position + yPosition=yTarget=rand()%800; + xPosition=xTarget=rand()%1200; } void Creature::Behavior() { - hp--; - //Detection - - - //Priorities + health--; //Decrements health each time a behavior is executed + this->Priority(); //Checks which action has priority (doesn't really do this right now) + this->Action(); //Does action +} - //Action - this->Action(); +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;i<location.size();i++) + { + if(location[i].type==2) + { + xTarget = location[i].x; + yTarget = location[i].y; + } + } } void Creature::Action() { - //std::cout << (sqrt(((x-xT)^2)+((y-yT)^2)); - if((sqrt(((x-xT)^2)+((y-yT)^2)))<2) - return; //eat//reproduce//etc; + //If the distance is close, will return an bool + if(sqrt(pow(xPosition - xTarget, 2) + pow(yPosition - yTarget, 2)) < 2) + return; //<--- eat action should be here - if(x==xT) + //Makes moves towards target coordinates + if(xPosition==xTarget) { - if(y<yT) - y++; + if(yPosition<yTarget) + yPosition++; else - y--; + yPosition--; } - else if(y==yT) - { - if(x<xT) - x++; - else - x--; - } - else if(x<xT) + + else if(yPosition==yTarget) { - if(y<yT) - { - x++; - y++; - } + if(xPosition<xTarget) + xPosition++; else - { - x++; - y--; - } + xPosition--; } - else if (x>xT) + + else if(xPosition<xTarget) { - if(y<yT) + if(yPosition<yTarget) { - x--; - y++; + xPosition++; + yPosition++; } + else { - x--; - y--; + xPosition++; + yPosition--; } } - /* - else + else if (xPosition>xTarget) { - int z = rand()%2; - if(z) + if(yPosition<yTarget) { - if(x<xT) - x++; - else - x--; + xPosition--; + yPosition++; } + else { - if(y<yT) - y++; - else - y--; + xPosition--; + yPosition--; } } - */ } Location Creature::getLocation() { - Location L(x,y,1); + //returns location object of the specific creature + Location L(xPosition, yPosition, 1); return L; } |