summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom <tombarrett@siu.edu>2017-01-14 11:56:33 -0600
committertom <tombarrett@siu.edu>2017-01-14 11:56:33 -0600
commitf2f0867edd0d9624a77e4e5db39825b8ea1a55d0 (patch)
tree1ead1fa412bd7c2e791310c7cb7bbe84c79696a4
parentb97a54b8acd113e99871fdb0f683e27e0912bf42 (diff)
completly remove location structure and replaced it with sdl_rect
-rw-r--r--.gitignore2
-rwxr-xr-xexecbin318784 -> 307904 bytes
-rw-r--r--inc/creature.hpp17
-rw-r--r--inc/entity.hpp5
-rw-r--r--inc/list.hpp7
-rw-r--r--inc/location.hpp13
-rw-r--r--inc/main.hpp1
-rw-r--r--inc/resource.hpp5
-rw-r--r--src/creature.cpp83
-rw-r--r--src/entity.cpp14
-rw-r--r--src/list.cpp17
-rw-r--r--src/resource.cpp26
12 files changed, 73 insertions, 117 deletions
diff --git a/.gitignore b/.gitignore
index 88b85eb..6876957 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-natures
+exec
diff --git a/exec b/exec
index 9a0c8d7..a4b87a7 100755
--- a/exec
+++ b/exec
Binary files differ
diff --git a/inc/creature.hpp b/inc/creature.hpp
index 8515bc0..f51d083 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -4,23 +4,21 @@
#include <SDL2/SDL.h>
#include <list>
-#include "location.hpp"
#include "entity.hpp"
#include "constants.hpp"
class Creature: public Entity
{
public:
- Creature(Window m, int size);
+ Creature(Window m, SDL_Rect R);
void Behavior();
void Action();
void Priority();
void setTarget();
- void Move(Location l);
+ void Move(SDL_Rect R);
void giveN(list<Entity*> n){N = n;};
- Location getLocation(){return L;};
- double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));};
+ double Distance(SDL_Rect A, SDL_Rect B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));};
int getHealth(){return health;};
bool doesItHaveTarget(){return hasTarget;};
int getBestSense(){return bestSense;};
@@ -28,14 +26,15 @@ class Creature: public Entity
private:
bool hasTarget;
bool wander;
- Location wTarget;
+ SDL_Rect wTarget;
+
int health;
- int reach = CREATURE_REACH;
+ int reach;
int maxHealth;
bool hungry;
- int speed = CREATURE_SPEED;
+ int speed;
bool able;
- int bestSense = CREATURE_BEST_SENSE;
+ int bestSense;
list<Entity*> N;
Entity *target;
diff --git a/inc/entity.hpp b/inc/entity.hpp
index d02f689..5941a9c 100644
--- a/inc/entity.hpp
+++ b/inc/entity.hpp
@@ -2,17 +2,13 @@
#define entity_h
#include "window.hpp"
-#include "location.hpp"
class Entity
{
public:
- void Init(Window m);
- void Init(Window m, Location z);
void Place();
int getType(){return type;};
- Location getLocation(){return L;};
SDL_Rect getRect(){return rect;};
virtual void eat(void){};
@@ -21,7 +17,6 @@ class Entity
protected:
int type;
SDL_Rect rect;
- Location L;
SDL_Renderer* renderer;
};
diff --git a/inc/list.hpp b/inc/list.hpp
index 8cf409b..6800b34 100644
--- a/inc/list.hpp
+++ b/inc/list.hpp
@@ -1,12 +1,12 @@
#ifndef list_h
#define list_h
+#include <SDL2/SDL.h>
#include <list>
#include "creature.hpp"
#include "resource.hpp"
#include "window.hpp"
-#include "location.hpp"
#include "constants.hpp"
class List
@@ -15,12 +15,11 @@ class List
List(Window m);
void Behavior();
void Place();
- double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));};
+ double Distance(SDL_Rect A, SDL_Rect B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));};
list<Entity*> getNear(Creature C);
private:
- //vectors containing objects of each type
- Window main = Window("no");//will be needed for adding R's and C's after constructor.
+ Window main = Window("do not create new window.");
list<Resource> R;
list<Creature> C;
};
diff --git a/inc/location.hpp b/inc/location.hpp
deleted file mode 100644
index 5392e9b..0000000
--- a/inc/location.hpp
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef location_h
-#define location_h
-
-class Location
-{
- public:
- Location(int x1, int y1){x=x1;y=y1;};
- Location(){x=y=0;};
- int x;
- int y;
-};
-
-#endif
diff --git a/inc/main.hpp b/inc/main.hpp
index d20f7ef..82a78c8 100644
--- a/inc/main.hpp
+++ b/inc/main.hpp
@@ -7,7 +7,6 @@
#include "creature.hpp"
#include "resource.hpp"
#include "list.hpp"
-#include "location.hpp"
#include "timer.hpp"
#endif
diff --git a/inc/resource.hpp b/inc/resource.hpp
index 5f70585..ed9f098 100644
--- a/inc/resource.hpp
+++ b/inc/resource.hpp
@@ -2,16 +2,13 @@
#define resource_h
#include "entity.hpp"
-#include "location.hpp"
class Resource: public Entity
{
public:
- Resource(Window m, int size);
- Resource(Window m, int size, Location z);
+ Resource(Window m, SDL_Rect Rect);
void eat();
- Location getLocation(){return L;};
int getAmount(){return amount;};
private:
diff --git a/src/creature.cpp b/src/creature.cpp
index b81153e..a71a0b7 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -1,22 +1,26 @@
#include "creature.hpp"
-Creature::Creature(Window m, int size)
+Creature::Creature(Window m, SDL_Rect R)
{
- Init(m);
+ renderer = m.getRenderer();
type = 1;
+ rect = R;
- rect.h = rect.w = size;
-
- L.x = rect.x;
- L.y = rect.y;
+ if(rect.x == 0 && rect.y == 0){
+ rect.x = rand()%WINDOW_X;
+ rect.y = rand()%WINDOW_Y;
+ }
- health = CREATURE_START_HEALTH;
- maxHealth = CREATURE_MAX_HEALTH;
-
- hungry = false;
- hasTarget = false;
- wander = false;
- able = true;
+ health = CREATURE_START_HEALTH;
+ maxHealth = CREATURE_MAX_HEALTH;
+ reach = CREATURE_REACH;
+ speed = CREATURE_SPEED;
+ bestSense = CREATURE_BEST_SENSE;
+
+ hungry = false;
+ hasTarget = false;
+ wander = false;
+ able = true;
}
void Creature::Behavior()
@@ -61,7 +65,8 @@ void Creature::setTarget()
if(!hasTarget&&!wander){
wander = true;
- wTarget = Location(rand()%WINDOW_X,rand()%WINDOW_Y);
+ SDL_Rect r = {rand()%WINDOW_X, rand()%WINDOW_Y, 0, 0};
+ wTarget = r;
}
}
@@ -69,59 +74,57 @@ void Creature::setTarget()
void Creature::Action()
{
if(hasTarget){
- if(Distance(L,target->getLocation())<reach){
+ if(Distance(rect,target->getRect())<reach){
target->eat();
health+=10;
}
else
- Move(target->getLocation());
+ Move(target->getRect());
if(target->getAmount()<=0){
hasTarget = false;
}
}
else{
- if(Distance(L,wTarget)<5)
+ if(Distance(rect,wTarget)<5)
wander = false;
else
Move(wTarget);
}
}
-void Creature::Move(Location l)
+void Creature::Move(SDL_Rect R)
{
- if( L.x == l.x ){
- if( L.y < l.y )
- L.y+=speed;
+ if( rect.x == R.x ){
+ if( rect.y < R.y )
+ rect.y+=speed;
else
- L.y-=speed;
+ rect.y-=speed;
}
- else if( L.y == l.y ){
- if( L.x < l.x )
- L.x+=speed;
+ else if( rect.y == R.y ){
+ if( rect.x < R.x )
+ rect.x+=speed;
else
- L.x-=speed;
+ rect.x-=speed;
}
- else if( L.x < l.x ){
- if( L.y < l.y ){
- L.x+=speed;
- L.y+=speed;
+ else if( rect.x < R.x ){
+ if( rect.y < R.y ){
+ rect.x+=speed;
+ rect.y+=speed;
}
else{
- L.x+=speed;
- L.y-=speed;
+ rect.x+=speed;
+ rect.y-=speed;
}
}
- else if ( L.x > l.x ){
- if( L.y < l.y ){
- L.x-=speed;
- L.y+=speed;
+ else if ( rect.x > R.x ){
+ if( rect.y < R.y ){
+ rect.x-=speed;
+ rect.y+=speed;
}
else{
- L.x-=speed;
- L.y-=speed;
+ rect.x-=speed;
+ rect.y-=speed;
}
}
- rect.x = L.x;
- rect.y = L.y;
}
diff --git a/src/entity.cpp b/src/entity.cpp
index ff6799e..6ad0e3f 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -1,19 +1,5 @@
#include "entity.hpp"
-void Entity::Init(Window m)
-{
- renderer = m.getRenderer();
- rect.y = rand()%WINDOW_Y;
- rect.x = rand()%WINDOW_X;
-}
-
-void Entity::Init(Window m, Location z)
-{
- renderer = m.getRenderer();
- rect.y = z.y;
- rect.x = z.x;
-}
-
void Entity::Place()
{
if(type == 1)
diff --git a/src/list.cpp b/src/list.cpp
index b5e05da..b8d2888 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -4,13 +4,17 @@ List::List(Window m)
{
int i;
+ SDL_Rect Rect = {0,0,CREATURE_SIZE,CREATURE_SIZE};
+
for(i=0;i<CREATURES;i++){
- Creature X(m,CREATURE_SIZE);
+ Creature X(m,Rect);
C.push_back(X);
}
+ Rect = {0,0,RESOURCE_SIZE,RESOURCE_SIZE};
+
for(i=0;i<RESOURCES;i++){
- Resource Y(m,RESOURCE_SIZE);
+ Resource Y(m,Rect);
R.push_back(Y);
}
@@ -40,9 +44,8 @@ void List::Behavior()
it->Behavior();
if(it->getHealth()<=0){
- Location z = it->getLocation();
- SDL_Rect rect = it->getRect();
- Resource r = Resource(main,rect.w,z);
+ SDL_Rect Rect = it->getRect();
+ Resource r = Resource(main,Rect);
R.push_back(r);
C.erase(it--);
}
@@ -54,14 +57,14 @@ list<Entity*> List::getNear(Creature nC)
list<Entity*> N;
for(list <Resource>::iterator it = R.begin(); it!=R.end(); it++){
- if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) )
+ if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) )
N.push_back(&(*it));
}
for(list <Creature>::iterator it = C.begin(); it!=C.end(); it++){
if( &nC == &(*it))
continue;
- else if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) )
+ else if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) )
N.push_back(&(*it));
}
diff --git a/src/resource.cpp b/src/resource.cpp
index 326c047..eec1113 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -1,31 +1,19 @@
#include "resource.hpp"
-Resource::Resource(Window m, int size)
+Resource::Resource(Window m, SDL_Rect R)
{
- Init(m);
+ renderer = m.getRenderer();
type = 2;
-
- rect.h = rect.w = size;
+ rect = R;
- L.x = rect.x;
- L.y = rect.y;
+ if(rect.x == 0 && rect.y == 0){
+ rect.x = rand()%WINDOW_X;
+ rect.y = rand()%WINDOW_Y;
+ }
amount = RESOURCE_AMOUNT;
}
-Resource::Resource(Window m, int size, Location z)
-{
- Init(m,z);
- type = 2;
-
- rect.h = rect.w = size;
-
- L.x = rect.x;
- L.y = rect.y;
-
- amount = RESOURCE_AMOUNT;
-}
-
void Resource::eat()
{
amount-=10;