//==============================================================================
//	★人形 アニメライブラリ
//　人形を使うボススクリプト冒頭にて、ボス個別ライブラリの次にインクルードする
//==============================================================================
//　定数、変数
//==============================================================================
//　カット別矩形座標定数（左上の座標）
//------------------------------------------------------------------------------
let DOLL_RECT_LENGTH	= 32;
let DOLL_RECT_STAND0	= [ 6 * RECT_LENGTH, 0 ];
let DOLL_RECT_STAND1	= [ 6 * RECT_LENGTH, DOLL_RECT_LENGTH ];
let DOLL_RECT_STAND2	= [ 6 * RECT_LENGTH, 2 * DOLL_RECT_LENGTH ];
let DOLL_RECT_SPELL0	= [ 6 * RECT_LENGTH + DOLL_RECT_LENGTH, 0 ];
let DOLL_RECT_SPELL1	= [ 6 * RECT_LENGTH + DOLL_RECT_LENGTH, DOLL_RECT_LENGTH ];

//------------------------------------------------------------------------------
//　その他変数（人形自動描画データ）
//------------------------------------------------------------------------------
let dollGapLength = 24;
let dollGapAngle = GetCommonDataDefault( "DollGapAngle", -90 );
let dollCount = GetCommonDataDefault( "DollCount", 0 );

//==============================================================================
//　アクション指示関連関数
//==============================================================================
//　アクション初期設定（人形使用版）
//------------------------------------------------------------------------------
task InitializeActionWithDoll(){
	InitializeAction();
	loop{
		while( stopAnime ){ yield; }
		dollGapAngle -= 2;
		dollCount ++;
		yield;
	}
}

//==============================================================================
//　描画関連関数
//==============================================================================
//　ボスアニメ描画（人形自動描画 通常）
//------------------------------------------------------------------------------
function DrawBossWithDoll(
	let imageFile		//　画像ファイルパス
){
	_DrawBossWithDoll( imageFile, GetX(), GetY() );
}

//------------------------------------------------------------------------------
//　ボスアニメ描画（人形自動描画 座標指定）
//------------------------------------------------------------------------------
function _DrawBossWithDoll(
	let imageFile,		//　画像ファイルパス
	let x,				//	中心x座標
	let y				//	中心y座標
){
	let sinRatio = 1;
	if( character == CHAR_MEDICINE ){ sinRatio = 2; }
	let dollX = x + dollGapLength * cos( dollGapAngle );
	let dollY = y + dollGapLength * sin( dollGapAngle * sinRatio ) * 0.4;
	if( sin( dollGapAngle ) > 0 ){
		_DrawBoss( imageFile, x, y ,false,255,255,255);
		DrawDoll( imageFile, dollX, dollY );
	}
	else{
		DrawDoll( imageFile, dollX, dollY );
		_DrawBoss( imageFile, x, y ,false,255,255,255);
	}
}

//------------------------------------------------------------------------------
//　人形描画
//------------------------------------------------------------------------------
function DrawDoll(
	let imageFile,		//　画像ファイルパス
	let x,				//	中心x座標
	let y				//	中心y座標
){
	let rect = [];
	//　3コマ（スペル） ----------------------------------------------
	if( character == CHAR_MEDICINE && action == _ACT_SPELL5 ){
		let cutFrameA = 6;
		if( actionCountMax < 36 ){ cutFrameA = trunc( actionCountMax / 6 ); }
		let cutFrameB = actionCountMax - cutFrameA;
		let animePattern = [ 0, 1, 0 ];
		let framePattern = [ cutFrameA, cutFrameB, actionCountMax ];
		let anime = animePattern[ 2 ];
		ascent( let i in 0 .. 3 ){
			if( actionCount < framePattern[ i ] ){
				anime = animePattern[ i ];
				break;
			}
		}
		rect = [ DOLL_RECT_SPELL0, DOLL_RECT_SPELL1 ][ anime ];
	}
	//　3コマループ（立ち） ------------------------------------------
	else{
		let cutFrame = 12;
		let anime = trunc( dollCount / cutFrame ) % 3;
		rect = [ DOLL_RECT_STAND0, DOLL_RECT_STAND1, DOLL_RECT_STAND2 ][ anime ];
	}
	//　描画 ---------------------------------------------------------
	SetTexture( imageFile );
	SetGraphicRect( rect[ 0 ], rect[ 1 ], rect[ 0 ] + DOLL_RECT_LENGTH, rect[ 1 ] + DOLL_RECT_LENGTH );
SetGraphicScale(1.5,1.5);
	DrawGraphic( x, y );
SetGraphicScale(1.0,1.0);
}

task EffectDrawDoll(
	let imageFile,		//　画像ファイルパス
	let x,				//	中心x座標
	let y,				//	中心y座標
	let layer
){
	let rect = [];
	//　3コマ（スペル） ----------------------------------------------
	if( character == CHAR_MEDICINE && action == _ACT_SPELL5 ){
		let cutFrameA = 6;
		if( actionCountMax < 36 ){ cutFrameA = trunc( actionCountMax / 6 ); }
		let cutFrameB = actionCountMax - cutFrameA;
		let animePattern = [ 0, 1, 0 ];
		let framePattern = [ cutFrameA, cutFrameB, actionCountMax ];
		let anime = animePattern[ 2 ];
		ascent( let i in 0 .. 3 ){
			if( actionCount < framePattern[ i ] ){
				anime = animePattern[ i ];
				break;
			}
		}
		rect = [ DOLL_RECT_SPELL0, DOLL_RECT_SPELL1 ][ anime ];
	}
	//　3コマループ（立ち） ------------------------------------------
	else{
		let cutFrame = 12;
		let anime = trunc( dollCount / cutFrame ) % 3;
		rect = [ DOLL_RECT_STAND0, DOLL_RECT_STAND1, DOLL_RECT_STAND2 ][ anime ];
	}
	//　描画 ---------------------------------------------------------
/*
	SetTexture( imageFile );
	SetGraphicRect( rect[ 0 ], rect[ 1 ], rect[ 0 ] + DOLL_RECT_LENGTH, rect[ 1 ] + DOLL_RECT_LENGTH );
SetGraphicScale(1.5,1.5);
	DrawGraphic( x, y );
SetGraphicScale(1.0,1.0);
*/
EffectDraw;

	task EffectDraw
	{
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imageFile);
	//ObjEffect_SetRenderState(obj, ADD);
	ObjEffect_SetLayer(obj,layer);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj, 4);

	ObjEffect_SetVertexUV(obj, 0, rect[ 0 ], rect[ 1 ]);
	ObjEffect_SetVertexUV(obj, 1, rect[ 0 ] + DOLL_RECT_LENGTH, rect[ 1 ]);
	ObjEffect_SetVertexUV(obj, 2, rect[ 0 ] + DOLL_RECT_LENGTH, rect[ 1 ] + DOLL_RECT_LENGTH);
	ObjEffect_SetVertexUV(obj, 3, rect[ 0 ], rect[ 1 ] + DOLL_RECT_LENGTH);

	ObjEffect_SetVertexXY(obj, 0, -DOLL_RECT_LENGTH/2, -DOLL_RECT_LENGTH/2);
	ObjEffect_SetVertexXY(obj, 1, DOLL_RECT_LENGTH/2, -DOLL_RECT_LENGTH/2);
	ObjEffect_SetVertexXY(obj, 2, DOLL_RECT_LENGTH/2, DOLL_RECT_LENGTH/2);
	ObjEffect_SetVertexXY(obj, 3, -DOLL_RECT_LENGTH/2, DOLL_RECT_LENGTH/2);

	Obj_SetPosition(obj, GetX, GetY);


	yield;
	Obj_Delete(obj);
	}

}

//==============================================================================
//　その他関数
//==============================================================================
//　人形自動描画データ保存
//	連続再生で次のスクリプトがある場合、@Finalizeで呼ぶこと
//------------------------------------------------------------------------------
function SetDollData{
	SetCommonData( "DollGapAngle", dollGapAngle );
	SetCommonData( "DollCount", dollCount );
}

//------------------------------------------------------------------------------
//　人形自動描画データ削除
//	連続再生最終スクリプトの@Finalizeで呼ぶこと
//------------------------------------------------------------------------------
function DeleteDollData{
	DeleteCommonData( "DollGapAngle"  );
	DeleteCommonData( "DollCount" );
}
