//////////////////////////////////////////////////////////////////////
//	スペルカードスクリプト
//////////////////////////////////////////////////////////////////////

//霊夢　夢符　高速ボム　夢符「封魔陣」
script_spell Fumajin
{
	#include_function "..\common\function_value.dnh"
		
	let se = GetCurrentScriptDirectory() ~ "players\ReimuB\sound\bomb0.wav";
	let img = GetCurrentScriptDirectory() ~ "players\ReimuB\img\spell_reimu.png";
	let Time    = 180;
	@Initialize {
		SetPlayerInvincibility( Time );
		TMain();
	}

	@Finalize {
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );
		TFumajin( Time - 60 );
		loop ( Time - 48 ) { yield; }
		End;
	}

	task TFumajin( let timeBase ) {
		let num = 4;
		let angles = [ 0, 180, 90, 270 ];
		let speeds = [ 7, 7, 11, 11  ];
		let graphicAngles = [ 180, 0, -90, 90 ];
		let srcXs = [ 1, 1, 1, 1 ];
		let srcYs = [ 1, 1, 257, 257 ];
		ascent ( let i in 0..num ) {
			__TFumajin( angles[ i ], speeds[ i ], graphicAngles[ i ], srcXs[ i ], srcYs[ i ] );
		}
	}

	//ダメージ・弾消しは爆発にかかわらずずっと付与
	task __TFumajin( let angleBase, let speed, let graphicAngle, let srcX, let srcY ) {
		let obj = Obj_Create( OBJ_SPELL );
		let srcWidth = 95;
		let srcHeight = 255;
		let scale = 2;
		let maxAlpha = 224;
		let dmg = 3.35;
		let phase0 = 40;//maxAlphaへ
		let phase2 = 40;//減速しながら動く
		let phase3 = 40;//止まってAlpha0へ
		let isYoko = [ false, true ][ angleBase == 90 || angleBase == 270 ];

		Obj_SetAlpha( obj, 0 );
		Obj_SetAutoDelete( obj, false );
		Obj_SetAngle( obj, angleBase );
		ObjEffect_SetTexture( obj, img );
		ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
		ObjEffect_CreateVertex( obj, 4 );
		ascent ( let j in 0..4 ) {
			let xIndex = truncate( j / 2 );
			let yIndex = j % 2;
			ObjEffect_SetVertexXY( obj, j,
								   scale * srcWidth / 2 * [ -1, 1 ][ xIndex ],
								   scale * srcHeight * [ -1, 1 ][ yIndex ] );
			ObjEffect_SetVertexUV( obj, j,
								   srcX + [ 0, srcWidth ][ xIndex ],
								   srcY + [ 0, srcHeight ][ yIndex ] );
			ObjEffect_SetVertexColor( obj, j, 0, 255, 255, 255 );
		}
		ObjEffect_SetLayer( obj, 2 );
		ObjEffect_SetRenderState( obj, ADD );
		ObjEffect_SetAngle( obj, 0, 0, graphicAngle );
		ObjEffect_SetScale( obj, 1, 1 );
		Obj_SetPosition( obj, GetPlayerX(), GetPlayerY() );
		////
		ascent ( let t in 0..phase0 ) {
			let alpha = Smooth( 0, maxAlpha, t, phase0 );
			ascent ( let j in 0..4 ) {
				ObjEffect_SetVertexColor( obj, j, alpha, 255, 255, 255 );
			}
			ObjSpell_SetIntersecrionLine( obj,
										  Obj_GetX( obj ) + [ 0, -srcHeight * scale ][ isYoko == true ],
										  Obj_GetY( obj ) + [ -srcHeight * scale, 0 ][ isYoko == true ],
										  Obj_GetX( obj ) + [ 0, srcHeight * scale ][ isYoko == true ],
										  Obj_GetY( obj ) + [ srcHeight * scale, 0 ][ isYoko == true ],
										  srcWidth * scale, dmg, true );
			yield;
		}
		ascent ( let t in 0..( phase2 + phase3 ) ) {
			Obj_SetSpeed( obj, SlowDown( speed, 0, t, phase2 + phase3 ) );
			let alpha = Smooth( maxAlpha, 0, t - phase2, phase3 );
			ascent ( let j in 0..4 ) {
				ObjEffect_SetVertexColor( obj, j, alpha, 255, 255, 255 );
			}
			ObjSpell_SetIntersecrionLine( obj,
										  Obj_GetX( obj ) + [ 0, -srcHeight * scale ][ isYoko == true ],
										  Obj_GetY( obj ) + [ -srcHeight * scale, 0 ][ isYoko == true ],
										  Obj_GetX( obj ) + [ 0, srcHeight * scale ][ isYoko == true ],
										  Obj_GetY( obj ) + [ srcHeight * scale, 0 ][ isYoko == true ],
										  srcWidth, dmg, true );
			yield;
		}
		Obj_Delete( obj );
	}
}


//霊夢　夢符　低速ボム　夢符「二重結界」
script_spell NijuKekkai
{
	#include_function "..\common\function_value.dnh"
		
	let se = GetCurrentScriptDirectory() ~ "players\ReimuB\sound\bomb0.wav";
	let img = GetCurrentScriptDirectory() ~ "players\ReimuB\img\spell_reimu2.png";
	let Time    = 240;
	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 2.5, 1.0 );
		TMain();
	}

	@Finalize {
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );
		TKekkai( Time - 120 );
		loop ( Time - 48 - 60 ) { yield; }
		SetSpeed( 4, 1.6 );
		loop ( 60 ) { yield; }
		End;
	}

	task TKekkai( let timeBase ) {
		let num = 3;
		let daStarts = [ 0, 0, -10 ];
		let daEnds = [ 0, 10, 0 ];
		let scaleStarts = [ 1.0, 0, 1.5 ];
		let scaleEnds = [ 0.75, 1.0, 0 ];
		let timeBases = [ timeBase, timeBase - 30, timeBase - 30 ];
		ascent ( let i in 0..num ) {
			__TKekkai( daStarts[ i ], daEnds[ i ], scaleStarts[ i ], scaleEnds[ i ],
					   timeBases[ i ], i );
		}
	}

	//ダメージ・弾消しは爆発にかかわらずずっと付与
	task __TKekkai( let daStart, let daEnd, let scaleStart, let scaleEnd, let time, let i ) {
		let obj = Obj_Create( OBJ_SPELL );
		let srcX = 0;
		let srcY = 0;
		let srcWidth = 512;
		let srcHeight = srcWidth;
		let baseWidth = 326;
		let scaleBase = baseWidth / srcWidth;
		let maxAlpha = 224;
		let dmg = 12.6;
		let dmgScale = 1.2;
		let angle = 0;

		Obj_SetAlpha( obj, maxAlpha );
		Obj_SetAutoDelete( obj, false );
		Obj_SetAngle( obj, 0 );
		ObjEffect_SetTexture( obj, img );
		ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
		ObjEffect_CreateVertex( obj, 4 );
		ascent ( let j in 0..4 ) {
			let xIndex = truncate( j / 2 );
			let yIndex = j % 2;
			ObjEffect_SetVertexXY( obj, j,
								   scaleBase * srcWidth / 2 * [ -1, 1 ][ xIndex ],
								   scaleBase * srcHeight / 2 * [ -1, 1 ][ yIndex ] );
			ObjEffect_SetVertexUV( obj, j,
								   srcX + [ 0, srcWidth ][ xIndex ],
								   srcY + [ 0, srcHeight ][ yIndex ] );
			ObjEffect_SetVertexColor( obj, j, maxAlpha, 255, 255, 255 );
		}
		ObjEffect_SetLayer( obj, 2 );
		ObjEffect_SetRenderState( obj, ADD );
		ObjEffect_SetAngle( obj, 0, 0, 0 );
		Obj_SetPosition( obj, GetPlayerX(), GetPlayerY() );
		////
		ascent ( let t in 0..( time + 60 ) ) {
			let alpha = Accelerate( maxAlpha, 0, t, time / [ 1, 1.1, 1.2 ][ i ] );
			let scale = [ Accelerate( scaleStart, scaleEnd, t, time / 1.5 ),
						  SlowDown( scaleStart, scaleEnd, t, time / 2 ) ][ i == 0 || i == 1 ];
			let da = [ Accelerate( daStart, daEnd, t, time ),
					   SlowDown( daStart, daEnd, t, time ) ][ i == 0 || i == 1 ];
			ascent ( let j in 0..4 ) {
				ObjEffect_SetVertexColor( obj, j, alpha, 255, 255, 255 );
			}
			ObjEffect_SetScale( obj, scale, scale );
			ObjEffect_SetAngle( obj, 0, 0, angle );
			if ( i == 0 ) {
				ObjSpell_SetIntersecrionLine( obj,
											  Obj_GetX( obj ),
											  Obj_GetY( obj ) - scaleBase * scale * srcHeight / 2 * dmgScale,
											  Obj_GetX( obj ),
											  Obj_GetY( obj ) + scaleBase * scale * srcHeight / 2 * dmgScale,
											  scaleBase * scale * srcWidth * dmgScale, dmg, true );
			}
			else if ( t <= time ) {
				ObjSpell_SetIntersecrionCircle( obj, Obj_GetX( obj ), Obj_GetY( obj ),
												scaleBase * scale * srcWidth * dmgScale / 2, 0, true );
			}
			angle += da;
			yield;
		}
		Obj_Delete( obj );
	}
}
