summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/constants.hpp43
-rw-r--r--inc/creature.hpp39
-rw-r--r--inc/entity.hpp6
-rw-r--r--inc/functions.hpp12
-rw-r--r--inc/list.hpp2
-rw-r--r--inc/resource.hpp5
-rw-r--r--inc/timer.hpp2
-rw-r--r--src/creature.cpp92
-rw-r--r--src/entity.cpp13
-rw-r--r--src/list.cpp7
-rw-r--r--src/resource.cpp21
11 files changed, 155 insertions, 87 deletions
diff --git a/inc/constants.hpp b/inc/constants.hpp
index 8496e5c..fd00fb1 100644
--- a/inc/constants.hpp
+++ b/inc/constants.hpp
@@ -1,29 +1,30 @@
#ifndef constants_h
#define constants_h
-const int CREATURES = 100;
-const int RESOURCES = 1000;
-const int MINIMUM_RESOURCES = 700;
-const int WINDOW_X = 1080;
-const int WINDOW_Y = 640;
+const int CREATURES = 10;
+const int RESOURCES = 100;
+const int MINIMUM_RESOURCES = 70;
+const int WINDOW_X = 500;
+const int WINDOW_Y = 500;
-const int CREATURE_TYPE = 1;
-const int RESOURCE_TYPE = 2;
+const int CREATURE_TYPE = 1;
+const int RESOURCE_TYPE = 2;
-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 = 5;
-const int CREATURE_SIZE_START = 1;
-const int CREATURE_BITE = 10;
-const int CREATURE_AMOUNT_TO_GROW = 50;
+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 = 10;
+const int CREATURE_SIZE_START = 5;
+const int CREATURE_BITE = 10;
+const int CREATURE_AMOUNT_TO_GROW = 50;
+const int CREATURE_EXPECTED_PREGNANCY_TIME = 100;
-const int RESOURCE_SIZE_START = 5;
-const int RESOURCE_SIZE_MAX = 2;
-const int RESOURCE_AMOUNT_START = 100;
-const int RESOURCE_AMOUNT_MAX = 200;
-const int RESOURCE_GROW = 1;
+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;
#endif
diff --git a/inc/creature.hpp b/inc/creature.hpp
index edc253f..79ec4c3 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -6,28 +6,32 @@
#include "entity.hpp"
#include "constants.hpp"
+#include "functions.hpp"
class Creature: public Entity
{
public:
Creature(Window m, SDL_Rect R);
- void Behavior();
- void Action();
- void Priority();
- void setTarget();
- void Move(SDL_Rect R);
-
+ void Behavior();
+ void Action();
+ void Priority();
+ void setTarget();
+ void checkTarget();
+ void Move(SDL_Rect R);
+ void impregnate();
void giveN(list<Entity*> 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;};
-
+ bool getGender(){return gender;};
+ bool getPregnancyReady(){return pregnancyReady;};
+ void hadPregnancy(){pregnate = pregnancyReady = false;};
+
private:
- bool hasTarget;
- bool wander;
- SDL_Rect wTarget;
-
+ SDL_Rect wTarget;
+ Entity *target;
+ list<Entity*> N;
+
int health;
int reach;
int maxHealth;
@@ -36,13 +40,14 @@ class Creature: public Entity
int bite;
int amountAte;
int amountToGrow;
+ int pregnancyTime;
+ int expectedPregnancyTime;
bool hungry;
- bool gender;
+ bool pregnancyReady;
bool able;
-
- list<Entity*> N;
- Entity *target;
+ bool hasTarget;
+ bool wander;
};
#endif
diff --git a/inc/entity.hpp b/inc/entity.hpp
index 6587f11..a7b7f58 100644
--- a/inc/entity.hpp
+++ b/inc/entity.hpp
@@ -12,10 +12,14 @@ class Entity
SDL_Rect getRect(){return rect;};
virtual void eat(int bite){};
- virtual int getAmount(void){};
+ virtual void impregnate(void){};
+ virtual bool getGender(void){};
+ virtual int getAmount(void){};
protected:
int type;
+ int gender;
+ bool pregnate;
SDL_Rect rect;
SDL_Renderer* renderer;
};
diff --git a/inc/functions.hpp b/inc/functions.hpp
new file mode 100644
index 0000000..2ddcda9
--- /dev/null
+++ b/inc/functions.hpp
@@ -0,0 +1,12 @@
+#ifndef functions_h
+#define functions_h
+
+static double Distance(SDL_Rect A, SDL_Rect B){
+ return sqrt( pow(A.x-B.x,2) + pow(A.y-B.y,2));
+}
+
+static int map(int x, int inMin, int inMax, int outMin, int outMax){
+ return (x-inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
+}
+
+#endif
diff --git a/inc/list.hpp b/inc/list.hpp
index 64ca522..cc78031 100644
--- a/inc/list.hpp
+++ b/inc/list.hpp
@@ -4,6 +4,7 @@
#include <SDL2/SDL.h>
#include <list>
+#include "functions.hpp"
#include "creature.hpp"
#include "resource.hpp"
#include "window.hpp"
@@ -16,7 +17,6 @@ class List
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<Entity*> getNear(Creature C);
private:
diff --git a/inc/resource.hpp b/inc/resource.hpp
index 5c740fa..cb5946c 100644
--- a/inc/resource.hpp
+++ b/inc/resource.hpp
@@ -2,16 +2,15 @@
#define resource_h
#include "entity.hpp"
+#include "functions.hpp"
class Resource: public Entity
{
public:
Resource(Window m, SDL_Rect Rect);
- void eat(int bite);
-
int getAmount(){return amount;};
void grow();
- int map(int x, int inMin, int inMax, int outMin, int outMax);
+ void eat(int bite);
private:
int amount;
diff --git a/inc/timer.hpp b/inc/timer.hpp
index c8f43ff..d4e46e8 100644
--- a/inc/timer.hpp
+++ b/inc/timer.hpp
@@ -11,7 +11,7 @@ class Timer
void Stop();
void Pause();
void unPause();
- int getTicks();
+ int getTicks();
bool isStarted(){return started;};
bool isPaused(){return paused;};
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 <Entity*>::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;
+ }
+}
diff --git a/src/entity.cpp b/src/entity.cpp
index 6ad0e3f..a2c2d87 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -2,9 +2,16 @@
void Entity::Place()
{
- if(type == 1)
- SDL_SetRenderDrawColor(renderer,255,0,255,255);
- else
+ if(type == CREATURE_TYPE){
+ if(gender)
+ SDL_SetRenderDrawColor(renderer,255,0,0,255);
+ else
+ if(pregnate)
+ SDL_SetRenderDrawColor(renderer,255,0,255,255);
+ else
+ SDL_SetRenderDrawColor(renderer,0,0,255,255);
+ }
+ else if (type == RESOURCE_TYPE)
SDL_SetRenderDrawColor(renderer,0,255,0,255);
SDL_RenderFillRect(renderer, &rect);
diff --git a/src/list.cpp b/src/list.cpp
index 5309109..d49f97a 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -40,6 +40,13 @@ void List::Behavior()
list<Entity*> N = getNear(*it);
it->giveN(N);
it->Behavior();
+ if(it->getPregnancyReady()){
+ SDL_Rect Rect = it->getRect();
+ Rect.h = Rect.w = CREATURE_SIZE_START;
+ Creature X(main,Rect);
+ C.push_back(X);
+ it->hadPregnancy();
+ }
}
for(list<Resource>::iterator it = R.begin(); it!=R.end(); it++)
diff --git a/src/resource.cpp b/src/resource.cpp
index 07f7ca9..2f2181f 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -2,13 +2,13 @@
Resource::Resource(Window m, SDL_Rect R)
{
- renderer = m.getRenderer();
- type = RESOURCE_TYPE;
- rect = R;
+ renderer = m.getRenderer();
+ type = RESOURCE_TYPE;
+ 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;
}
amount = RESOURCE_AMOUNT_START;
@@ -22,13 +22,8 @@ void Resource::eat(int bite)
void Resource::grow()
{
- if(amount < RESOURCE_AMOUNT_MAX)
+ 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;
+ rect.h = rect.w = map(amount,0,RESOURCE_AMOUNT_MAX,0,RESOURCE_SIZE_MAX);
+ }
}