#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["KOI"]
#Title["Koishi Komeiji"]
#Image["./Face.png"]
#ReplayName["Koishi"]

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

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

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

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

	let count = [60,4,4];
	let counter = 0;
	let angle;
	let soundS = false;

	ascent(i in 0..2)
		{		
		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 c = 0;
		
		let shot = CreatePlayerShotA1(x,y,20,angle,sdr*120*5*0.5/60,1,KOI_RED);
		if(IsFocus == true)
			{
			ObjShot_SetGraphic(shot,KOI_BLUE);
			c = 240;
			}
		else
			{
			c = 330;
			}
		ObjMove_SetAcceleration(shot,0.1);
		ObjMove_SetMaxSpeed(shot,100);
		ObjRender_SetAngleZ(shot,-90);

		let shotPrior = 0.36;
		let alpha = 155;	

		Obj_SetRenderPriority(shot,shotPrior);
		ObjRender_SetAlpha(shot,alpha);
		let color = Obj_GetValueD(objPlayer,"COLORHSV",255);
		ObjRender_SetColorHSV(shot,c,65,color);
			
		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 dir = num*2-1;
		let scl = 9;

		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,155);
		ObjRender_SetColorHSV(obj,0,0,175);
		ObjRender_SetScaleXYZ(obj,1,1,0);
	
		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 gA = 1;
		let foc = false;
		loop
			{
			let xM = 50*dir;
			let yM = 0;
			let gM = 1;
			if(IsPlayerDown == false)
				{
				foc = IsFocus;
				}		
			if(foc)
				{	
				xM = dir*25;
				}
			else if(IsShooting == true)
				{
				gM = 20.5;
				}
			xA += (xM-xA)/3;
			yA += (yM-yA)/3;
			xP += (GetPlayerX-xP)/2;
			yP += (GetPlayerY-yP)/2;
			gA += (gM-gA)/30;
			x = xP+xA;	
			y = yP+yA;
			ObjRender_SetPosition(obj,x,y,0);

			g += gA;
			ObjSprite2D_SetSourceRect(obj,38,200,134,256);
			ObjRender_SetAngleZ(obj,g*2);
			ObjSprite2D_SetDestRect(obj,-(100/64)*scl,-scl,(100/64)*scl,scl);
			let color = Obj_GetValueD(objPlayer,"COLORHSV",255);
			if(color != 255)
				{
				ObjRender_SetColorHSV(obj,0,0,color);
				}
			if(IsShooting == true)
				{
				if(foc)
					{
					if(counter%count[0] == 0)
						{
						counter+=1;
						TSpam2(x,y,0);
						}
					else if(counter%count[0] != 0)
						{
						counter+=1;
						}
					
					}
				else
					{
					if(counter%count[1] == 0)
						{
						counter+=1;
						TSpam2(x,y,1);
						}
					else if(counter%count[1] != 0)
						{
						counter+=1;
						}
					
					}
				}
			else if(counter%(count[0]*count[1]) != 0)
				{
				counter+=1;
				}
			yield;
			}
		Obj_Delete(obj);

		task TSpam2(x,y,type)
			{
			if(IsPermitPlayerShot == false){return;}
			let angle = 270;
			let prc = 1000;
			let dmg = 0;
			let g = HEART[SHOT_PURPLE];
			let color = [330,65];
			let alpha = 65;
			if(type == 1)
				{
				prc = 1;
				dmg = 50;
				g = HEART[SHOT_BLUE];
				color = [240,65];
				let alpha = 105;
				}
			let shot = CreatePlayerShotA1(x,y,1+type*24,angle,sdr*dmg*count[type]/60,prc,g);	
			ObjMove_SetAcceleration(shot,1/30);
			ObjMove_SetMaxSpeed(shot,100);
			ObjRender_SetScaleXYZ(shot,1,1.25,1);
	
			let shotPrior = 0.36;

			Obj_SetRenderPriority(shot,shotPrior);
			ObjRender_SetAlpha(shot,alpha);
			
			let t = -1;
			let enm = FHomingEnemy(x,y,GetStgFrameWidth,GetStgFrameHeight,false);
			if(enm != ID_INVALID)
				{
				ObjMove_SetAngle(shot,atan2(ObjMove_GetY(enm)-y,ObjMove_GetX(enm)-x));
				}
			angle = ObjMove_GetAngle(shot);
			while(!Obj_IsDeleted(shot))
				{
				t+=1;	
				x = ObjMove_GetX(shot);	
				y = ObjMove_GetY(shot);
				if((type == 0 && t < 135))
					{
					if(Obj_IsDeleted(enm))
						{
						enm=FHomingEnemy(x,y,GetStgFrameWidth,GetStgFrameHeight,false);
						}
					let a = FAngleToHomingEnemy(enm,x,y,angle,1);
					ObjMove_SetAngle(shot,a);
					}
				angle = ObjMove_GetAngle(shot);
				if(type == 0 && t%count[2] == 0)
					{
					TSpam3(x,y);
					}
				let c = Obj_GetValueD(objPlayer,"COLORHSV",255);
				ObjRender_SetColorHSV(shot,color[0],color[1],c);
				yield;
				}	
			return;
			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);
			}
		}
	task TSpam3(x,y)
		{
		if(IsPermitPlayerShot == false){return;}
		let angle = rand(0,360);
		let g = [SHOT_RED,SHOT_BLUE,SHOT_PURPLE][trunc(rand(0,3))];
		let shot = CreatePlayerShotA1(x,y,1,angle,sdr*9*count[2]/60,1000,trunc(rand(ROSE_RED,ROSE_PURPLE+1)));

		let shotPrior = 0.35;
		let alpha = 105;	

		Obj_SetRenderPriority(shot,shotPrior);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		ObjRender_SetScaleXYZ(shot,1,0.67,1);	
		ObjRender_SetAngleZ(shot,angle);
			
		descent(t in 0..45)
			{
			ObjRender_SetAlpha(shot,t*alpha/45);
			let c = Obj_GetValueD(objPlayer,"COLORHSV",255);
			if(c != 255)
				{
				ObjRender_SetBlendType(shot,BLEND_ALPHA);
				ObjRender_SetColorHSV(shot,0,0,c);
				}
			yield;
			if(Obj_IsDeleted(shot)){return;}
			}
		Obj_Delete(shot);
		}
}

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

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

	let spread = 3;
	let IsSound = false;
	TSound;
	
	SetPlayerInvincibilityFrame(5*5*10+120);	
	let c = 25;	
	ascent(t in 0..5)
		{
		ascent(i0 in 0..5)
			{
			ascent(i1 in 0..spread+1)
				{
				TSpam(GetStgFrameWidth+8,(i1+i0*0.15+t*0.05)*GetStgFrameHeight/spread,180,0);
				TSpam((i1+i0*0.15+t*0.05)*GetStgFrameWidth/spread,-8,90,1);
				}
			PlaySound("se_Shot4",85);
			loop(10)
				{
				c-=1;
				ObjRender_SetAlpha(objPlayer,c*255/25);
				yield;	
				}
			}
		}
	
	loop(30){yield;}
	PlaySound("se_Die5",120);
	SpellSwitch = true;
	ShotPermit = true;
	PlayerSpeed = MainSpeed;	
	IsGrazeEnable = true;
	c=0;
	ascent(i in 0..20)
		{
		ObjRender_SetAlpha(GetPlayerObjectID,(i+1)*255/20);
		yield;
		}
	SpellSwitch = false;
	ShotPermit = true;
	task TSound
		{
		while(SpellSwitch == true)
			{
			if(IsSound == true)
				{
				PlaySound("se_Shot5",70);
				IsSound = false;
				}
			yield;
			}
		}
	task TSpam(x,y,angle,graph)
		{
		let spell = ObjSpell_Create;
		ObjSpell_Regist(spell);
		ObjSpell_SetEraseShot(spell,true);
		ObjSpell_SetDamage(spell,135*bdr/60);

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(obj,0.5);
		let scale = 8;
		let scaleM = 64;	
		let damage = false;
		let alpha = 255;
		while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(spell) && x > -9 && x < GetStgFrameWidth+9 && y > -9 && y < GetStgFrameHeight+9)
			{
			let color = Obj_GetValueD(objPlayer,"COLORHSV",255);
			if(GetDistance(GetPlayerX,GetPlayerY,x,y) < 64 && SpellSwitch == true)
				{
				if(scale < scaleM)
					{
					scale+=scaleM/7;
					}
				else
					{
					IsSound = true;
					scale=scaleM;
					}
				damage = true;
				ObjPrim_SetTexture(obj,textureO[graph+1]);
				ObjSprite2D_SetSourceRect(obj,0,0,38,38);
				ObjRender_SetColorHSV(obj,graph,0,255*color/255);
				}
			else
				{
				if(scale > 8)
					{
					scale-=(scaleM-8)/7;
					}
				else
					{
					scale = 8;
					ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "../../system/img/WhiteCircleAlpha.png");
					ObjSprite2D_SetSourceRect(obj,0,0,64,64);
					}
				damage = false;
				ObjRender_SetColorHSV(obj,graph*240,155,255*color/255);
				}		
			if(SpellSwitch == false)
				{	
				damage = true;
				alpha-=255/20;
				ObjRender_SetAlpha(obj,alpha);
				scale+=scaleM/20;
				}
			if(damage == true)
				{
				ObjSpell_SetIntersectionCircle(spell,x,y,scale);
				}
			x+=4*cos(angle);
			y+=4*sin(angle);
			ObjRender_SetPosition(obj,x,y,0);
			ObjSprite2D_SetDestRect(obj,-scale,-scale,scale,scale);
			yield;
			}
		Obj_Delete(obj);
		Obj_Delete(spell);
		}
}

function FFlash
{
	TPlayerBlink(30,60);
	SetPlayerInvincibilityFrame(30);
	IsGrazeEnable = false;

	let spread = [32,5];
	let xM = GetPlayerX;	
	let yM = GetPlayerY;
	let c = 90;

	TFlash;
	loop(30){yield;}
	IsGrazeEnable = true;

	task TFlash
		{
		ascent(i in 0..ceil(spread[0]))
			{
			TSpam(0,i*720/(spread[0]-1)+90,i,c);
			loop(1)
				{
				yield;
				c+=6;
				}
			}
		}
	task TSpam(dist,angle,numb,counter)
		{
		let spell = ObjSpell_Create;
		ObjSpell_Regist(spell);
		ObjSpell_SetEraseShot(spell,true);
		ObjSpell_SetDamage(spell,0);

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "../../system/img/WhiteCircleAlpha.png");
		ObjSprite2D_SetSourceRect(obj,0,0,64,64);
		Obj_SetRenderPriority(obj,0.34);
		let scalem = 3;
		let scaleM = 28;
		let scale = scalem;
		let delay = 10;
		let count = 0;
		while(floor(count/3) < 80)
			{
			count+=1;
			counter+=6;
			dist+=2;
			let x = xM+dist*cos(angle);	
			let y = yM+dist*sin(angle);
			if(cos(counter-angle) >= cos(spread[1]*360/spread[0]))
				{
				if(scale < scaleM)
					{
					scale += (scaleM-scalem)/((360/spread[0])/3);
					}
				else
					{	
					scale = scaleM;
					}
				ObjSpell_SetIntersectionCircle(spell,x,y,scale);
				}
			else
				{
				if(scale > scalem)	
					{
					scale-=(scaleM-scalem)/spread[1];
					}
				else
					{
					scale = scalem;
					}
				}
			ObjRender_SetPosition(obj,x,y,0);
			ObjSprite2D_SetDestRect(obj,-scale,-scale,scale,scale);
			let color = Obj_GetValueD(objPlayer,"COLORHSV",255);
			ObjRender_SetColorHSV(obj,240,255,120*color/255);
			yield;
			}
		Obj_Delete(obj);			
		Obj_Delete(spell);
		}
}