From a9281bfcf21861e621a3243ecb633a299c8d8e52 Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 21 Jan 2017 08:58:12 -0600 Subject: -took basic math functions and put it into functions.hpp -spaced various lines -reorganized variables by datatype -implemented reproduction -reorganized pathing so once a target is set, the creature checks if that same target is near every cycle --- src/creature.cpp | 92 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 27 deletions(-) (limited to 'src/creature.cpp') diff --git a/src/creature.cpp b/src/creature.cpp index 7c4c108..ff6290d 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -2,28 +2,31 @@ Creature::Creature(Window m, SDL_Rect R) { - renderer = m.getRenderer(); - rect = R; + renderer = m.getRenderer(); + rect = R; if(rect.x == 0 && rect.y == 0){ - rect.x = rand()%WINDOW_X; - rect.y = rand()%WINDOW_Y; + rect.x = rand()%WINDOW_X; + rect.y = rand()%WINDOW_Y; } - type = CREATURE_TYPE; - health = CREATURE_START_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; - able = true; + type = CREATURE_TYPE; + health = CREATURE_START_HEALTH; + maxHealth = CREATURE_MAX_HEALTH; + reach = CREATURE_REACH; + speed = CREATURE_SPEED; + bestSense = CREATURE_BEST_SENSE; + bite = CREATURE_BITE; + amountToGrow = CREATURE_AMOUNT_TO_GROW; + expectedPregnancyTime = CREATURE_EXPECTED_PREGNANCY_TIME; + + gender = rand() % 2; + hungry = false; + hasTarget = false; + wander = false; + pregnate = false; + pregnancyReady = false; + able = true; } void Creature::Behavior() @@ -34,8 +37,17 @@ void Creature::Behavior() if(!hasTarget) this->setTarget(); + else + this->checkTarget(); this->Action(); + + if(pregnate){ + pregnancyTime++; + if(pregnancyTime > expectedPregnancyTime) + pregnancyReady = true; + } + } void Creature::Priority() @@ -59,20 +71,36 @@ void Creature::setTarget() wander = false; break; } + if( (*it)->getType() == CREATURE_TYPE && able && (*it)->getGender() != gender ){ + target = *it; + hasTarget = true; + wander = false; + break; + } } if(!hasTarget&&!wander){ wander = true; - SDL_Rect r = {rand()%WINDOW_X, rand()%WINDOW_Y, 0, 0}; - wTarget = r; + SDL_Rect r = {rand() % WINDOW_X, rand() % WINDOW_Y, 0, 0}; + wTarget = r; } } +void Creature::checkTarget() +{ + for(list ::iterator it = N.begin(); it!=N.end(); it++) + if( target == *it ) + return; + + hasTarget = false; + return; +} + void Creature::Action() { if(hasTarget){ - if( Distance(rect,target->getRect() ) < reach ){ + if( Distance(rect,target->getRect()) < reach && target->getType() == RESOURCE_TYPE){ target->eat(bite); health+=bite; amountAte++; @@ -80,15 +108,17 @@ void Creature::Action() amountAte = 0; rect.w = rect.h = rect.w + 1; } + if(target->getAmount()<=0) + hasTarget = false; + } + else if( Distance(rect,target->getRect()) < reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ + target->impregnate(); + hasTarget = false; } else - Move(target->getRect()); - - if(target->getAmount()<=0){ - hasTarget = false; - } + Move(target->getRect()); } - else{ + else if(wander){ if(Distance(rect,wTarget) < reach) wander = false; else @@ -131,3 +161,11 @@ void Creature::Move(SDL_Rect R) } } } + +void Creature::impregnate() +{ + if(!pregnate){ + pregnate = true; + pregnancyTime = 0; + } +} -- cgit v1.2.3