summaryrefslogtreecommitdiff
path: root/src
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 /src
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
Diffstat (limited to 'src')
-rw-r--r--src/creature.cpp80
-rw-r--r--src/list.cpp79
-rw-r--r--src/main.cpp2
-rw-r--r--src/resource.cpp5
4 files changed, 81 insertions, 85 deletions
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;
+}