From 60bc5dcb19a983e8ae335ba89f860c59473303fb Mon Sep 17 00:00:00 2001 From: tom Date: Sun, 12 Feb 2017 08:42:55 -0600 Subject: -added DNA combining, currently it just takes the average of the mother and father -there maybe a random segfault, further investigation is needed --- README.md | 2 +- inc/creature.hpp | 12 +++++++----- inc/dna.hpp | 1 + inc/entity.hpp | 3 ++- src/creature.cpp | 5 +++-- src/dna.cpp | 17 +++++++++++++++++ src/list.cpp | 2 +- 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a27a79d..8124d17 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ ideas - click to add entities - randomly generate new types of creatures - gui speed up / down -- DNA which is mixed with mate and passed to child +- DNA mutations - pick a c naming/coding standard and stick with it - show framerate in gui diff --git a/inc/creature.hpp b/inc/creature.hpp index c3ed2d8..56954fe 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -20,10 +20,11 @@ class Creature: public Entity void setTarget(); void checkTarget(); void Move(SDL_Rect R); - void impregnate(); + void impregnate(Dna D); void giveN(std::vector n){N = n;}; Dna getDNA(){return mine;}; + Dna getChildDNA(){return childs;}; int getHealth(){return health;}; int getBestSense(){return mine.bestSense;}; bool getGender(){return gender;}; @@ -31,10 +32,11 @@ class Creature: public Entity void hadPregnancy(){pregnate = pregnancyReady = false;}; private: - SDL_Rect wTarget; - Entity *target; - std::vectorN; - Dna mine; + SDL_Rect wTarget; + Entity *target; + std::vector N; + Dna mine; + Dna childs; int health; int amountAte; diff --git a/inc/dna.hpp b/inc/dna.hpp index 4924e15..cd76731 100644 --- a/inc/dna.hpp +++ b/inc/dna.hpp @@ -7,6 +7,7 @@ class Dna { public: Dna(); + Dna combine(Dna D); int maxHealth; int speed; int reach; diff --git a/inc/entity.hpp b/inc/entity.hpp index a7b7f58..0eb8f60 100644 --- a/inc/entity.hpp +++ b/inc/entity.hpp @@ -2,6 +2,7 @@ #define entity_h #include "window.hpp" +#include "dna.hpp" class Entity { @@ -12,7 +13,7 @@ class Entity SDL_Rect getRect(){return rect;}; virtual void eat(int bite){}; - virtual void impregnate(void){}; + virtual void impregnate(Dna D){}; virtual bool getGender(void){}; virtual int getAmount(void){}; diff --git a/src/creature.cpp b/src/creature.cpp index 0b7a438..1cd0a52 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -107,7 +107,7 @@ void Creature::Action() hasTarget = false; } else if( Distance(rect,target->getRect()) < mine.reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ - target->impregnate(); + target->impregnate(mine); hasTarget = false; } else @@ -157,10 +157,11 @@ void Creature::Move(SDL_Rect R) } } -void Creature::impregnate() +void Creature::impregnate(Dna D) { if(!pregnate){ pregnate = true; pregnancyTime = 0; + childs = mine.combine(D); } } diff --git a/src/dna.cpp b/src/dna.cpp index 36eda3d..1145276 100644 --- a/src/dna.cpp +++ b/src/dna.cpp @@ -12,3 +12,20 @@ Dna::Dna() expectedAge = CREATURE_EXP_AGE; sizeMax = CREATURE_SIZE_MAX; } + +Dna Dna::combine(Dna D) +{ + Dna N; + + N.maxHealth = (this->maxHealth + D.maxHealth)/2; + N.speed = (this->speed + D.speed)/2; + N.reach = (this->reach + D.reach)/2; + N.bestSense = (this->bestSense + D.bestSense)/2; + N.bite = (this->bite + D.bite)/2; + N.amountToGrow = (this->amountToGrow + D.amountToGrow)/2; + N.expectedPregnancyTime = (this->expectedPregnancyTime + D.expectedPregnancyTime)/2; + N.expectedAge = (this->expectedAge + D.expectedAge)/2; + N.sizeMax = (this->sizeMax + D.sizeMax)/2; + + return N; +} diff --git a/src/list.cpp b/src/list.cpp index 0439278..4f2ddc4 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -43,7 +43,7 @@ void List::Behavior() it->Behavior(); if(it->getPregnancyReady()){ - Dna D = it->getDNA(); + Dna D = it->getChildDNA(); SDL_Rect Rect = it->getRect(); Rect.h = Rect.w = D.sizeMax / 5; Creature X(main,Rect,D); -- cgit v1.2.3