#include "Unit.h"
#include "Game.h"
#include "DarkGDK.h"
Unit::Unit( PLAYER_ID owner, float x, float y, float facing, QuadTree* tree, int spriteid, UNIT_TYPE type ) : GameObject( owner, x, y, facing, tree, spriteid)
{
alive = true;
deathtime = 0.0f;
SetFacing( angle );
switch( type )
{
case( UNIT_TYPE_MAGE ):
{
life = 10;
lifereg = 0;
mana = 3;
manareg = 0.25;
speed = 100;
animationspersec = 7;
} break;
case( UNIT_TYPE_ENEMY ):
{
life = 1;
lifereg = 0;
mana = 0;
manareg = 0;
speed = 30;
animationspersec = 7;
} break;
case( UNIT_TYPE_BOSS ):
{
life = 3;
lifereg = 0.25;
mana = 2;
manareg = 0.1;
speed = 50;
animationspersec = 10;
} break;
}
/*posx = posx - dbSpriteWidth(unitid) / 2.0f * dbCos(angle) + dbSpriteHeight(unitid) / 2.0f * dbSin(angle);
posy = posy - dbSpriteHeight(unitid) / 2.0f * dbCos(angle) - dbSpriteWidth(unitid) / 2.0f * dbSin(angle);
dbRotateSprite(unitid, angle);*/
} // end Konstructor
Unit::~Unit()
{
} // Destructor
void Unit::SetFacing( float value )
{
angle = value;
// check unit facing
if( angle <= 45 || angle > 315 )
{
facing = UNIT_FACING_EAST;
animationstart = 9;
animationend = 12;
}
else if( angle <= 135 )
{
facing = UNIT_FACING_SOUTH;
animationstart = 1;
animationend = 4;
}
else if( angle <= 225 )
{
facing = UNIT_FACING_WEST;
animationstart = 5;
animationend = 8;
}
else if( angle <= 315 )
{
facing = UNIT_FACING_NORTH;
animationstart = 13;
animationend = 16;
}
} // end SetFacing
void Unit::Update()
{
const float deltatime = GAME->GetDeltaTime();
if( !alive )
{
// fade unit away
float alpha = 255*deathtime;
dbSetSpriteDiffuse( spriteid, alpha, alpha, alpha );
dbSetSpriteAlpha( spriteid, alpha );
deathtime -= deltatime;
if( deathtime <= 0 )
removeable = true;
}
else
{
if( owner == PLAYER_ID_ENEMY )
{
// enemy units walk automatically towards the mage
Rect r = GAME->mage->GetBoundary();
SetFacing( dbWrapValue( dbAtanFull( r.GetCenterY() - rect.GetCenterY(), r.GetCenterX() - rect.GetCenterX() ) ) );
}
float newx = rect.topleft_x + (speed*deltatime) * dbCos( angle );
float newy = rect.topleft_y + (speed*deltatime) * dbSin( angle );
SetPosition( newx, newy );
}
} // end Update