From f2f0867edd0d9624a77e4e5db39825b8ea1a55d0 Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 14 Jan 2017 11:56:33 -0600 Subject: completly remove location structure and replaced it with sdl_rect --- src/creature.cpp | 83 +++++++++++++++++++++++++++++--------------------------- src/entity.cpp | 14 ---------- src/list.cpp | 17 +++++++----- src/resource.cpp | 26 +++++------------- 4 files changed, 60 insertions(+), 80 deletions(-) (limited to 'src') 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())getRect())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;iBehavior(); 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 List::getNear(Creature nC) list N; for(list ::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 ::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; -- cgit v1.2.3