// Jason Brillante "Damdoshi"
// Hanged Bunny Studio 2014-2015
//
// KoBang!

#include		<stdio.h>
#include		<iostream>
#include		<math.h>
#include		"kobang.hpp"

void			kobang::act(void)
{
  std::list<sf::Vector2f>::iterator	it;
  double		x;

  if (msg_timer > 0)
    msg_timer -= 1;
  if (heroshield != 1 && (shield_regen += 1) > 50)
    {
      shield_regen = 0;
      if ((heroshield += 1) > 1)
	heroshield = 1;
    }

  if (serie_cnt > 20)
    {
      if (serie_cnt % 2)
	turn_left();
      else
	turn_right();
    }

  if (serie_cnt > 30)
    {
      eye_counter += 1;
      set_eye_size(50 + cos(eye_counter * M_PI / 180.0) * 20);
    }


  for (it = bombs.begin(); it != bombs.end();)
    {
      sf::Vector2f	&b = *it;

      if ((b.x += global_speed) > deepness)
	{
	  catch_serie = 0;
	  shield_regen = 0;
	  if ((heroshield -= 1) < 0)
	    {
	      if ((herolife -= 1) < 0)
		{
		  start();
		  return ;
		}
	      bombs.clear();
	      bomb_timer = 50;
	      msg = "outch !";
	      msg_timer = 20;
	      if ((global_speed -= 0.3) < 0.1)
		global_speed = 0.1;
	      return ;
	    }
	  it = bombs.erase(it);
	}
      else if ((int)b.x >= deepness - 1 && heropos == (int)b.y)
	{
	  char		buffer[128];

	  heroscore += 10 + catch_serie++;
	  gets.setPitch((fmod(rand(), 1000.0) - 200.0) / 1000.0 + 1.0);
	  gets.play();
	  if (catch_serie)
	    {
	      if (catch_serie < 100 && catch_serie % 10 == 0)
		{
		  snprintf(&buffer[0], sizeof(buffer), "super combo ! x%d", catch_serie);
		  msg = &buffer[0];
		  msg_timer = 20;
		}
	      else if (catch_serie < 1000 && catch_serie % 100 == 0)
		{
		  snprintf(&buffer[0], sizeof(buffer), "hyper combo ! x%d", catch_serie);
		  msg = &buffer[0];
		  msg_timer = 20;
		}
	      else if (catch_serie < 10000 && catch_serie % 1000 == 0)
		{
		  snprintf(&buffer[0], sizeof(buffer), "ultra combo ! x%d", catch_serie);
		  msg = &buffer[0];
		  msg_timer = 20;
		}
	    };

	  it = bombs.erase(it);
	}
      else
	++it;
    }
  if (bomberpos >= nextpos - global_speed * 4 && bomberpos <= nextpos + global_speed * 4)
    {
      nextpos = rand() % (width - 1);
      sens = !sens;
    }
  else if (sens)
    {
      if ((bomberpos += global_speed * 4) >= width)
	bomberpos = 0;
    }
  else
    if ((bomberpos -= global_speed * 4) <= 0)
      bomberpos = width;

  if (!started)
    {
      heropos = (heropos + 1) % width;
      return ;
    }

  x = bomberpos - floor(bomberpos);
  if ((bomb_timer -= 1) < 0 && (x <= 0.2 || x >= 0.8))
    {
      shots.setPitch((fmod(rand(), 1000.0) - 500.0) / 1000.0 + 1.0);
      shots.play();
      bombs.push_back(sf::Vector2f(0.5, ::round(bomberpos)));
      if ((serie += 1) > 20)
	{
	  serie = 0;
	  bomb_timer = 50;
	  serie_cnt += 1;
	  leveling();
	}
      else
	bomb_timer = bomb_delay;
    }
}
