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

//魔理沙　恋符　高速ボム　恋符「ノンディレクショナルレーザー」
script_spell NonDirectionalLaser
{
	let se = GetCurrentScriptDirectory() ~ "players\MarisaB\sound\bomb1.wav";
	let img = GetCurrentScriptDirectory() ~ "players\MarisaB\img\shot_marisa.png";
	//let img = GetCurrentScriptDirectory() ~ "players\MarisaB\img\shot_all.png";
	let Time    = 300;
	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 1.8, 1.0 );
		TMain();
	}

	@Finalize {
		SetSpeed( 5, 2.0 );
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );
		TRoundLaser( Time - 48 );
		loop ( Time - 48 ) { yield; }
		End;
	}
	
	task TRoundLaser( let timeBase ) {
		//let srcXs = [ 1, 31, 61 ];
		//let srcYs = [ 222, 222, 222 ];
		
		let srcXs = [ 211, 211, 211 ];
		let srcYs = [ 192, 192, 192 ];
		
		//let srcXs = [ 436, 376, 376 ];
		
		//let srcXs = [ 466, 466, 466 ];
		//let srcYs = [ 302, 302, 302 ];
		
		//let srcXs = [ 382, 382, 382 ];
		//let srcYs = [ 332, 332, 332 ];
		let colors = [ [ 165, 165, 255 ], [ 175, 255, 255 ], [ 175, 255, 255 ] ];
		//let imgWid = 17;
		//let imgHgt = 17;
		let imgWid = 30;
		let imgHgt = 30;
		let angles = [ 270, 30, 150 ];
		//let wid = 160;
		let wid = 128;
		let hgt = 1024;
		let num = 3;
		//let laserGap = hgt / 2 - 160;
		let laserGap = hgt / 2 - 64;
		let objs = [];
		let da = 0;
		let dir = [ 1, -1 ][ rand_int( 0, 1 ) ];
		let dmg = 11.22;
		let dmgCenter = 5;
		let centerWidth = 12;
		
		ascent ( let j in 0..num ) {
			let obj = Obj_Create( OBJ_SPELL );
			Obj_SetAlpha( obj, 0 );
			ObjEffect_SetTexture( obj, img );
			ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
			ObjEffect_CreateVertex( obj, 4 );
			ascent ( let i in 0..4 ) {
				let x = truncate( i / 2 );
				let y = i % 2;
				ObjEffect_SetVertexColor( obj, i, 0, 255, 255, 255 );
				ObjEffect_SetVertexXY( obj, i,
									   wid / 2 * [ -1, 1 ][ x ],
									   hgt / 2 * [ -1, 1 ][ y ] );
				ObjEffect_SetVertexUV( obj, i,
									   srcXs[ j ] + [ 0, imgWid ][ x ],
									   srcYs[ j ] + [ 0, imgHgt ][ y ] );
				ObjEffect_SetVertexColor( obj, i, 0, 0, 0, 0 );
			}
			let nDstX = GetPlayerX() + sin( angles[ j ] ) * laserGap;
			let nDstY = GetPlayerY() - cos( angles[ j ] ) * laserGap;
			ObjEffect_SetAngle( obj, 0, 0, angles[ j ] );
			ObjEffect_SetLayer( obj, 2 );
			Obj_SetPosition( obj, nDstX, nDstY );
			ObjEffect_SetRenderState( obj, ADD );
			Obj_SetAutoDelete( obj, false );
			objs = objs ~ [ obj ];
		}
		
		ascent ( let t in 0..timeBase ) {
			let alpha;
			let alphaBegin = 30;
			let alphaEnd = 90;
			let alphaMax = 255;
			if ( t < alphaBegin ) {
				alpha = alphaMax * t / 30;
			}
			else if ( timeBase - alphaEnd < t ) {
				alpha = alphaMax - alphaMax * ( t - ( timeBase - alphaEnd ) ) / alphaEnd;
			}
			else {
				alpha = alphaMax;
			}
			da += 5 * t / timeBase * dir;
			ascent ( let j in 0..num ) {
				let nDstX = GetPlayerX() + sin( angles[ j ] + da ) * laserGap;
				let nDstY = GetPlayerY() - cos( angles[ j ] + da ) * laserGap;
				let nDstX2 = GetPlayerX() + sin( angles[ j ] + da ) * centerWidth;
				let nDstY2 = GetPlayerY() - cos( angles[ j ] + da ) * centerWidth;
				Obj_SetPosition( objs[ j ], nDstX, nDstY );
				ObjEffect_SetAngle( objs[ j ], 0, 0, angles[ j ] + da );
				ascent ( let i in 0..4 ) {
					ObjEffect_SetVertexColor( objs[ j ], i, alpha,
											  colors[ j ][ 0 ],
											  colors[ j ][ 1 ],
											  colors[ j ][ 2 ] );
				}
				ObjSpell_SetIntersecrionLine( objs[ j ], GetPlayerX(), GetPlayerY(),
											  nDstX, nDstY, wid / 4, dmg, true );
				ObjSpell_SetIntersecrionLine( objs[ j ], GetPlayerX(), GetPlayerY(),
											  nDstX2, nDstY2, centerWidth, dmgCenter, true );
			}
			yield;
		}
		ascent ( let j in 0..num ) {
			Obj_Delete( objs[ j ] );
		}
	}
	
}

//魔理沙　恋符　低速ボム　恋符「マスタースパーク」
script_spell MasterSpark
{
#include_function "..\common\function_value.dnh"
	
	let se = GetCurrentScriptDirectory() ~ "players\MarisaB\sound\bomb1.wav";
	let img = GetCurrentScriptDirectory() ~ "players\MarisaB\img\shot_marisa.png";
	//let img = GetCurrentScriptDirectory() ~ "players\MarisaB\img\shot_all.png";
	let Time = 360;
	let count = 0;

	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 0.5, 0.5 );
		TMain;
	}

	@Finalize {
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );
		TMainLaser( Time - 48 );
		loop ( Time - 48 - 60 ) { yield; }
		SetSpeed( 5, 2.0 );
		loop ( 60 ) { yield; }
		End;
	}

	task TMainLaser( let time ) {
		let angles = [ -16, -10.66, -5.33, 0, 5.33, 10.66, 16 ];
		//let srcXs = [ 256, 286, 316, 346, 376, 406, 436 ];
		//let srcYs = [ 302, 302, 302, 302, 302, 302, 302 ];
		let srcXs = [ 1, 31, 61, 91, 121, 151, 181 ];
		let srcYs = [ 192, 192, 192, 192, 192, 192, 192 ];
		let num = 7;
		
		ascent ( let j in 0..num ) {
			__TMainLaser( time, angles[ j ], srcXs[ j ], srcYs[ j ] );
		}
	}
	
	task __TMainLaser( let time, let angle, let srcX, let srcY ) {
		let imgWid = 30;
		let imgHgt = 30;
		let wid = 512;
		let hgt = 1024;
		let MS_SPARK_GAP_Y				= 440;//392;
		let MS_SPARK_ALPHA_DECAY		= 160;//160
		let timePhase0 = 50;
		let timePhase2 = 90;
		let timePhase1 = time - timePhase0 - timePhase2;
		let nBrightMax = 255;
		let nAlphaMax = 128;
		let dmg = 1.8;

		let obj = Obj_Create( OBJ_SPELL );
		Obj_SetAlpha( obj, 0 );
		ObjEffect_SetTexture( obj, img );
		ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
		ObjEffect_CreateVertex( obj, 4 );
		ascent ( let i in 0..4 ) {
			let x = truncate( i / 2 );
			let y = i % 2;
			ObjEffect_SetVertexColor( obj, i, 0, 255, 255, 255 );
			ObjEffect_SetVertexXY( obj, i,
								   wid / 2 * [ -1, 1 ][ x ],
								   hgt / 2 * [ -1, 1 ][ y ] );
			ObjEffect_SetVertexUV( obj, i,
								   srcX + [ 0, imgWid ][ x ],
								   srcY + [ 0, imgHgt ][ y ] );
			ObjEffect_SetVertexColor( obj, i, 0, 0, 0, 0 );
		}
		ObjEffect_SetAngle( obj, 0, 0, angle );
		ObjEffect_SetLayer( obj, 2 );
		ObjEffect_SetRenderState( obj, ADD );
		Obj_SetAutoDelete( obj, false );

		//DoFlashDamage( obj, 2.2 );
		ascent( let i in 0..timePhase0 ){
			let nGapY = SlowDown( 0, MS_SPARK_GAP_Y, i, timePhase0 );
			Obj_SetPosition( obj, GetPlayerX() + sin( angle ) * nGapY , GetPlayerY() - nGapY );
			let nScale = SlowDown( 0, 1, i, timePhase0 );
			ObjEffect_SetScale( obj, Min( nScale * 2, 1 ), nScale );
			let nBright = Accelerate( 0, nBrightMax, i, timePhase0 );
			ascent( let j in 0..4 ){
				ObjEffect_SetVertexColor( obj, j, nAlphaMax, nBright, nBright, nBright );
			}
			let nDstX = GetPlayerX() + cos( 270 + angle ) * hgt;
			let nDstY = GetPlayerY() + sin( 270 + angle ) * hgt;
			ObjSpell_SetIntersecrionLine( obj, GetPlayerX(), GetPlayerY(),
										  nDstX, nDstY, wid, dmg, true );
			yield;
		}
		ascent( let i in 0..timePhase1 ){
			let nAngleSustain = Accelerate( angle, angle / 3, i, timePhase1 );
			let nAlphaDecay = Smooth( 32, -32, absolute( angle ), 20 );
			let nBright = Accelerate( nBrightMax, MS_SPARK_ALPHA_DECAY + nAlphaDecay,
									  i, timePhase1 );
			ascent( let j in 0..4 ){
				ObjEffect_SetVertexColor( obj, j, nAlphaMax, nBright, nBright, nBright );
			}
			Obj_SetPosition( obj, GetPlayerX() + sin( nAngleSustain )
									* MS_SPARK_GAP_Y, GetPlayerY() - MS_SPARK_GAP_Y );
			ObjEffect_SetAngle( obj, 0, 0, nAngleSustain );
			let nDstX = GetPlayerX() + cos( 270 + angle ) * hgt;
			let nDstY = GetPlayerY() + sin( 270 + angle ) * hgt;
			ObjSpell_SetIntersecrionLine( obj, GetPlayerX(), GetPlayerY(),
										  nDstX, nDstY, wid, dmg, true );
			yield;
		}
		ascent( let i in 0..timePhase2 ){
			let nAlphaDecay = Smooth( 32, -32, absolute( angle ), 20 );
			let nBright = SlowDown( MS_SPARK_ALPHA_DECAY + nAlphaDecay, 0, i, timePhase2 );
			let nScaleX = Accelerate( 1, 0, i, timePhase2 );
			ascent( let j in 0..4 ){
				ObjEffect_SetVertexColor( obj, j, nAlphaMax, nBright, nBright, nBright );
			}
			Obj_SetPosition( obj, GetPlayerX() + sin( angle / 3 )
									* MS_SPARK_GAP_Y, GetPlayerY() - MS_SPARK_GAP_Y );
			ObjEffect_SetScale( obj, nScaleX, 1 );
			let nDstX = GetPlayerX() + cos( 270 + angle ) * hgt;
			let nDstY = GetPlayerY() + sin( 270 + angle ) * hgt;
			ObjSpell_SetIntersecrionLine( obj, GetPlayerX(), GetPlayerY(),
										  nDstX, nDstY, wid, dmg, true );
			yield;
		}
		Obj_Delete( obj );
	}

}
