diff options
author | tom <tombarrett@siu.edu> | 2017-01-19 13:33:46 -0600 |
---|---|---|
committer | tom <tombarrett@siu.edu> | 2017-01-19 13:33:46 -0600 |
commit | 11c5d147b845d4323f2e94c1ac47b6001420d8e6 (patch) | |
tree | eafc67bb6844dc14b56ad5d8eadf6d200b78e0d1 /src | |
parent | 0c2335f4a717e53c2456e49407dfbbd0ffa20f06 (diff) |
-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
Diffstat (limited to 'src')
-rw-r--r-- | src/creature.cpp | 43 | ||||
-rw-r--r-- | src/list.cpp | 61 | ||||
-rw-r--r-- | src/main.cpp | 5 | ||||
-rw-r--r-- | src/resource.cpp | 22 |
4 files changed, 76 insertions, 55 deletions
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 <Entity*>::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())<reach){ - target->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<CREATURES;i++){ Creature X(main,Rect); C.push_back(X); } - Rect = {0,0,RESOURCE_SIZE,RESOURCE_SIZE}; + Rect = {0,0,RESOURCE_SIZE_START,RESOURCE_SIZE_START}; for(i=0;i<RESOURCES;i++){ Resource Y(main,Rect); R.push_back(Y); @@ -19,59 +19,62 @@ List::List(Window m) } -void List::Place() -{ - for(list<Creature>::iterator it = C.begin(); it!=C.end(); it++) - it->Place(); - - for(list<Resource>::iterator it = R.begin(); it!=R.end(); it++){ - if(it->getAmount()<=0) - R.erase(it--); - else - it->Place(); - } +void List::Remove() +{ + for(list<Creature>::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<Resource>::iterator it = R.begin(); it!=R.end(); it++) + if(it->getAmount()<=0) + R.erase(it--); } void List::Behavior() { for(list<Creature>::iterator it = C.begin(); it!=C.end(); it++){ - list<Entity*> 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<Resource>::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<Creature>::iterator it = C.begin(); it!=C.end(); it++) + it->Place(); + + for(list<Resource>::iterator it = R.begin(); it!=R.end(); it++) + it->Place(); } list<Entity*> List::getNear(Creature nC) { list<Entity*> N; - for(list <Resource>::iterator it = R.begin(); it!=R.end(); it++){ + for(list <Resource>::iterator it = R.begin(); it!=R.end(); it++) if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) ) N.push_back(&(*it)); - } - - for(list <Creature>::iterator it = C.begin(); it!=C.end(); it++){ + + for(list <Creature>::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; } |