#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["MARI"]
#Title["Marisa Kirisame"]
#Image["./Face.png"]
#ReplayName["Marisa"]

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

task TMain
{
	sdr*=1.1;
	PlayerPath1 = GetCurrentScriptDirectory ~ "Sprite0.png";
	PlayerPath2 = GetCurrentScriptDirectory ~ "Sprite0.png";
	PlayerSpeed = [5,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 angleS;

	let countO = 12;
	let spreadO = 0;

	let spread = [0,0];	
	

	TOption;
	
	loop
		{	
		pow = min(Pmax,GetPlayerPower);
		alternative(trunc(pow))
			case(0)
				{
				spread = [0,0];
				spreadO = 0;
				}
			case(1)
				{
				spread = [0,0];
				spreadO = 1;
				}
			case(2)
				{
				spread = [4,0];
				spreadO = 1;
				}
			case(3)
				{
				spread = [4,0];
				spreadO = 2;
				}
			case(4)
				{
				spread = [4,4];
				spreadO = 3;
				}
		if(IsFocus == true)
			{
			angleS = [60*0.625,95*0.625];
			}
		else
			{
			angleS = [60*1.125,85*1.125];
			}
		if(IsKeyState([VK_SHOT],[KEY_PUSH,KEY_HOLD]))
			{
			if(GetPlayerState == STATE_NORMAL && IsPermitPlayerShot == true && ShotPermit == true)
				{
				if(counter%count == 0)
					{
					PlaySound("se_PlayerShot1",100);
					ascent(i in 0..2)
						{
						TSpam(GetPlayerX+(i*2-1)*8,GetPlayerY,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 shot = CreatePlayerShotA1(x,y,22,angle,120*count*r*sdr/60,1,MARI);
		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);
		ObjRender_SetScaleXYZ(obj,1.125,1.125,0);
		Obj_SetRenderPriority(obj,0.35);	
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		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;
				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)
				{
				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*(10+n*20);
						let ym = (-27.5+n*15);
						xA += (1-dF)*xm; //Unfocus x position
						xA += (dF)*(xm*0.6+dir*1.5); //Focus x position
						yA += (1-dF)*ym; //Unfocus y position
						yA += (dF)*(ym*0.95-3); //Focus y position
						}
					x = x ~ [xP+d*xA*0.5];
					y = y ~ [yP+d*yA*0.5-18];
					ObjSpriteList2D_SetSourceRect(obj,144,144,160,160);
					ObjRender_SetPosition(obj,x[i]+0.5,y[i],0);
					ObjSpriteList2D_SetDestCenter(obj);
					ObjSpriteList2D_AddVertex(obj);
					if(IsShooting == true)
						{
						if(IsFocus == true)
							{
							if(c%countO == n*floor(countO/4))
								{
								CreateMariMissile(x[i],y[i],4,floor(countO/4));
								}
							}
						}
					}
				}
			if((IsShooting == false && c%countO != 0) || IsShooting == true)
				{
				c+=1;
				}	
			if(IsShooting && IsFocus == false && IsLaserActive == false)
				{
				TLaser;
				}

			pp = p;
			yield;
			}
		task TLaser
			{	
			IsLaserActive = true;

			let shot = [];

			let l = [];
			ascent(i in 0..p)
				{
				l = l ~ [0];
				shot = shot ~ [ObjShot_Create(OBJ_STRAIGHT_LASER)];
				ObjShot_Regist(shot[i]);
				ObjShot_SetGraphic(shot[i],BLANK);
				ObjRender_SetBlendType(shot[i],BLEND_ALPHA);
				ObjShot_SetDamage(shot[i],0);
				ObjShot_SetAutoDelete(shot[i],false);
				ObjShot_SetPenetration(shot[i],INF);
				ObjStLaser_SetSource(shot[i],false);
				Obj_SetRenderPriority(shot[i],0.4);
	
				}
			let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
			ObjPrim_SetTexture(obj,PlayerPath1);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			Obj_SetRenderPriority(obj, 0.4);
			let pA = length(shot);
			let pn = trunc(pow);

			let wM = 24;
			let w = 0.1;

			let c = 0;
		
			while(w >= 0.025 && pn == trunc(pow))
				{
				c+=1;
				if(IsFocus == false && IsShooting == true)
					{
					w += (1-w)/6;
					ascent(i in 0..pA)
						{
						ObjShot_SetIntersectionEnable(shot[i],true);
						}
					}
				else
					{
					w += (0-w)/3;
					ascent(i in 0..pA)
						{
						ObjShot_SetIntersectionEnable(shot[i],false);
						}
					}
	
				ObjSpriteList2D_ClearVertexCount(obj);
				ascent(i in 0..pA)
					{
					let n = absolute(i-pA*0.5+0.5)*2;
					let dir = 0;
					if(i-pA*0.5+0.5 != 0)
						{
						dir = n*0.5/(i-pA*0.5+0.5);
						}
					let angle = 270+dir*0.5*(0.5+n*6.5);
					ObjStLaser_SetAngle(shot[i],angle);

					if(l[i] < GetStgFrameHeight*1.25)
						{
						l[i] = l[i]+GetStgFrameHeight*1.25/15;
						}

					let IsIntersecting = false;
					let array = GetAllEnemyID;
					ascent(iA in 0..length(array))
						{
						let enm = array[iA];
						let enmx = ObjMove_GetX(enm);
						let enmy = ObjMove_GetY(enm);
						if(IsIntersected_Line_Circle(x[i],y[i],x[i]+l[i]*cos(angle),y[i]+l[i]*sin(angle),wM,enmx,enmy,16))
							{
							IsIntersecting = true;
							CreatePlayerShotA1(enmx,enmy,0,0,2.25*100/60,1,BLANK);
							}
						while(IsIntersected_Line_Circle(x[i],y[i],x[i]+l[i]*cos(angle),y[i]+l[i]*sin(angle),wM,enmx,enmy,14) && l[i] > 0){l[i]=l[i]-1;}
						}
					ObjLaser_SetLength(shot[i],l[i]);
					ObjLaser_SetRenderWidth(shot[i],w*wM);
					ObjLaser_SetIntersectionWidth(shot[i],w*wM*2);
					ObjMove_SetPosition(shot[i],x[i],y[i]);
					if(IsIntersecting == true)
						{
						TDel(x[i],y[i],l[i],angle,floor(c/2)%3);
						}
	
					ObjSpriteList2D_SetSourceRect(obj,-c*6,184-6,l[i]-c*6,184+6);
					ObjRender_SetScaleXYZ(obj,1,w*wM*0.5/12,0);
					ObjRender_SetPosition(obj,x[i]+l[i]*0.5*cos(angle),y[i]+l[i]*0.5*sin(angle),0);
					ObjRender_SetAngleZ(obj,angle);	
					ObjRender_SetColorHSV(obj,0,0,255);	
					ObjSpriteList2D_SetDestCenter(obj);
					ObjSpriteList2D_AddVertex(obj);
	
					ascent(t in 0..2)
						{
						ObjSpriteList2D_SetSourceRect(obj,0,192,32,224);
						let scl = w*18*(1-t*0.15)/32;
						ObjRender_SetScaleXYZ(obj,scl,scl,0);
						ObjRender_SetPosition(obj,x[i],y[i]-1,0);
						ObjRender_SetAngleZ(obj,(t*2-1)*c*(5+t*0.67));
						ObjRender_SetColorHSV(obj,300+60*cos(c*3),155,255);	
						ObjSpriteList2D_SetDestCenter(obj);
						ObjSpriteList2D_AddVertex(obj);
						}	
					}
				yield;			
				}	
			ascent(i in 0..length(shot))
				{
				Obj_Delete(shot[i]);
				}
			Obj_Delete(obj);
			IsLaserActive = false;
			task TDel(x,y,l,angle,g)
				{
				if(IsPermitPlayerShot == false || y+l*sin(angle) < -32 || y+l*sin(angle) > GetStgFrameHeight+32  || x+l*cos(angle) < -32 || x+l*cos(angle) > GetStgFrameWidth+32){return;}	
				alternative(g)
					case(0)
						{
						g = SHOT_RED;
						}
					case(1)
						{
						g = SHOT_PURPLE;
						}
					case(2)
						{
						g = SHOT_BLUE;
						}
				let obj = CreatePlayerShotA1(x+(l-4)*cos(angle),y+(l-4)*sin(angle),3,angle+rand(-1,1)*5,0,INF,SMALL[g]);
				ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	
				Obj_SetRenderPriority(obj,0.49);

				let scl = 1;	
				let alphaM = 125;
				let alpha = alphaM;
				loop(15)
					{
					alpha-=alphaM/15;
					scl-=1/15;
					ObjRender_SetAlpha(obj,alpha);	
					ObjRender_SetScaleXYZ(obj,1*scl,1*scl,0);
					yield;
					}
				Obj_Delete(obj);
				}
			}
		}
}

task CreateMariMissile(x,y,speed,n)
	{
	if(IsPermitPlayerShot == false){return;}

	let shot = CreatePlayerShotA1(x,y,speed,270,0*n*100/60,1,MARI_MISSILE);
	ObjRender_SetAngleZ(shot,-90);
	ObjMove_SetAcceleration(shot,1/6);
	ObjMove_SetMaxSpeed(shot,100);
	
	let alpha = 205;
	
	Obj_SetRenderPriority(shot,0.4);
	ObjRender_SetAlpha(shot,alpha);
	ObjRender_SetColorHSV(shot,0,0,175);
			
	while(!Obj_IsDeleted(shot))
		{	
		x = ObjMove_GetX(shot);	
		y = ObjMove_GetY(shot);
		yield;
		}	
	loop(2)
		{
		let angle = rand(0,360);
		let dist = rand(0,16);
		CreateMariMissileSplash(x+dist*cos(angle),y+dist*sin(angle),MARI_SPLASH,n,sdr*5*(1/2)*(1/16)*n*125/60);
		}
	
	}
task CreateMariMissileSplash(x,y,g,n,d)
	{
	if(IsPermitPlayerShot == false || y < -32 || y > GetStgFrameHeight+32  || x < -32 || x > GetStgFrameWidth+32){return;}
	let time = 16;
	let shot = CreatePlayerShotA1(x,y,0,0,d,INF,g);
	ObjRender_SetAlpha(shot,55);
	ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
	ascent(t in 0..time)
		{
		if(t%2 == 0 && t != 0)
			{
			g+=1;
			ObjShot_SetGraphic(shot,g);
			}
		yield; 
		}
	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 = [MainSpeed[1],MainSpeed[1]];

	let time = [90,45,45];
	let switch = false;

	let damageA = 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 xM = GetPlayerX;
		let yM = GetPlayerY;	
		let angleM = 0;	

		let wM = 24;
		let spd = GetStgFrameHeight*1.5/8;
		let vcount = 6;
	
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetTexture(obj,"");
		Obj_SetRenderPriority(obj, 0.57);
		ObjPrim_SetVertexCount(obj, (vcount+1)*2);
		ObjSprite2D_SetSourceRect(obj,0,0,1,1);
		ObjRender_SetColorHSV(obj,0,0,0);
	
		let obj2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetTexture(obj2,"");
		Obj_SetRenderPriority(obj2, 0.57);
		ObjPrim_SetVertexCount(obj2, (vcount+1)*2);
		ObjSprite2D_SetSourceRect(obj2,0,0,1,1);
		ObjRender_SetBlendType(obj2,BLEND_ADD_ARGB);
	
		descent(t in -time[1]-time[2]..time[0])
			{
			let dist = 32+max(t,0)*(GetStgFrameHeight*1.5-32)/time[0];
			let array = GetShotIdInCircleA2(GetStgFrameWidth*0.5,GetStgFrameHeight*0.5,dist+64,TARGET_ENEMY);
			if(t > -time[1])
				{
				ascent(i in 0..length(array))
					{
					let shot = array[i];
					if(!Obj_IsDeleted(shot) && !	ObjShot_IsSpellResist(shot))
						{
						ObjShot_ToItem(shot);
						}
					}
				}
			if(t == time[0]-20)
				{
				angleM = atan2(GetPlayerY-yM,GetPlayerX-xM);
				xM = GetPlayerX;
				yM = GetPlayerY;
				ascent(i in 0..4)
					{
					TShot(i*90+angleM,i);
					}
				}
			if(t == -time[1])
				{
				PlaySound("se_Laser",100);
				switch = true;
				}
			ascent(i in 0..vcount+1)
				{
				let ind = i*2;
				let angle = max(t,0)*360/time[0]+i*360/vcount-90;
				if(t > -time[1])
					{
					ObjPrim_SetVertexPosition(obj,ind,GetPlayerX+dist*cos(angle),GetPlayerY+dist*sin(angle),0);
					ObjPrim_SetVertexPosition(obj,ind+1,GetPlayerX+GetStgFrameHeight*1.5*cos(angle),GetPlayerY+GetStgFrameHeight*1.5*sin(angle),0);
					}
				else if(t == -time[1])
					{
					Obj_Delete(obj);
					}
				let s = 1;
				let alpha = 1;
				let a = rand(205/255,1);
				if(t <= -time[1])
					{
					let n = min(10,absolute(t+time[1]))/10;
					s = 1+n;
					alpha = 1-n;
					}
				ObjPrim_SetVertexPosition(obj2,ind,GetPlayerX+dist*s*cos(angle),GetPlayerY+dist*s*sin(angle),0);
				ObjPrim_SetVertexPosition(obj2,ind+1,GetPlayerX+(dist*s+5)*cos(angle),GetPlayerY+(dist*s+5)*sin(angle),0);
				ObjPrim_SetVertexAlpha(obj2,ind,a*alpha*255);
				ObjPrim_SetVertexAlpha(obj2,ind+1,a*alpha*255);
				}
			yield;
			}
		Obj_Delete(obj2);

		task TShot(angle,n)
			{
			let dist = 0;
			while(dist < GetStgFrameHeight*1.5)
				{
				if(dist >= spd || n == 0 || n == 1)
					{
					TLaser(xM+dist*cos(angle),yM+dist*sin(angle));
					}
				dist+=spd;
				if(n == 0)
					{
					PlaySound("se_LaserDelay",120);
					}
				loop(6){yield;}
				}
			
			task TLaser(xM,yM)
				{
				let x = xM;
				let y = yM;
				let dist = 0;
				let w = 4;
				let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
				ObjShot_Regist(obj);
				ObjShot_SetAutoDelete(obj,false);
				ObjShot_SetGraphic(obj,OVAL[SHOT_YELLOW]);
				ObjShot_SetDamage(obj,0);
				ObjShot_SetPenetration(obj,INF);
				ObjStLaser_SetAngle(obj,angle-90);
				ObjStLaser_SetSource(obj,false);
				Obj_SetRenderPriority(obj,0.56);
		
				let counter = -1;
				while(counter < time[2])
					{
					if(switch == true)
						{
						counter+=1;
						}	
					let a = rand(205,255);
					dist = min(GetStgFrameHeight*1.5,dist+17.5);
					x = xM+dist*cos(angle+90);
					y = yM+dist*sin(angle+90);
					if(counter == 0 )
						{
						ObjShot_SetDamage(obj,4500/time[2]);
						ObjShot_SetSpellFactor(obj,true);
						}	
					if(counter >= 0)
						{
						if(counter < 5)
							{
							w = 4+counter*(wM-4)/4;
							}	
						else
							{
							w = wM*(1-(counter-4)/(time[2]-4));
							}
						}
					ObjMove_SetPosition(obj,x,y);
					ObjLaser_SetLength(obj,dist*2);
					ObjLaser_SetRenderWidth(obj,w);
					ObjLaser_SetIntersectionWidth(obj,w);
					ObjLaser_SetInvalidLength(obj,0,0);
					ObjRender_SetColorHSV(obj,60,255,a);
					yield;
					}
				Obj_Delete(obj);
				}
			}
		}
	
}

function FFlash
{
}