summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Barrett <tombarrett@cornell.engr.siu.edu>2015-05-08 10:14:42 -0500
committerTom Barrett <tombarrett@cornell.engr.siu.edu>2015-05-08 10:14:42 -0500
commit39d7fe95633baafe4539f604186b151f30401a39 (patch)
tree242d91eafe56a15db046a1068d8e970624c6abfd
parent76e10e651906b756c4482274cdf1d8ccb686b915 (diff)
parent8c5121486a0e5d94ae5e5ee67370a31530cf8b6f (diff)
Merge branch 'master' of http://github.com/majortom6/natures
-rw-r--r--inc/creature.hpp12
-rw-r--r--inc/entity.hpp4
-rw-r--r--inc/list.hpp4
-rw-r--r--inc/location.hpp14
-rw-r--r--inc/resource.hpp2
-rw-r--r--src/creature.cpp114
-rw-r--r--src/entity.cpp2
-rw-r--r--src/list.cpp48
-rw-r--r--src/main.cpp2
-rw-r--r--src/resource.cpp16
-rw-r--r--src/window.cpp8
11 files changed, 121 insertions, 105 deletions
diff --git a/inc/creature.hpp b/inc/creature.hpp
index f72dfbc..9bb73ee 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -10,14 +10,16 @@ class Creature: public Entity
Creature(Window m, std::string s);
void Behavior();
void Action();
+ void Priority();
Location getLocation();
+ void giveKnown(std::vector<Location> Z){location = Z;};
private:
- int xT;
- int yT;
- int hp;
- int hu;
- Location K[3];
+ int xTarget; //x-coordinate of creature's target position
+ int yTarget; //y-coordinate of creature's target position
+ 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
};
#endif
diff --git a/inc/entity.hpp b/inc/entity.hpp
index 5ed2e74..079da9b 100644
--- a/inc/entity.hpp
+++ b/inc/entity.hpp
@@ -10,8 +10,8 @@ class Entity
SDL_Texture* loadTexture(std::string path, Window main);
protected:
- int x, y;
- int height, width;
+ int xPosition, yPosition; //Coordinates of entity on window
+ int height, width; //Dimensions of image on window
int degrees = 0;
SDL_Texture* texture;
SDL_Renderer* renderer;
diff --git a/inc/list.hpp b/inc/list.hpp
index 203ad7f..dc5c412 100644
--- a/inc/list.hpp
+++ b/inc/list.hpp
@@ -12,9 +12,11 @@ class List
List(Window m);
void Behavior();
void Place();
+ double Distance(Location A, Location B);
private:
- //Window main;
+ //vectors containing objects of each type
+ Window * main;//will be needed for adding R's and C's after constructor.
std::vector<Resource> R;
std::vector<Creature> C;
std::vector<Location> L;
diff --git a/inc/location.hpp b/inc/location.hpp
index 2042734..8bd512c 100644
--- a/inc/location.hpp
+++ b/inc/location.hpp
@@ -4,14 +4,12 @@
class Location
{
public:
- Location(){x=y=t=0;};
- Location(int x, int y, int z){};
- int getType(){return t;};
-
- private:
- int x;
- int y;
- int t;
+ Location(){x=y=type=0;}; //is this line needed?
+ Location(int x1, int y1, int t1){x=x1;y=y1;type=t1;};
+
+ int x; //x-coordinate of entity
+ int y; //y-coordinate of entity
+ int type; //value associated with type of entity at location. 1: Creature, 2: Resource
};
#endif
diff --git a/inc/resource.hpp b/inc/resource.hpp
index 2738f44..383e9ef 100644
--- a/inc/resource.hpp
+++ b/inc/resource.hpp
@@ -11,7 +11,7 @@ class Resource: public Entity
Location getLocation();
private:
- int amount;
+ int amount; //value associated with the amount of whatever (food, etc) left in the resource
};
#endif
diff --git a/src/creature.cpp b/src/creature.cpp
index 9d50291..5c3bb8e 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -1,105 +1,95 @@
#include "creature.hpp"
-Creature::Creature(Window m, std::string s)
+Creature::Creature(Window m, std::string s) //Constructor
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
- hp = 100;
- hu = 0;
+ health = 100;
+ hunger = 0;
- int zy = rand()%800;
- int zx = rand()%1200;
- y=zy;
- x=zx;
- //std::cout << x << ' ' << y << std::endl;
-
- //For the test resource
- xT=yT=300;
+ //initializes random start coordinates for creature, target position is equivalent to it's position
+ yPosition=yTarget=rand()%800;
+ xPosition=xTarget=rand()%1200;
}
void Creature::Behavior()
{
- hp--;
- //Detection
-
-
- //Priorities
+ health--; //Decrements health each time a behavior is executed
+ this->Priority(); //Checks which action has priority (doesn't really do this right now)
+ this->Action(); //Does action
+}
- //Action
- this->Action();
+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++)
+ {
+ if(location[i].type==2)
+ {
+ xTarget = location[i].x;
+ yTarget = location[i].y;
+ }
+ }
}
void Creature::Action()
{
- //std::cout << (sqrt(((x-xT)^2)+((y-yT)^2));
- if((sqrt(((x-xT)^2)+((y-yT)^2)))<2)
- return; //eat//reproduce//etc;
+ //If the distance is close, will return an bool
+ if(sqrt(pow(xPosition - xTarget, 2) + pow(yPosition - yTarget, 2)) < 2)
+ return; //<--- eat action should be here
- if(x==xT)
+ //Makes moves towards target coordinates
+ if(xPosition==xTarget)
{
- if(y<yT)
- y++;
+ if(yPosition<yTarget)
+ yPosition++;
else
- y--;
+ yPosition--;
}
- else if(y==yT)
- {
- if(x<xT)
- x++;
- else
- x--;
- }
- else if(x<xT)
+
+ else if(yPosition==yTarget)
{
- if(y<yT)
- {
- x++;
- y++;
- }
+ if(xPosition<xTarget)
+ xPosition++;
else
- {
- x++;
- y--;
- }
+ xPosition--;
}
- else if (x>xT)
+
+ else if(xPosition<xTarget)
{
- if(y<yT)
+ if(yPosition<yTarget)
{
- x--;
- y++;
+ xPosition++;
+ yPosition++;
}
+
else
{
- x--;
- y--;
+ xPosition++;
+ yPosition--;
}
}
- /*
- else
+ else if (xPosition>xTarget)
{
- int z = rand()%2;
- if(z)
+ if(yPosition<yTarget)
{
- if(x<xT)
- x++;
- else
- x--;
+ xPosition--;
+ yPosition++;
}
+
else
{
- if(y<yT)
- y++;
- else
- y--;
+ xPosition--;
+ yPosition--;
}
}
- */
}
Location Creature::getLocation()
{
- Location L(x,y,1);
+ //returns location object of the specific creature
+ Location L(xPosition, yPosition, 1);
return L;
}
diff --git a/src/entity.cpp b/src/entity.cpp
index f7e8857..10a3ccc 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -2,7 +2,7 @@
void Entity::Place()
{
- SDL_Rect rect = {x, y, width/8, height/8};
+ SDL_Rect rect = {xPosition, yPosition, width/8, height/8};
SDL_RenderCopyEx(renderer,texture,NULL,&rect,degrees,NULL,SDL_FLIP_NONE);
}
diff --git a/src/list.cpp b/src/list.cpp
index c3246c9..2140a75 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -1,14 +1,16 @@
#include "list.hpp"
-List::List(Window m)
+List::List(Window m) //Constructor
{
int i;
- for(i=0;i<5;i++)
+
+ for(i=0;i<10;i++)
{
- Creature X = Creature(m,"img/Cbasic.png");
+ 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++)
{
Resource Y(m,"img/Rbasic.png");
@@ -17,31 +19,51 @@ List::List(Window m)
}
}
-void List::Behavior()
-{
- int i;
- for(i = 0; i < C.size(); i++)
- {
- C[i].Behavior();
- }
-}
-
void List::Place()
{
int i;
+ //if any locations are creatures, erases them from vector L
for(i = 0;i < L.size(); i++)
- if(L.getType()==1)
+ 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 all resources
for(i = 0; i < R.size(); i++)
{
R[i].Place();
}
}
+
+void List::Behavior()
+{
+ int i, j;
+ std::vector<Location> Z;
+
+ //
+ for(i = 0; i < C.size(); i++)
+ {
+ C[i].Behavior(); //executes the behavior of the creature at i
+ for(j = 0; j < L.size(); j++)
+ if(200>(Distance(C[i].getLocation(),L[j]))) //if the distance between the creature and L[j] is less than 200, insert L[j] into vector Z.
+ {
+ Z.push_back(L[j]);
+ }
+
+ C[i].giveKnown(Z); //sets creature's target location?
+ Z.clear(); //clear vector Z for next creature
+ }
+}
+
+double List::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/main.cpp b/src/main.cpp
index 1dec02c..4290386 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -22,7 +22,7 @@ int main()
L.Behavior();
main.Render();
- SDL_Delay(5);
+ SDL_Delay(10);
}
main.Destroy();
diff --git a/src/resource.cpp b/src/resource.cpp
index a087aca..5044760 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -1,17 +1,19 @@
#include "resource.hpp"
-Resource::Resource(Window m, std::string s)
+Resource::Resource(Window m, std::string s) //Constructor
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
- int zy = rand()%800;
- int zx = rand()%1200;
- y=zy;
- x=zx;
+
+ //Initialized random position coordinates
+ int yStart = rand()%800;
+ int xStart = rand()%1200;
+ yPosition = yStart;
+ xPosition = xStart;
}
-Location Resource::getLocation()
+Location Resource::getLocation() //Returns resource object
{
- Location L(x,y,2);
+ Location L(xPosition,yPosition,2);
return L;
}
diff --git a/src/window.cpp b/src/window.cpp
index b25f11e..66c7a4c 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -1,6 +1,6 @@
#include "window.hpp"
-Window::Window()
+Window::Window() //Constructor
{
SDL_Init(SDL_INIT_VIDEO);
main = SDL_CreateWindow("main",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,1280,800,SDL_WINDOW_SHOWN);
@@ -8,19 +8,19 @@ Window::Window()
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
}
-void Window::Destroy()
+void Window::Destroy() //Kills window
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(main);
SDL_Quit();
}
-void Window::Clear()
+void Window::Clear() //Clears renderer
{
SDL_RenderClear(renderer);
}
-void Window::Render()
+void Window::Render() //Brings image forward
{
SDL_RenderPresent(renderer);
}