diff options
author | dakjos <dsojka@siu.edu> | 2015-05-07 19:53:39 -0500 |
---|---|---|
committer | dakjos <dsojka@siu.edu> | 2015-05-07 19:53:39 -0500 |
commit | c63d77ddddb69ef0f641d90a8c3922fdc356224c (patch) | |
tree | 6867ee8ee11718eca9bb144b98f17c695a891495 | |
parent | ffcee12acf3fe3f972f322c18b3203212682205d (diff) |
Some variable names changed and many comments made so I understand what the fuck is going on
-rw-r--r-- | inc/creature.hpp | 12 | ||||
-rw-r--r-- | inc/entity.hpp | 4 | ||||
-rw-r--r-- | inc/list.hpp | 2 | ||||
-rw-r--r-- | inc/location.hpp | 10 | ||||
-rw-r--r-- | inc/resource.hpp | 2 | ||||
-rw-r--r-- | src/creature.cpp | 131 | ||||
-rw-r--r-- | src/entity.cpp | 2 | ||||
-rw-r--r-- | src/list.cpp | 54 | ||||
-rw-r--r-- | src/resource.cpp | 16 | ||||
-rw-r--r-- | src/window.cpp | 8 |
10 files changed, 115 insertions, 126 deletions
diff --git a/inc/creature.hpp b/inc/creature.hpp index a0046fb..9a26ee8 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -12,14 +12,14 @@ class Creature: public Entity void Action(); void Priority(); Location getLocation(); - void giveKnown(std::vector<Location> Z){K=Z;}; + void giveKnown(std::vector<Location> Z){location = Z;}; private: - int xT; - int yT; - int hp; - int hu; - std::vector<Location> K; + 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 creatures location on window or target location? }; #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 96509aa..074e045 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -15,7 +15,7 @@ class List double Distance(Location A, Location B); private: - //Window main; + //vectors containing objects of each type std::vector<Resource> R; std::vector<Creature> C; std::vector<Location> L; diff --git a/inc/location.hpp b/inc/location.hpp index 6a20b4e..8bd512c 100644 --- a/inc/location.hpp +++ b/inc/location.hpp @@ -4,12 +4,12 @@ class Location { public: - Location(){x=y=t=0;}; - Location(int x1, int y1, int t1){x=x1;y=y1;t=t1;}; + Location(){x=y=type=0;}; //is this line needed? + Location(int x1, int y1, int t1){x=x1;y=y1;type=t1;}; - int x; - int y; - int t; + 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 ae3122b..ef3d6c1 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -1,119 +1,100 @@ #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=yT=zy; - x=xT=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 + int yStart = rand()%800; + int xStart = rand()%1200; + yPosition=yTarget=yStart; + xPosition=xTarget=xStart; } void Creature::Behavior() { - hp--; + health--; //Decrements health each time a behavior is executed //Detection + this->Priority(); //Checks which action has priority (doesn't really do this right now) + this->Action(); //Does action +} - //Priorities - this->Priority(); - //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++) + { + std::cout << location[i].type; + if(location[i].type==2) + { + xTarget = location[i].x; + yTarget = location[i].y; + std::cout << xTarget << "IN\n"; + } + } } void Creature::Action() { - //std::cout << (sqrt(((x-xT)^2)+((y-yT)^2)); - if(sqrt(pow(x-xT,2)+pow(y-yT,2))<2) - return; //eat//reproduce//etc; + //If the distance is too small, do something? + 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) + + else if(yPosition==yTarget) { - if(x<xT) - x++; + if(xPosition<xTarget) + xPosition++; else - x--; + xPosition--; } - else if(x<xT) - { - if(y<yT) - { - x++; - y++; - } - else - { - x++; - y--; - } - } - 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(x<xT) - x++; - else - x--; - } - else + if(yPosition<yTarget) { - if(y<yT) - y++; - else - y--; + xPosition--; + yPosition++; } - } - */ -} -void Creature::Priority() -{ - int i; - for(i=0;i<K.size();i++) - { - std::cout << K[i].t; - if(K[i].t==2) + else { - xT = K[i].x; - yT = K[i].y; - std::cout << xT << "IN\n"; + xPosition--; + yPosition--; } } } Location Creature::getLocation() { - Location L(x,y,1); + //returns location vector of creatures position coordinates + 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 2612d3e..0101d8c 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; + //Creates 5 creatures, inserts them into vector C for(i=0;i<5;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,48 +19,52 @@ List::List(Window m) } } -void List::Behavior() -{ - int i, j; - std::vector<Location> Z; - - for(i = 0; i < C.size(); i++) - { - C[i].Behavior(); - for(j = 0; j < L.size(); j++) - if(200>(Distance(C[i].getLocation(),L[j]))) - { - Z.push_back(L[j]); - } - - C[i].giveKnown(Z); - Z.clear(); - } -} - 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].t==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) { - double z = sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2)); - //std::cout << z << "\n"; + //computes distance between two points + double z = sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); return z; } diff --git a/src/resource.cpp b/src/resource.cpp index a087aca..c4a9341 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 location vector { - 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); } |