#include "missileactor.h"

MissileActor::MissileActor(f32 xi, f32 yi, f32 xf, f32 yf, Type owner, int splitafter)
  : state(MS_NULL), xi(xi), yi(yi), xf(xf), yf(yf), x(xi), y(yi),
    d(0), t(0), owner(owner), splitTime(splitafter)
{
  maxd = sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi));
  theta = atan2(xi-xf,yf-yi);
  splitParent = NULL;
}

extern const f32 SILOY;

void MissileActor::explode()
{
  state = MS_EXPLODE;
  if(splitParent)
    splitParent->notifyExploded();
}

void MissileActor::notifyExploded()
{
  splitCount--;
  if(splitCount <= 0)
    state = MS_NULL;
}


void MissileActor::split(int n)
{
  state = MS_SPLIT;
  splitCount = n;
}

void MissileActor::activate()
{
  x = xi;
  y = yi;
  d = 0;
  t = 0;
  maxd = sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi));
  theta = atan2(xf-xi,yi-yf);
  state = MS_FLIGHT;
}

void MissileActor::update()
{
  switch(state) {
    case MS_FLIGHT:
      if(owner == PLAYER)
        d += MSPEED;
      else if(owner == ENEMY)
        d += ENEMYSPEED;
      if(d >= maxd) {
        x = xf;
        y = yf;
        explode();
      } else {
        x = xi + d * sin(theta);
        y = yi - d * cos(theta);
      }
      break;
    default:
      break;
  }

  if(state != MS_NULL)
    t++;
}
