#TouhouDanmakufu[Player]
#ScriptVersion[3]
#Title[""]
#Text["Made by Vigor[r]Flashbomb gives power[r]No deathbomb[r]Don't hesitate to bomb to[r]prevent death[r]Glitch is rare to occur"]
#Image["./Shadow.png"]
#ReplayName["Yago"]

let objPlayer = GetPlayerObjectID;
let objSlowShot = ID_INVALID;
let dir = GetCurrentScriptDirectory;
let sevol=80;
let death=false;
let spell=false;
let focus=false;

let fbmax=30;
let fbgauge=0;

let pmax=1000;

let defaultd;

@Initialize
{
	let path = dir ~ "VigorShot.txt";
	LoadPlayerShotData(path);
	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 2, 24);
	TImage;
	TShot;
	FlashGauge;
	PowerGauge;
	Hitbox;
	Bombing;
}

@MainLoop
{
	if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){focus=true;}
	if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL || GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE){focus=false;}
	if(GetPlayerPower<pmax/5){defaultd=1;}
	if(GetPlayerPower>=pmax/5){defaultd=2;}
	if(GetPlayerPower>=pmax/5*3){defaultd=3;}
	if(GetPlayerPower>=pmax){defaultd=4;}
	yield;
}

@Finalize
{
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
		SetScriptResult(true);
	}
	case(EV_HIT)
	{
		BarrierCrash(GetPlayerX,GetPlayerY);
	}
	case(EV_PLAYER_SHOOTDOWN)
	{
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,GetPlayerX,GetPlayerY,120);
		PowerAdd(-pmax/5);
		death=true;
	}
	case(EV_GRAZE)
	{
		fbgauge++;
		SEPlay(dir ~ "thrill.wav",60);
		if(fbgauge==fbmax){SEPlay(dir ~ "charge_up.wav",60);}
	}
	case(EV_PLAYER_REBIRTH)
	{
		SetPlayerSpell(3);
		SetPlayerInvincibilityFrame(180);
		death=false;
	}
}

task Bombing
{
	loop
	{
		if(GetVirtualKeyState(VK_BOMB) == KEY_PUSH || GetVirtualKeyState(VK_BOMB) == KEY_HOLD)
		{
			let bomb=GetPlayerSpell;
			if(focus==false && fbgauge>=fbmax)
			{
				SetScriptResult(true);
				FlashBomb(GetPlayerX);
				fbgauge=0;
				SetPlayerInvincibilityFrame(60);
			}
			if(focus==true && bomb>=1 && spell==false)
			{
				SetScriptResult(true);
				SpellAttack;SetPlayerSpell(bomb-1);
				spell=true;
				SetPlayerInvincibilityFrame(180);
			}
		}
		yield;
	}
}

task Hitbox
{
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj1, dir ~ "Charge.png");
	ObjSprite2D_SetSourceRect(obj1,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
	let c=0;
	let cc=0;
	let size1=6;
	let ag=rand(0,360);
	let delay=20;
	let lp=13;
	loop(lp)
	{
		SpinParticle(ag,36,-0.5,20,2);
		SpinParticle(ag,24,0.5,15,1);
		SpinParticle(ag,6,-1,10,1);
		ag+=360/lp;
	}
	loop
	{
		if(death==false && focus==true && c<1){c+=1/delay;}
		if(death==true || focus==false){if(c>0){c-=1/delay;}}
		if(death==false && cc<1){cc+=1/delay;}
		if(death==true && cc>0){cc-=1/delay;}
		ObjRender_SetAlpha(obj1,128*c);
		ObjRender_SetScaleXYZ(obj1,size1/256*c,size1/256*c,1);
		ObjRender_SetPosition(obj1,GetPlayerX,GetPlayerY,0);
		yield;
	}
	
	task SpinParticle(a,r,av,size3,mod)
	{
		let colorid=12;
		let objx = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objx, dir ~ "VigorBullet.png");
		ObjSprite2D_SetSourceRect(objx,32*colorid,128,32*(colorid+1),160);
		ObjSprite2D_SetDestCenter(objx);
		ObjRender_SetBlendType(objx,BLEND_ADD_ARGB);
		loop
		{
			if(mod==1)
			{
				ObjRender_SetScaleXYZ(objx,size3/32*c,size3/32*c,1);
				ObjRender_SetAlpha(objx,32*c);
				ObjRender_SetPosition(objx,GetPlayerX+r*c*sin(a),GetPlayerY+r*c*cos(a),0);
			}
			if(mod==2)
			{
				ObjRender_SetScaleXYZ(objx,size3/32*cc,size3/32*cc,1);
				ObjRender_SetAlpha(objx,64*cc);
				ObjRender_SetPosition(objx,GetPlayerX+(r+GetPlayerInvincibilityFrame/2)*cc*sin(a),GetPlayerY+(r+GetPlayerInvincibilityFrame/2)*cc*cos(a),0);
			}
			ObjRender_SetAngleZ(objx,-a+180);
			a+=av;
			yield;
		}
	}
}

task PowerGauge
{
	let path = dir ~ "Barrier.png";
	let objx = ObjPrim_Create(OBJ_SPRITE_2D);
	let objy = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj4 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objx, path);
	ObjPrim_SetTexture(objy, path);
	ObjPrim_SetTexture(obj1, path);
	ObjPrim_SetTexture(obj2, path);
	ObjPrim_SetTexture(obj3, path);
	ObjPrim_SetTexture(obj4, path);
	Obj_SetRenderPriority(objx, 1);
	Obj_SetRenderPriority(objy, 1);
	Obj_SetRenderPriority(obj1, 1);
	Obj_SetRenderPriority(obj2, 1);
	Obj_SetRenderPriority(obj3, 1);
	Obj_SetRenderPriority(obj4, 1);
	ObjRender_SetAngleZ(objx,90);
	ObjRender_SetAngleZ(objy,-90);
	ObjRender_SetAngleZ(obj1,-90);
	ObjRender_SetAngleZ(obj2,-90);
	ObjRender_SetAngleZ(obj3,-90);
	ObjRender_SetAngleZ(obj4,-90);
	ObjRender_SetPosition(objx,615,355,0);
	ObjRender_SetPosition(objy,615,355+0.67*256/2,0);
	ObjRender_SetPosition(obj1,615,355+0.67*256/2,0);
	ObjRender_SetPosition(obj2,615,355+0.67*256/2,0);
	ObjRender_SetPosition(obj3,615,355+0.67*256/2,0);
	ObjRender_SetPosition(obj4,615,355+0.67*256/2,0);
	ObjSprite2D_SetSourceRect(objx,0,0,256,32);
	ObjSprite2D_SetSourceRect(objy,0,0,256,32);
	ObjSprite2D_SetSourceRect(obj1,0,0,256,32);
	ObjSprite2D_SetSourceRect(obj2,0,0,256,32);
	ObjSprite2D_SetSourceRect(obj3,0,0,256,32);
	ObjSprite2D_SetSourceRect(obj4,0,0,256,32);
	ObjSprite2D_SetDestCenter(objx);
	ObjSprite2D_SetDestRect(objy,0,-16,256,16);
	ObjSprite2D_SetDestRect(obj1,0,-16,256,16);
	ObjSprite2D_SetDestRect(obj2,0,-16,256,16);
	ObjSprite2D_SetDestRect(obj3,0,-16,256,16);
	ObjSprite2D_SetDestRect(obj4,0,-16,256,16);
	ObjRender_SetScaleXYZ(objx,0.7,0.5,1);
	ObjRender_SetAlpha(objx,128);
	ObjRender_SetColor(objx,0,0,0);
	let whiten=false;
	let c=0;
	let r=255;
	let g=255;
	let b=255;
	let al=192;
	let alp=96;
	loop
	{
		if(GetPlayerPower<0){SetPlayerPower(0);}
		if(GetPlayerPower>=pmax){SetPlayerPower(pmax);}
		if(GetPlayerPower>=pmax){whiten=true;}
		if(GetPlayerPower<pmax){whiten=false;}
		if(whiten==true && c<1){c+=1/5;}
		if(whiten==false && c>0){c-=1/5;}
		ObjRender_SetColor(objy,r,g*c,b*c);
		ObjRender_SetScaleXYZ(objy,0.67*GetPlayerPower/pmax,0.3,1);
		ObjRender_SetScaleXYZ(obj1,0.67*(pmax/5)/pmax,0.3,1);
		ObjRender_SetScaleXYZ(obj2,0.67*(pmax/5*2)/pmax,0.3,1);
		ObjRender_SetScaleXYZ(obj3,0.67*(pmax/5*3)/pmax,0.3,1);
		ObjRender_SetScaleXYZ(obj4,0.67*(pmax/5*4)/pmax,0.3,1);
		ObjRender_SetAlpha(objy,al);
		ObjRender_SetAlpha(obj1,al/4);
		ObjRender_SetAlpha(obj2,al/4);
		ObjRender_SetAlpha(obj3,al/4);
		ObjRender_SetAlpha(obj4,al/4);
		yield;
	}
}

task FlashGauge
{
	let path = dir ~ "Barrier.png";
	let objx = ObjPrim_Create(OBJ_SPRITE_2D);
	let objy = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objx, path);
	ObjPrim_SetTexture(objy, path);
	Obj_SetRenderPriority(objx, 1);
	Obj_SetRenderPriority(objy, 1);
	ObjRender_SetAngleZ(objx,90);
	ObjRender_SetAngleZ(objy,-90);
	ObjRender_SetPosition(objx,580,355,0);
	ObjRender_SetPosition(objy,580,355+0.67*256/2,0);
	ObjSprite2D_SetSourceRect(objx,0,0,256,32);
	ObjSprite2D_SetSourceRect(objy,0,0,256,32);
	ObjSprite2D_SetDestCenter(objx);
	ObjSprite2D_SetDestRect(objy,0,-16,256,16);
	ObjRender_SetScaleXYZ(objx,0.7,0.5,1);
	ObjRender_SetAlpha(objx,128);
	let rainbow=false;
	let c=0;
	let r=255;
	let g=0;
	let b=0;
	let al=192;
	let alp=96;
	loop
	{
		if(fbgauge<0){fbgauge=0;}
		if(fbgauge>=fbmax){fbgauge=fbmax;}
		if(fbgauge>=fbmax){rainbow=true;}
		if(fbgauge<fbmax){rainbow=false;}
		if(rainbow==true && c<1){c+=1/5;}
		if(rainbow==false && c>0){c-=1/5;}
		if(r>0 && g<255 && b==0){r-=5;g+=5;}
		if(g>0 && b<255 && r==0){g-=5;b+=5;}
		if(b>0 && r<255 && g==0){b-=5;r+=5;}
		ObjRender_SetColor(objy,r*c,g*c,b*c);
		ObjRender_SetScaleXYZ(objy,0.67*fbgauge/fbmax,0.3,1);
		ObjRender_SetAlpha(objy,al);
		if(rainbow==true){al-=alp;alp*=-1;}
		if(rainbow==false){al=192;alp=96;}
		yield;
	}
}

task TImage
{
	let path = dir~"dot_yago.png";
	ObjPrim_SetTexture(objPlayer, path);
	ObjSprite2D_SetSourceRect(objPlayer, 0, 0, 32, 64);
	ObjSprite2D_SetDestCenter(objPlayer);
	let cidle=0;
	let ccidle=1;
	let dimy=1;
	let anim=5;
	let idle=true;
	let wait=false;
	let end=false;
	loop
	{
		if(idle==false && wait==false && end==false){ccidle=-1;end=true;}
		if(cidle<2 && idle==false && end==true){idle=true;cidle=0;ccidle=1;dimy=1;end=false;}
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){if(idle==true && end==false){idle=false;dimy=2;cidle=0;}}
		if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){if(idle==true && end==false){idle=false;dimy=3;cidle=0;}}
		cidle+=ccidle;
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){wait=true;if(dimy==3){wait=false;}}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){wait=true;if(dimy==2){wait=false;}}
		else{wait=false;}
		if(cidle>=8 && idle==false && ccidle==1 && end==false){ccidle=-1;}
		if(cidle<=5 && idle==false && ccidle==-1 && end==false){ccidle=1;}
		if(cidle>8 && idle==true && end==false){cidle=0;}
		loop(anim)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 32*(cidle-1), 64*(dimy-1), 32*cidle, 64*dimy);
			yield;
		}
	}
}

task TShot
{
	let anim=5;
	let a=180;
	let aa=30;
	let c=0;
	let cc=1;
	let ccc=1;
	loop
	{
		loop(anim)
		{
			if(focus==true && c>0.4){c-=1/60;}
			if(focus==false && c<1){c+=1/60;}
			if(cc>=1){ccc=-1;}
			if(cc<=0.2){ccc=1;}
			cc+=ccc/180;
			yield;
		}
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD && IsPermitPlayerShot==true && death==false && spell==false)
		{
			if(GetPlayerPower>=pmax/5*0)
			{
				KnifeShot(GetPlayerX,GetPlayerY,1,1,18,a+(aa/2*(3+c*2))*rand(0.8,1.2)*c*cc,1.5,1,10,30,20,60-30*cc, 1);
				KnifeShot(GetPlayerX,GetPlayerY,1,1,18,a-(aa/2*(3+c*2))*rand(0.8,1.2)*c*cc,1.5,1,10,30,20,60-30*cc,-1);
			}
			if(GetPlayerPower>=pmax/5*2)
			{
				KnifeShot(GetPlayerX,GetPlayerY,1,1,18,a+(aa/2*3)*rand(0.8,1.2)*c*cc      ,1.5,1,10,30,25,60,       1);
				KnifeShot(GetPlayerX,GetPlayerY,1,1,18,a-(aa/2*3)*rand(0.8,1.2)*c*cc      ,1.5,1,10,30,25,60,      -1);
			}
			if(GetPlayerPower>=pmax/5*4)
			{
				KnifeShot(GetPlayerX,GetPlayerY,1,1,18,a+(aa/2*(3-c*2))*rand(0.8,1.2)*c*cc,1.5,1,10,30,30,60-30*cc, 1);
				KnifeShot(GetPlayerX,GetPlayerY,1,1,18,a-(aa/2*(3-c*2))*rand(0.8,1.2)*c*cc,1.5,1,10,30,30,60+30*cc,-1);
			}
		}
	}
}

task KnifeShot(x,y,rx,ry,s,a,d,p,w,t,wt,av,m)
{
	let st=212;
	if(GetPlayerPower>=pmax/5){st=213;}
	if(GetPlayerPower>=pmax/5*3){st=214;}
	if(GetPlayerPower>=pmax){st=215;}
	let ao=atan2(y+rx*cos(a)-y,x+ry*sin(a)-x);
	let obj = CreateShotA1(x+rx*sin(a),y+ry*cos(a),0,ao,st,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjShot_SetAutoDelete(obj,false);
	ObjShot_SetDamage(obj,defaultd);
	ObjShot_SetPenetration(obj,p);
	let al=0;
	let xyz=0;
	loop(w)
	{
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetScaleXYZ(obj,xyz,xyz,0);
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+s/w);
		al+=128/w;
		xyz+=1/w;
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "j_shot_01.wav",60);}
	ObjRender_SetAlpha(obj,128);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjMove_SetSpeed(obj,s);
	ObjShot_SetIntersectionEnable(obj,true);
	loop(wt)
	{
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-s/wt);
		yield;
	}
	ObjShot_SetAutoDelete(obj,true);
	ObjMove_SetAngle(obj,ao+av*m);
	let xx=0;
	let yy=0;
	while(!Obj_IsDeleted(obj))
	{
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+s/t);
		yield;
	}
	if(xx>=0 && xx<=GetStgFrameWidth && yy>=0 && yy<=GetStgFrameHeight){AddScore(1);SEPlay(dir ~ "damage_01.wav",60);}
}

task FlashBomb(x)
{
	PowerAdd(pmax/10);
	let lp=4;
	let d=10;
	let t=10;
	ThinLaser(x,GetStgFrameHeight/2+240,-90,d,t);
	let aa=4;
	loop(lp)
	{
		loop(5){yield;}
		d+=5;
		t+=5;
		ThinLaser(x+aa,GetStgFrameHeight/2+240,-90,d,t);
		ThinLaser(x-aa,GetStgFrameHeight/2+240,-90,d,t);
		aa+=4;
	}
}

task ThinLaser(x,y,a,d,t)
{
	let obj = CreateStraightLaserA1(x,y,a,0,32,10,235,d);
	ObjShot_SetDamage(obj,0);
	ObjShot_SetPenetration(obj,1000);
	let c=0;
	loop(d)
	{
		c+=1/d;
		ObjShot_SetDamage(obj,(defaultd*2)*c);
		ObjLaser_SetLength(obj,480*c);
		let xc=0;
		loop(15)
		{
			DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,x,y-480*xc,8);
			xc+=1/15;
		}
		yield;
	}
	SEPlay(dir ~ "laser_3.wav",60);
	loop(t)
	{
		c-=1/t;
		ObjShot_SetDamage(obj,(defaultd*2)*c);
		ObjLaser_SetLength(obj,480*c);
		let xc=0;
		loop(15)
		{
			DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,x,y-480*xc,8);
			xc+=1/15;
		}
		yield;
	}
	Obj_Delete(obj);
}

task SpellAttack
{
	let xx=GetPlayerX;
	let yy=GetPlayerY;
	let aa=0;
	let m=truncate(rand(-4,4));
	if(m==0){m=1;}
	let lp=truncate(rand(3,6));
	loop(lp)
	{
		StarLaser(xx,yy,4.8,180,aa,3,1,m);
		StarLaser(xx,yy,4.8,180,aa,3,-1,m);
		aa+=360/lp;
	}
	loop(120){yield;}
	spell=false;
}

task StarLaser(x,y,s,a,aa,av,m,mm)
{
	let d=60;
	let dt=60;
	let ro=0;
	let rr=480;
	let rmax=16;
	let fade=15;
	let obj = CreateStraightLaserA1(x+ro*sin(a),y+ro*cos(a),a,0,32,dt,235,d);
	let c=0;
	let cc=0;
	let count=0;
	let al=256;
	loop(d+dt)
	{
		count++;
		if(count<d){c+=1/d;cc+=1/d;if(al<256){al+=256/fade;}}
		if(count==d){SEPlay(dir ~ "laser_3.wav",60);}
		if(count>=d){c-=1/dt;}
		if(count>d+dt-fade){al-=256/fade;}
		ro+=s*c;
		aa+=av*mm*c;
		ObjRender_SetAlpha(obj,al);
		ObjShot_SetDamage(obj,(defaultd*3)*c);
		ObjLaser_SetLength(obj,rr*c);
		ObjMove_SetPosition(obj,x+ro*sin(a)+rmax*cc*sin(aa),y+ro*cos(a)+rmax*cc*cos(aa));
		ObjStLaser_SetAngle(obj,-aa+90+90*m);
		let ang = ObjStLaser_GetAngle(obj);
		let xx = ObjMove_GetX(obj);
		let yy = ObjMove_GetY(obj);
		let xc = 0;
		loop(10)
		{
			DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,xx+160*sin(ang),yy+160*cos(ang),16);
			xc+=1/10;
		}
		yield;
	}
	Obj_Delete(obj);
	
}

task BarrierCrash(x,y)
{
	let ag=rand(0,360);
	loop(10)
	{
		crashpiece(x,y,ag,1);
		crashpiece(x,y,ag+12,0.5);
		crashpiece(x,y,ag-12,0.5);
		ag+=36;
	}
	SEPlay(dir~"j_barrier.wav",240);
	task crashpiece(x,y,a,scale)
	{
		let path = dir~"Barrier.png";
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(obj, 0.60);
		ObjPrim_SetTexture(obj, path);
		ObjSprite2D_SetSourceRect(obj, 0, 0, 128, 128);
		ObjSprite2D_SetDestCenter(obj);
		let xyz=rand(1/8,1/16);
		ObjRender_SetScaleXYZ(obj,xyz,1/8-xyz,1);
		let c=0;
		let r=0;
		let time=60;
		let rr=rand(1,2)*scale;
		let ax=rand(0,360);
		let axv=rand(4,8);
		let ay=rand(0,360);
		let ayv=rand(4,8);
		let az=rand(0,360);
		let azv=rand(4,8);
		loop(time/2)
		{
			c+=1/(time/2);
			r+=rr;
			ax+=axv*c;
			ay+=ayv*c;
			az+=azv*c;
			ObjRender_SetAlpha(obj,128*c);
			ObjRender_SetPosition(obj,x+r*sin(a),y+r*cos(a),0);
			ObjRender_SetAngleXYZ(obj,ax,ay,az);
			yield;
		}
		loop(time/2)
		{
			c-=1/(time/2);
			r+=rr;
			ax+=axv*c;
			ay+=ayv*c;
			az+=azv*c;
			ObjRender_SetAlpha(obj,128*c);
			ObjRender_SetPosition(obj,x+r*sin(a),y+r*cos(a),0);
			ObjRender_SetAngleXYZ(obj,ax,ay,az);
			yield;
		}
		Obj_Delete(obj);
	}
}

task SEPlay(path,duration)
{
	let seobj=ObjSound_Create;
	ObjSound_Load(seobj,path);
	ObjSound_SetRestartEnable(seobj,false);
	ObjSound_SetLoopEnable(seobj,false);
	ObjSound_SetVolumeRate(seobj,sevol);
	ObjSound_Play(seobj);
	loop(duration){yield;}
	Obj_Delete(seobj);
}

task PowerAdd(score)
{
	let p=GetPlayerPower;
	loop(30)
	{
		SetPlayerPower(p+score/30);
		yield;
	}
	SetPlayerPower(p+score);
}