From 7394b069537ed7a490a343381d62862eb22abdcf Mon Sep 17 00:00:00 2001 From: majortom6 Date: Sun, 26 Feb 2017 11:14:31 -0600 Subject: -REMOVED ENTITY, RESOURCE, AND CREATURE ! -replaced them all with one class, organism (always subject to change) -the dna type now is what differs creatures and resources -removed dead constants -may be a rogue segfault -also weird artifacts start showing if running long --- inc/constants.hpp | 17 ++-- inc/creature.hpp | 51 ------------ inc/dna.hpp | 11 ++- inc/entity.hpp | 32 -------- inc/list.hpp | 15 ++-- inc/opengl/spritebatch.hpp | 6 +- inc/organism.hpp | 63 +++++++++++++++ inc/quadtree.hpp | 10 +-- inc/resource.hpp | 24 ------ src/creature.cpp | 175 ---------------------------------------- src/dna.cpp | 57 +++++++------ src/entity.cpp | 7 -- src/list.cpp | 50 ++++++------ src/main.cpp | 4 +- src/organism.cpp | 193 +++++++++++++++++++++++++++++++++++++++++++++ src/quadtree.cpp | 8 +- src/resource.cpp | 30 ------- 17 files changed, 350 insertions(+), 403 deletions(-) delete mode 100644 inc/creature.hpp delete mode 100644 inc/entity.hpp create mode 100644 inc/organism.hpp delete mode 100644 inc/resource.hpp delete mode 100644 src/creature.cpp delete mode 100644 src/entity.cpp create mode 100644 src/organism.cpp delete mode 100644 src/resource.cpp diff --git a/inc/constants.hpp b/inc/constants.hpp index dc0ddec..ce4c3ff 100644 --- a/inc/constants.hpp +++ b/inc/constants.hpp @@ -14,28 +14,23 @@ const int RESOURCE_TYPE = 2; // Creatures const int CREATURE_MAX_HEALTH = 1000; -const int CREATURE_REACH = 1; -const int CREATURE_BEST_SENSE = 1; +const int CREATURE_BEST_SENSE = 2; const int CREATURE_BITE = 10; -const int CREATURE_AMOUNT_TO_GROW = 50; const int CREATURE_EXP_PREG_TIME = 100; -const int CREATURE_EXP_AGE = 1000000; -const int CREATURE_SIZE_MAX = 10; +const int CREATURE_EXP_AGE = 1000; const float CREATURE_SPEED = .1; const float CREATURE_MUTATION_PERCENT = .25; const float CREATURE_MUTATION_CHANCE = .05; +const float CREATURE_REACH = .1; // Resource -const int RESOURCE_SIZE_START = 1; -const int RESOURCE_SIZE_MAX = 4; -const int RESOURCE_AMOUNT_START = 100; -const int RESOURCE_AMOUNT_MAX = 200; -const int RESOURCE_GROW = 1; +const int RESOURCE_MAX_HEALTH = 200; +const int RESOURCE_GROW_AMOUNT = 1; // Opengl const int NUM_SHADERS = 3; const int NUM_UNIFORMS = 3; -const float CREATURE_SIDES = 4.0; +const float CREATURE_SIDES = 4; const float RESOURCE_SIDES = 10; // Quadtree diff --git a/inc/creature.hpp b/inc/creature.hpp deleted file mode 100644 index 8174cf6..0000000 --- a/inc/creature.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef creature_h -#define creature_h - -#include -#include - -#include "entity.hpp" -#include "functions.hpp" - -class Creature: public Entity -{ - public: - Creature(Rectangle r, DNA d); - - void Behavior(); - void Action(); - void Priority(); - void setTarget(); - void checkTarget(); - void moveTowards(Rectangle r); - void impregnate(DNA d); - void giveNearMe(std::vector n){nearMe = n;}; - - DNA getDNA(){return myDNA;}; - DNA getChildsDNA(){return childsDNA;}; - int getHealth(){return health;}; - int getBestSense(){return myDNA.bestSense;}; - bool getGender(){return gender;}; - bool getPregnancyReady(){return pregnancyReady;}; - void hadPregnancy(){pregnate = pregnancyReady = false;}; - - private: - Rectangle wTarget; - Entity* target; - std::vector nearMe; - DNA myDNA; - DNA childsDNA; - - int health; - int amountAte; - int pregnancyTime; - int age; - - bool hungry; - bool pregnancyReady; - bool able; - bool hasTarget; - bool wander; -}; - -#endif diff --git a/inc/dna.hpp b/inc/dna.hpp index b549c27..8c4effa 100644 --- a/inc/dna.hpp +++ b/inc/dna.hpp @@ -1,25 +1,28 @@ #ifndef dna_h #define dna_h +#include + #include "constants.hpp" #include "functions.hpp" class DNA { public: - DNA(); + DNA(){}; + DNA(std::string s); DNA combine(DNA D); + int type; int maxHealth; - int reach; int bestSense; int bite; - int amountToGrow; int expectedPregnancyTime; int expectedAge; - int sizeMax; + int growAmount; + float reach; float speed; float mutationPercent; float mutationChance; diff --git a/inc/entity.hpp b/inc/entity.hpp deleted file mode 100644 index 20c87aa..0000000 --- a/inc/entity.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef entity_h -#define entity_h - -#include "dna.hpp" -#include "rectangle.hpp" - -#include "opengl/graphicsdata.hpp" - -class Entity -{ - public: - void Place(); - - virtual void eat(int bite){}; - virtual void impregnate(DNA D){}; - - int getType(){return type;}; - virtual bool getGender(void){}; - virtual int getAmount(void){}; - Rectangle getRectangle(){return rect;}; - GraphicsData getGFXD(){return gfxData;}; - - - protected: - int type; - int gender; - bool pregnate; - Rectangle rect; - GraphicsData gfxData; -}; - -#endif diff --git a/inc/list.hpp b/inc/list.hpp index f62112c..93a0537 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -5,22 +5,21 @@ #include #include "constants.hpp" -#include "creature.hpp" -#include "resource.hpp" +#include "organism.hpp" #include "quadtree.hpp" class List { public: List(); - void Behavior(); - void Place(); - void Remove(); + void Behavior(); + void Place(); + void Remove(); - std::vector getNear(Creature c); + std::vector getNear(Organism o); - std::list resources; - std::list creatures; + std::list resources; + std::list creatures; Quadtree tree; std::vector drawQuadTree(); diff --git a/inc/opengl/spritebatch.hpp b/inc/opengl/spritebatch.hpp index 2618054..63e45eb 100644 --- a/inc/opengl/spritebatch.hpp +++ b/inc/opengl/spritebatch.hpp @@ -1,12 +1,12 @@ #ifndef spritebatch_h #define spritebatch_h +#include +#include #include + #include "graphicsdata.hpp" -#include #include "geoshader.hpp" -#include - class RenderBatch { public: diff --git a/inc/organism.hpp b/inc/organism.hpp new file mode 100644 index 0000000..9534fbb --- /dev/null +++ b/inc/organism.hpp @@ -0,0 +1,63 @@ +#ifndef organism_h +#define organism_h + +#include +#include + +#include "dna.hpp" +#include "rectangle.hpp" +#include "functions.hpp" + +#include "opengl/graphicsdata.hpp" + +class Organism +{ + public: + Organism(Rectangle r, DNA d); + + void Behavior(); + void Action(); + void Priority(); + void Place(); + void setTarget(); + void checkTarget(); + void moveTowards(Rectangle r); + void passDNA(DNA d); + void giveNearMe(std::vector n){nearMe = n;}; + void grow(); + void takeBite(int bite); + void hadPregnancy(){pregnate = pregnancyReady = false;}; + + DNA getDNA() {return myDNA;}; + DNA getChildsDNA() {return childsDNA;}; + GraphicsData getGFXD() {return gfxData;}; + Rectangle getRectangle() {return rect;}; + int getHealth() {return health;}; + int getBestSense() {return myDNA.bestSense;}; + int getType() {return myDNA.type;}; + bool getGender() {return gender;}; + bool getPregnancyReady() {return pregnancyReady;}; + + private: + Rectangle wTarget; + Organism* target; + std::vector nearMe; + DNA myDNA; + DNA childsDNA; + Rectangle rect; + GraphicsData gfxData; + + int health; + int pregnancyTime; + int age; + + bool gender; + bool pregnate; + bool hungry; + bool pregnancyReady; + bool able; + bool hasTarget; + bool wander; +}; + +#endif diff --git a/inc/quadtree.hpp b/inc/quadtree.hpp index bf308f8..8520584 100644 --- a/inc/quadtree.hpp +++ b/inc/quadtree.hpp @@ -4,9 +4,7 @@ #include #include "constants.hpp" -#include "creature.hpp" -#include "resource.hpp" -#include "entity.hpp" +#include "organism.hpp" #include "rectangle.hpp" #include "opengl/graphicsdata.hpp" @@ -18,10 +16,10 @@ class Quadtree { Quadtree(int pLevel,Rectangle pBounds); void clear(); - void insert(Entity* iter); + void insert(Organism* iter); - std::vector retrieve(std::vector returnObject, GraphicsData obj); - std::vector objects; + std::vector retrieve(std::vector returnObject, GraphicsData obj); + std::vector objects; Quadtree* nodes; diff --git a/inc/resource.hpp b/inc/resource.hpp deleted file mode 100644 index e559463..0000000 --- a/inc/resource.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef resource_h -#define resource_h - -#include - -#include "entity.hpp" -#include "functions.hpp" - -class Resource: public Entity -{ - public: - Resource(Rectangle r); - - void grow(); - void eat(int bite); - - int getAmount(){return amount;}; - - private: - int amount; - int growAmount; -}; - -#endif diff --git a/src/creature.cpp b/src/creature.cpp deleted file mode 100644 index 9cfdacb..0000000 --- a/src/creature.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include "creature.hpp" - -Creature::Creature(Rectangle r, DNA d) -{ - rect = r; - myDNA = d; - - if(rect.x == 0 && rect.y == 0){ - rect.x = getRandom(30); - rect.y = getRandom(30); - } - - type = CREATURE_TYPE; - health = myDNA.maxHealth/2; - gender = rand() % 2; - age = 0; - pregnancyTime = 0; - pregnancyReady = false; - pregnate = false; - hasTarget = false; - - if(gender) - gfxData = GraphicsData(rect.x, rect.y, 1, 0, 0, CREATURE_SIDES); - else - gfxData = GraphicsData(rect.x, rect.y, 0, 0, 1, CREATURE_SIDES); -} - -void Creature::Behavior() -{ - health-=1; - - this->Priority(); - - if(!hasTarget) - this->setTarget(); - else - this->checkTarget(); - - this->Action(); - - if(pregnate){ - pregnancyTime++; - if(pregnancyTime > myDNA.expectedPregnancyTime) - pregnancyReady = true; - } - - age++; - if(age > myDNA.expectedAge) - health = 0; -} - -void Creature::Priority() -{ - if(health < myDNA.maxHealth / 2){ - hungry = true; - able = false; - } - else{ - hungry = false; - able = true; - } -} - -void Creature::setTarget() -{ - std::random_shuffle(nearMe.begin(),nearMe.end()); - - for(std::vector ::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ - if( (*it)->getType() == RESOURCE_TYPE && hungry){ - target = *it; - hasTarget = true; - wander = false; - break; - } - if( (*it)->getType() == CREATURE_TYPE && able && (*it)->getGender() != gender ){ - target = *it; - hasTarget = true; - wander = false; - break; - } - } - - if(!hasTarget&&!wander){ - wander = true; - Rectangle tmp; - tmp.x = getRandom(30); - tmp.y = getRandom(30); - wTarget = tmp; - } -} - -void Creature::checkTarget() -{ - for(std::vector ::iterator it = nearMe.begin(); it!=nearMe.end(); it++) - if( target == *it ) - return; - - hasTarget = false; -} - - -void Creature::Action() -{ - if(hasTarget){ - if(Distance(rect,target->getRectangle()) < myDNA.reach){ - if(target->getType() == RESOURCE_TYPE){ - target->eat(myDNA.bite); - health+=myDNA.bite; - amountAte++; - if(target->getAmount()<=0) - hasTarget = false; - } - else if (target->getType() == CREATURE_TYPE){ - if(target->getGender() != gender){ - target->impregnate(myDNA); - } - hasTarget = false; - } - - } - else - moveTowards(target->getRectangle()); - } - else if(wander){ - if(Distance(rect,wTarget) < myDNA.reach) - wander = false; - else - moveTowards(wTarget); - } -} - -void Creature::moveTowards(Rectangle t) -{ - if( rect.x == t.x ){ - if( rect.y < t.y ) - rect.y+=myDNA.speed; - else - rect.y-=myDNA.speed; - } - else if( rect.y == t.y ){ - if( rect.x < t.x ) - rect.x+=myDNA.speed; - else - rect.x-=myDNA.speed; - } - else if( rect.x < t.x ){ - if( rect.y < t.y ){ - rect.x+=myDNA.speed; - rect.y+=myDNA.speed; - } - else{ - rect.x+=myDNA.speed; - rect.y-=myDNA.speed; - } - } - else if ( rect.x > t.x ){ - if( rect.y < t.y ){ - rect.x-=myDNA.speed; - rect.y+=myDNA.speed; - } - else{ - rect.x-=myDNA.speed; - rect.y-=myDNA.speed; - } - } -} - -void Creature::impregnate(DNA D) -{ - if(!pregnate){ - pregnate = true; - pregnancyTime = 0; - childsDNA = myDNA.combine(D); - } -} diff --git a/src/dna.cpp b/src/dna.cpp index 5d3fc8f..5d727d2 100644 --- a/src/dna.cpp +++ b/src/dna.cpp @@ -1,33 +1,46 @@ #include "dna.hpp" -DNA::DNA() +DNA::DNA(std::string s) { - maxHealth = CREATURE_MAX_HEALTH; - speed = CREATURE_SPEED; - reach = CREATURE_REACH; - bestSense = CREATURE_BEST_SENSE; - bite = CREATURE_BITE; - amountToGrow = CREATURE_AMOUNT_TO_GROW; - expectedPregnancyTime = CREATURE_EXP_PREG_TIME; - expectedAge = CREATURE_EXP_AGE; - sizeMax = CREATURE_SIZE_MAX; - mutationPercent = CREATURE_MUTATION_PERCENT; - mutationChance = CREATURE_MUTATION_CHANCE; + if(s == "creature"){ + type = CREATURE_TYPE; + maxHealth = CREATURE_MAX_HEALTH; + bestSense = CREATURE_BEST_SENSE; + bite = CREATURE_BITE; + expectedPregnancyTime = CREATURE_EXP_PREG_TIME; + expectedAge = CREATURE_EXP_AGE; + growAmount = 0; + reach = CREATURE_REACH; + speed = CREATURE_SPEED; + mutationPercent = CREATURE_MUTATION_PERCENT; + mutationChance = CREATURE_MUTATION_CHANCE; + } + else if(s == "resource"){ + type = RESOURCE_TYPE; + maxHealth = RESOURCE_MAX_HEALTH; + bestSense = 0; + bite = 0; + expectedPregnancyTime = 0; + expectedAge = 0; + growAmount = RESOURCE_GROW_AMOUNT; + reach = 0; + speed = 0; + mutationPercent = 0; + mutationChance = 0; + } } DNA DNA::combine(DNA D) { - DNA N; - + DNA N("empty"); + N.type = CREATURE_TYPE; N.maxHealth = (maxHealth + D.maxHealth)/2; N.speed = (speed + D.speed)/2; N.reach = (reach + D.reach)/2; N.bestSense = (bestSense + D.bestSense)/2; N.bite = (bite + D.bite)/2; - N.amountToGrow = (amountToGrow + D.amountToGrow)/2; N.expectedPregnancyTime = (expectedPregnancyTime + D.expectedPregnancyTime)/2; N.expectedAge = (expectedAge + D.expectedAge)/2; - N.sizeMax = (sizeMax + D.sizeMax)/2; N.mutationPercent = (mutationPercent + D.mutationPercent)/2; N.mutationChance = (mutationChance + D.mutationChance)/2; @@ -37,18 +50,16 @@ DNA DNA::combine(DNA D) pn = 1; else pn = -1; - switch(rand()%11){ + switch(rand()%9){ case 0: N.maxHealth = abs(N.maxHealth *(N.mutationPercent+pn)); break; case 1: N.speed = abs(N.speed *(N.mutationPercent+pn)); break; case 2: N.reach = abs(N.reach *(N.mutationPercent+pn)); break; case 3: N.bestSense = abs(N.bestSense *(N.mutationPercent+pn)); break; case 4: N.bite = abs(N.bite *(N.mutationPercent+pn)); break; - case 5: N.amountToGrow = abs(N.amountToGrow *(N.mutationPercent+pn)); break; - case 6: N.expectedPregnancyTime = abs(N.expectedPregnancyTime *(N.mutationPercent+pn)); break; - case 7: N.expectedAge = abs(N.expectedAge *(N.mutationPercent+pn)); break; - case 8: N.sizeMax = abs(N.sizeMax *(N.mutationPercent+pn)); break; - case 9: N.mutationPercent = abs(N.expectedAge *(N.mutationPercent+pn)); break; - case 10:N.mutationChance = abs(N.mutationChance *(N.mutationPercent+pn)); break; + case 5: N.expectedPregnancyTime = abs(N.expectedPregnancyTime *(N.mutationPercent+pn)); break; + case 6: N.expectedAge = abs(N.expectedAge *(N.mutationPercent+pn)); break; + case 7: N.mutationPercent = abs(N.expectedAge *(N.mutationPercent+pn)); break; + case 8: N.mutationChance = abs(N.mutationChance *(N.mutationPercent+pn)); break; } } return N; diff --git a/src/entity.cpp b/src/entity.cpp deleted file mode 100644 index ab94d23..0000000 --- a/src/entity.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "entity.hpp" - -void Entity::Place() -{ - gfxData.x = rect.x; - gfxData.y = rect.y; -} diff --git a/src/list.cpp b/src/list.cpp index 30b0eb1..826208e 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -3,16 +3,17 @@ List::List() { int i; - DNA d; + DNA d = DNA("creature"); Rectangle tmp; for(i=0;i::iterator it = creatures.begin(); it!= creatures.end(); it++) + for(std::list::iterator it = creatures.begin(); it!= creatures.end(); it++) if(it->getHealth()<=0){ - Resource Y(it->getRectangle()); - resources.push_back(Y); + DNA d = DNA("resource"); + Organism X(it->getRectangle(), d); + resources.push_back(X); creatures.erase(it--); } - for(std::list::iterator it = resources.begin(); it!= resources.end(); it++) - if(it->getAmount()<=0) + for(std::list::iterator it = resources.begin(); it!= resources.end(); it++) + if(it->getHealth()<=0) resources.erase(it--); } void List::Behavior() { - for(std::list::iterator it = creatures.begin(); it!= creatures.end(); it++){ - std::vector N = getNear(*it); - it->giveNearMe(N); + for(std::list::iterator it = creatures.begin(); it!= creatures.end(); it++){ + std::vector near = getNear(*it); + it->giveNearMe(near); it->Behavior(); if(it->getPregnancyReady()){ - Creature X(it->getRectangle(),it->getChildsDNA()); + Organism X(it->getRectangle(),it->getChildsDNA()); creatures.push_back(X); it->hadPregnancy(); } } - for(std::list::iterator it = resources.begin(); it!= resources.end(); it++) + for(std::list::iterator it = resources.begin(); it!= resources.end(); it++) it->grow(); } @@ -56,35 +58,37 @@ void List::Place() tree.clear(); Rectangle tmp; + DNA d = DNA("resource"); while(resources.size() < MINIMUM_RESOURCES){ - Resource Y(tmp); - resources.push_back(Y); + Organism X(tmp, d); + resources.push_back(X); } - for(std::list::iterator it = creatures.begin(); it!= creatures.end(); it++){ + for(std::list::iterator it = creatures.begin(); it!= creatures.end(); it++){ it->Place(); tree.insert(&(*it));; } - for(std::list::iterator it = resources.begin(); it!=resources.end(); it++){ + for(std::list::iterator it = resources.begin(); it!=resources.end(); it++){ it->Place(); tree.insert(&(*it));; } } -std::vector List::getNear(Creature c) +std::vector List::getNear(Organism c) { - std::vector near; + std::vector near; near = tree.retrieve(near, c.getGFXD()); - for(std::vector::iterator it = near.begin(); it!= near.end(); it++) + for(std::vector::iterator it = near.begin(); it!= near.end(); it++) if(c.getBestSense() < Distance(c.getRectangle(),(*it)->getRectangle())) near.erase(it--); return near; } -std::vector List::drawQuadTree(){ +std::vector List::drawQuadTree() +{ return tree.Draw(); } diff --git a/src/main.cpp b/src/main.cpp index d5a192d..43ec1e4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,10 +47,10 @@ int main() shader.Bind(); _spriteBatch.begin(); - for(std::list::iterator it = L.creatures.begin(); it != L.creatures.end(); it++) + for(std::list::iterator it = L.creatures.begin(); it != L.creatures.end(); it++) _spriteBatch.draw(it->getGFXD()); - for(std::list::iterator it = L.resources.begin(); it != L.resources.end(); it++) + for(std::list::iterator it = L.resources.begin(); it != L.resources.end(); it++) _spriteBatch.draw(it->getGFXD()); _spriteBatch.end(); diff --git a/src/organism.cpp b/src/organism.cpp new file mode 100644 index 0000000..27f6f8a --- /dev/null +++ b/src/organism.cpp @@ -0,0 +1,193 @@ +#include "organism.hpp" + +Organism::Organism(Rectangle r, DNA d) +{ + rect = r; + myDNA = d; + + if(rect.x == 0 && rect.y == 0){ + rect.x = getRandom(30); + rect.y = getRandom(30); + } + + health = myDNA.maxHealth/2; + gender = rand() % 2; + age = 0; + pregnancyTime = 0; + pregnancyReady = false; + pregnate = false; + hasTarget = false; + wander = false; + + if(myDNA.type == CREATURE_TYPE){ + if(gender) + gfxData = GraphicsData(rect.x, rect.y, 1, 0, 0, CREATURE_SIDES); + else + gfxData = GraphicsData(rect.x, rect.y, 0, 0, 1, CREATURE_SIDES); + } + else + gfxData = GraphicsData(rect.x, rect.y, 0, 1, 0, RESOURCE_SIDES); +} + +void Organism::Behavior() +{ + health-=1; + + this->Priority(); + + if(!hasTarget) + this->setTarget(); + else + this->checkTarget(); + + this->Action(); + + if(pregnate){ + pregnancyTime++; + if(pregnancyTime > myDNA.expectedPregnancyTime) + pregnancyReady = true; + } + + age++; + if(age > myDNA.expectedAge) + health = 0; +} + +void Organism::Priority() +{ + if(health < myDNA.maxHealth / 2){ + hungry = true; + able = false; + } + else{ + hungry = false; + able = true; + } +} + +void Organism::setTarget() +{ + std::random_shuffle(nearMe.begin(),nearMe.end()); + + for(std::vector::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ + if( (*it)->getType() == RESOURCE_TYPE && hungry){ + target = *it; + hasTarget = true; + wander = false; + break; + } + if( (*it)->getType() == CREATURE_TYPE && able && (*it)->getGender() != gender ){ + target = *it; + hasTarget = true; + wander = false; + break; + } + } + + if(!hasTarget&&!wander){ + wander = true; + Rectangle tmp(getRandom(30),getRandom(30),0,0); + wTarget = tmp; + } +} + +void Organism::checkTarget() +{ + for(std::vector::iterator it = nearMe.begin(); it!=nearMe.end(); it++) + if( target == *it ) + return; + + hasTarget = false; +} + + +void Organism::Action() +{ + if(hasTarget){ + if(Distance(rect,target->getRectangle()) < myDNA.reach){ + if(target->getType() == RESOURCE_TYPE){ + target->takeBite(myDNA.bite); + health+=myDNA.bite; + if(target->getHealth()<=0) + hasTarget = false; + } + else if (target->getType() == CREATURE_TYPE){ + if(target->getGender() != gender){ + target->passDNA(myDNA); + } + hasTarget = false; + } + + } + else + moveTowards(target->getRectangle()); + } + else if(wander){ + if(Distance(rect,wTarget) < myDNA.reach) + wander = false; + else + moveTowards(wTarget); + } +} + +void Organism::moveTowards(Rectangle t) +{ + if( rect.x == t.x ){ + if( rect.y < t.y ) + rect.y+=myDNA.speed; + else + rect.y-=myDNA.speed; + } + else if( rect.y == t.y ){ + if( rect.x < t.x ) + rect.x+=myDNA.speed; + else + rect.x-=myDNA.speed; + } + else if( rect.x < t.x ){ + if( rect.y < t.y ){ + rect.x+=myDNA.speed; + rect.y+=myDNA.speed; + } + else{ + rect.x+=myDNA.speed; + rect.y-=myDNA.speed; + } + } + else if ( rect.x > t.x ){ + if( rect.y < t.y ){ + rect.x-=myDNA.speed; + rect.y+=myDNA.speed; + } + else{ + rect.x-=myDNA.speed; + rect.y-=myDNA.speed; + } + } +} + +void Organism::passDNA(DNA d) +{ + if(!pregnate){ + pregnate = true; + pregnancyTime = 0; + childsDNA = myDNA.combine(d); + } +} + +void Organism::grow() +{ + if(health < myDNA.maxHealth) + health+=myDNA.growAmount; +} + +void Organism::takeBite(int bite) +{ + health-=bite; +} + +void Organism::Place() +{ + gfxData.x = rect.x; + gfxData.y = rect.y; +} diff --git a/src/quadtree.cpp b/src/quadtree.cpp index 909c875..ada839a 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -76,7 +76,7 @@ int Quadtree::getIndex(GraphicsData object) { return index; } -void Quadtree::insert(Entity* iter){ +void Quadtree::insert(Organism* iter){ if (!nodes[0].isNull) { int index = getIndex((*iter).getGFXD()); if (index != -1) { @@ -92,7 +92,7 @@ void Quadtree::insert(Entity* iter){ split(); int index; - for(std::vector::iterator it = objects.begin(); it!=objects.end();it++){ + for(std::vector::iterator it = objects.begin(); it!=objects.end();it++){ index = getIndex((*it)->getGFXD()); if (index != -1) { nodes[index].insert(*it); @@ -126,12 +126,12 @@ std::vector Quadtree::Draw(){ return retdat; } -std::vector Quadtree::retrieve(std::vector returnObjects, GraphicsData obj) { +std::vector Quadtree::retrieve(std::vector returnObjects, GraphicsData obj) { int index = getIndex(obj); if (index != -1 && !nodes[0].isNull) returnObjects = nodes[index].retrieve(returnObjects, obj); - for(std::vector::iterator it = objects.begin(); it!=objects.end(); it++) + for(std::vector::iterator it = objects.begin(); it!=objects.end(); it++) returnObjects.emplace_back(*it); return returnObjects; diff --git a/src/resource.cpp b/src/resource.cpp deleted file mode 100644 index c3e61d6..0000000 --- a/src/resource.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "resource.hpp" - -Resource::Resource(Rectangle r) -{ - rect = r; - - if(rect.x == 0 && rect.y == 0){ - rect.x = getRandom(30); - rect.y = getRandom(30); - } - - gfxData = GraphicsData(rect.x, rect.y, 0, 1, 0, RESOURCE_SIDES); - - type = RESOURCE_TYPE; - amount = RESOURCE_AMOUNT_START; - growAmount = RESOURCE_GROW; -} - -void Resource::eat(int bite) -{ - 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); - } -} -- cgit v1.2.3