From 11c5d147b845d4323f2e94c1ac47b6001420d8e6 Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 19 Jan 2017 13:33:46 -0600 Subject: -added entity growing -added bite functionality for creature/resource -added gender (next largest feature is reproduction) -refractored creature setTarget function to make more sense -added constants for starting size of entity and max size of entity -refractored list to make make much more sense -added constants for types --- inc/constants.hpp | 35 +++++++++++++++++++------------ inc/creature.hpp | 33 +++++++++++++++++------------- inc/entity.hpp | 2 +- inc/list.hpp | 1 + inc/resource.hpp | 5 ++++- src/creature.cpp | 43 +++++++++++++++++++++------------------ src/list.cpp | 61 +++++++++++++++++++++++++++++-------------------------- src/main.cpp | 5 +++-- src/resource.cpp | 22 ++++++++++++++++---- 9 files changed, 123 insertions(+), 84 deletions(-) diff --git a/inc/constants.hpp b/inc/constants.hpp index 08f548e..45ff0e7 100644 --- a/inc/constants.hpp +++ b/inc/constants.hpp @@ -1,20 +1,29 @@ #ifndef constants_h #define constants_h -const int CREATURES = 10; -const int RESOURCES = 100; -const int MINIMUM_RESOURCES = 90; -const int WINDOW_X = 1080; -const int WINDOW_Y = 640; +const int CREATURES = 10; +const int RESOURCES = 100; +const int MINIMUM_RESOURCES = 90; +const int WINDOW_X = 1080; +const int WINDOW_Y = 640; -const int CREATURE_START_HEALTH = 500; -const int CREATURE_MAX_HEALTH = 1000; -const int CREATURE_BEST_SENSE = 100; -const int CREATURE_SPEED = 1; -const int CREATURE_REACH = 5; -const int CREATURE_SIZE = 10; +const int CREATURE_TYPE = 1; +const int RESOURCE_TYPE = 2; -const int RESOURCE_SIZE = 5; -const int RESOURCE_AMOUNT = 100; +const int CREATURE_START_HEALTH = 500; +const int CREATURE_MAX_HEALTH = 1000; +const int CREATURE_BEST_SENSE = 100; +const int CREATURE_SPEED = 1; +const int CREATURE_REACH = 5; +const int CREATURE_SIZE_MAX = 20; +const int CREATURE_SIZE_START = 10; +const int CREATURE_BITE = 10; +const int CREATURE_AMOUNT_TO_GROW = 50; + +const int RESOURCE_SIZE_START = 5; +const int RESOURCE_SIZE_MAX = 10; +const int RESOURCE_AMOUNT_START = 100; +const int RESOURCE_AMOUNT_MAX = 200; +const int RESOURCE_GROW = 1; #endif diff --git a/inc/creature.hpp b/inc/creature.hpp index f51d083..edc253f 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -17,24 +17,29 @@ class Creature: public Entity void setTarget(); void Move(SDL_Rect R); - void giveN(list n){N = n;}; - double Distance(SDL_Rect A, SDL_Rect B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; - int getHealth(){return health;}; - bool doesItHaveTarget(){return hasTarget;}; - int getBestSense(){return bestSense;}; + void giveN(list n){N = n;}; + double Distance(SDL_Rect A, SDL_Rect B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; + int getHealth(){return health;}; + bool doesItHaveTarget(){return hasTarget;}; + int getBestSense(){return bestSense;}; private: - bool hasTarget; - bool wander; + bool hasTarget; + bool wander; SDL_Rect wTarget; - int health; - int reach; - int maxHealth; - bool hungry; - int speed; - bool able; - int bestSense; + int health; + int reach; + int maxHealth; + int speed; + int bestSense; + int bite; + int amountAte; + int amountToGrow; + + bool hungry; + bool gender; + bool able; list N; Entity *target; diff --git a/inc/entity.hpp b/inc/entity.hpp index 5941a9c..6587f11 100644 --- a/inc/entity.hpp +++ b/inc/entity.hpp @@ -11,7 +11,7 @@ class Entity int getType(){return type;}; SDL_Rect getRect(){return rect;}; - virtual void eat(void){}; + virtual void eat(int bite){}; virtual int getAmount(void){}; protected: diff --git a/inc/list.hpp b/inc/list.hpp index 6800b34..64ca522 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -15,6 +15,7 @@ class List List(Window m); void Behavior(); void Place(); + void Remove(); double Distance(SDL_Rect A, SDL_Rect B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; list getNear(Creature C); diff --git a/inc/resource.hpp b/inc/resource.hpp index ed9f098..5c740fa 100644 --- a/inc/resource.hpp +++ b/inc/resource.hpp @@ -7,12 +7,15 @@ class Resource: public Entity { public: Resource(Window m, SDL_Rect Rect); - void eat(); + void eat(int bite); int getAmount(){return amount;}; + void grow(); + int map(int x, int inMin, int inMax, int outMin, int outMax); private: int amount; + int growAmount; }; #endif diff --git a/src/creature.cpp b/src/creature.cpp index a71a0b7..7c4c108 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -3,7 +3,6 @@ Creature::Creature(Window m, SDL_Rect R) { renderer = m.getRenderer(); - type = 1; rect = R; if(rect.x == 0 && rect.y == 0){ @@ -11,12 +10,16 @@ Creature::Creature(Window m, SDL_Rect R) rect.y = rand()%WINDOW_Y; } + type = CREATURE_TYPE; health = CREATURE_START_HEALTH; - maxHealth = CREATURE_MAX_HEALTH; + maxHealth = CREATURE_MAX_HEALTH; reach = CREATURE_REACH; speed = CREATURE_SPEED; bestSense = CREATURE_BEST_SENSE; + bite = CREATURE_BITE; + amountToGrow= CREATURE_AMOUNT_TO_GROW; + gender = rand() % 2; hungry = false; hasTarget = false; wander = false; @@ -29,7 +32,8 @@ void Creature::Behavior() this->Priority(); - this->setTarget(); + if(!hasTarget) + this->setTarget(); this->Action(); } @@ -48,19 +52,13 @@ void Creature::Priority() void Creature::setTarget() { - if(hasTarget) - return; - for(list ::iterator it = N.begin(); it!=N.end(); it++){ - if((*it)->getType() == 2 && hungry){ - if(!hasTarget){ - target = *it; - hasTarget = true; - wander = false; - } - else - break; - } + if( (*it)->getType() == RESOURCE_TYPE && hungry){ + target = *it; + hasTarget = true; + wander = false; + break; + } } if(!hasTarget&&!wander){ @@ -73,10 +71,15 @@ void Creature::setTarget() void Creature::Action() { - if(hasTarget){ - if(Distance(rect,target->getRect())eat(); - health+=10; + if(hasTarget){ + if( Distance(rect,target->getRect() ) < reach ){ + target->eat(bite); + health+=bite; + amountAte++; + if(rect.w <= CREATURE_SIZE_MAX && amountToGrow <= amountAte){ + amountAte = 0; + rect.w = rect.h = rect.w + 1; + } } else Move(target->getRect()); @@ -86,7 +89,7 @@ void Creature::Action() } } else{ - if(Distance(rect,wTarget)<5) + if(Distance(rect,wTarget) < reach) wander = false; else Move(wTarget); diff --git a/src/list.cpp b/src/list.cpp index 3127d76..5309109 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -5,13 +5,13 @@ List::List(Window m) main = m; int i; - SDL_Rect Rect = {0,0,CREATURE_SIZE,CREATURE_SIZE}; + SDL_Rect Rect = {0,0,CREATURE_SIZE_START,CREATURE_SIZE_START}; for(i=0;i::iterator it = C.begin(); it!=C.end(); it++) - it->Place(); - - for(list::iterator it = R.begin(); it!=R.end(); it++){ - if(it->getAmount()<=0) - R.erase(it--); - else - it->Place(); - } +void List::Remove() +{ + for(list::iterator it = C.begin(); it!=C.end(); it++) + if(it->getHealth()<=0){ + SDL_Rect Rect = it->getRect(); + Resource r = Resource(main,Rect); + R.push_back(r); + C.erase(it--); + } + + for(list::iterator it = R.begin(); it!=R.end(); it++) + if(it->getAmount()<=0) + R.erase(it--); } void List::Behavior() { for(list::iterator it = C.begin(); it!=C.end(); it++){ - list N = getNear(*it); it->giveN(N); - it->Behavior(); - - if(it->getHealth()<=0){ - SDL_Rect Rect = it->getRect(); - Resource r = Resource(main,Rect); - R.push_back(r); - C.erase(it--); - } } + + for(list::iterator it = R.begin(); it!=R.end(); it++) + it->grow(); +} - SDL_Rect Rect = {0,0,RESOURCE_SIZE,RESOURCE_SIZE}; +void List::Place() +{ + SDL_Rect Rect = {0,0,RESOURCE_SIZE_START,RESOURCE_SIZE_START}; while(R.size() < MINIMUM_RESOURCES){ Resource Y(main,Rect); R.push_back(Y); } + + for(list::iterator it = C.begin(); it!=C.end(); it++) + it->Place(); + + for(list::iterator it = R.begin(); it!=R.end(); it++) + it->Place(); } list List::getNear(Creature nC) { list N; - for(list ::iterator it = R.begin(); it!=R.end(); it++){ + for(list ::iterator it = R.begin(); it!=R.end(); it++) if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) ) N.push_back(&(*it)); - } - - for(list ::iterator it = C.begin(); it!=C.end(); it++){ + + for(list ::iterator it = C.begin(); it!=C.end(); it++) if( &nC == &(*it)) continue; else if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) ) N.push_back(&(*it)); - } - + return N; } diff --git a/src/main.cpp b/src/main.cpp index 48d3c64..460582a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,9 +22,10 @@ int main() } main.Clear(); - + + L.Remove(); + L.Behavior(); L.Place(); - L.Behavior(); main.Render(); diff --git a/src/resource.cpp b/src/resource.cpp index eec1113..07f7ca9 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -3,7 +3,7 @@ Resource::Resource(Window m, SDL_Rect R) { renderer = m.getRenderer(); - type = 2; + type = RESOURCE_TYPE; rect = R; if(rect.x == 0 && rect.y == 0){ @@ -11,10 +11,24 @@ Resource::Resource(Window m, SDL_Rect R) rect.y = rand()%WINDOW_Y; } - amount = RESOURCE_AMOUNT; + amount = RESOURCE_AMOUNT_START; + growAmount = RESOURCE_GROW; } -void Resource::eat() +void Resource::eat(int bite) { - amount-=10; + amount-=bite; +} + +void Resource::grow() +{ + if(amount < RESOURCE_AMOUNT_MAX) + amount+=growAmount; + + rect.h = rect.w = map(amount,0,RESOURCE_AMOUNT_MAX,0,RESOURCE_SIZE_MAX); +} + +int Resource::map(int x, int inMin, int inMax, int outMin, int outMax) +{ + return (x-inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } -- cgit v1.2.3