#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["UFO non-spell 01 hard"]
#Text["UFO non-spell 01 hard"]

#include "script/thdcs/system/shotlibrary.txt"
#include "script/thdcs/functions/function_common.txt"
#include "script/thdcs/functions/function_global.txt"
#include "script/thdcs/functions/function_bosseff.txt"

@Initialize {
	renderBoss(22);

	SetCommonData("bossCircleType",0);
	SetCommonData("bossAttackType",1);
	setInvulnerableTime(bossObj,360);
	
	ObjMove_SetDestAtSpeed(bossObj,192,GetCenterY-128,3);
	SetShotAutoDeleteClip(16,16,16,16);
	SetAutoDeleteObject(true);
	mainTask;
} 

@Event { 
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(260);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(43);
	}
} 

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;	
	PlaySFX(SFX_UFO);
	ufoSFX;
	fire;
	movement;
	wait(60);
	fire2;
	fire3;
	itazurana;
} 

task itazurana {
	while(!Obj_IsDeleted(bossObj)) {
		if(GetPlayerY < bossY-16) { 
			CreateStraightLaserA1(bossX-48,bossY,GetAngleToPlayer(bossObj),512,16,10,2,10);
			CreateStraightLaserA1(bossX+48,bossY,GetAngleToPlayer(bossObj),512,16,10,2,10);
			wait(5);
		}
		yield;
	}
}

task ufoSFX { 
	loop(6) { 
		PlaySFX(SFX_UFOALERT);
		wait(30);
	}
}

// movement
task movement { 
	while(!Obj_IsDeleted(bossObj)) {
		ObjMove_SetDestAtSpeed(bossObj,GetPlayerX,GetCenterY+rand_int(-32,-160),1);
		wait(90);
	}
}

// laser fire shot
task fire {
	let posX = [[-48,120],[-24,110],[-12,100],[0,90],[12,80],[24,70],[48,60]];
	let posPick = 0;
	let posSwitch = false;
	while(!Obj_IsDeleted(bossObj)) {
		PlaySFX(SFX_LAZER01);
		let laserShot00 = CreateStraightLaserA1(bossX+posX[posPick][0],bossY+38,posX[posPick][1],512,16,10,3,15);
		ObjMove_SetPosition(laserShot00,ObjMove_GetX(bossObj)+posX[posPick][0],ObjMove_GetY(bossObj)+38);
		if(posPick < 7 && !posSwitch) { posPick++; if(posPick == 7) { posSwitch = true; } } 		
		if(posPick > 0 && posSwitch) { posPick--; if(posPick == 0) { posSwitch = false; } } 		
		wait(20);
	}
}

// radio wave shot
task fire2 {
	let dir = 45; 
	while(!Obj_IsDeleted(bossObj)) {
		PlaySFX(SFX_KIRA00);
		ascent(i in 0..12) { 
			CreateShotA1(bossX+64*cos(dir),bossY+64*sin(dir),2.5,dir,41,15);
			dir+=90/12;
		}
		dir = rand_int(42,48);
		wait(20);
	}
}

task fire3 {
	let dir = 270-45;
	while(!Obj_IsDeleted(bossObj)) {
		PlaySFX(SFX_TAN01);
		ascent(i in 0..8) { 
			ufoSeeker(bossX,bossY-32,2,dir,185,10);
			ufoSeeker(bossX,bossY-32,2.5,dir,187,10);
			ufoSeeker(bossX,bossY-32,3,dir,184,10);
			dir+=90/8;
			wait(1);
		}
		dir = 270-45;
		wait(120);
	}
}

// special aimed bullet shot
task ufoSeeker(x,y,v,dir,graphic,delay) {
	let obj = ObjShot_Create(OBJ_SHOT);

	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,v);
	ObjMove_SetAngle(obj,dir);
	ObjShot_SetGraphic(obj,graphic);
	ObjShot_SetDelay(obj,delay);
	
	wait(45);
	ObjMove_SetAngle(obj,GetPointToPointAngle(GetPlayerX,GetPlayerY,ObjMove_GetX(obj),ObjMove_GetY(obj)));
	
}

task scriptEnding {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}

	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(bossObj);

	CloseScript(GetOwnScriptID());
}
 