/* _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

FILE	: animeCalc.cpp

+ ------ Explanation of file --------------------------------------------------------------------------

+ _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ */

#include "Common.h"

#include <mmsystem.h>

#include "animeCalc.h"

#define GETTIME		(float)(timeGetTime() / 1000.0f)

//! vectorソート用
bool operator<(const ANIMECALC& left, const ANIMECALC& right)
{
	return left.id < right.id;
}
bool operator>(const ANIMECALC& left, const ANIMECALC& right)
{
	return left.id > right.id;
}

//! データセット
void animeCalcClass::set(int id, float time, VEC3 pos, VEC3 deg, float scale)
{
	ANIMECALC	anime;
	anime.id = id;
	anime.time = time;
	anime.pos = pos;
	anime.degree = deg;
	anime.scale = scale;

	m_animeList.push_back(anime);
}

//! 初期化
void animeCalcClass::clear(void)
{
	m_state = ANIMESTATE_NON;
	m_animeID = -1;
	m_startTime = 0.0f;
	m_elapsedTime = 0.0f;
	m_delayTime = 0.0f;
	m_isLoop = false;
	m_animeList.clear();
}

//! 開始
void animeCalcClass::start(void)
{
	sort(m_animeList.begin(), m_animeList.end());
	m_state = ANIMESTATE_RUN;
	m_animeID = m_animeList.begin()->id;
	m_startTime = GETTIME;
	m_elapsedTime = 0.0f;
}

//! 再開
void animeCalcClass::resume(void)
{
	m_state = ANIMESTATE_RUN;
	m_startTime = GETTIME - m_elapsedTime;	// 経過時間をそのままに
}

//! 停止
void animeCalcClass::stop(void)
{
	m_state = ANIMESTATE_STOP;
	m_elapsedTime = GETTIME - m_startTime;	// 更新
}

//! スキップ
void animeCalcClass::skip(void)
{
	m_state = ANIMESTATE_END;
	m_animeID = -1;
}

//! 計算
ANIMECALC animeCalcClass::calc(void)
{
	ANIMECALC ret;

	if(m_state == ANIMESTATE_END){
		ret = m_animeList.back();
	}else{
		// 時間更新
		if(m_state == ANIMESTATE_RUN){
			m_elapsedTime = GETTIME - m_startTime;
		}

		// 遅延内なので開始しない
		if(m_elapsedTime < m_delayTime){
			ret = m_animeList.front();
		}else{
			// 再生中アニメ取得
			ANIMECALC	now, back;								// 現在アニメ, 前アニメ
			float		elapsed = m_elapsedTime - m_delayTime;	// 経過時間計算
			float		time = 0.0f;							// 現在アニメまでの経過時間
			bool		isAnime = false;						// 再生中か
			for(iteAnmCalc ite = m_animeList.begin(); ite != m_animeList.end(); ite++){
				if(elapsed < time + ite->time){
					m_animeID = ite->id;
					if(ite->id == m_animeList.begin()->id){
						// 初期要素
						now = back = *ite;
					}else{
						now = *ite;
						back = *(ite - 1);
					}
					isAnime = true;
					break;
				}
				time += ite->time;
			}

			if(isAnime){
				float prog = (elapsed - time) / now.time;		// 現在アニメの進行度
				ret.pos		= back.pos	 + ((now.pos	- back.pos	) * prog);
				ret.degree	= back.degree+ ((now.degree	- back.degree) * prog);
				ret.scale	= back.scale + ((now.scale	- back.scale) * prog);
			}else if(m_isLoop){
				// 再計算
				m_startTime += getSumTime();
				ret = calc();
			}else{
				m_state = ANIMESTATE_END;
				m_animeID = -1;
				ret = m_animeList.back();
			}
		}
	}

	return(ret);
}

//! 座標取得
VEC3 animeCalcClass::getPos(void)
{
	return(calc().pos);
}

//! 角度取得
VEC3 animeCalcClass::getDegree(void)
{
	return(calc().degree);
}

//! 拡縮率取得
float animeCalcClass::getScale(void)
{
	return(calc().scale);
}

//! 合計時間取得
float animeCalcClass::getSumTime(void)
{
	float ret = 0.0f;
	for(iteAnmCalc ite = m_animeList.begin(); ite != m_animeList.end(); ite++){
		ret += ite->time;
	}

	return(ret);
}

//! 実行判定
bool animeCalcClass::isRun(void)
{
	return(m_state == ANIMESTATE_RUN);
}

//! 終了判定
bool animeCalcClass::isEnd(void)
{
	return(m_state == ANIMESTATE_END);
}

