#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["REIS"]
#Title["Reisen Udongein Inaba"]
#Image["./Face.png"]
#ReplayName["Reisen"]

#include "./../main.txt"
#include "./Shot_Const.txt"

task TMain
{
	PlayerPath1 = GetCurrentScriptDirectory ~ "Sprite0.png";
	PlayerPath2 = GetCurrentScriptDirectory ~ "Sprite0.png";
	PlayerSpeed = [3.75,2];

	textureO = textureO ~ [GetCurrentScriptDirectory ~"../../system/img/AllStarShot.png"];
	textureO = textureO ~ [GetCurrentScriptDirectory ~"img/Spell.png"];
	ascent(i in 0..length(textureO))
		{
		LoadTexture(textureO[i]);
		}
}

task TShot
{	
	let IsShooting = false;
	let optionCount = 1;
	let pow = 0;

	let count = 5;
	let counter = 0;
	let angle;
	let soundS = false;

	ascent(i in 0..4)
		{		
		TOption(i);
		}
	
	loop
		{
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{
			IsFocus = true;
			}
		else
			{
			IsFocus = false;
			}
		if((GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD) && !IsPlayerDown)
			{
			if(GetPlayerState == STATE_NORMAL && IsPermitPlayerShot == true && ShotPermit == true)
				{
				if(counter%5 == 0)
					{
					PlaySound("se_PlayerShot1",120);
					ascent(i in 0..2)
						{
						i = i*2-1;
						TSpam(GetPlayerX+i*7,GetPlayerY+24);
						}
					}
				IsShooting = true;
				}
			else
				{
				IsShooting = false;
				}
			counter+=1;
			}
		else if(counter%5 != 0)
			{
			counter+=1;
			IsShooting = false;
			}
		else
			{
			IsShooting = false;
			}
		yield;
		}

	task TSpam(x,y)
		{
		if(IsPermitPlayerShot == false){return;}
		let angle = 270;
		let shotScale = [1,1];	
		let shotColor = [0,0,255];
		
		let shot = CreatePlayerShotA1(x,y,20,angle,sdr*280/5,1,REIS);
		ObjMove_SetAcceleration(shot,0.1);
		ObjMove_SetMaxSpeed(shot,100);
		ObjRender_SetAngleZ(shot,-90);

		let shotPrior = 0.46;
		let alpha = 255;	

		Obj_SetRenderPriority(shot,shotPrior);
		ObjRender_SetAlpha(shot,alpha);
			
		return;
		if(IsPermitPlayerShot == false || y < -64 || y > GetStgFrameWidth+64){return;}	
		while(!Obj_IsDeleted(shot))
			{	
			x = ObjMove_GetX(shot);	
			y = ObjMove_GetY(shot);
			angle = ObjMove_GetAngle(shot);
			yield;
			}
		if(IsPermitPlayerShot == false || y < -64 || y > GetStgFrameHeight+64  || y < -64 || y > GetStgFrameWidth+64){return;}	
		let obj = CreatePlayerShotA1(x,y,rand(1,1.5),angle+rand(-1,1)*30,0,10^10,DOT_GRAY);
		Obj_SetRenderPriority(obj,shotPrior);
		let alphaM = 155;
		alpha = alphaM;	
		let scl = 2;
		loop(15)
			{
			alpha-=alphaM/15;
			scl-=2/15;
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetScaleXYZ(obj,scl,scl,0);
			yield;
			}
		Obj_Delete(obj);
		}

	task TOption(num)
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Sprite0.png");
		Obj_SetRenderPriority(obj,0.31);	
		ObjRender_SetScaleXYZ(obj,1.125,1.125,0);
		ObjRender_SetAlpha(obj,125);
		let counter = 0;	
		let xP = GetPlayerX;
		let yP = GetPlayerY;
		let x = xP;
		let y = yP;
		let xA = 0;
		let yA = 0;
		let g = 0;

		let foc = false;
		loop
			{
			let xM = 50*cos(num*90+270);
			let yM = 50*sin(num*90+270);
			if(IsPlayerDown == false)
				{
				foc = IsFocus;
				}		
			if(foc)
				{	
				alternative(num)	
					case(0)
						{	
						xM = -27.5;
						yM = 0;
						} 
					case(2)
						{	
						xM = 27.5;
						yM = 0;
						} 
					case(1)
						{	
						xM = 12.5;
						yM = -22.5;
						} 
					case(3)
						{	
						xM = -12.5;
						yM = -22.5;
						} 
				}
			xA += (xM-xA)/3;
			yA += (yM-yA)/3;
			xP += (GetPlayerX-xP)/4;
			yP += (GetPlayerY-yP)/4;
			x = xP+xA;	
			y = yP+yA;
			ObjRender_SetPosition(obj,x,y,0);

			g = (g+1/6)%4;
			ObjSprite2D_SetSourceRect(obj,128.5+floor(g)*16,148,128+floor(g+1)*16,158);
			ObjSprite2D_SetDestCenter(obj);

			if(IsShooting == true)
				{
				if(counter%count == 0)
					{
					counter+=1;
					if(foc)
						{
						TSpam2(x,y,0);
						}
					else
						{	
						loop(2)
							{
							TSpam2(x,y,1);
							}
						}
					
					}
				else if(counter%count != 0)
					{
					counter+=1;
					}
				}
			else if(counter%(60*count) != 0)
				{
				counter+=1;
				}
			yield;
			}
		Obj_Delete(obj);

		task TSpam2(x,y,type)
			{
			if(IsPermitPlayerShot == false){return;}
			let angle = 270;
			let prc = 1000;
			let dmg = 41.9;
			let g = REIS_F;
			let color = 0;
			if(type == 1)
				{
				angle = 270+rand(-1,1)*22.5;
				prc = 1;
				dmg = 51.4*0.5;
				g = REIS_U;
				color = 105;
				}
			let shot = CreatePlayerShotA1(x,y,0,angle,sdr*dmg/count,prc,g);	
			ObjMove_AddPatternA2(shot,1,20,NO_CHANGE,0.1,0,100);
			ObjRender_SetAngleZ(shot,-90);
			ObjRender_SetColorHSV(shot,300,color,255);
	
			let shotPrior = 0.46;
			let alpha = 125;

			Obj_SetRenderPriority(shot,shotPrior);
			ObjRender_SetAlpha(shot,alpha);
			
			return;
			if(type == 0){return;}
			while(!Obj_IsDeleted(shot))
				{	
				x = ObjMove_GetX(shot);	
				y = ObjMove_GetY(shot);
				angle = ObjMove_GetAngle(shot);
				yield;
				}
			if(IsPermitPlayerShot == false || y < -64 || y > GetStgFrameHeight+64  || y < -64 || y > GetStgFrameWidth+64){return;}	
			let obj = CreatePlayerShotA1(x,y,rand(1,1.5),angle+rand(-1,1)*30,0,10^10,DOT_RED);
			Obj_SetRenderPriority(obj,shotPrior);
			let alphaM = 155;
			alpha = alpha;	
			let scl = 2;
			loop(15)
				{
				alpha-=alphaM/15;
				scl-=2/15;
				ObjRender_SetAlpha(obj,alpha);
				ObjRender_SetScaleXYZ(obj,scl,scl,0);
				yield;
				}
			Obj_Delete(obj);
			}
		}
}

function FSpell
{
	SpellSwitch = true;	

	TSpellShadow;
	TMagicCircle(90,120);
	let hit = HitBox;
	let graze = GrazeBox;
	
	ReisSpellCount = 3;
	ascent(i in 0..ReisSpellCount)
		{
		TSpam(i);
		}

	let array = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,200,TARGET_ENEMY);
	ascent(i in 0..length(array))
		{
		if(!Obj_IsDeleted(array[i]) && ObjShot_IsSpellResist(array[i]) == false)
			{
			TDeleteShot(array[i]);
			}
		}
	TBox;
	loop(60)
		{
		yield;
		}
	SpellSwitch = false;
	while(ReisSpellCount > 0){yield;}

	SpellSwitch = false;
	loop(15){yield;}
	ShotPermit = true;

	task TSpam(num)
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Sprite0.png");
		ObjSprite2D_SetSourceRect(obj,64*(3-num),192,-1+64*(4-num),255);
		Obj_SetRenderPriority(obj,0.3);
		let angle = 0;
		let angleA = (3-num)*((num%2)*2-1);
		let scale = 0;
		while(ReisSpellCount > num)
			{
			if(scale < 1)
				{
				scale+=1/10;
				}
			angle+=angleA;
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			ObjRender_SetAlpha(obj,scale*255);
			ObjRender_SetAngleZ(obj,angle);
			ObjSprite2D_SetDestCenter(obj);
			yield;
			}
		let array = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,200,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			if(!Obj_IsDeleted(array[i]) && ObjShot_IsSpellResist(array[i]) == false)
				{
				TDeleteShot(array[i]);
				}
			}
		TPlayerBlink(30,120);
		ShotPermit = true;
		IsPlayerHit = true;
		descent(t in 0..15)
			{
			scale += (2-scale)/15;
			angle+=angleA;
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			ObjRender_SetAngleZ(obj,angle);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetAlpha(obj,t*255/14);
			yield;
			}
		ReisSwitch = false;
		Obj_Delete(obj);
		loop(15){yield;}
		IsPlayerHit = false;	
		Obj_SetValue(objPlayer,"DAMAGE",0);	
		}
	task TDeleteShot(shot)
		{
		ObjShot_ScaleDelete(shot,11);
		loop(10){yield;}
		if(Obj_IsDeleted(shot)){return;}
		ObjShot_ToItem(shot);
		}
	task TBox
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Sprite0.png");
		Obj_SetRenderPriority(obj,0.58);
		let num = 0;
		let alphaM = 0;
		let alpha = 0;
		while(ReisSpellCount > 0)
			{
			if(IsFocus == true)
				{
				alphaM = 255;
				}
			else
				{
				alphaM = 155;
				}
			alpha += (alphaM-alpha)/15;
			num = ReisSpellCount;
			HitBox = hit*(1+num*3/3);
			GrazeBox = graze*(1+num*3/3);
			ObjSprite2D_SetSourceRect(obj,192+16*(3-num),144,192+16*(4-num),160);
			ObjRender_SetPosition(obj,GetPlayerX+0.5,GetPlayerY+0.5,0);
			ObjRender_SetAlpha(obj,alpha);
			ObjSprite2D_SetDestCenter(obj);
			yield;
			}
		HitBox = hit;
		Obj_Delete(obj);
		}
}

function FFlash
{
}