#include <math.h>
#include "siloactor.h"

void SiloActor::directHit()
{
  health -= DIRECTDMG;
  if(health < 0) {
    state = SI_DEAD;
    health = 0;
  }
}


void SiloActor::splashHit()
{
  health -= SPLASHDMG;
  if(health < 0) {
    state = SI_DEAD;
    health = 0;
  }
}

void SiloActor::activate()
{
  health = maxhealth = 1;
  ammo = maxammo = 3;
  tx = x;
  ty = y-1;
  state = SI_ALIVE;
}
  
void SiloActor::update()
{
  switch(state) {
    case SI_ALIVE:
      {
        f32 targettheta = atan2(tx-x,y-ty);
        if(targettheta > 7.0*M_PI/16.0)
          targettheta = 7.0*M_PI/16.0;
        if(targettheta < -7.0*M_PI/16.0)
          targettheta = -7.0*M_PI/16.0;
        if(theta < targettheta)
          theta += .05;
        if(theta > targettheta)
          theta -= .05;
        if(fabs(theta - targettheta) < .05)
          theta = targettheta;
        if(theta > 7.0*M_PI/16.0)
          theta = 7.0*M_PI/16.0;
        if(theta < -7.0*M_PI/16.0)
          theta = -7.0*M_PI/16.0;
        health += HEALTHREGENSLOW;
        if(health > maxhealth)
          health = maxhealth;
        ammo += AMMOREGENSLOW;
        if(ammo > maxammo)
          ammo = maxammo;
      }
      break;
    default:
      break;
  }
}

f32 SiloActor::getX() {
  return x;
}

f32 SiloActor::getY() {
  return y;
}

f32 SiloActor::getRotation() {
  f32 ret = theta*180.0/M_PI;
  if(ret > 90)
    ret = 90;
  if(ret < -90)
    ret = -90;
  return ret;
}
