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

script_spell Demarcation
{
	//低速移動時は、敵の座標に直に出現
	//高速移動時は、自機の周囲に出現
	let bSlowMove = GetArgument();//低速移動フラグ
	let current = GetCurrentScriptDirectory();
	let imgEffect = current~"players\Rumia\img\spell_rumia.png";

	@Initialize
	{
		LoadGraphic(imgEffect);
		SetPlayerInvincibility(300);//無敵時間を設定
		run();
	}
	@MainLoop
	{
		yield;
	}
	@Finalize
	{
		DeleteGraphic(imgEffect);
	}
	task run()
	{
		loop(48){yield;}//組み込みのカットインが終わるまで待機
		effect(0);
		effect(1);
		loop(220){;yield;}
		End();
	}

	task effect(num)
	{	//エフェクト用タスク
		let tv=0;
		let angle=0;
		let alpha=0;	
		task alphaState
		{	//α値を変更するためのタスク
			alpha=0;
			loop(256/3){alpha+=3; yield;}
			loop(90){yield;}
			loop{alpha-=8; yield;}
		}
		task lineEffect
		{
			loop(120){concentrationLine();loop(1){yield;}}
		}
		alphaState;
		if(bSlowMove==true){lineEffect;}
		
		let obj = Obj_Create(OBJ_SPELL);//スペルオブジェクトを作成
		ObjEffect_SetTexture(obj, imgEffect);//テクスチャを設定
		ObjEffect_SetRenderState(obj, ADD);//描画方法を設定
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);//プリミティブタイプを設定
		ObjEffect_CreateVertex(obj, 10);//頂点を10個つくる
		
		ascent(i in 0..5)
		{
			let angle = -45+i*90;
			let x;
			let y;
			if(bSlowMove)
			{
				//低速移動時は範囲が狭い
				x = 192*cos(angle);
				y = 192*sin(angle);
			}
			else
			{
				//高速移動時は広範囲
				x = 512*cos(angle);
				y = 512*sin(angle);
			}
			ObjEffect_SetVertexXY(obj, i*2, x, y);//頂点の座標を設定
			ObjEffect_SetVertexXY(obj, i*2+1, 0, 0);
			ObjEffect_SetVertexColor(obj, i*2, 0, 255, 255, 255);//頂点カラーを設定
		}
		
		while(!Obj_BeDeleted(obj))
		{
			tv-=1;
			if(num == 0){angle+=1;}
			else if(num == 1){angle-=1;}
			ObjEffect_SetAngle(obj, 0, 0, angle);//描画角度を設定
			if(bSlowMove)
			{	//低速移動時
				Obj_SetPosition(obj, GetEnemyX, GetEnemyY);
				SetIntersectionCircle(GetEnemyX, GetEnemyY, 81, 5, true);//当たり判定を登録				
			}
			else 
			{	//高速移動時
				Obj_SetPosition(obj, GetPlayerX, GetPlayerY);//座標を設定
				SetIntersectionCircle(GetPlayerX, GetPlayerY, 256, 2, true);//当たり判定を登録
			}
			ascent(i in 0..5)
			{
				ObjEffect_SetVertexUV(obj, i*2,   16*i,tv);//テクスチャの座標を設定
				ObjEffect_SetVertexUV(obj, i*2+1 ,32,  64+tv);
				
				ObjEffect_SetVertexUV(obj, i*2,   16*i, tv);	
				ObjEffect_SetVertexUV(obj, i*2+1, 32,   64+tv);
				
				ObjEffect_SetVertexColor(obj, i*2+1, alpha, 255, 255, 255);		
			}
			yield;
		}
		
	}
	
	task concentrationLine()
	{	//線が集まる感じのエフェクト
		let angle=rand(0, 360);
		let alpha=0;	
		let scale=3;
		
		let obj = Obj_Create(OBJ_SPELL);//スペルオブジェクトを作成
		ObjEffect_SetTexture(obj, imgEffect);//テクスチャを設定
		ObjEffect_SetRenderState(obj, ADD);//描画方法を設定
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);//プリミティブタイプを設定
		ObjEffect_CreateVertex(obj, 3);//頂点を3個つくる
		ObjEffect_SetAngle(obj, 0, 0, angle);//描画角度を設定
		
		//頂点座標を設定
		ObjEffect_SetVertexXY(obj, 0,  0,  10);
		ObjEffect_SetVertexXY(obj, 1, -10, 50);
		ObjEffect_SetVertexXY(obj, 2, 10,  50);
		
		//頂点のUV値を設定
		ObjEffect_SetVertexXY(obj, 0, 10,  10);
		ObjEffect_SetVertexXY(obj, 1,  0,  50);
		ObjEffect_SetVertexXY(obj, 2, 20,  50);
		
		//色設定
		ObjEffect_SetVertexColor(obj, 0, alpha, 255, 0, 255);
		ObjEffect_SetVertexColor(obj, 1, 0, 255, 0, 255);
		ObjEffect_SetVertexColor(obj, 2, 0, 255, 0, 255);
		
		loop(60)
		{
			scale-=3/60;
			alpha+=5;
			ObjEffect_SetVertexColor(obj, 0, alpha, 255, 0, 255);
			ObjEffect_SetScale(obj, scale, scale*3);	
			Obj_SetPosition(obj, GetEnemyX, GetEnemyY);	
			yield;	
		}
		Obj_Delete(obj);
	}


}
