summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/creature.hpp6
-rw-r--r--inc/functions.hpp4
-rw-r--r--src/creature.cpp102
-rw-r--r--src/list.cpp1
-rw-r--r--src/main.cpp1
-rw-r--r--src/resource.cpp38
-rw-r--r--src/window.cpp1
7 files changed, 78 insertions, 75 deletions
diff --git a/inc/creature.hpp b/inc/creature.hpp
index 0f9128f..01f6557 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -2,7 +2,9 @@
#define creature_h
#include <cstdlib>
+#include <vector>
#include <list>
+#include <algorithm>
#include "entity.hpp"
#include "functions.hpp"
@@ -19,7 +21,7 @@ class Creature: public Entity
void checkTarget();
void moveTowards(Rectangle t);
void impregnate(DNA D);
- void giveNearMe(std::list<Entity*> n){nearMe = n;};
+ void giveNearMe(std::list<Entity*> n){nearMe = {std::begin(n),std::end(n)};};
DNA getDNA(){return myDNA;};
DNA getChildsDNA(){return childsDNA;};
@@ -32,7 +34,7 @@ class Creature: public Entity
private:
Rectangle wTarget;
Entity* target;
- std::list<Entity*> nearMe;
+ std::vector<Entity*> nearMe;
DNA myDNA;
DNA childsDNA;
diff --git a/inc/functions.hpp b/inc/functions.hpp
index 7ce9f8d..b0ecb65 100644
--- a/inc/functions.hpp
+++ b/inc/functions.hpp
@@ -13,4 +13,8 @@ static int map(int x, int inMin, int inMax, int outMin, int outMax){
return (x-inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
+static float getRandom(float x){
+ return (-x + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(x-(-x)))));
+}
+
#endif
diff --git a/src/creature.cpp b/src/creature.cpp
index 9f8204b..bf0ad43 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -2,66 +2,61 @@
Creature::Creature(Rectangle t, DNA D)
{
- rect = t;
- myDNA = D;
-
- if(rect.x == 0 && rect.y == 0){
- rect.x = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30))));
- rect.y = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30))));
- }
+ rect = t;
+ myDNA = D;
- gfxData.sides = 4.0;
- gfxData.x = rect.x;
- gfxData.y = rect.y;
-
- type = CREATURE_TYPE;
- health = myDNA.maxHealth/2;
- gender = rand() % 2;
- age = 0;
- pregnancyTime = 0;
- pregnancyReady = false;
- pregnate = false;
- hasTarget = false;
-
- if(gender){
- gfxData.r = 1.0;
- gfxData.g = 0.0;
- gfxData.b = 0.0;
- }
- else if(pregnate){
- gfxData.r = 1.0;
- gfxData.g = 0.0;
- gfxData.b = 1.0;
- }
- else{
- gfxData.r = 0.0;
- gfxData.g = 0.0;
- gfxData.b = 1.0;
- }
+ if(rect.x == 0 && rect.y == 0){
+ rect.x = getRandom(50);
+ rect.y = getRandom(50);
+ }
+
+ gfxData.sides = 4.0;
+ gfxData.x = rect.x;
+ gfxData.y = rect.y;
+
+ type = CREATURE_TYPE;
+ health = myDNA.maxHealth/2;
+ gender = rand() % 2;
+ age = 0;
+ pregnancyTime = 0;
+ pregnancyReady = false;
+ pregnate = false;
+ hasTarget = false;
+
+ if(gender){
+ gfxData.r = 1.0;
+ gfxData.g = 0.0;
+ gfxData.b = 0.0;
+ }
+ else{
+ gfxData.r = 0.0;
+ gfxData.g = 0.0;
+ gfxData.b = 1.0;
+ }
}
void Creature::Behavior()
{
- health-=1;
+ health-=1;
this->Priority();
- if(!hasTarget)
- this->setTarget();
- else
- this->checkTarget();
+ if(!hasTarget)
+ this->setTarget();
+ else
+ this->checkTarget();
this->Action();
- if(pregnate){
- pregnancyTime++;
- if(pregnancyTime > myDNA.expectedPregnancyTime)
- pregnancyReady = true;
- }
+ if(pregnate){
+ pregnancyTime++;
+ if(pregnancyTime > myDNA.expectedPregnancyTime)
+ pregnancyReady = true;
+ }
- age++;
- if(age > myDNA.expectedAge)
- health = 0;
+ age++;
+ if(age > myDNA.expectedAge)
+ health = 0;
}
void Creature::Priority()
@@ -78,8 +73,9 @@ void Creature::Priority()
void Creature::setTarget()
{
- //std::random_shuffle(N.begin(),N.end());
- for(std::list <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){
+ std::random_shuffle(nearMe.begin(),nearMe.end());
+
+ for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){
if( (*it)->getType() == RESOURCE_TYPE && hungry){
target = *it;
hasTarget = true;
@@ -96,8 +92,8 @@ void Creature::setTarget()
if(!hasTarget&&!wander){
wander = true;
- float x = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30))));
- float y = -30.0 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30-(-30))));
+ float x = getRandom(50);
+ float y = getRandom(50);
Rectangle tmp;
tmp.x = x;
tmp.y = y;
@@ -107,7 +103,7 @@ void Creature::setTarget()
void Creature::checkTarget()
{
- for(std::list <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++)
+ for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++)
if( target == *it )
return;
diff --git a/src/list.cpp b/src/list.cpp
index 71c0ba9..e91e1aa 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -11,7 +11,6 @@ List::List()
creatures.push_back(X);
}
- //rect = {0,0,RESOURCE_SIZE_START,RESOURCE_SIZE_START};
for(i=0;i<RESOURCES;i++){
Resource Y(tmp);
resources.push_back(Y);
diff --git a/src/main.cpp b/src/main.cpp
index d5a192d..1cec997 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -46,6 +46,7 @@ int main()
shader.Bind();
_spriteBatch.begin();
+ printf("%d\n",L.creatures.size());
for(std::list<Creature>::iterator it = L.creatures.begin(); it != L.creatures.end(); it++)
_spriteBatch.draw(it->getGFXD());
diff --git a/src/resource.cpp b/src/resource.cpp
index 9933c4f..81fd6b7 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -2,23 +2,23 @@
Resource::Resource(Rectangle t)
{
- rect = t;
-
- if(rect.x == 0 && rect.y == 0){
- rect.x = -30 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30.0-(-30.0))));
- rect.y = -30 + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(30.0-(-30.0))));
- }
+ rect = t;
- gfxData.x = rect.x;
- gfxData.y = rect.y;
- gfxData.r = 0.0;
- gfxData.g = 1.0;
- gfxData.b = 0.0;
- gfxData.sides = 10.0;
+ if(rect.x == 0 && rect.y == 0){
+ rect.x = getRandom(50);
+ rect.y = getRandom(50);
+ }
- type = RESOURCE_TYPE;
- amount = RESOURCE_AMOUNT_START;
- growAmount = RESOURCE_GROW;
+ gfxData.x = rect.x;
+ gfxData.y = rect.y;
+ gfxData.r = 0.0;
+ gfxData.g = 1.0;
+ gfxData.b = 0.0;
+ gfxData.sides = 10.0;
+
+ type = RESOURCE_TYPE;
+ amount = RESOURCE_AMOUNT_START;
+ growAmount = RESOURCE_GROW;
}
void Resource::eat(int bite)
@@ -28,8 +28,8 @@ void Resource::eat(int 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);
- }
+ if(amount < RESOURCE_AMOUNT_MAX){
+ amount+=growAmount;
+ rect.h = rect.w = map(amount,0,RESOURCE_AMOUNT_MAX,0,RESOURCE_SIZE_MAX);
+ }
}
diff --git a/src/window.cpp b/src/window.cpp
index 9306b15..bd6fdcf 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -13,6 +13,7 @@ Window::Window(int width, int height, const std::string& title)
main = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_OPENGL);
glContext = SDL_GL_CreateContext(main);
+ SDL_GL_SetSwapInterval(0);
GLenum status = glewInit();
closed = false;