//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * アニメーションセット実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Animation/System/AnimationSet.h"
#include "Animation/System/AnimationManager.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
AnimationSet::AnimationSet(String name, AnimationManager* manager) :
	Animation(name, manager){
}
//------------------------------------------------------------------------------
// デストラクタ
AnimationSet::~AnimationSet(){
}
//------------------------------------------------------------------------------
// バインド
bool AnimationSet::bind(Scene* scene){
	// 全てのバインドが成功すれば成功
	bool result = true;
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		if(!getAnimation(i)->bind(scene)){ result = false; }
	}
	return result;
}
//------------------------------------------------------------------------------
// バインド解除
void AnimationSet::unbind(){
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		getAnimation(i)->unbind();
	}
}
//------------------------------------------------------------------------------
// シーケンス数の取得
int AnimationSet::getSequenceCount() const{
	// 一番小さいシーケンス数を取り出す
	int animationCount = getAnimationCount();
	if(animationCount == 0){ return 0; }
	int result = getAnimation(0)->getSequenceCount();
	for(int i = 1; i < animationCount; i++){
		int count = getAnimation(i)->getSequenceCount();
		if(count < result){ result = count; }
	}
	return result;
}
//------------------------------------------------------------------------------
// シーケンスの設定
void AnimationSet::setSequence(int sequence, float time){
	Assert(sequence >= 0);
	Assert(sequence < getSequenceCount());
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		getAnimation(i)->setSequence(sequence, time);
	}
}
//------------------------------------------------------------------------------
// シーケンスの取得
int AnimationSet::getSequence() const{
	// 一番小さいシーケンスを取り出す
	int animationCount = getAnimationCount();
	if(animationCount == 0){ return 0; }
	int result = getAnimation(0)->getSequence();
	for(int i = 1; i < animationCount; i++){
		int sequence = getAnimation(i)->getSequence();
		if(sequence < result){ result = sequence; }
	}
	return result;
}
//------------------------------------------------------------------------------
// 時間の設定
void AnimationSet::setTime(float time){
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){ getAnimation(i)->setTime(time); }
}
//------------------------------------------------------------------------------
// 時間の取得
float AnimationSet::getTime() const{
	// 一番小さい時間を取り出す
	int animationCount = getAnimationCount();
	if(animationCount == 0){ return 0.f; }
	float result = getAnimation(0)->getTime();
	for(int i = 1; i < animationCount; i++){
		float time = getAnimation(i)->getTime();
		if(time < result){ result = time; }
	}
	return result;
}
//------------------------------------------------------------------------------
// アニメーション
bool AnimationSet::animate(float deltaTime, AnimationMask mask){
	// 有効でなければアニメーションしない
	if(!isEnabled()){ return true; } 
	// 全てのアニメーションが終了していればtrue
	bool result = true;
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		if(!getAnimation(i)->animate(deltaTime, mask)){ result = false; }
	}
	return result;
}
//------------------------------------------------------------------------------
// 長さの取得
float AnimationSet::getLength() const{
	// 一番長い長さを取得
	int animationCount = getAnimationCount();
	if(animationCount == 0){ return 0.f; }
	float result = getAnimation(0)->getLength();
	for(int i = 1; i < animationCount; i++){
		float length = getAnimation(i)->getLength();
		if(length > result){ result = length; }
	}
	return result;
}
//------------------------------------------------------------------------------
// 終了しているか
bool AnimationSet::isFinished() const{
	// 全てが終了していればtrue
	bool result = true;
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		if(!getAnimation(i)->isFinished()){ result = false; }
	}
	return result;
}
//------------------------------------------------------------------------------
// ループしているか
bool AnimationSet::isLooped() const{
	// 一つでもループしていればtrue;
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		if(getAnimation(i)->isLooped()){ return true; }
	}
	return false;
}
//------------------------------------------------------------------------------
// アニメーションセットのコピー
AnimationSet* AnimationSet::copyAnimationSet(DataCopyMask dataCopyMask) const{
	AnimationSet* animation = getManager()->createAnimationSet(getName());
	animation->setEnabled(isEnabled());
	int animationCount = getAnimationCount();
	for(int i = 0; i < animationCount; i++){
		animation->addAnimation(getAnimation(i)->copy(dataCopyMask));
	}
	return animation;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
