#include "GameObject.h"
#include "DarkGDK.h"
#include "Game.h"
#include "Quadtree.h"
int GameObject::counter = 1000;
GameObject::GameObject( PLAYER_ID owner, float x, float y, float facing, QuadTree* tree, int spriteid )
{
id = GameObject::counter;
this->spriteid = spriteid;
this->owner = owner;
angle = facing;
qtree = tree;
scale = 1.0f;
destructable = false;
removeable = false;
pathing = true;
height = dbSpriteHeight(spriteid) * scale;
width = dbSpriteWidth(spriteid) * scale;
collsize = std::max( width, height );
rect = Rect( x, y, x + width, y + height );
animationstart = 1;
animationend = 1;
animationspersec = 0;
animationcurrent = animationstart;
if( pathing && qtree )
qtree->UpdateObjectInformation( this );
dbSetSpriteFrame( spriteid, animationcurrent );
dbPasteSprite( spriteid, x, y );
++GameObject::counter;
} // end Konstructor
GameObject::~GameObject()
{
if( qtree )
qtree->RemoveObject( this );
} // end Destructor
void GameObject::Render()
{
dbSetSpriteFrame( spriteid, animationcurrent );
dbPasteSprite( spriteid, rect.topleft_x, rect.topleft_y );
} // end Render
void GameObject::SetPosition(float x, float y)
{
const float deltat = GAME->GetDeltaTime();
// animate the sprite
if( animationcurrent < animationstart )
animationcurrent = animationstart;
else if( animationcurrent < animationend+1 )
animationcurrent+=(deltat*animationspersec);
if( animationcurrent >= animationend+1 )
animationcurrent = animationstart;
if( pathing && qtree )
{
// check if new position isn't blocked by any "enemy" object
std::vector<GameObject*> v = qtree->GetObjects( x, y, collsize );
Rect r = Rect( x, y, x+width, y+height );
for( std::vector<GameObject*>::iterator it = v.begin(); it != v.end(); ++it )
{
//Rect r = (*it)->GetBoundary();
//if( (*it) != this && rect.IntersectsWithRect( r ) )
if( (*it) != this && owner != (*it)->GetOwner() && r.IntersectsWithRect( (*it)->GetBoundary() ) )
{
// if it is blocked -> don't move the object
x = rect.topleft_x;
y = rect.topleft_y;
// found a blocking object
// so don't move the current object if it wants to walk towards the object
/*const float ang = dbWrapValue( dbAtanFull( rect.topleft_y - r.GetCenterY(), rect.topleft_x - r.GetCenterX() ) );
if( ang <= 45 || ang > 315 )
{
x = r.botright_x+1;
y = rect.topleft_y;
}
else if( ang <= 135 )
{
x = rect.topleft_x;
y = r.botright_y+1;
}
else if( ang <= 225 )
{
x = r.topleft_x-width-1;
y = rect.topleft_y;
}
else if( ang <= 315 )
{
x = rect.topleft_x;
y = r.topleft_y-height-1;
}*/
break;
}
}
}
rect = Rect( x, y, x + width, y + height );
Render();
if( pathing && qtree )
qtree->UpdateObjectInformation( this );
} // end SetPosition