#include "empire.h"

void create_particle(int x, int y, int type);
void create_particle(int x, int y, int type, int color, string text);

vector<_particle> p_effects;

void _particle::create(int _x, int _y, int _type)
{
  int a, c;
  c = WHITE;
  switch(type)
  {
    case P_FLAME:      
      a = rand()%3;
      if (a == 0) c = RED;
      if (a == 1) c = ORANGE;
      if (a == 2) c = YELLOW;
      break;
  }
  create(_x, _y, _type, c);
}

void _particle::create(int _x, int _y, int _type, int _color, string _text)
{
  int a, b;
  x = float(_x);
  y = float(_y);
  type = _type;
  a = rand()%2;
  if (a == 0) a = -1;
  b = rand()%2;
  if (b == 0) b = -1;
  color = _color;
  text = _text;
	delay = 0;
  switch(type)
  {
    case P_FLAME:      
      xv = (float(rand()%30) / 10) * float(a);
      yv = (float(rand()%30) / 10) * float(b);
      xa = 0;
      ya = 0;
      life = 8;
      break;
    case P_DUST:
      xv = (float(rand()%5) / 10) * float(a);
      yv = (float(rand()%5) / 10) * float(b) - 0.5;
      xa = 0;
      ya = 0;
      life = 10;
      break;
    case P_SMOKE:
      xv = (float(rand()%5) / 10) * float(a);
      yv = (float(rand()%2) / 10) * float(a);
      xa = 0;
      ya = -0.02;
      life = 25;
      break;
    case P_AIRTRAIL:
      xv = (float(rand()%6) / 10) * float(a);
      yv = (float(rand()%6) / 10) * float(b);
      xa = 0;
      ya = 0;
      life = 10;
      break;
    case P_SEATRAIL:
      xv = (float(rand()%5) / 10) * float(a);
      yv = (float(rand()%5) / 10) * float(b);
      xa = 0;
      ya = 0;
      life = 30;
      break;
    case P_HORIZONTAL:
      xv = (3.0 + float(rand()%12)) * float(a);
      yv = 0;
      xa = 0;
      ya = 0;
      life = 20;
      break;
    case P_FADETEXT:
      xv = 0;
      yv = 0;
      xa = 0;
      ya = 0;
      life = 50;
      break;
    case P_SHOCKWAVE:
      xv = 0;
      yv = 0;
      xa = 0;
      ya = 0;
      life = 25;
      break;
    case P_TELEPORT:
      xv = 0;
      yv = 0;
      xa = 0;
      ya = 0;
      life = 16;
      break;
		case P_SHOTHIT:
			xv = 0;
		  yv = 0;
		  xa = 0;
		  ya = 0;
		  life = 6;
		  break;
  }
  exists = true;
}

void _particle::move()
{
  xv += xa;
  x += xv;
  yv += ya;
  y += yv;
  life--;
  if (life <= 0)
  {
    exists = false;
  }
}

void _particle::draw()
{
  int screenx = int(x) - (worldmap.scroll_x * MAP_TILE_SIZE) + worldmap.offset_x;
  int screeny = int(y) - (worldmap.scroll_y * MAP_TILE_SIZE) + worldmap.offset_y;
  int r, t1, t2;
  if ((screenx > -10) && (screenx < 650) && (screeny > -10) && (screeny < 490))
  {
    switch(type)
    {
      case P_FLAME:
        r = (10 - life) / 5;
        buffer_circlefill(screenx, screeny, r, color);
        break;
      case P_DUST:
        r = (10 - life) / 5;
        buffer_circlefill(screenx, screeny, float(r + 1), LGREY, float(life) * 0.3);
        break;
      case P_SMOKE:
        r = (25 - life) / 5;
        buffer_circlefill(screenx, screeny, r, GREY, float(life) / 15.0);
        break;
      case P_AIRTRAIL:
        buffer_putpixel(screenx, screeny, WHITE, 0.6);
        break;
      case P_SEATRAIL:
        buffer_putpixel(screenx, screeny, makecol(170, 170, 255));
        break;
      case P_HORIZONTAL:
        buffer_putpixel(screenx, screeny, color);
        if (life % 5 == 0)
        {
          buffer_putpixel(screenx + 1, screeny, color);
          buffer_putpixel(screenx - 1, screeny, color);
          buffer_putpixel(screenx, screeny + 1, color);
          buffer_putpixel(screenx, screeny - 1, color);
        }
        else
        {
          buffer_putpixel(screenx + 1, screeny + 1, color);
          buffer_putpixel(screenx - 1, screeny + 1, color);
          buffer_putpixel(screenx + 1, screeny - 1, color);
          buffer_putpixel(screenx - 1, screeny - 1, color);
        }        
        break;
      case P_FADETEXT:
        buffer_textout_ex(big, text, int(x), int(y), color, float(life) / 50);
        break;
      case P_SHOCKWAVE:
        r = (25 - life) * 3;
        buffer_circlefill(screenx, screeny, r, WHITE, float(life) / 25.0);
        break;
      case P_TELEPORT:
        t1 = screenx + MAP_TILE_SIZE / 2;
        t2 = screeny + MAP_TILE_SIZE / 2;
        if (life > 8)
        {
          buffer_rectfill(t1 - (16 - life)*2, 0, t1 + (16 - life)*2, t2, WHITE);
          buffer_circlefill(t1, t2, (16 - life)*2, WHITE);
        }
        else
        {
          buffer_rectfill(t1 - life*2, 0, t1 + life*2, t2, WHITE);
          buffer_circlefill(t1, t2, life*2, WHITE);
        }
        break;
			case P_SHOTHIT:
				buffer_circlefill(screenx, screeny, life, makecol(220 - life * 20, 0, 0), 0.5);
			  create_particle(int(x), int(y), P_SMOKE);
			  break;
    }
  }
}

void create_particle(int x, int y, int type)
{
  create_particle(x, y, type, WHITE);
}

void create_particle(int x, int y, int type, int color, string text)
{
  unsigned int i = 0;
  bool created = false;
  _particle t;
  if (global.particles == 0)
  {
    return;
  }
  while ((i < p_effects.size()) && (!created))
  {
    if (!p_effects[i].exists)
    {
      p_effects[i].create(x, y, type, color, text);
      created = true;
    }
    i++;
  }
  if (!created)
  {
    p_effects.push_back(t);
    i = p_effects.size() - 1;
    p_effects[i].create(x, y, type, color, text);
  }
}

void do_particles()
{
  bool all_empty = true;
  unsigned int i = 0;
  while (i < p_effects.size())
  {
    if (p_effects[i].exists)
    {
      all_empty = false;
			if (p_effects[i].delay > 0)
			{
				p_effects[i].delay--;
			}
			else
			{
        p_effects[i].move();
        p_effects[i].draw();
			}
    }
    i++;
  }
  if ((all_empty) && (p_effects.size() > 0))
  {
    p_effects.resize(0);
  }
}

void clear_particles()
{
  p_effects.clear();
}
