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

#include "./../functions1.txt"

PlayerSpeed = [4.33,2.33];

task TMain
{
	PlayerPath = GetCurrentScriptDirectory ~ "Sprite.png";
}

task TShot
{
	let spread = [3,2];
	let counter = 0;
	let angle;
	let soundS = false;

	let IsShooting = false;
	let IsFocus = false;

	let angleO1 = 180;
	let angleO2 = 0;
	let angleO3 = 0;
	let distOM = 0;
	let distO = 0;	
	let type = 0;

	ascent(i in 0..2)
		{
		TOption(i);
		}
	loop
		{
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{
			angle = [180,20];
			distOM = 25;
			IsFocus = true;
			type = 1;
			}
		else
			{
			angle = [180,120];
			distOM = 50;
			IsFocus = false;
			type = 0;
			}
		distO += (distOM-distO)/5;
		if((GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD))
			{
			if(GetPlayerState == STATE_NORMAL && IsPermitPlayerShot == true && ShotPermit == true)
				{
				if(counter%5 == 0)
					{
					PlaySound("se_PlayerShot1",120);
					ascent(i0 in 0..1)
						{
						ascent(i1 in 0..spread[i0])
							{
							i1 = i1-spread[i0]/2+0.5;
							TSpam(PlayerX+distO*cos(i1*360/spread[i0]+270),PlayerY-15+4*sin(i1*360/spread[i0]+270),270+i1*angle[1]/spread[i0]);
							}
						}
					}
				IsShooting = true;
				}
			else
				{
				IsShooting = false;
				}
			counter+=1;
			}
		else if(counter%5 != 0)
			{
			counter+=1;
			IsShooting = false;
			}
		else
			{
			IsShooting = false;
			}
		yield;
		}

	task TSpam(x,y,angle)
		{
		if(IsPermitPlayerShot == false){return;}
		let shot = CreatePlayerShotA1(x,y,25,angle,1.5,2,0);
		ObjMove_SetAcceleration(shot,0.1);
		ObjMove_SetMaxSpeed(shot,100);
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Sprite.png");
		Obj_SetRenderPriority(obj,0.3);
		ObjSprite2D_SetSourceRect(obj,64+type*32,144,96+type*32,176);
		ObjSprite2D_SetDestRect(obj,-16,-16,16,16);
		ObjRender_SetScaleXYZ(obj,0.875,0.875,0);	
		ObjRender_SetColorHSV(obj,0,0,105);
		
		let xm = 5;
		let xM = 17;
		let numb = 18;
		let alpha = 155;
		while(!Obj_IsDeleted(shot))
			{
			ObjRender_SetPosition(obj,ObjMove_GetX(shot),ObjMove_GetY(shot),0);
			ObjRender_SetAngleZ(obj,ObjMove_GetAngle(shot)+90);
			ObjRender_SetAlpha(obj,alpha);	
			if(numb < 18+7*2)
				{
				ObjRender_SetScaleY(obj,(numb-18)/28+1);
				xm = xM;
				xM+=numb;
				numb+=2;
				}
			x = ObjMove_GetX(shot);	
			y = ObjMove_GetY(shot);
			if(x < -16 || x > GetStgFrameWidth+16)
				{
				ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
				}
			if(y < -16)
				{
				ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
				}
			if(alpha > 75)
				{
				alpha-=80/10;
				}
			else
				{
				alpha = 75;
				}
			yield;
			}	
		if(Effect < 2)
			{
			let alpha = 100;
			x = ObjRender_GetX(obj);
			y = ObjRender_GetY(obj);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			loop(30)
				{
				alpha-=100/30;
				x+=cos(angle);
				y+=sin(angle);
				ObjRender_SetAlpha(obj,alpha);
				ObjRender_SetPosition(obj,x,y,0);
				ObjSprite2D_SetDestCenter(obj);
				yield;
				}
			}
		Obj_Delete(obj);
		}

	task TOption(dir)
		{
		let angleZ = dir*180;
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Sprite.png");
		ObjSprite2D_SetSourceRect(obj,2,256,100,300);
		let scale = 9;
		ObjSprite2D_SetDestRect(obj,-(100/64)*scale,-scale,(100/64)*scale,scale);
		ObjRender_SetScaleXYZ(obj,dir*2-1,dir*2-1,0);
		Obj_SetRenderPriority(obj,0.31);
		ObjRender_SetColorHSV(obj,0,0,175);		
		ObjRender_SetAlpha(obj,155);
		let counter = 0;
		while(PlayerDead == false)
			{
			let angle1 = angleO2;
			let x = PlayerX+distO*cos(angle1+dir*180);
			let y = PlayerY-15+distO*sin(angle1+dir*180);
			angleZ+=2;
			ObjRender_SetAngleZ(obj,angleZ);
			ObjRender_SetPosition(obj,x,y,0);
			if(IsShooting == true)
				{
				if(IsFocus == true)
					{
					if(counter%60 == 0)
						{
						TSpam2(x,y,type);
						counter+=1;
						}
					else if(counter%60 != 0)
						{
						counter+=1;
						}
					}
				else
					{
					if(counter%5 == 0)
						{
						TSpam2(x,y,type);
						counter+=1;
						}
					else if(counter%5 != 0)
						{
						counter+=1;
						}
					}
				}
			Obj_SetVisible(obj,Obj_IsVisible(img));
			yield;
			}
		Obj_Delete(obj);
		task TSpam2(x,y,type)
			{
			if(IsPermitPlayerShot == false){return;}
			let shot = CreatePlayerShotA1(x,y,25-type*24,0,2,2+type*100,0);
			ObjMove_SetAcceleration(shot,1/30);
			ObjMove_SetMaxSpeed(shot,100);
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "Sprite.png");
			ObjRender_SetAlpha(obj,35+type*15);	
			Obj_SetRenderPriority(obj,0.3);
			ObjSprite2D_SetSourceRect(obj,96-type*32,144,128-type*32,176);
			ObjSprite2D_SetDestRect(obj,-16,-16,16,16);
			ObjRender_SetScaleXYZ(obj,0.875,0.875,0);
			let xm = 5;
			let xM = 17;
			let numb = 18;	
			let counter = 0;
			let enemy = ID_INVALID;
			ascent(i in 0..length(GetEnemyBossObjectID))
				{
				enemy = GetEnemyBossObjectID[i];	
				}	
			let enemy1 = enemy;
			let array = GetAllEnemyID;
			let dist = GetStgFrameHeight*1.5;
			if(enemy == ID_INVALID || Obj_IsDeleted(enemy) || ObjMove_GetY(enemy) > ObjMove_GetY(objPlayer))
				{
				ascent(i in 0..length(array))
					{
					let obj = array[i];
					if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"PLAYER",false) == false && GetObjectDistance(objPlayer,obj) < dist && ObjMove_GetY(obj) < ObjMove_GetY(objPlayer))
						{
						enemy = obj;
						dist = GetObjectDistance(objPlayer,enemy);
						}
					}
				}	
			if(enemy == ID_INVALID || Obj_IsDeleted(enemy))
				{
				ascent(i in 0..length(array))
					{
					let obj = array[i];
					if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"PLAYER",false) == false && GetObjectDistance(objPlayer,obj) < dist && ObjMove_GetY(obj) < ObjMove_GetY(objPlayer))
						{
						enemy = obj;
						dist = GetObjectDistance(objPlayer,enemy);
						}
					}
				}	
			if(enemy == ID_INVALID || Obj_IsDeleted(enemy))
				{
				ascent(i in 0..length(array))
					{
					let obj = array[i];
					if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"PLAYER",false) == false && GetObjectDistance(objPlayer,obj) < dist)
						{
						enemy = obj;
						dist = GetObjectDistance(objPlayer,enemy);
						}
					}
				}		
			let angle = 270;
			if(enemy != ID_INVALID)
				{
				let angleA = atan2(ObjMove_GetY(enemy)-y,ObjMove_GetX(enemy)-x);
				if(type == 1)
					{
					angle+=30*sin(angleA-angle);
					}
				else
					{
					angle = angleA;
					}
				}
			ObjMove_SetAngle(shot,angle);
			while(!Obj_IsDeleted(shot))
				{
				counter+=1;
				ObjRender_SetAngleZ(obj,ObjMove_GetAngle(shot)+90);
				ObjRender_SetPosition(obj,ObjMove_GetX(shot),ObjMove_GetY(shot),0);
				if(numb < 18+7*2)
					{
					ObjRender_SetScaleY(obj,(numb-18)/28+1);
					xm = xM;
					xM+=numb;
					numb+=2;
					}	
				let angleA;	
				if(enemy == ID_INVALID || Obj_IsDeleted(enemy))	
					{
					angleA = 270;		
					}	
				else
					{
					angleA = atan2(ObjMove_GetY(enemy)-ObjMove_GetY(shot),ObjMove_GetX(enemy)-ObjMove_GetX(shot));
					}	
				angle += (1.5-type)*sin(angleA-angle);
				ObjMove_SetAngle(shot,angle);
				x = ObjMove_GetX(shot);	
				y = ObjMove_GetY(shot);
				if(type == 1 && counter%5 == 0)
					{
					TSpam3(x,y);
					}
				yield;
				}	
			if(Effect < 2)
				{
				let alpha = 100;
				x = ObjRender_GetX(obj);
				y = ObjRender_GetY(obj);
				ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
				loop(30)
					{
					alpha-=100/30;
					x+=cos(angle);
					y+=sin(angle);
					angleZ+=7;
					ObjRender_SetAngleZ(obj,angleZ);
					ObjRender_SetAlpha(obj,alpha);
					ObjRender_SetPosition(obj,x,y,0);
					yield;
					}
				}
			Obj_Delete(obj);
			}
		task TSpam3(x,y)
			{
			if(IsPermitPlayerShot == false){return;}
			let type = round(rand(0,1));
			let angle = rand(0,360);
			let alpha = 40;
			let shot = CreatePlayerShotA1(x,y,1,angle,0.5,1000,0);
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "../../system/img/Default_Shot.png");
			ObjRender_SetAlpha(obj,alpha);	
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			Obj_SetRenderPriority(obj,0.3);
			ObjSprite2D_SetSourceRect(obj,1+type*140,290,26+type*140,316);
			ObjSprite2D_SetDestRect(obj,-8,-8,8,8);
			let xm = 5;
			let xM = 17;
			let numb = 18;	
			let counter = 0;
			while(!Obj_IsDeleted(shot))
				{
				counter+=1;
				ObjRender_SetAngleZ(obj,ObjMove_GetAngle(shot)+90);
				ObjRender_SetPosition(obj,ObjMove_GetX(shot),ObjMove_GetY(shot),0);
				if(numb < 18+7*2)
					{
					ObjRender_SetScaleY(obj,(numb-18)/28+1);
					xm = xM;
					xM+=numb;
					numb+=2;
					}	
				x = ObjMove_GetX(shot);	
				y = ObjMove_GetY(shot);
				if(counter > 45)
					{
					Obj_Delete(shot);
					}
				yield;
				}	
			x = ObjRender_GetX(obj);
			y = ObjRender_GetY(obj);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			loop(15)
				{
				alpha-=55/15;
				x+=cos(angle);
				y+=sin(angle);
				ObjRender_SetAlpha(obj,alpha);
				ObjRender_SetPosition(obj,x,y,0);
				yield;
				}
			Obj_Delete(obj);
			}
		}
}