summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
6 files changed, 102 insertions, 88 deletions
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);
}