From ffcee12acf3fe3f972f322c18b3203212682205d Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 7 May 2015 12:35:15 -0500 Subject: woo! detection is implemented albeit shitty --- src/creature.cpp | 38 ++++++++++++++++++++++++++------------ src/list.cpp | 21 +++++++++++++++++++-- src/main.cpp | 2 +- 3 files changed, 46 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/creature.cpp b/src/creature.cpp index 9d50291..ae3122b 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -9,30 +9,29 @@ Creature::Creature(Window m, std::string s) int zy = rand()%800; int zx = rand()%1200; - y=zy; - x=zx; + y=yT=zy; + x=xT=zx; //std::cout << x << ' ' << y << std::endl; - + //For the test resource - xT=yT=300; + //xT=yT=300; } void Creature::Behavior() { hp--; - //Detection - - - //Priorities + //Detection - //Action - this->Action(); + //Priorities + this->Priority(); + //Action + this->Action(); } void Creature::Action() { //std::cout << (sqrt(((x-xT)^2)+((y-yT)^2)); - if((sqrt(((x-xT)^2)+((y-yT)^2)))<2) + if(sqrt(pow(x-xT,2)+pow(y-yT,2))<2) return; //eat//reproduce//etc; if(x==xT) @@ -76,7 +75,7 @@ void Creature::Action() } } - /* + /* else { int z = rand()%2; @@ -98,6 +97,21 @@ void Creature::Action() */ } +void Creature::Priority() +{ + int i; + for(i=0;i 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(); } } @@ -31,7 +41,7 @@ void List::Place() int i; for(i = 0;i < L.size(); i++) - if(L.getType()==1) + if(L[i].t==1) L.erase(L.begin()+i); for(i = 0; i < C.size(); i++) @@ -45,3 +55,10 @@ void List::Place() R[i].Place(); } } + +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"; + return z; +} diff --git a/src/main.cpp b/src/main.cpp index 1dec02c..1e8424a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ int main() L.Behavior(); main.Render(); - SDL_Delay(5); + SDL_Delay(15); } main.Destroy(); -- cgit v1.2.3 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 --- src/creature.cpp | 131 ++++++++++++++++++++++++------------------------------- src/entity.cpp | 2 +- src/list.cpp | 54 +++++++++++++---------- src/resource.cpp | 16 ++++--- src/window.cpp | 8 ++-- 5 files changed, 100 insertions(+), 111 deletions(-) (limited to 'src') 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 From 84f94931fa790572b3ce57d72e290f0bd860c661 Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 8 May 2015 08:05:57 -0500 Subject: merging --- src/list.cpp | 2 +- src/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/list.cpp b/src/list.cpp index 2612d3e..ee0d14d 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -3,7 +3,7 @@ List::List(Window m) { int i; - for(i=0;i<5;i++) + for(i=0;i<10;i++) { Creature X = Creature(m,"img/Cbasic.png"); C.push_back(X); diff --git a/src/main.cpp b/src/main.cpp index 1e8424a..4290386 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ int main() L.Behavior(); main.Render(); - SDL_Delay(15); + SDL_Delay(10); } main.Destroy(); -- cgit v1.2.3 From 04069434be15c06666a7493b1b6329a78c00d1d1 Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 8 May 2015 08:09:32 -0500 Subject: merging --- src/list.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src') diff --git a/src/list.cpp b/src/list.cpp index c21dd52..b86f077 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -3,12 +3,8 @@ List::List(Window m) //Constructor { int i; -<<<<<<< HEAD + for(i=0;i<10;i++) -======= - //Creates 5 creatures, inserts them into vector C - for(i=0;i<5;i++) ->>>>>>> c63d77ddddb69ef0f641d90a8c3922fdc356224c { Creature X(m,"img/Cbasic.png"); C.push_back(X); -- cgit v1.2.3 From 8c5121486a0e5d94ae5e5ee67370a31530cf8b6f Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 8 May 2015 08:23:06 -0500 Subject: fixed some comments and cleaned around --- src/creature.cpp | 13 ++++--------- src/list.cpp | 3 +-- src/resource.cpp | 2 +- 3 files changed, 6 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/creature.cpp b/src/creature.cpp index ef3d6c1..5c3bb8e 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -8,16 +8,13 @@ Creature::Creature(Window m, std::string s) //Constructor hunger = 0; //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; + yPosition=yTarget=rand()%800; + xPosition=xTarget=rand()%1200; } void Creature::Behavior() { 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 } @@ -28,19 +25,17 @@ void Creature::Priority() int i; for(i=0;i