#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["REI"]
#Title["Reimu Hakurei"]
#Image["./Face.png"]
#ReplayName["Protagonist Team"]

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

task TMain
{
	PlayerPath1 = GetCurrentScriptDirectory ~ "Sprite0.png";
	PlayerPath2 = GetCurrentScriptDirectory ~ "Sprite1.png";
	PlayerSpeed = [4,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 pow = 0;

	let IsShooting = false;

	let count = 4;
	let counter = 0;

	let countO = [4,8];	

	TOption;
	
	loop
		{	
		pow = min(Pmax,GetPlayerPower);
		if(IsKeyState([VK_SHOT],[KEY_PUSH,KEY_HOLD]))
			{
			if(IsPlayerDown == false && IsPermitPlayerShot == true && ShotPermit == true)
				{
				if(counter%count == 0)
					{
					PlaySound("se_PlayerShot1",100);
					ascent(i in 0..2)
						{
						TSpam(GetPlayerX+(i*2-1)*8,GetPlayerY+12,270,1*0.5);
						}
					}
				IsShooting = true;
				}
			else
				{
				IsShooting = false;
				}
			counter+=1;
			}
		else if(counter%count != 0)
			{
			counter+=1;
			IsShooting = false;
			}
		else
			{
			IsShooting = false;
			}
		yield;
		}

	task TSpam(x,y,angle,r)
		{
		if(IsPermitPlayerShot == false){return;}
		let g = MAIN_U;
		let shot = CreatePlayerShotA1(x,y,22,angle,120*count*r*sdr/60,1,g);
		ObjRender_SetAlpha(shot,175);
		ObjRender_SetScaleXYZ(shot,1,1,0);
		ObjRender_SetAngleZ(shot,-90);

		Obj_SetRenderPriority(shot,0.32);
		}

	task TOption
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj,PlayerPath1);
		Obj_SetRenderPriority(obj,0.35);	
		ObjRender_SetAlpha(obj,255);
		let p = 0;
		let pp = -1;
		let d = 0;
		let Fcount = 3;
		let Fc = Fcount;
		let dF = 0;
		let xP = GetPlayerX;
		let yP = GetPlayerY;
		let angleZ = 0;
		let x = [];
		let y = [];
		let a = 0;
		let c = 0;
		let DownState = false;
		
		let angleO = 0;
		let IsLaserActive = false;

		loop
			{
			p = min(Pmax,floor(pow));
			if(GetPlayerState == STATE_DOWN)
				{
				DownState = true;
				}
			if(DownState == true)
				{
				xP = GetPlayerX;
				yP = GetPlayerY;
				d = 0;
				IsLaserActive = false;
				}
			else
				{
				if(p != pp)
					{
					d = 0;
					}
				if(d < 1)
					{
					d += (1-d)/6;
					}
				let n = 3.5;
				if(IsFocus == true)
					{
					dF += (1-dF)/6;
					n = 1;
					a+=1.25;
					}
				else
					{
					dF += (0-dF)/6;
					a+=1;
					}
				xP+=(GetPlayerX-xP)/n;
				yP+=(GetPlayerY-yP)/n;
				}
			if(GetPlayerState != STATE_DOWN)
				{
				DownState = false;
				}
			ObjSpriteList2D_ClearVertexCount(obj);
			x = [];
			y = [];	
			if(GetPlayerState != STATE_DOWN)
				{
				a+=1;
				ascent(i in 0..p)
					{
					let iA = (i-p*0.5+0.5);
					let xA = 0;
					let yA = 0;
					let n = absolute(iA)*2;
					let dir = 0;
					if(iA != 0)
						{
						dir = n*0.5/iA;
						}
					local
						{
						let xm = dir*(5+n*10)*1.5;
						let xM = dir*(27)*1.5;
						let ym = (115-n*15);
						let yM = (95-n*20);
						xA += (1-dF)*xm; //Unfocus x position
						xA += (dF)*(xM); //Focus x position
						yA += (1-dF)*ym; //Unfocus y position
						yA += (dF)*(yM); //Focus y position
						}
					x = x ~ [xP+d*xA*0.5];
					y = y ~ [yP+d*yA*0.5-18];
					ascent(iA in 0..2)
						{
						let s = scaleP;
						if(iA == 0)
							{
							s = 1-s;
							}
						ObjSpriteList2D_SetSourceRect(obj,80-iA*16,144,96-iA*16,160);
						ObjRender_SetPosition(obj,x[i]+0.5,y[i],0);	
						ObjRender_SetAngleZ(obj,-((i%2)*2-1)*(iA*2-1)*a+30);	
						ObjRender_SetScaleXYZ(obj,(1.5-s*0.5)*1.125,(1.5-s*0.5)*1.125,0);
						ObjRender_SetAlpha(obj,s*255);
						ObjSpriteList2D_SetDestCenter(obj);
						ObjSpriteList2D_AddVertex(obj);
						}
					if(IsShooting == true)
						{
						if(IsFocus == false)
							{
							if(c%countO[0] == n*floor(countO[0]/4))
								{
								TFamShotU(x[i],y[i],270+iA*0.5*60,1);
								}
							}
						else
							{
							if(c%countO[1] == n*floor(countO[1]/4))
								{
								ascent(nA in 0..2)
									{	
									let a = 270+iA*(1.75-nA*6.25);
									if(iA == 0)
										{
										a = 270+(nA*2-1)*2.5;
										}
									TFamShotU(x[i],y[i],a,0.5);
									TFamShotF(x[i]+3,y[i],a,1);
									TFamShotF(x[i]-3,y[i],a,1);
									}
								}
							}
						}
					}
				}
			if((IsShooting == false && c%(countO[0]*countO[1]) != 0) || IsShooting == true)
				{
				c+=1;
				}	

			pp = p;
			yield;
			}
		}

	task TFamShotU(x,y,angle,r)
		{
		if(IsPermitPlayerShot == false){return;}
		
		let shot = CreatePlayerShotA1(x,y,7.5,angle,80*countO[0]*r*sdr/60,1,SUB_U);
		ObjMove_SetAcceleration(shot,1/40);
		ObjMove_SetMaxSpeed(shot,100);
		ObjRender_SetAlpha(shot,175);
		ObjRender_SetScaleXYZ(shot,1,1,0);
		ObjRender_SetAngleZ(shot,-90);

		Obj_SetRenderPriority(shot,0.32);

		let t = -1;
		let enm = FHomingEnemy(x,y,GetStgFrameWidth*0.5,100,false);
		while(!Obj_IsDeleted(shot))
			{
			t+=1;
			x = ObjMove_GetX(shot);
			y = ObjMove_GetY(shot);
			if(t < 45)
				{
				if(Obj_IsDeleted(enm))
					{
					enm = FHomingEnemy(x,y,GetStgFrameWidth*0.5,100,false);
					}
				let a = FAngleToHomingEnemy(enm,x,y,angle,15);
				ObjMove_SetAngle(shot,a);
				}
			angle = ObjMove_GetAngle(shot);
			yield;
			}
		}

	task TFamShotF(x,y,angle,r)
		{
		if(IsPermitPlayerShot == false){return;}
		let shot = CreatePlayerShotA1(x,y,15,angle,20*countO[1]*r*sdr/60,1,SUB_F);
		ObjRender_SetAlpha(shot,135);
		ObjRender_SetScaleXYZ(shot,0.5,0.5,0);
		ObjRender_SetAngleZ(shot,-90);

		Obj_SetRenderPriority(shot,0.32);
		}
}



function FSpell
{
	SpellSwitch = true;
	ShotPermit = false;
	IsGrazeEnable = false;
	NotifyEventAll(EV_PLAYER_SHOOTDOWN,[]);

	TSpellShadow;
	let DespairTime = 240;
	TMagicCircle(5*5*10+30+75,120);
	let hit = HitBox;
	let graze = GrazeBox;
	
	let MainSpeed = PlayerSpeed;
	PlayerSpeed = [MainSpeed[1],MainSpeed[1]];

	let time = [60,15,DespairTime-90,15];
	let switch = false;

	SetPlayerInvincibilityFrame(time[0]+time[1]+time[2]+30);	
	
	TMain;

	loop(time[0]+time[1]+time[2]+30){yield;}
	PlaySound("se_Die5",120);
	SpellSwitch = true;
	ShotPermit = true;
	PlayerSpeed = MainSpeed;	
	IsGrazeEnable = true;
	SpellSwitch = false;
	ShotPermit = true;

	task TMain
		{
		let xP = GetPlayerX;
		let yP = GetPlayerY;

		let spell = ObjSpell_Create;
		ObjSpell_Regist(spell);
		ObjSpell_SetEraseShot(spell,true);
		ObjSpell_SetDamage(spell,90*bdr/60);

		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Despair.png");
		Obj_SetRenderPriority(obj, 0.35);
		ObjSpriteList2D_SetSourceRect(obj,0,0,128,128);
		ObjSpriteList2D_SetDestRect(obj,0,0,1,1);
		ObjRender_SetPosition(obj,xP,yP,0);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
		let timeA = time[0]-10;
		descent(t in 0..timeA)
			{
			ObjSpriteList2D_ClearVertexCount(obj);
			let sc = t*GetStgFrameWidth*2/timeA;
			ObjRender_SetScaleXYZ(obj,sc,sc,0);
			ObjRender_SetAlpha(obj,(1-t/timeA)*255);
			ObjRender_SetColorHSV(obj,0,0,0);
			ObjSpriteList2D_AddVertex(obj);
			yield;
			}
		ObjSpriteList2D_ClearVertexCount(obj);
		loop(10){yield;}
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		timeA = DespairTime-time[0]-10;
		ascent(t in 0..timeA)
			{
			ObjSpriteList2D_ClearVertexCount(obj);
			let sc = min(t,time[1])*180/time[1];
			let alpha = 1;
			if(t >= timeA-time[3])
				{
				sc = 180*(2-(timeA-t)/time[3]);
				alpha = (timeA-t)/time[3];
				}
			descent(n in 0..2)
				{
				let nA = 1;
				let tA = 0;
				if(t >= time[1] && t <= timeA-time[3] && (t-time[1])%75 >=45)
					{
					nA = 2;
					tA = (t-time[1])%75;
					}
				descent(i in 0..nA)
					{
					if(i == 0)
						{
						ObjRender_SetScaleXYZ(obj,sc,sc,0);	
						ObjRender_SetAlpha(obj,(n*155+100)*alpha);
						}
					else
						{
						let tB = (tA-45)/30;
						ObjRender_SetScaleXYZ(obj,sc*(1+tB),sc*(1+tB),0);	
						ObjRender_SetAlpha(obj,(n*155+100)*(1-tB)*alpha);
						}
					ObjRender_SetAngleZ(obj,(n*2-1)*t*(3+n*0.5));
					ObjRender_SetColorHSV(obj,n*240,(1-n)*255,255);	
					ObjSpriteList2D_AddVertex(obj);
					}
				}
			if(t < timeA-time[3])
				{
				ObjSpell_SetIntersectionCircle(spell,xP,yP,sc*0.45);
				if(t >= time[1]+30 && (t-time[1]-30)%1 == 0)
					{
					ascent(i in 0..4)
						{
						ascent(dir in 0..2)
							{
							dir = dir*2-1;
							let a = 30+dir*(i*90+t*8);
							let shot = CreatePlayerShotA1(xP+sc*0.45*cos(a),yP+sc*0.45*sin(a),4,a-dir*60,450*1*sdr/60,INF,FIRE[SHOT_RED]);
							ObjMove_SetAcceleration(shot,1/6);
							ObjMove_SetMaxSpeed(shot,100);
							ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
							ObjShot_SetEraseShot(shot,true);
							ObjShot_SetSpellFactor(shot,true);
							Obj_SetRenderPriority(obj,0.56);
							}
						}
					}
				}
			yield;
			}
		Obj_Delete(spell);
		Obj_Delete(obj);
		}
	
}

function FFlash
{
	
	
}
