/*Copyright (C) 2010 Deveaux Duncan

This file is part of Hikou no mizu.

Hikou no mizu is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Hikou no mizu is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Animation.hpp"

Animation::Animation(std::string pCheminAnimation, int pNbFrames, sf::Sprite *pImageMoteurRendu) :
iFrame(0), nbFrames(pNbFrames), cheminFrames(pCheminAnimation), imageMoteurRendu(pImageMoteurRendu)
{
	initialiseImages();
}

void Animation::changeFrame(int pFrame)
{
	if (pFrame < nbFrames && pFrame >= 0) iFrame = pFrame;
	
	imageMoteurRendu->SetImage(*animationSprites[iFrame].GetImage(), true);
}

void Animation::suivant()
{
	int bufferFrame = iFrame;
	if (bufferFrame < nbFrames - 1) bufferFrame++;
	else bufferFrame = 0;
	
	changeFrame(bufferFrame);
}

void Animation::precedent()
{
	int bufferFrame = iFrame;
	if (bufferFrame > 0) bufferFrame--;
	else bufferFrame = nbFrames - 1;
	
	changeFrame(bufferFrame);
}

void Animation::initialiseImages()
{
	GestionnaireImage *gestionnaireAnim = GestionnaireImage::recupereInstance();
	
	for (int i=0; i < nbFrames; i++)
	{
		//Récupération du chemin de l'image
		std::ostringstream convertInt;
		convertInt << i + 1;
		std::string nomAnimation = cheminFrames + "f"+convertInt.str()+".png";
		
		//Création du Sprite et mise en place de celui ci dans le vector animationSprites
		sf::Sprite spriteImage;
		spriteImage.SetImage(*gestionnaireAnim->recupereImage(nomAnimation));
		
		animationSprites.push_back(spriteImage);
	}
}

//Fonctions get/set
int Animation::getNbFrames() const
{
	return nbFrames;
}

int Animation::getActFrame() const
{
	return iFrame;
}
