summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom <spalf0@gmail.com>2015-05-26 14:04:04 -0500
committerTom <spalf0@gmail.com>2015-05-26 14:04:04 -0500
commit6b967f3bd7a3e7203b57bd9e4edb0db82bc9ed1c (patch)
tree8dfb8ce54fb9ef775bd2153aae67307d1f943e78
parentce6b35d717de85a7ac45fa98197edf13e1820c8a (diff)
revamped the list and creatures
two bugs i can see right now -somehow when a resource has less than 0 amount it still sticks around (somewhat rare) -some creatures just bounce back and forth until they die not getting to their target
-rw-r--r--inc/creature.hpp9
-rw-r--r--inc/list.hpp1
-rw-r--r--inc/resource.hpp2
-rw-r--r--inc/window.hpp2
-rw-r--r--src/creature.cpp80
-rw-r--r--src/list.cpp79
-rw-r--r--src/main.cpp2
-rw-r--r--src/resource.cpp5
8 files changed, 91 insertions, 89 deletions
diff --git a/inc/creature.hpp b/inc/creature.hpp
index 7c5c8cb..24cad37 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -2,6 +2,7 @@
#define creature_h
#include "entity.hpp"
+#include "resource.hpp"
#include "location.hpp"
class Creature: public Entity
@@ -12,15 +13,19 @@ class Creature: public Entity
bool Action();
void Priority();
Location getLocation();
- void giveKnown(std::vector<Location> Z){location = Z;};
+ void give(vector<Resource*> n){nR=n;};
int getHealth(){return health;};
+ double Distance(Location A, Location B);
private:
int xTarget; //x-coordinate of creature's target position
int yTarget; //y-coordinate of creature's target position
+ bool hasTarget;
int health; //health of a creature (0-100)
int hunger; //value associated with a creatures want to find food (0-100)
- std::vector<Location> location; //vector containing objects near the creature
+ int speed = 1;
+ vector<Resource*> nR; //vector containing objects near the creature
+ int n; // counter for which place in resource array is targeted
};
#endif
diff --git a/inc/list.hpp b/inc/list.hpp
index f370d24..934c319 100644
--- a/inc/list.hpp
+++ b/inc/list.hpp
@@ -19,7 +19,6 @@ class List
Window main = Window("no");//will be needed for adding R's and C's after constructor.
std::vector<Resource> R;
std::vector<Creature> C;
- std::vector<Location> L;
};
#endif
diff --git a/inc/resource.hpp b/inc/resource.hpp
index a1dd988..c551844 100644
--- a/inc/resource.hpp
+++ b/inc/resource.hpp
@@ -10,7 +10,7 @@ class Resource: public Entity
Resource(Window m, std::string s);
Resource(Window m, std::string s, Location z);
Location getLocation();
- void eat(){amount-=10;};
+ void eat();
int getAmount(){return amount;};
private:
diff --git a/inc/window.hpp b/inc/window.hpp
index 073a106..1ea2733 100644
--- a/inc/window.hpp
+++ b/inc/window.hpp
@@ -13,6 +13,8 @@
#include <chrono>
#include <random>
+using namespace std;
+
class Window
{
public:
diff --git a/src/creature.cpp b/src/creature.cpp
index c7b36ca..9590dde 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -10,74 +10,99 @@ Creature::Creature(Window m, std::string s) //Constructor
//initializes random start coordinates for creature, target position is equivalent to it's position
yPosition=yTarget=rand()%800;
xPosition=xTarget=rand()%1200;
+ hasTarget = false;
+ n=0;
}
int Creature::Behavior()
{
health-=1; //Decrements health each time a behavior is executed
- this->Priority(); //Checks which action has priority (doesn't really do this right now)
+
+ this->Priority();
if(this->Action())
{
- health+=10;
- return 2;
+ if(nR.size())
+ {
+ nR[n]->eat();
+ if(health<500)
+ health+=10;
+ }
}
+
return 0;
}
void Creature::Priority()
{
- //Traverses location vector, if object at [i] is resource (2), then creature's target coordinates are set
- int i;
- for(i=0;i<location.size();i++)
+ double d; // lol
+
+ for(int i = 0; i < nR.size(); i++)
{
- if(location[i].type==2)
+ if(!i)
+ d = Distance(this->getLocation(),nR[0]->getLocation());
+
+ if(d>Distance(this->getLocation(),nR[i]->getLocation()))
{
- xTarget = location[i].x;
- yTarget = location[i].y;
+ d=Distance(this->getLocation(),nR[i]->getLocation());
+ n=i;
}
}
+
+ if(nR.size())
+ {
+ xTarget = nR[n]->getLocation().x;
+ yTarget = nR[n]->getLocation().y;
+ hasTarget = true;
+ }
+ else
+ hasTarget = false;
}
bool Creature::Action()
{
//If the distance is close, will return an bool
-
//if(xPosition == xTarget && yPosition == yTarget)
// return false;
- if(sqrt(pow(xPosition - xTarget, 2) + pow(yPosition - yTarget, 2)) < 2)
- return true;
+ if(nR.size())
+ if(5 > Distance(this->getLocation(),nR[n]->getLocation()))
+ {
+ if(hasTarget)
+ return true;
+ else
+ return false;
+ }
//Makes moves towards target coordinates
if(xPosition==xTarget)
{
if(yPosition<yTarget)
- yPosition++;
+ yPosition+=speed;
else
- yPosition--;
+ yPosition-=speed;
}
else if(yPosition==yTarget)
{
if(xPosition<xTarget)
- xPosition++;
+ xPosition+=speed;
else
- xPosition--;
+ xPosition-=speed;
}
else if(xPosition<xTarget)
{
if(yPosition<yTarget)
{
- xPosition++;
- yPosition++;
+ xPosition+=speed;
+ yPosition+=speed;
}
else
{
- xPosition++;
- yPosition--;
+ xPosition+=speed;
+ yPosition-=speed;
}
}
@@ -85,16 +110,17 @@ bool Creature::Action()
{
if(yPosition<yTarget)
{
- xPosition--;
- yPosition++;
+ xPosition-=speed;
+ yPosition+=speed;
}
else
{
- xPosition--;
- yPosition--;
+ xPosition-=speed;
+ yPosition-=speed;
}
}
+
return false;
}
@@ -104,3 +130,9 @@ Location Creature::getLocation()
Location L(xPosition, yPosition, 1);
return L;
}
+
+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));
+}
diff --git a/src/list.cpp b/src/list.cpp
index cd69f8f..93d2312 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -4,18 +4,16 @@ List::List(Window m) //Constructor
{
int i;
- for(i=0;i<10;i++)
+ for(i=0;i<25;i++)
{
Creature X(m,"img/Cbasic.png");
C.push_back(X);
}
- //Creates 5 resources, inserts them into vector R; inserts locations of resources into vector L
- for(i=0;i<5;i++)
+ for(i=0;i<100;i++)
{
Resource Y(m,"img/Rbasic.png");
R.push_back(Y);
- L.push_back(Y.getLocation());
}
main = m;
@@ -23,73 +21,34 @@ List::List(Window m) //Constructor
void List::Place()
{
- int i;
-
- //if any locations are creatures, erases them from vector L
- for(i = 0;i < L.size(); i++)
- if(L[i].type==1)
- L.erase(L.begin()+i);
-
- //places each creature on window, inserts their locations into vector L
- for(i = 0; i < C.size(); i++)
- {
- C[i].Place();
- L.push_back(C[i].getLocation());
- }
+ //places each creature on window
+ for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++)
+ it->Place();
//places all resources
- for(i = 0; i < R.size(); i++)
+ for(int j = 0; j<R.size(); j++)
{
- R[i].Place();
+ if(R[j].getAmount()<=0)
+ R.erase(R.begin()+j);
+ else
+ R[j].Place();
}
}
void List::Behavior()
{
- int i, j, k, l;
- std::vector<Location> Z;
-
- for(i = 0; i < C.size(); i++)
+ for(int i = 0; i<C.size(); i++)
{
- int o = C[i].Behavior();
-
- if(o==1)
- {
- //If next to creature
- }
-
- if(o==2)
- {
- //If next to resource
- Location tmp = C[i].getLocation();
- for(k=0;k<R.size();k++)
- {
- if(Distance(tmp,R[k].getLocation())<2)
- {
- R[k].eat();
- if(R[k].getAmount()<=0)
- {
- R.erase(R.begin()+k);
- for(l=0;l<L.size();l++)
- {
- if(L[l].x==R[k].getLocation().x&&L[l].y==R[k].getLocation().y) // NEED TO OPERATOR OVERLOAD FOR THIS
- {
- L.erase(L.begin()+l);
- std::cout << "removing";
- }
- }
- }
- }
- }
- }
+ C[i].Behavior();
- //if the distance between the creature and L[j] is less than 200, insert L[j] into vector Z.
- for(j = 0; j < L.size(); j++)
- if(200>(Distance(C[i].getLocation(),L[j])))
- Z.push_back(L[j]);
+ vector<Resource*> N;
- C[i].giveKnown(Z); //sets creature's target location?
- Z.clear(); //clear vector Z for next creature
+ for(int j = 0; j < R.size(); j++)
+ if(250>Distance(C[i].getLocation(),R[j].getLocation()))
+ N.push_back(&R[j]);
+
+ C[i].give(N);
+ N.clear();
// This kills the creature
if(C[i].getHealth()<=0)
diff --git a/src/main.cpp b/src/main.cpp
index 4290386..ed41895 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -22,7 +22,7 @@ int main()
L.Behavior();
main.Render();
- SDL_Delay(10);
+ //SDL_Delay(10);
}
main.Destroy();
diff --git a/src/resource.cpp b/src/resource.cpp
index 4f12e2d..71a13e3 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -28,3 +28,8 @@ Location Resource::getLocation() //Returns resource object
Location L(xPosition,yPosition,2);
return L;
}
+
+void Resource::eat()
+{
+ amount-=10;
+}