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

//咲夜　時符　高速ボム　時符「パーフェクトスクウェア」
script_spell PerfectSquare
{
	let img = GetCurrentScriptDirectory() ~ "players\SakuyaB\img\spell_sakuya.png";
	let img2 = GetCurrentScriptDirectory() ~ "players\SakuyaB\img\spell_sakuya2.png";
	let se = GetCurrentScriptDirectory() ~ "players\SakuyaB\sound\bomb1.wav";
	let Time    = 240;
	//let randomAry = [];
	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 8, 4.4 );
		/*loop ( 1024 ) {
			randomAry = randomAry ~ [ rand( -1, 1 ) ];
		}*/
		TMain();
	}

	@Finalize {
		
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );
		//CreateEnemyFromScript( TimeStoper, GetCenterX(), GetCenterY(), 0, 0, Time );
		TColorSquareScreen( Time - 48 );
		
		loop ( Time - 48 - 60 ) { yield; }
		DeleteEnemyShotToItem( SHOT );
		SetSpeed( 4, 2.2 );
		loop ( 60 ) { yield; }
		End;
	}

	task TColorSquareScreen( let time ) {
		let quadTime = 90;
		let wid = 128;
		let hgt = 128;
		let srcXs = [ 0, 128, 0, 128 ];
		let srcYs = [ 0, 0, 0, 0 ];
		let xs = [ GetCenterX() - 128, GetCenterX() + 128, GetCenterX() - 128, GetCenterX() + 128 ];
		let ys = [ GetCenterY() - 128, GetCenterY() - 128, GetCenterY() + 128, GetCenterY() + 128 ];
		let objs = [];
		let scaleStarts = [ 6.0, 6.0, 0,0, 0.0 ];
		let scaleEnds = [ 0.0, 0.0, 6.0, 6.0 ];
		let alpha = 64;
		let delays = [ 0, 20, 40, 60 ];
		//let rmax = 24;
		let dmg = 0.26;

		ascent ( let j in 0..4 ) {
			let obj = Obj_Create( OBJ_SPELL );
			Obj_SetPosition( obj, xs[ j ], ys[ j ] );
			Obj_SetAlpha( obj, 0 );
			Obj_SetCollisionToPlayer( obj, false );
			ObjEffect_SetTexture( obj, img2 );
			ObjEffect_SetRenderState( obj, ADD );
			ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
			ObjEffect_CreateVertex( obj, 4 );
			ObjEffect_SetAngle( obj, 0, 0, 90 * j );
			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, wid ][ x ],
									   srcYs[ j ] + [ 0, hgt ][ y ] );
			}
			ObjEffect_SetLayer( obj, 2 );
			objs = objs ~ [ obj ];
		}

		ascent ( let t in 0..time ) {
			/*let r;
			if ( t >= time ) {
				r = rmax;
			}
			else if ( t <= 0 ) {
				r = 0;
			}
			else {
				r = rmax * t / time;
			}*/
			ascent ( let j in 0..4 ) {
				if ( delays[ j ] <= t && t < delays[ j ] + quadTime ) {
					let count = t - delays[ j ];
					let scale;
					if ( count >= quadTime ) {
						scale = scaleEnds[ j ];
					}
					else if ( count <= 0 ) {
						scale = scaleStarts[ j ];
					}
					else {
						scale = scaleStarts[ j ]
							+ ( scaleEnds[ j ] - scaleStarts[ j ] ) * count / quadTime;
					}
					ObjEffect_SetScale( objs[ j ], scale, scale );
					ascent ( let i in 0..4 ) {
						ObjEffect_SetVertexColor( objs[ j ], i, alpha, 255, 255, 255 );
					}
					Obj_SetPosition( objs[ j ],
									 xs[ j ]/* + r * randomAry[ t * ( j + 1 ) ]*/,
									 ys[ j ]/* + r * randomAry[ t * ( j + 1 ) ]*/ );
					ObjSpell_SetIntersecrionCircle( objs[ j ],
													xs[ j ], ys[ j ],
													wid * 1.4 / 2 * scale, dmg, false );
				}
				else {
					ascent ( let i in 0..4 ) {
						ObjEffect_SetVertexColor( objs[ j ], i, 0, 255, 255, 255 );
					}
				}
			}
			yield;
		}
		ascent ( let j in 0..4 ) {
			Obj_Delete( objs[ j ] );
		}
	}
}

//咲夜　時符　低速ボム　時符「プライベートスクウェア」
script_spell PrivateSquare
{
#include_function "..\common\function_value.dnh"
	
	let img = GetCurrentScriptDirectory() ~ "players\SakuyaB\img\spell_sakuya.png";
	let img2 = GetCurrentScriptDirectory() ~ "players\SakuyaB\img\spell_sakuya2.png";
	let se = GetCurrentScriptDirectory() ~ "players\SakuyaB\sound\bomb1.wav";
	let Time = 420;
	//let randomAry = [];
	let count = 0;

	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 6, 3.3 );
		/*loop ( 1024 ) {
			randomAry = randomAry ~ [ rand( -1, 1 ) ];
		}*/
		TMain;
	}

	@Finalize {
	}

	@MainLoop {
		yield;
	}

	task TMain {
		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );

		TColorSquareScreen( Time - 120 );
		loop ( Time - 120 - 48 ) { yield; }
		DeleteEnemyShotToItem( SHOT );
		SetSpeed( 4, 2.2 );
		loop ( 120 ) { yield; }
		End;
	}
	
	task TColorSquareScreen( let time ) {
		let quadTime = 90;
		let wid = 128;
		let hgt = 128;
		let srcXs = [ 0, 128 ];
		let srcYs = [ 0, 0 ];
		let objx = GetPlayerX();
		let objy = GetPlayerY();
		let objs = [];
		let scale = 1.1;
		let alphaStart = 160;
		let alphaEnd = 64;
		//let rmax = 8;
		let vmax = 6;
		let acc = vmax / 60;
		let lmax = 128;
		let nAddAngle = 1.75;//1フレームあたりの旋回角度の上限
		let revLimit = 30;
		let nAngle;
		let blurObjsRed = [];
		let blurObjsBlue = [];
		let isRev = false;
		let dmg = 0.32;

		ascent ( let k in 0..10 ) {
			ascent ( let j in 0..2 ) {
				let obj = Obj_Create( OBJ_SPELL );
				Obj_SetPosition( obj, objx, objy );
				Obj_SetAlpha( obj, 0 );
				Obj_SetCollisionToPlayer( obj, false );
				Obj_SetSpeed( obj, 0 );
				Obj_SetAngle( obj,
							  GetGapAngle( Obj_GetX( obj ), Obj_GetY( obj ),
										   GetPlayerX(), GetPlayerY() ) );
				ObjEffect_SetTexture( obj, img2 );
				ObjEffect_SetRenderState( obj, ADD );
				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, wid ][ x ],
										   srcYs[ j ] + [ 0, hgt ][ y ] );
				}
				ObjEffect_SetLayer( obj, 2 );
				Obj_SetAutoDelete( obj, false );
				if ( k == 0 ) {
					objs = objs ~ [ obj ];
				}
				else if ( j == 0 ) {
					blurObjsRed = blurObjsRed ~ [ obj ];
				}
				else if ( j == 1 ) {
					blurObjsBlue = blurObjsBlue ~ [ obj ];
				}
			}
		}

		nAngle = GetGapAngle( Obj_GetX( objs[ 0 ] ), Obj_GetY( objs[ 0 ] ),
							  GetPlayerX(), GetPlayerY() );
		ascent ( let t in 0..time ) {
			let r;
			let alpha;
			/*if ( t >= time ) {
				r = rmax;
			}
			else if ( t <= 0 ) {
				r = 0;
			}
			else {
				r = rmax * t / time;
			}*/
			if ( t >= time ) {
				alpha = alphaEnd;
			}
			else if ( t <= time - 45 ) {
				alpha = alphaStart;
			}
			else {
				alpha = alphaStart + ( alphaEnd - alphaStart ) * ( t - time + 45 ) / 45;
			}
			let l = GetGapLength( Obj_GetX( objs[ 0 ] ), Obj_GetY( objs[ 0 ] ),
								  GetPlayerX(), GetPlayerY() );
			let angleToPlayer = GetGapAngle( Obj_GetX( objs[ 0 ] ), Obj_GetY( objs[ 0 ] ),
							  GetPlayerX(), GetPlayerY() );
			if ( nAngle >= 360 ) {
				nAngle %= 360;
			}
			while ( nAngle < 0 ) {
				nAngle += 360;
			}
			if ( angleToPlayer >= 360 ) {
				angleToPlayer %= 360;
			}
			while ( angleToPlayer < 0 ) {
				angleToPlayer += 360;
			}
			let nDiffAngle = angleToPlayer - nAngle;
			if ( nDiffAngle >= 360 ) {
				nDiffAngle %= 360;
			}
			while ( nDiffAngle < 0 ) {
				nDiffAngle += 360;
			}
			if ( ( 180 - revLimit < nDiffAngle && nDiffAngle < 180 + revLimit )
				 && isRev == false ) {
				isRev = true;
			}
			let v = Obj_GetSpeed( objs[ 0 ] ) + acc * l / lmax * [ 1, -1 ][ isRev == true ];
			if ( isRev == true ) {
				if ( v < 0 ) {
					v *= -1;
					nAngle = angleToPlayer;
					isRev = false;
				}
			}
			else if ( l < 10 && absolute( Obj_GetSpeed( objs[ 0 ] ) ) < acc * 2 ) {
				nAngle = angleToPlayer;
			}
			else if ( ( 0 <= nDiffAngle && nDiffAngle < nAddAngle )
					  || ( 360 - nAddAngle < nDiffAngle && nDiffAngle <= 360 ) ) {
				nAngle = angleToPlayer;
			}
			else if( ( nDiffAngle > 0 && nDiffAngle <= 180 )
				|| nDiffAngle <= - 180 ) {
				nAngle += nAddAngle;
			}
			else if( nDiffAngle > 180
					 || ( nDiffAngle < 0 && nDiffAngle > - 180 ) ) {
				nAngle -= nAddAngle;
			}
			ascent ( let j in 0..2 ) {
				Obj_SetSpeed( objs[ j ], MinMax( v, -vmax, vmax ) );
				Obj_SetAngle( objs[ j ], nAngle );
				ObjEffect_SetScale( objs[ j ], scale, scale );
				ObjEffect_SetAngle( objs[ j ], 0, 0, 4 * t * [ 1, -1 ][ j ] );//赤が時計周り
				ascent ( let i in 0..4 ) {
					ObjEffect_SetVertexColor( objs[ j ], i, alpha, 255, 255, 255 );
				}
				ObjSpell_SetIntersecrionCircle( objs[ j ],
												Obj_GetX( objs[ j ] ), Obj_GetY( objs[ j ] ),
												wid * 1.4 / 2 * scale, dmg, true );
			}
			ascent ( let i in 0..length( blurObjsRed ) ) {
				let aheadObj;
				if ( i == 0 ) {
					aheadObj = objs[ 0 ];
				}
				else {
					aheadObj = blurObjsRed[ i - 1 ];
				}
				let lmax = 32;
				let l = GetGapLength( Obj_GetX( blurObjsRed[ i ] ), Obj_GetY( blurObjsRed[ i ] ),
									  Obj_GetX( aheadObj ), Obj_GetY( aheadObj ) );
				let v = Obj_GetSpeed( aheadObj ) * Min( l, lmax ) / lmax;
				let nextx = Obj_GetX( aheadObj )
					+ Obj_GetSpeed( aheadObj ) * cos( Obj_GetAngle( aheadObj ) );
				let nexty = Obj_GetY( aheadObj )
					+ Obj_GetSpeed( aheadObj ) * sin( Obj_GetAngle( aheadObj ) );
				let angle = GetGapAngle( Obj_GetX( blurObjsRed[ i ] ), Obj_GetY( blurObjsRed[ i ] ),
										 nextx, nexty );
				let blurAlpha = [ alpha - 20 * ( i + 1 ), 0 ][ l < 1 ];
				Obj_SetSpeed( blurObjsRed[ i ], v );
				Obj_SetSpeed( blurObjsBlue[ i ], v );
				Obj_SetAngle( blurObjsRed[ i ], angle );
				Obj_SetAngle( blurObjsBlue[ i ], angle );
				ObjEffect_SetScale( blurObjsRed[ i ], scale, scale );
				ObjEffect_SetScale( blurObjsBlue[ i ], scale, scale );
				ObjEffect_SetAngle( blurObjsRed[ i ], 0, 0, 4 * ( t + 20 * ( i + 1 ) ) );
				ObjEffect_SetAngle( blurObjsBlue[ i ], 0, 0, -4 * ( t + 20 * ( i + 1 ) ) );
				ascent ( let j in 0..4 ) {
					ObjEffect_SetVertexColor( blurObjsRed[ i ], j,
											  Max( 0, blurAlpha ),
											  255, 255, 255 );
					ObjEffect_SetVertexColor( blurObjsBlue[ i ], j,
											  Max( 0, blurAlpha ),
											  255, 255, 255 );
				}

			}
			yield;
		}
		ascent ( let j in 0..2 ) {
			Obj_Delete( objs[ j ] );
		}
	}

}

//自機咲夜ボム用時止め敵スクリプト
/*script_enemy TimeStoper
{
	let time = GetArgument();
	let count = 0;
	
	@Initialize {
		SetLife( 10000 );
		SetInvincibility( 600 );
		SetDamageRate( 0, 0 );
		SetCollisionA( GetX(), GetY(), 0 );
		SetCollisionB( GetX(), GetY(), 0 );
		TimeStop( time, 0, 1, 0 );
	}

	@MainLoop {
		if ( count >= time ) {
			VanishEnemy();
		}
		SetCollisionA( GetX(), GetY(), 0 );
		SetCollisionB( GetX(), GetY(), 0 );
		count++;
		yield;
	}

	@DrawLoop {}
	@Finalize {}
}
*/