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

//魔理沙　魔符　高速ボム　魔符「スターダストレヴァリエ」
script_spell StardustReverie
{
	#include_function "..\common\function_value.dnh"
		
	let se = GetCurrentScriptDirectory() ~ "players\MarisaA\sound\bomb0.wav";
	let img = GetCurrentScriptDirectory() ~ "players\MarisaA\img\shot_marisa.png";
	let Time    = 240;
	@Initialize {
		SetPlayerInvincibility( Time );
		TMain();
	}

	@Finalize {
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }//時間停止
		PlaySE( se );
		TRoundStar( Time - 60 );
		loop ( Time - 48 ) { yield; }
		End;
	}

	task TRoundStar( let timeBase ) {
		//R: 1, G: 97, B: 161, W: 225
		//let srcXs = [ 1, 161, 97, 1, 161, 97, 161, 97 ];
		//let srcXs = [ 225, 225, 225, 225, 225, 225, 225, 225 ];
		//let srcYs = [ 384, 384, 384, 384, 384, 384, 384, 384 ];
		let srcXs = [ 289, 289, 289, 289, 289, 289, 289, 289 ];
		let srcYs = [ 1, 1, 1, 1, 1, 1, 1, 1 ];
		/*let srcColors = [ [ 255, 255, 255 ], [ 255, 255, 255 ], [ 255, 255, 255 ],
						  [ 255, 255, 255 ], [ 255, 255, 255 ], [ 255, 255, 255 ],
						  [ 255, 255, 255 ], [ 255, 255, 255 ] ];*/
		let red = [ 255, 160, 160, ];
		let green = [ 160, 255, 160, ];
		let blue = [ 160, 160, 255, ];
		let srcColors = [ red, blue, green, red, blue, green, blue, green ];
		let dir = 1;
		let num = 8;
		ascent ( let i in 0..num ) {
			__TRoundStar( GetPlayerX(), GetPlayerY(), 90 + 360 / num * i,
						  srcXs[ i ], srcYs[ i ], srcColors[ i ], timeBase, dir );
			dir *= -1;
		}
	}

	task __TRoundStar( let x, let y, let angleBase, let srcX, let srcY, let colors, let time, let dir ) {
		let objs = [ Obj_Create( OBJ_SPELL ),
					 Obj_Create( OBJ_SPELL ),
					 Obj_Create( OBJ_SPELL ) ];
		let rGap = [ 0, 32, 48 ];
		let randAngle = 20;
		let angleGap = [ 0,
						 210 + rand_int( -randAngle, randAngle ),
						 30 + rand_int( -randAngle, randAngle ) ];
		//let srcWidth = 32;
		//let srcHeight = 32;
		//let objBaseScale = 2;
		//let objScales = [ 1.2, 2.3, 3.0 ];
		let srcWidth = 223;
		let srcHeight = 223;
		let objBaseScale = 1;
		let objScales = [ 0.27, 0.6, 0.9 ];
		let objAngles = [ rand_int( 0, 360 ), rand_int( 0, 360 ), rand_int( 0, 360 ) ];
		let da = 5;
		let maxAlpha = 160;
		let speed = 2;
		let dmg = 1.43;
		ascent ( let i in 0..length( objs ) ) {
			let obj = objs[ i ];
			Obj_SetAlpha( obj, maxAlpha );
			Obj_SetAutoDelete( obj, false );
			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,
									   objBaseScale * objScales[ i ] * srcWidth / 2 * [ -1, 1 ][ xIndex ],
									   objBaseScale * objScales[ i ] * srcHeight / 2 * [ -1, 1 ][ yIndex ] );
				ObjEffect_SetVertexUV( obj, j,
									   srcX + [ 0, srcWidth ][ xIndex ],
									   srcY + [ 0, srcHeight ][ yIndex ] );
				ObjEffect_SetVertexColor( obj, j, maxAlpha, colors[ 0 ], colors[ 1 ], colors[ 2 ] );
			}
			ObjEffect_SetLayer( obj, 2 );
			ObjEffect_SetRenderState( obj, ADD );
		}
		ascent ( let t in 0..time ) {
			ascent ( let i in 0..length( objs ) ) {
				let obj = objs[ i ];
				let r = Smooth( 0, rGap[ i ], t, 20 );
				ObjEffect_SetAngle( obj, 0, 0, objAngles[ i ] );
				ascent ( let j in 0..4 ) {
					ObjEffect_SetVertexColor( obj, j, maxAlpha,
											  colors[ 0 ], colors[ 1 ], colors[ 2 ] );
				}
				Obj_SetPosition( obj,
								 x + r * cos( angleGap[ i ] ),
								 y + r * sin( angleGap[ i ] ) );
				objAngles[ i ] = objAngles[ i ] + da * dir;
				ObjSpell_SetIntersecrionCircle( obj,
												x + r * cos( angleGap[ i ] ),
												y + r * sin( angleGap[ i ] ),
												objBaseScale * objScales[ i ] * 192 / 2,
												dmg, true );
			}
			x = x + speed * cos( angleBase );
			y = y + speed * sin( angleBase );
			yield;
		}
		ascent ( let i in 0..length( objs ) ) {
			Obj_Delete( objs[ i ] );
		}
	}
}

//魔理沙　魔符　低速ボム　魔符「ミルキーウェイ」
script_spell MilkyWay
{
	#include_function "..\common\function_value.dnh"
		
	let se = GetCurrentScriptDirectory() ~ "players\MarisaA\sound\bomb2.wav";
	let img = GetCurrentScriptDirectory() ~ "players\MarisaA\img\shot_marisa.png";
	let Time    = 300;
	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 2.5, 1 );
		TMain();
	}

	@Finalize {
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }//時間停止
		TPlaySE();
		TRailStar( Time - 48 );
		loop ( Time - 60 - 48 ) { yield; }
		SetSpeed( 5, 2.0 );
		loop ( 60 ) { yield; }
		End;
	}

	task TPlaySE() {
		let w = 7;
		loop ( 130 / w ) {
			PlaySE( se );
			loop ( w ) { yield; }
		}
	}

	task TRailStar( let timeBase ) {
		//R: 1, G: 97, B: 161, W: 225
		let srcX = 289;
		let srcY = 1;
		let red = [ 255, 160, 160 ];
		let green = [ 160, 255, 160 ];
		let blue = [ 160, 160, 255 ];
		let srcColors = [ green, blue, red ];
		let dir = 1;
		let num = 24;
		let interval = 120 / 24;
		ascent ( let i in 0..num ) {
			__TRailStar( GetPlayerX(), GetPlayerY(), 270 + rand( -25, 25 ),
						  srcX, srcY, srcColors[ i % 3 ], timeBase, dir );
			dir *= -1;
			loop ( interval ) { yield; }
		}
		
	}

	task __TRailStar( let x, let y, let angleBase, let srcX, let srcY, let colors, let time, let dir ) {
		let objs = [ Obj_Create( OBJ_SPELL ),
					 Obj_Create( OBJ_SPELL ),
					 Obj_Create( OBJ_SPELL ) ];
		let rGap = [ -96, -48, 0 ];
		let srcWidth = 223;
		let srcHeight = 223;
		let objBaseScale = 1;
		let objScales = [ 0.27, 0.6, 0.9 ];
		let objAngles = [ rand_int( 0, 360 ), rand_int( 0, 360 ), rand_int( 0, 360 ) ];
		let da = 5;
		let maxAlpha = 160;
		let maxSpeed = 15;
		let dmg = 0.3;
		ascent ( let i in 0..length( objs ) ) {
			let obj = objs[ i ];
			Obj_SetAlpha( obj, maxAlpha );
			Obj_SetAutoDelete( obj, false );
			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,
									   objBaseScale * objScales[ i ] * srcWidth / 2 * [ -1, 1 ][ xIndex ],
									   objBaseScale * objScales[ i ] * srcHeight / 2 * [ -1, 1 ][ yIndex ] );
				ObjEffect_SetVertexUV( obj, j,
									   srcX + [ 0, srcWidth ][ xIndex ],
									   srcY + [ 0, srcHeight ][ yIndex ] );
				ObjEffect_SetVertexColor( obj, j, maxAlpha, colors[ 0 ], colors[ 1 ], colors[ 2 ] );
			}
			ObjEffect_SetLayer( obj, 2 );
			ObjEffect_SetRenderState( obj, ADD );
		}
		ascent ( let t in 0..time ) {
			ascent ( let i in 0..length( objs ) ) {
				let obj = objs[ i ];
				let r = Accelerate( 0, rGap[ i ], t, 60 );
				ObjEffect_SetAngle( obj, 0, 0, objAngles[ i ] );
				ascent ( let j in 0..4 ) {
					ObjEffect_SetVertexColor( obj, j, maxAlpha,
											  colors[ 0 ], colors[ 1 ], colors[ 2 ] );
				}
				Obj_SetPosition( obj,
								 x + r * cos( angleBase ),
								 y + r * sin( angleBase ) );
				objAngles[ i ] = objAngles[ i ] + da * dir;
				ObjSpell_SetIntersecrionCircle( obj,
												x + r * cos( angleBase ),
												y + r * sin( angleBase ),
												objBaseScale * objScales[ i ] * 192 / 2,
												dmg, true );
			}
			let speed = Accelerate( 0, maxSpeed, t, 90 );
			x = x + speed * cos( angleBase );
			y = y + speed * sin( angleBase );
			yield;
		}
		ascent ( let i in 0..length( objs ) ) {
			Obj_Delete( objs[ i ] );
		}
	}
}
