#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["TRES_TENKO_A"]
#Title["Heaven Team"]
#Image["./Face.png"]
#ReplayName["Heaven Team"]

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

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

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

task TShot
{	

	let pow = 0;

	let IsShooting = false;

	let count = 3;
	let counter = 0;
	
	loop
		{	
		pow = min(Pmax,GetPlayerPower);
		if(IsKeyState([VK_SHOT],[KEY_PUSH,KEY_HOLD]))
			{
			if(GetPlayerState == STATE_NORMAL && IsPermitPlayerShot == true && ShotPermit == true)
				{
				if(counter%count == 0)
					{
					//PlaySound. Shot sound.
					ascent(i in 0..2)
						{
						TSpam(i*2-1);
						}
					}
				IsShooting = true;
				}
			else
				{
				IsShooting = false;
				}
			counter+=1;
			}
		else if(counter%count != 0)
			{
			counter+=1;
			IsShooting = false;
			}
		else
			{
			IsShooting = false;
			}
		yield;
		}

	task TSpam(dir)
		{
		if(IsPermitPlayerShot == false){return;}
		let g = TEN_MAIN;
		let x = GetPlayerX+dir*8;
		let y = GetPlayerY;
		let enm = FHomingEnemy(x,y,10000,10000,true);
		let angle = FAngleToHomingEnemy(enm,x,y,270,0);
		let shot = CreatePlayerShotA1(x,y,26,angle,80*count*(1/2)*sdr/60,1,g);
		ObjRender_SetAlpha(shot,205);
		ObjRender_SetScaleXYZ(shot,0.25,0.25,0);
		ObjRender_SetAngleZ(shot,-90);

		Obj_SetRenderPriority(shot,0.32);
		}

	task TOption
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj,textureO[0]);
		Obj_SetRenderPriority(obj,0.35);
		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 x = [];
		let y = [];
		let a = 0;
		let c = 0;
		let IsLaserActive = false;
		let DownState = false;
		loop
			{
			p = min(Pmax,floor(pow));
			if(GetPlayerState == STATE_DOWN)
				{
				DownState = true;
				}
			if(DownState == true)
				{
				xP = GetPlayerX;
				yP = GetPlayerY;
				d = 0;
				}
			else
				{
				if(p != pp)
					{
					d = 0;
					}
				if(d < 1)
					{
					d += (1-d)/6;
					}
				let n = 3;
				if(IsFocus == true)
					{
					dF += (1-dF)/4;
					n = 2;
					a+=1.25;
					}
				else
					{
					dF += (0-dF)/4;
					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;
					local
						{
						let dist = [24,48];
						let angle = i*360/p+a*1.5;
						xA += (dF)*dist[0]*cos(angle); //Focus x position
						xA += (1-dF)*dist[1]*cos(angle); //Unfocus x position
						yA += (dF)*dist[0]*sin(angle); //Focus y position
						yA += (1-dF)*dist[1]*sin(angle); //Unfocus y position
						}
					x = x ~ [xP+d*xA];
					y = y ~ [yP+d*yA+2];
					ObjSpriteList2D_SetSourceRect(obj,floor(a/6)%4*40.5,0,(floor(a/6)%4+1)*40.5,36);
					ObjSpriteList2D_SetDestCenter(obj);
					ObjRender_SetPosition(obj,x[i]+2.5,y[i],0);
					ObjRender_SetAlpha(obj,d*d*255);
					ObjRender_SetScaleXYZ(obj,(2.5-d*1.5)*0.75,(2.5-d*1.5)*0.75,0);
					ObjSpriteList2D_AddVertex(obj);
					}
				if(IsShooting == true && IsLaserActive == false)
					{
					TLaser;
					}
				}
			pp = p;
			yield;
			}
		task TLaser
			{
			IsLaserActive = true;

			let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
			ObjPrim_SetTexture(obj,textureO[1]);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			Obj_SetRenderPriority(obj, 0.41);

			let l = [];
			let wM = 16;
			let w = 0.1;
			let sta = 5;
			if(IsFocus == true)
				{
				sta = 3;
				}

			let c = 0;
			let IntersectEnable = false;
			while(w >= 0.1 && GetPlayerState == STATE_NORMAL)
				{		
				if(length(l) < p)
					{
					l = [];
					ascent(i in 0..p)
						{
						l = l ~ [0];
						}
					}
				let xA = x;
				let yA = y;
				ObjSpriteList2D_ClearVertexCount(obj);
				c+=1;
				if(IsShooting == true)
					{
					w += (1-w)/6;
					IntersectEnable = true;
					}
				else
					{
					w += (0-w)/3;
					IntersectEnable = false;
					}
				ascent(i in 0..p)
					{
					let x = xA[i];
					let y = yA[i];
					if(l[i] < GetStgFrameHeight*1.25)
						{
						l[i] = l[i]+GetStgFrameHeight*1.25/15;
						}
					let stA = 5;
					if(IsFocus == true)
						{
						stA = 2	;
						}
					sta += (stA-sta)/30;
					let angle = 270+sta*cos(atan2(y-GetPlayerY,x-GetPlayerX));
					let array = GetAllEnemyID;
					ascent(n in 0..length(array))
						{
						let IsIntersecting = false;
						let enm = array[n];
						let enmx = ObjMove_GetX(enm);
						let enmy = ObjMove_GetY(enm);
						if(IsIntersected_Line_Circle(x,y,x+l[i]*cos(angle),y+l[i]*sin(angle),wM,enmx,enmy,16))
							{		
							IsIntersecting = true;
							}
						let lA = l[i];
						while(IsIntersected_Line_Circle(x,y,x+lA*cos(angle),y+lA*sin(angle),wM,enmx,enmy,15) && lA > 0){lA-=15;}
						if(IntersectEnable == true && IsIntersecting == true)
							{
							let spam = CreatePlayerShotA1(enmx,enmy,0,0,(124-7*pow)*(1/4)*sdr/60,1,BLANK);
							Obj_SetVisible(spam,false);
							ascent(i in 0..3)
								{
								let wA = w*7;
								let xA = x+(lA-4)*cos(angle);
								let yA = y+(lA-4)*sin(angle);
								let angleA = atan2(yA-enmy,xA-enmx)+(i-2*0.5+rand(0,1))*180/3;
								ObjSpriteList2D_SetSourceRect(obj,76,4,332,36);	
								ObjSpriteList2D_SetDestRect(obj,0,0,1,1);
								ObjRender_SetScaleXYZ(obj,5*wA,1*wA,0);
								ObjRender_SetPosition(obj,xA,yA,0);
								ObjRender_SetAngleZ(obj,angleA);	
								ObjRender_SetColorHSV(obj,0,0,255);
								ObjRender_SetAlpha(obj,155);
								ObjSpriteList2D_AddVertex(obj);
								}
							}
						}
					ObjSpriteList2D_SetSourceRect(obj,4,4,68,20);	
					ObjSpriteList2D_SetDestRect(obj,0,0,1,1);
					ObjRender_SetScaleXYZ(obj,l[i],w*wM*0.5,0);
					ObjRender_SetPosition(obj,x+l[i]*0.5*cos(angle),y+l[i]*0.5*sin(angle),0);
					ObjRender_SetAngleZ(obj,angle);	
					ObjRender_SetColorHSV(obj,0,0,255);
					ObjRender_SetAlpha(obj,155);
					ObjSpriteList2D_AddVertex(obj);
	
					ascent(i in 0..6)
						{
						let wA = w*7;
						let angleA = (i-6*0.5+rand(0,1))*180/6+angle;
						ObjSpriteList2D_SetSourceRect(obj,76,4,332,36);	
						ObjSpriteList2D_SetDestRect(obj,0,0,1,1);
						ObjRender_SetScaleXYZ(obj,4*wA,2*wA,0);
						ObjRender_SetPosition(obj,x+2*cos(angle)+wA*cos(angleA),y+2*sin(angle)+wA*sin(angleA),0);
						ObjRender_SetAngleZ(obj,angleA);	
						ObjRender_SetColorHSV(obj,0,0,255);
						ObjRender_SetAlpha(obj,75);
						ObjSpriteList2D_AddVertex(obj);
						}
					}
				yield;
				}
			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;}	
			let obj = CreatePlayerShotA1(x+(l-4)*cos(angle),y+(l-4)*sin(angle),3,angle+rand(-1,1)*5,0,10^10,TEN_MAIN);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);

			Obj_SetRenderPriority(obj,0.41);
			let scl = 1;	
			let alphaM = 255;
			let alpha = alphaM;
			loop(3)
				{
				alpha-=alphaM/3;
				scl-=1/3;
				ObjRender_SetAlpha(obj,alpha);	
				ObjRender_SetScaleXYZ(obj,0.25*scl,0.25*scl,0);
				yield;
				}
			Obj_Delete(obj);
			}
		}
}

function FSpell
{
	SpellSwitch = true;	
	ShotPermit = false;

	TSpellShadow;

	let xP = GetPlayerX;
	let yP = GetPlayerY;

	let time = [5,60,300,10];
	let timeA = 0;
	ascent(i in 0..length(time))
		{
		timeA+=time[i];
		}
	TMagicCircle(timeA+5*5+15,120);
	let dir = floor(rand(0,2))*2-1;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,textureO[2]);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.6);

	let oC = 0;
	let colCount;
	ascent(i in 0..5)
		{
		let angle = rand(-1,1)*7.5;
		alternative(i)
			case(1)
				{
				angle += dir*360/5;
				}
			case(2)
				{
				angle -= dir*2*360/5;
				}
			case(3)
				{
				angle -= dir*360/5;	
				}
			case(4)
				{
				angle += dir*2*360/5;	
				}
		TOrb(i,angle);
		loop(5){yield;}	
		}
	loop(timeA)
		{
		yield;
		}
	Obj_Delete(obj);
	SpellSwitch = false;
	loop(15){yield;}
	ShotPermit = true;
	
	task TOrb(n,angleAM)
		{
		let angleM = rand(0,360);
		let angleA = 270+angleAM;
		let x = xP;
		let y = yP;
		let r = 40;
		let s = 0.875;
		let a = 0;
		let w = 1.5;
		let dir = floor(rand(0,2))*2-1;

		let spell = [];
		ascent(i in 0..2)
			{
			spell = spell ~ [ObjSpell_Create];
			ObjSpell_Regist(spell[i]);
			}
		ObjSpell_SetEraseShot(spell[0],false);
		ObjSpell_SetDamage(spell[1],0);
		ascent(t in 0..timeA)
			{
			let speed = GetDistance(0,GetPlayerY,0,y)/GetStgFrameHeight;
			if(GetPlayerY > y)
				{	
				speed = 2.25-speed;
				}
			else
				{
				speed+=1.5;
				}
			let angleB = -0.33+(t*t)*13/(timeA*timeA);

			if(t > time[0]+time[1]+time[2])
				{
				s+=0.5/(time[3]-1);
				a-=1/(time[3]-1);
				w+=1/(time[3]-1);
				}
			else if(t >= time[0] && t < time[0]+time[1])	
				{
				s+=0.125/time[1];
				a+=1/time[1];
				angleM+=dir*angleB;
				}
			else
				{
				s+=t*2/(time[2]*time[2]);
				angleM+=dir*angleB;
				}
			x+=t*speed*cos(angleA)/timeA;
			y+=t*speed*sin(angleA)/timeA;
			if(n == oC)
				{
				colCount = 1.5;
				ObjSpriteList2D_ClearVertexCount(obj);
				}
			if(t >= time[0])
				{	
				let d = r*2/86;
				ascent(i in 0..2)
					{
					ObjSpell_SetIntersectionLine(spell[i],x+s*(r+16)*cos(angleM),y+s*(r+16)*sin(angleM),x+s*(r+16)*cos(angleM+180),y+s*(r+16)*sin(angleM+180),w*s*0.5*256/12);
					}
				ObjSpell_SetDamage(spell[0],4500*bdr*0.5*(1/(colCount))/(timeA-time[0]));
				let col = ObjCol_GetListOfIntersectedEnemyID(spell[0]);
				if(length(col) > 0)
					{
					colCount+=4;
					}
				//Bolt
				local
					{
					let anim = floor(t/3+0*2)%12;
					ObjSpriteList2D_SetSourceRect(obj,anim*256/12,0,(anim+1)*256/12,86);	
					ObjSpriteList2D_SetDestCenter(obj);
					ObjRender_SetScaleXYZ(obj,w*s*0.5,s*d,0);
					ObjRender_SetPosition(obj,x,y,0);
					ObjRender_SetAngleZ(obj,angleM-90);	
					ObjRender_SetAlpha(obj,a*255);
					ObjSpriteList2D_AddVertex(obj);
					}
				ascent(i in 0..2)
					{	
					//Orbs
					local
						{
						let angle = angleM+i*180;
						let animX = floor(t/3+i)%4;
						let animY = floor(t/(4*3)+i)%3;
						ObjSpriteList2D_SetSourceRect(obj,animX*64,86+animY*64,(animX+1)*64,86+(animY+1)*64);	
						ObjSpriteList2D_SetDestCenter(obj);
						ObjRender_SetScaleXYZ(obj,w*s*0.5,w*s*0.5,0);
						ObjRender_SetPosition(obj,x+s*r*cos(angle),y+s*r*sin(angle),0);
						ObjRender_SetAngleZ(obj,angle);	
						ObjRender_SetAlpha(obj,a*255);
						ObjSpriteList2D_AddVertex(obj);
						}
					}
				}	
			yield;
			}			
		ascent(i in 0..length(spell))	
			{
			Obj_Delete(spell[i]);
			}
		oC += 1;
		}
}

function FFlash
{
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, textureO[3]);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	let scaleM = 165;

	let x = GetPlayerX;
	let y = GetPlayerY;
	let angle = rand(0,360);
	let scale = 0;
	let scaleA = 180;
	let scaleB = 0;
	let blink = false;
	let angleM = 270;
	let enemy = FHomingEnemy(x,y,GetStgFrameWidth*0.33,GetStgFrameHeight,false);
	if(enemy != ID_INVALID)
		{
		angleM+=min(10,max(-10,90*cos(FAngleToHomingEnemy(enemy,x,y,angleM,0))));
		}
	let time = 90;
	ascent(t in 0..time)
		{
		if(t >= time-6)	
			{
			scale-=1/6;
			}
		else if(scale < 1)
			{
			scale+=1/6;
			}	
		let s = ((time-3)-t)*5/(time-3);
		if(t < 3)
			{
			s = 0;
			}
		x+=s*cos(angleM);
		y+=s*sin(angleM);
		scaleA+=9;
		scaleB = scale*scaleM*(0.95+0.05*cos(scaleA));

		angle+=4;

		let array = GetShotIdInCircleA2(x,y,scaleB,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			let shot = array[i];
			if(!ObjShot_IsSpellResist(shot))
				{
				let xS = ObjMove_GetX(shot);
				let yS = ObjMove_GetY(shot);
				let distS = GetDistance(x,y,xS,yS);
				if(distS <= scaleB*0.4)
					{
					let angle = atan2(yS-y,xS-x);
					let dist = min(distS+2,scaleB+2);
					ObjMove_SetPosition(shot,x+dist*cos(angle),y+dist*sin(angle));
					ObjMove_SetSpeed(shot,2);
					ObjMove_SetAngle(shot,angle);
					}
				}
			}
		
		ObjSpriteList2D_ClearVertexCount(obj);
		TRender;
		if(!IsPermitPlayerSpell)
			{
			break;
			}
		yield;
		}	
	Obj_Delete(obj);

	task TRender
		{
		ObjSpriteList2D_SetDestRect(obj, 0,0,1,1);
		ObjRender_SetColorHSV(obj,0,0,225);
		ObjRender_SetPosition(obj,x,y,0);
	
		ascent(i in 0..2)
			{
			ObjSpriteList2D_SetSourceRect(obj, 0, 0,160, 160);
			ObjRender_SetAngleZ(obj,angle*(1+i*0.25));
			ObjRender_SetScaleXYZ(obj,scaleB*1/1.125,scaleB*1/1.125,0);
			Obj_SetRenderPriority(obj, 0.3);
			ObjRender_SetAlpha(obj,45+i*80);
			ObjSpriteList2D_AddVertex(obj);
			
			ObjSpriteList2D_SetSourceRect(obj, 0, 0,160, 160);
			ObjRender_SetAngleZ(obj,angle*1.125*(1+i*0.25));
			ObjRender_SetScaleXYZ(obj,scaleB,scaleB,0);
			Obj_SetRenderPriority(obj, 0.29);
			ObjRender_SetAlpha(obj,65+i*30);
			ObjSpriteList2D_AddVertex(obj);
			}
		}
}