// Jason Brillante "Damdoshi"
// Hanged Bunny Studio 2014-2015
//
// Sewer Glue

#include		<sstream>
#include        <math.h>
#include		"Ingame.hpp"

void			hbs::Ingame::Display(sf::RenderTarget		&screen)
{
  static const int	miam_anim[9] =
    {
      0, 1, 2, 3, 3, 2, 1, 0, 0
    };
  std::list<Element>::iterator	it;
  int				i;
  int				j;
  int				xp;
  int				yp;

  background.setPosition(camera_position, 0);
  screen.draw(background);

  for (it = people.begin(); it != people.end(); ++it)
    if (it->layer == BACK)
      {
	Element			&el = *it;

	if (el.speed < 0)
	  el.people.setScale(-1, 1);
	else
	  el.people.setScale(1, 1);
	el.people.setTextureRect(sf::IntRect(el.subtype * 30 + ((int)el.anim % 2) * 15, 0, 15, 27));
	el.people.setPosition(el.x + camera_position, 16);
	screen.draw(el.people);
      }

  middleground.setPosition(camera_position, 0);
  screen.draw(middleground);

  if (monster_hidden && monster_hidingplace < 5)
    {
      backmonster_hidden.setPosition
	(sewers[monster_hidingplace].left + camera_position, sewers[monster_hidingplace].top);
      screen.draw(backmonster_hidden);
    }

  if (monster_hidden == false)
    {
      monster.setPosition(monster_x + camera_position, monster_y);
      monster.setTextureRect(sf::IntRect(((monster_counter / 10) % 2) * 15, 0, 15, 15));
      screen.draw(monster);
    }

  if (miam != 0 && monster_hidingplace < 5)
    {
      backmonster.setTextureRect(sf::IntRect(miam_anim[miam / 4] * 32, 0, 32, 43));
      backmonster.setPosition
	(sewers[monster_hidingplace].left + camera_position - 3,
	 sewers[monster_hidingplace].top - backmonster.getGlobalBounds().height + 3);
      screen.draw(backmonster);
    }

  /// Car
  for (it = car.begin(); it != car.end(); ++it)
    if (it->layer == BACK)
      {
	Element			&el = *it;

	el.people.setScale(-1, 1);
	el.people.setPosition(el.x + camera_position, 43);
	screen.draw(el.people);
      }
  for (it = car.begin(); it != car.end(); ++it)
    if (it->layer == FORE)
      {
	Element			&el = *it;

	el.people.setScale(1, 1);
	el.people.setPosition(el.x + camera_position, 59);
	screen.draw(el.people);
      }

  foreground.setPosition(camera_position, 0);
  screen.draw(foreground);

  if (monster_hidden && monster_hidingplace >= 5)
    {
      foremonster_hidden.setPosition
	(sewers[monster_hidingplace].left + camera_position, sewers[monster_hidingplace].top);
      screen.draw(foremonster_hidden);
    }

  for (it = people.begin(); it != people.end(); ++it)
    if (it->layer == FORE)
      {
	Element			&el = *it;

	if (el.speed < 0)
	  el.people.setScale(-1, 1);
	else
	  el.people.setScale(1, 1);
	el.people.setTextureRect(sf::IntRect(el.subtype * 30 + ((int)el.anim % 2) * 15, 0, 15, 27));
	el.people.setPosition(el.x + camera_position, 63);
	screen.draw(el.people);
      }

  if (miam != 0 && monster_hidingplace >= 5)
    {
      foremonster.setTextureRect(sf::IntRect(miam_anim[miam / 4] * 22, 0, 22, 31));
      foremonster.setPosition
	(sewers[monster_hidingplace].left + camera_position + 2,
	 sewers[monster_hidingplace].top - foremonster.getGlobalBounds().height +
	 sewers[monster_hidingplace].height - 2);
      screen.draw(foremonster);
    }

  if (outch_delay > 0)
    lifebar.setPosition(5 + cos(32 * outch_delay * M_PI / 180.0) * 2, 5);
  else
    lifebar.setPosition(5, 5);
  lifebar.setTextureRect(sf::IntRect(0, life * 8, 32, 8));
  screen.draw(lifebar);

  std::list<Word>::iterator		itw;

  for (itw = words.begin(); itw != words.end(); ++itw)
    {
      xp = itw->x;
      yp = itw->y;
      if (itw->cam)
	xp += camera_position;
      if (itw->color)
	font.setColor(sg->vic20palette[rand() % 16]);
      else
	font.setColor(sf::Color::White);
      for (i = 0; i < itw->fade; ++i)
	if (itw->str[i] == '\n')
	  {
	    yp += 8;
	    xp = itw->x;
	    if (itw->cam)
	      xp += camera_position;
	  }
	else
	  {
	    font.setTextureRect(sf::IntRect(itw->str[i] * 5, 0, 5, 7));
	    font.setPosition(xp, yp);
	    screen.draw(font);
	    xp += 6;
	  }
    }
  std::stringstream			score;
  std::string				sco;

  score << people_get;
  sco = score.str();
  for (i = 0, xp = 5; i < (int)sco.size(); ++i, xp += 6)
    {
      font.setTextureRect(sf::IntRect(sco[i] * 5, 0, 5, 7));
      font.setPosition(xp, sg->GetScreenHeight() - 9);
      screen.draw(font);
    }

  if (warping || (victory_step >= 200 && victory_step < 209))
    for (j = 0; j < sg->GetScreenHeight(); j += 7)
      for (i = 0; i < sg->GetScreenWidth(); i += 5)
	{
	  font.setColor(sf::Color::Black);
	  font.setTextureRect(sf::IntRect(('!' - warp_animation - 1) * 5, 0, 5, 7));
	  font.setPosition(i, j);
	  screen.draw(font);
	}

  if (arriving)
    for (j = 0; j < sg->GetScreenHeight(); j += 7)
      for (i = 0; i < sg->GetScreenWidth(); i += 5)
	{
	  font.setColor(sf::Color::Black);
	  font.setTextureRect(sf::IntRect(('!' - arriving - 1) * 5, 0, 5, 7));
	  font.setPosition(i, j);
	  screen.draw(font);
	}
}

