From c63d77ddddb69ef0f641d90a8c3922fdc356224c Mon Sep 17 00:00:00 2001 From: dakjos Date: Thu, 7 May 2015 19:53:39 -0500 Subject: Some variable names changed and many comments made so I understand what the fuck is going on --- inc/creature.hpp | 12 ++--- inc/entity.hpp | 4 +- inc/list.hpp | 2 +- inc/location.hpp | 10 ++--- inc/resource.hpp | 2 +- src/creature.cpp | 131 ++++++++++++++++++++++++------------------------------- src/entity.cpp | 2 +- src/list.cpp | 54 +++++++++++++---------- src/resource.cpp | 16 ++++--- 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 Z){K=Z;}; + void giveKnown(std::vector Z){location = Z;}; private: - int xT; - int yT; - int hp; - int hu; - std::vector 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; //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 R; std::vector C; std::vector 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;ixT) + + else if(xPositionxTarget) { - int z = rand()%2; - if(z) - { - if(x 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 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); } -- cgit v1.2.3