#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["S4"]
#Text["."]
#System["./System.txt"]
#include"./Functions/Index.txt"

let objScene=GetEnemyBossSceneObjectID();
let objBoss;
let frame=0;
let count=0;
let turn=0;
let time=0;
let waterpos;
let bsignal=0;
let charge;
let wsignal=2;
let timeSignal=1;
let GCSD=GetCurrentScriptDirectory~"";
let plusangle=-90;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(1500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(50)
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(10000)
	}
}

@MainLoop{
	frame++;
	plusangle++;
	if(ObjMove_GetSpeed(objBoss)>0&&timeSignal!=1){
		wsignal=1;
	}else{
		wsignal=0;
	}
	if(wsignal!=1){
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
		ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	}
	yield;
	
}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	TEnemyHitboxStart(objBoss,150);
	wait(30);
	PlaySound(objBoss,23,100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	Cutin(objBoss,"Water Mud Sign \"Remotely Organised Explosive\"");
	TShoot01;
	TShoot02;
	TChange;
	TBubble;
	TMovement;
	TWater;
	TEnd;
	yield;
}

task TMovement{
	wait(150);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let resultx=rand(0,1);
		let resulty=rand(0,1);
		if(resultx==0.5||resulty==0.5){
			break;
			TMovement;
		}
		resultx=round(resultx);
		resulty=round(resulty);
		let posx=rand(50,-50);
		if(posx<=10&&posx>=-10){
			if(resultx==1){
				posx=15;
			}else{
				posx=-15;
			}
		}
		if((ObjMove_GetX(objBoss)+posx)<=32||(ObjMove_GetX(objBoss)+posx)>=(GetStgFrameWidth()-32)){
			posx=-posx;
		}
		
		let posy=rand(20,-20);
		if(posy<=7.5&&posy>=-7.5){
			if(resulty==1){
				posy=10;
			}else{
				posy=-10;
			}
		}
		if((ObjMove_GetY(objBoss)+posy)<=32||(ObjMove_GetY(objBoss)+posy)>=GetStgFrameHeight()/3){
			posy=-posy;
		}
		ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss)+posx,ObjMove_GetY(objBoss)+posy,15,5);
		wait(135);
	}
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),10,0.5);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		loop(120){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TWater{
	while(ObjMove_GetSpeed(objBoss)>0){ yield; }
	timeSignal=2;
	TWater2;
	task TWater2{
		waterpos=TWaterHide(objBoss,"LARGE",true);
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
		ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
		PlaySoundNR(0,100,0);
		while(!Obj_IsDeleted(waterpos)&&wsignal==1){
			ObjRender_SetPosition(waterpos,ObjRender_GetX(objBoss),ObjRender_GetY(objBoss),0);
			yield;
		}
		Obj_Delete(waterpos);
		ObjRender_SetAlpha(objBoss,255);
		loop(5){
			TCallBubbles(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
		}
		PlaySoundNR(0,100,0);
		while(wsignal!=1){ yield; }
		TWater2;
	}
}

task TShoot01{
	wait(120);
	while(!Obj_IsDeleted(objBoss)){
		let obj=TShot01(0,GetStgFrameWidth,0);
		PlaySound(objBoss,7,100,0);
		wait(35);
		let obj2=TShot01(GetStgFrameWidth,0,180);
		PlaySound(objBoss,7,100,0);
		wait(35);
	}
	function TShot01(x,s,a){
		let y=rand(50,GetStgFrameWidth/2);
		let yp=0;
		let bullets=[];
		let obj=CreateShotA1(x,y,1.75,a,G_LARGE_BLUE,0);
		TTrail;
		let ys=rand(5,50);
		TAction;
		return obj;
		task TAction{
		while(!Obj_IsDeleted(obj)){
			ObjMove_SetY(obj,y+sin(yp)*ys);
			yp+=3;
			if(bsignal==1){
				ascent(i in 0..360){
					if(ObjMove_GetX(obj)<(ObjMove_GetX(objBoss)+160*cos(i))&&ObjMove_GetX(obj)>(ObjMove_GetX(objBoss)-160*cos(i))){
						if(ObjMove_GetY(obj)<(ObjMove_GetY(objBoss)+160*sin(i))&&ObjMove_GetY(obj)>(ObjMove_GetY(objBoss)-160*sin(i))){
							if(ObjMove_GetX(obj)==0&&ObjMove_GetY(obj)==0){
								Obj_Delete(obj);
								break;
							}
							loop(round(rand(1,2))){
								let s=rand(0.5,2);
								let obj2=CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),s*4,rand(0,360),-s/6,0,199,5);
								bullets=bullets~[obj2];
							}
							TCheckHp(objBoss);
							TExplosionA(ObjMove_GetX(obj),ObjMove_GetY(obj),15,0.3);
							Obj_Delete(obj);
							let bullets2=[];
							ascent(i in 0..length(bullets)){
								while(ObjMove_GetSpeed(bullets[i])>0){ yield; }
								if(!Obj_IsDeleted(bullets[i])){
									let angle=0;
									let rAngle=rand(0,360);
									while(angle<360){
										let obj2=CreateShotA2(ObjMove_GetX(bullets[i]),ObjMove_GetY(bullets[i]),5,rAngle+angle,-rand(0.3,0.6),0,189,5);
										bullets2=bullets2~[obj2];
										angle+=360/3;
									}
								}
								Obj_Delete(bullets[i]);
							}
							let bullets3=[];
							ascent(i in 0..length(bullets2)){
								while(ObjMove_GetSpeed(bullets2[i])>0){ yield; }
								if(!Obj_IsDeleted(bullets2[i])){
									let sAngle=0;
									let angle=0;
									while(angle<360){
										let obj2=CreateShotA2(ObjMove_GetX(bullets2[i]),ObjMove_GetY(bullets2[i]),5,sAngle*90+GetAngleToPlayer(objBoss)+angle,-0.2,0,190,5);
										bullets3=bullets3~[obj2];
										angle+=360/3;
									}
									sAngle++;
								}
								ObjMove_SetAngle(bullets2[i],GetAngleToPlayer(bullets2[i]));
								ObjMove_SetAcceleration(bullets2[i],0.05);
								ObjMove_SetMaxSpeed(bullets2[i],3);
							}
							ascent(i in 0..length(bullets3)){
								while(ObjMove_GetSpeed(bullets3[i])>0){ yield; }
								if(!Obj_IsDeleted(bullets3[i])){
									let sAngle=0;
									let angle=0;
									while(angle<360){
										CreateShotA2(ObjMove_GetX(bullets3[i]),ObjMove_GetY(bullets3[i]),5,sAngle*90+GetAngleToPlayer(objBoss)+angle,-0.2,1,189,5);
										angle+=360/3;
									}
									sAngle++;
								}
								ObjMove_SetAngle(bullets3[i],rand(0,360));
								ObjMove_SetAcceleration(bullets3[i],0.0075);
								ObjMove_SetMaxSpeed(bullets3[i],1.5);
							}
						}
					}
				}
			}
			if(s==0){
				if(ObjMove_GetX(obj)<=0){
					Obj_Delete(obj);
					break;
				}
			}
			if(s==GetStgFrameWidth){
				if(ObjMove_GetX(obj)>=GetStgFrameWidth){
					Obj_Delete(obj);
					break;
				}
			}
			if(ObjMove_GetX(obj)==0&&ObjMove_GetY(obj)==0){
					Obj_Delete(obj);
					break;
			}
			yield;
		}
		}
		task TTrail{
			while(!Obj_IsDeleted(obj)){
				TBullet;
				yield;
			}
			task TBullet{
				let obj2=CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0,0,195,30);
				wait(30);
				Obj_Delete(obj2);
			}
		}
	}
}

task TShoot02{
	wait(120);
	let bullets=[1,2];
	while(!Obj_IsDeleted(objBoss)){
		if(bsignal==0){
			let angle=0;
			while(angle<360){
				descent(i in 0..length(bullets)){
					CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),5,angle+GetAngleToPlayer(objBoss),-0.075,bullets[i],68,15);
				}
				angle+=360/10;
			}
		}
		PlaySound(objBoss,12,100,0);
		wait(60);
	}
}

task TChange{
	wait(240);
	while(!Obj_IsDeleted(objBoss)){
		TCheckHp(objBoss);
		PlaySound(objBoss,22,100,0);
		TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
		wait(60);
		bsignal=1;
		PlaySound(objBoss,13,100,0);
		PlaySound(objBoss,16,100,0);
		wait(5);
		bsignal=0;
		wait(150);
	}
}

task TBubble{
	wait(75);
	let x;
	let y;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/charge.png";
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0,0,0);
	ObjRender_SetPosition(obj,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	ObjRender_SetAlpha(obj,96);
	TMove;
	let scale=0;
	while(ObjRender_GetScaleX(obj)<2.5){
		ObjRender_SetScaleXYZ(obj,sin(scale)*2.5,sin(scale)*2.5,0);
		scale+=3;
		yield;
	}
	while(!Obj_IsDeleted(objBoss)){
		x=ObjMove_GetX(objBoss);
		y=ObjMove_GetY(objBoss);
		yield;
	}
	let alpha=128;
	scale=2.5;
	ObjRender_SetPosition(obj,x,y,0);
	loop(30){
		alpha-=64/30;
		scale-=2.5/30;
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,scale,scale,0);
		yield;
	}
	Obj_Delete(obj);
	task TMove{
		while(!Obj_IsDeleted(objBoss)){
			ObjRender_SetPosition(obj,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
			yield;
		}
	}
}