summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom <spalf0@gmail.com>2015-08-01 12:56:55 -0500
committerTom <spalf0@gmail.com>2015-08-01 12:56:55 -0500
commit1ddb996ac4a4fc78e5484acdadac2df95006b632 (patch)
tree853b1e65b6c8c08533c91ae08acf7b699cce2538
parent6b967f3bd7a3e7203b57bd9e4edb0db82bc9ed1c (diff)
pushing before format
-rw-r--r--inc/creature.hpp17
-rw-r--r--src/creature.cpp51
-rw-r--r--src/list.cpp28
3 files changed, 83 insertions, 13 deletions
diff --git a/inc/creature.hpp b/inc/creature.hpp
index 24cad37..64efd26 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -13,19 +13,30 @@ class Creature: public Entity
bool Action();
void Priority();
Location getLocation();
- void give(vector<Resource*> n){nR=n;};
+ void giveR(vector<Resource*> n){nR=n;};
+ void giveC(vector<Creature*> n){nC=n;};
int getHealth(){return health;};
double Distance(Location A, Location B);
+ bool doesItHaveTarget();
+ int getBestSense(){return bestSense;};
private:
int xTarget; //x-coordinate of creature's target position
int yTarget; //y-coordinate of creature's target position
+
bool hasTarget;
+ bool wandering;
+
int health; //health of a creature (0-100)
+ int maxHealth;
int hunger; //value associated with a creatures want to find food (0-100)
int speed = 1;
- vector<Resource*> nR; //vector containing objects near the creature
- int n; // counter for which place in resource array is targeted
+ bool able; // ability to reproduce
+ int bestSense = 100; // Distance it can see/smell/hear
+
+ vector<Resource*> nR; //vector containing resources near the creature
+ vector<Creature*> nC; //vector containing creatures near the creature
+ int n; // counter for which place in resource array is targeted
};
#endif
diff --git a/src/creature.cpp b/src/creature.cpp
index 9590dde..d43bc2a 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -4,13 +4,16 @@ Creature::Creature(Window m, std::string s) //Constructor
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
- health = 100;
+ health = 500;
+ maxHealth = 1000;
hunger = 0;
//initializes random start coordinates for creature, target position is equivalent to it's position
yPosition=yTarget=rand()%800;
xPosition=xTarget=rand()%1200;
hasTarget = false;
+ wandering = false;
+ able = true;
n=0;
}
@@ -25,7 +28,7 @@ int Creature::Behavior()
if(nR.size())
{
nR[n]->eat();
- if(health<500)
+ if(health<maxHealth)
health+=10;
}
}
@@ -37,10 +40,15 @@ void Creature::Priority()
{
double d; // lol
+ // Gets location for closest resource
for(int i = 0; i < nR.size(); i++)
{
- if(!i)
+ if(i==0)
+ {
d = Distance(this->getLocation(),nR[0]->getLocation());
+ n = 0;
+ continue;
+ }
if(d>Distance(this->getLocation(),nR[i]->getLocation()))
{
@@ -49,14 +57,37 @@ void Creature::Priority()
}
}
- if(nR.size())
+ if(nR.size()==0)
+ hasTarget=false;
+
+ // If there is available targets and the unit doesnt have a target, assign the closest one.
+ //cout << "size: " << nR.size() << endl;
+ //cout << "hastarget: "<< hasTarget << endl;
+ if(nR.size()>0&&!hasTarget)
{
xTarget = nR[n]->getLocation().x;
yTarget = nR[n]->getLocation().y;
hasTarget = true;
+ wandering = false;
+ }
+ // If there is not available targets and doesnt have a target, set a random location as a target
+ else if(nR.size()==0&&!hasTarget)
+ {
+ if(!wandering)
+ {
+ xTarget = rand()%1200;
+ yTarget = rand()%800;
+ wandering = true;
+ hasTarget = false;
+ }
+ else
+ {
+ Location L(xTarget,yTarget,1);
+ if(Distance(this->getLocation(),L)<5)
+ wandering = false;
+ hasTarget = false;
+ }
}
- else
- hasTarget = false;
}
bool Creature::Action()
@@ -69,7 +100,10 @@ bool Creature::Action()
if(5 > Distance(this->getLocation(),nR[n]->getLocation()))
{
if(hasTarget)
+ {
+ hasTarget = false;
return true;
+ }
else
return false;
}
@@ -136,3 +170,8 @@ double Creature::Distance(Location A, Location B)
//computes distance between two points
return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2));
}
+
+bool Creature::doesItHaveTarget()
+{
+ return hasTarget;
+}
diff --git a/src/list.cpp b/src/list.cpp
index 93d2312..379a113 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -4,7 +4,7 @@ List::List(Window m) //Constructor
{
int i;
- for(i=0;i<25;i++)
+ for(i=0;i<3;i++)
{
Creature X(m,"img/Cbasic.png");
C.push_back(X);
@@ -25,6 +25,14 @@ void List::Place()
for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++)
it->Place();
+ /*
+ if(R.size()<15)
+ {
+ Resource Y(m,"img/Rbasic.png");
+ R.push_back(Y);
+ }
+ */
+
//places all resources
for(int j = 0; j<R.size(); j++)
{
@@ -44,12 +52,24 @@ void List::Behavior()
vector<Resource*> N;
for(int j = 0; j < R.size(); j++)
- if(250>Distance(C[i].getLocation(),R[j].getLocation()))
+ if(C[i].getBestSense()>Distance(C[i].getLocation(),R[j].getLocation()))
N.push_back(&R[j]);
-
- C[i].give(N);
+
+ C[i].giveR(N);
N.clear();
+ vector<Creature*> M;
+ for(int j = 0; j < C.size(); j++)
+ {
+ if(j==i)
+ continue;
+ else if(C[i].getBestSense()>Distance(C[i].getLocation(),C[j].getLocation()))
+ M.push_back(&C[j]);
+ }
+
+ C[i].giveC(M);
+ M.clear();
+
// This kills the creature
if(C[i].getHealth()<=0)
{