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 --- inc/creature.hpp | 9 +++++-- inc/list.hpp | 1 - inc/resource.hpp | 2 +- inc/window.hpp | 2 ++ src/creature.cpp | 80 +++++++++++++++++++++++++++++++++++++++----------------- src/list.cpp | 79 ++++++++++++++----------------------------------------- src/main.cpp | 2 +- src/resource.cpp | 5 ++++ 8 files changed, 91 insertions(+), 89 deletions(-) diff --git a/inc/creature.hpp b/inc/creature.hpp index 7c5c8cb..24cad37 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -2,6 +2,7 @@ #define creature_h #include "entity.hpp" +#include "resource.hpp" #include "location.hpp" class Creature: public Entity @@ -12,15 +13,19 @@ class Creature: public Entity bool Action(); void Priority(); Location getLocation(); - void giveKnown(std::vector Z){location = Z;}; + void give(vector n){nR=n;}; int getHealth(){return health;}; + double Distance(Location A, Location B); private: int xTarget; //x-coordinate of creature's target position int yTarget; //y-coordinate of creature's target position + bool hasTarget; int health; //health of a creature (0-100) int hunger; //value associated with a creatures want to find food (0-100) - std::vector location; //vector containing objects near the creature + int speed = 1; + vector nR; //vector containing objects near the creature + int n; // counter for which place in resource array is targeted }; #endif diff --git a/inc/list.hpp b/inc/list.hpp index f370d24..934c319 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -19,7 +19,6 @@ class List Window main = Window("no");//will be needed for adding R's and C's after constructor. std::vector R; std::vector C; - std::vector L; }; #endif diff --git a/inc/resource.hpp b/inc/resource.hpp index a1dd988..c551844 100644 --- a/inc/resource.hpp +++ b/inc/resource.hpp @@ -10,7 +10,7 @@ class Resource: public Entity Resource(Window m, std::string s); Resource(Window m, std::string s, Location z); Location getLocation(); - void eat(){amount-=10;}; + void eat(); int getAmount(){return amount;}; private: diff --git a/inc/window.hpp b/inc/window.hpp index 073a106..1ea2733 100644 --- a/inc/window.hpp +++ b/inc/window.hpp @@ -13,6 +13,8 @@ #include #include +using namespace std; + class Window { public: 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