#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["MARI"]
#Title["Marisa Kirisame"]
#Text["Anti-Magic Type"]
#Image["./Face.png"]
#ReplayName["Marisa"]

#include "./../functions.txt"
#include "./Mari_Shot_Const.txt"

PlayerSpeed = [5,1.875];

let shotPrior = 0.36;

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

task TShot
{	
	let msPrc = 1;
	let msDmgM = 10;	
	let msDmg = msDmgM;

	let optionCount = 1;
	let pow = 0;

	let spread = [1,2];
	let counter = 0;
	let angle;
	let soundS = false;

	let angleMO = 0;

	ascent(i0 in 0..2)
		{			
		ascent(i1 in 0..2)
			{
			TOption(i0,i1*2-1);
			}
		}
	
	loop
		{
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{
			IsFocus = true;
			angleMO = rand(-20,5);
			}
		else
			{
			IsFocus = false;
			angleMO = rand(-5,15);
			}
		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..2)
						{
						ascent(i1 in 0..2)
							{
							i1 = i1*2-1;
							CreateMariMainShot(GetPlayerX+i1*7,GetPlayerY-12+i0*20,20);
							}
						}
					}
				IsShooting = true;
				}
			else
				{
				IsShooting = false;
				}
			counter+=1;
			}
		else if(counter%5 != 0)
			{
			counter+=1;
			IsShooting = false;
			}
		else
			{
			IsShooting = false;
			}
		yield;
		}

	task TOption(num,dir)
		{
		let scale = 14;

		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj,PlayerPath);
		ObjSpriteList2D_SetSourceRect(obj,144,144,160,160);

		let count = 12;
		let counter = 0;	
		let xP = GetPlayerX;
		let yP = GetPlayerY;
		let x = xP;
		let y = yP;
		let xA = 0;
		let yA = 0;

		let IsLaserActive = false;
		while(PlayerDead == false)
			{
			
			let xM = dir*(10+num*20);
			let yM = -(30.5-num*15)+3;
			if(IsFocus == true)
				{		
				xM *= 0.6;
				xM+=dir*1.5;
				yM *= 0.95;
				yM -= 3;
				}
			xA += (xM-xA)/3;
			yA += (yM-yA)/3;
			xP += (GetPlayerX-xP)/2;
			yP += (GetPlayerY-yP)/2;
			x = xP+xA;	
			y = yP+yA;
			ObjRender_SetPosition(obj,x,y,0);

			if(IsShooting == true)
				{
				if(counter%count == num*floor(count/2))
					{
					counter+=1;
					if(IsFocus == true)
						{
						CreateMariMissile(x,y,4);
						}
					}
				else if(counter%count != num*floor(count/2))
					{
					counter+=1;
					}	
				if(IsFocus == false && IsLaserActive == false)
					{
					TLaser;
					}
				}
			else if(counter%(60*count) != 0)
				{
				counter+=1;
				}

			local
				{
				ObjSpriteList2D_ClearVertexCount(obj);
				ObjSpriteList2D_SetDestCenter(obj);

				ObjRender_SetScaleXYZ(obj,scale*1.5/16,scale*1.5/16,0);
				ObjRender_SetAlpha(obj,125);
				Obj_SetRenderPriority(obj, 0.31);
				ObjSpriteList2D_AddVertex(obj);

				ObjRender_SetScaleXYZ(obj,scale/16,scale/16,0);
				ObjRender_SetAlpha(obj,255);
				Obj_SetRenderPriority(obj, 0.31);
				ObjSpriteList2D_AddVertex(obj);

				ObjSpriteList2D_CloseVertex(obj);
				}
			yield;
			}
		Obj_Delete(obj);
		
		task TLaser
			{	
			IsLaserActive = true;

			let angle = 270+dir*(2.5+num*8.5);

			let shot = ObjShot_Create(OBJ_STRAIGHT_LASER);
			ObjShot_Regist(shot);
			ObjShot_SetGraphic(shot,BLANK);
			ObjRender_SetBlendType(shot,BLEND_ALPHA);
			ObjShot_SetDamage(shot,25*100/60);
			ObjShot_SetAutoDelete(shot,false);
			ObjShot_SetPenetration(shot,10^10);
			ObjStLaser_SetSource(shot,false);
			Obj_SetRenderPriority(shot,shotPrior);
			ObjStLaser_SetAngle(shot,angle);

			let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
			ObjPrim_SetTexture(obj,PlayerPath);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			Obj_SetRenderPriority(obj, shotPrior);

			let l = 0;
			let wM = 24;
			let w = 0.1;

			let c = 0;
		
			while(w >= 0.025)
				{
				c+=1;
				if(l < GetStgFrameHeight*1.25)
					{
					l += GetStgFrameHeight*1.25/15;
					}
				if(IsFocus == false && IsShooting == true)
					{
					w += (1-w)/6;
					ObjShot_SetIntersectionEnable(shot,true);
					}
				else
					{
					w += (0-w)/3;
					ObjShot_SetIntersectionEnable(shot,false);
					}
				let IsIntersecting = false;
				let array = GetEnemyBossObjectID;
				ascent(i in 0..length(array))
					{
					let enm = array[i];
					let enmx = ObjMove_GetX(enm);
					let enmy = ObjMove_GetY(enm);
					if(IsIntersected_Line_Circle(x,y,x+l*cos(angle),y+l*sin(angle),wM,enmx,enmy,16))
						{
						IsIntersecting = true;
						}
					while(IsIntersected_Line_Circle(x,y,x+l*cos(angle),y+l*sin(angle),wM,enmx,enmy,14) && l > 0){l-=1;}
					}
				ObjLaser_SetLength(shot,l);
				ObjLaser_SetRenderWidth(shot,w*wM);
				ObjLaser_SetIntersectionWidth(shot,w*wM);
				ObjMove_SetPosition(shot,x,y);

				ObjSpriteList2D_ClearVertexCount(obj);

				ObjSpriteList2D_SetSourceRect(obj,-c*6,184-6,l-c*6,184+6);
				ObjRender_SetScaleXYZ(obj,1,w*wM*0.5/12,0);
				ObjRender_SetPosition(obj,x+l*0.5*cos(angle),y+l*0.5*sin(angle),0);
				ObjRender_SetAngleZ(obj,angle);	
				ObjRender_SetColorHSV(obj,0,0,255);	
				ObjSpriteList2D_SetDestCenter(obj);
				ObjSpriteList2D_AddVertex(obj);

				ascent(i in 0..2)
					{
					ObjSpriteList2D_SetSourceRect(obj,0,192,32,224);
					let scl = w*18*(1-i*0.15)/32;
					ObjRender_SetScaleXYZ(obj,scl,scl,0);
					ObjRender_SetPosition(obj,x-0.5,y-1,0);
					ObjRender_SetAngleZ(obj,(i*2-1)*c*(5+i*0.67));
					ObjRender_SetColorHSV(obj,300+60*cos(c*3),155,255);	
					ObjSpriteList2D_SetDestCenter(obj);
					ObjSpriteList2D_AddVertex(obj);
					}


				if(IsIntersecting == true && c%2 == 0)
					{
					TDel(floor(c/2)%3);
					}
				yield;			
				}
			Obj_Delete(shot);
			Obj_Delete(obj);
			IsLaserActive = false;

			task TDel(g)
				{
				if(IsPermitPlayerShot == false || Effect >= 2 || 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,10^10,SMALL[g]);
				ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	
				Obj_SetRenderPriority(obj,shotPrior);

				let scl = 1;	
				let alphaM = 255;
				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 CreatePlayerBulletA1(x,y,s,a,d,p,g)
	{
	alternative(g)
		case(MARI)
			{
			CreateMariMainShot(x,y,s);
			}
		case(MARI_DEL)
			{
			CreateMariMainDelete(x,y);
			}
		case(MARI_MISSILE)
			{
			CreateMariMissile(x,y,s);
			}
		case(DOT[SHOT_RED],DOT[SHOT_BLUE],DOT[SHOT_PURPLE],BLANK)
			{
			}
		others
			{
			CreateMariMissileSplash(x,y,g)
			}
	}
task CreateMariMainShot(x,y,speed)
	{
	if(IsPermitPlayerShot == false){return;}
	let angle = 270;
	
	let shot = CreatePlayerShotA1(x,y,speed,angle,85*0.25*5*100/60,1,MARI);
	ObjMove_SetAcceleration(shot,0.1);
	ObjMove_SetMaxSpeed(shot,100);
	ObjRender_SetAngleZ(shot,-90);

	let alpha = 155;	
	Obj_SetRenderPriority(shot,shotPrior);
	ObjRender_SetColorHSV(shot,0,0,155);
	ObjRender_SetAlpha(shot,alpha);
	
	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;
		}	
	CreateMariMainDelete(x,y);
	}
task CreateMariMainDelete(x,y)
	{
	if(IsPermitPlayerShot == false || Effect >= 2 || y < -32 || y > GetStgFrameHeight+32  || x < -32 || x > GetStgFrameWidth+32){return;}
	let obj = CreatePlayerShotA1(x,y,0,270,0,10^10,MARI_DEL);
	ObjRender_SetAngleZ(obj,-90);
	Obj_SetRenderPriority(obj,shotPrior);
	ObjRender_SetColorHSV(obj,0,0,155);
	let alphaM = 155;
	let alpha = alphaM;
	let scale = 1;	
	loop(15)
		{
		alpha-=alphaM/15;
		scale = scale+1/15;
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,scale,scale,0);
		yield;
		}
	Obj_Delete(obj);
	}
task CreateMariMissile(x,y,speed)
	{
	if(IsPermitPlayerShot == false){return;}

	let shot = CreatePlayerShotA1(x,y,speed,270,3*12*100/60,1,MARI_MISSILE);
	ObjRender_SetAngleZ(shot,-90);
	ObjMove_SetAcceleration(shot,1/6);
	ObjMove_SetMaxSpeed(shot,100);
	
	let alpha = 205;
	
	Obj_SetRenderPriority(shot,shotPrior);
	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);
		}
	
	}
task CreateMariMissileSplash(x,y,g)
	{
	if(IsPermitPlayerShot == false || y < -32 || y > GetStgFrameHeight+32  || x < -32 || x > GetStgFrameWidth+32){return;}
	let time = (8-(g-MARI_SPLASH))*2;
	let shot = CreatePlayerShotA1(x,y,0,0,6*(1/2)*(1/time)*12*100/60,10^10,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);
	}