#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Afro non-spell 03 hard"]
#Text["Afro non-spell 03 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(2);
	setAura(bossObj,64,64,255);
	SetCommonData("bossAttackType",1);
	SetCommonData("bossCircleType",0);
	setInvulnerableTime(bossObj,180);	
	
	ObjMove_SetDestAtSpeed(bossObj,192,120,5);
	SetShotAutoDeleteClip(32,32,32,32);
	SetAutoDeleteObject(true);
	mainTask;
} 

@Event { 
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(270);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(52);
	}
} 

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;	
	wait(120);
	vuur;
	beweeg;
} 

// Movement control
task beweeg { 
	wait(30);	
	while(!Obj_IsDeleted(bossObj)) {
		ObjMove_RandomPosWeight(bossObj,rand_int(30,60),rand_int(10,30),10,30,GetClipMinX+96,GetClipMinY+64,GetClipMaxX-96,GetCenterY);
		wait(120);
		ObjMove_SetDestAtWeight(bossObj,GetPlayerX,60,15,30);
		wait(120);
	}
}

// Main pattern 
task vuur { 
	let dir = 0;
	while(!Obj_IsDeleted(bossObj)) {
		ascent(i in 0..36) {
			PlaySFX(SFX_LAZER01);
			let funkyLaser00 = CreateLooseLaserA1(GetEnemyX+60*cos(dir),GetEnemyY+60*sin(dir),4,dir,300,10,4,0); 
			funkyLaserHandle(funkyLaser00,152,4,0); 
			let effb = CreateShotA1(GetEnemyX+60*cos(dir),GetEnemyY+60*sin(dir),0,0,152,0); 
			ObjShot_SetDeleteFrame(effb,60);			
			dir+=360/36;
			wait(3);
		}
		ascent(i in 0..36) { 
			PlaySFX(SFX_LAZER01);
			let funkyLaser01 = CreateLooseLaserA1(GetEnemyX+60*cos(dir),GetEnemyY+60*sin(dir),4,dir,300,10,7,0); 
			funkyLaserHandle(funkyLaser01,155,7,0); 
			let effb = CreateShotA1(GetEnemyX+60*cos(dir),GetEnemyY+60*sin(dir),0,0,155,0); 
			ObjShot_SetDeleteFrame(effb,60);					
			dir+=360/36;
			wait(3);
		}
	}
}

// Reflecting laser
task funkyLaserHandle(obj,delsource,graphic,bounce) {
	let x = 0;
	let y = 0;
	let dir = 0;
	let NotYetBounced = true;
	ObjStLaser_SetSource(obj,true);
	
	while(!Obj_IsDeleted(obj)) {
		x = ObjMove_GetX(obj);
		y = ObjMove_GetY(obj);
		dir = ObjMove_GetAngle(obj);
		
		// bounce code
		if(bounce < 1) {
			
			// ceiling reflection
			if(y <= GetClipMinY) {
				CreateLooseLaserA1(x,y,4,-ObjMove_GetAngle(obj),300,10,graphic,0); 
				let effb = CreateShotA1(x,y,0,0,delsource,0); 
				ObjShot_SetDeleteFrame(effb,60);
				PlaySFX(SFX_KIRA00);
				bounce++;
				break;
			} 
			
			// wall reflection
			if(((dir < -90 || 90 < dir) && x < GetClipMinX)) {
				CreateLooseLaserA1(x,y,4,180 - ObjMove_GetAngle(obj),300,10,graphic,0); 
				PlaySFX(SFX_KIRA00);
				let effb = CreateShotA1(x,y,0,0,delsource,0); 
				ObjShot_SetDeleteFrame(effb,60);				
				bounce++;
				break;
			}
			
			if(((dir < -90 || 90 < dir) && x > GetClipMaxX)) {
				CreateLooseLaserA1(x,y,4,180 - ObjMove_GetAngle(obj),300,10,graphic,0); 
				PlaySFX(SFX_KIRA00);
				let effb = CreateShotA1(x,y,0,0,delsource,0); 
				ObjShot_SetDeleteFrame(effb,60);				
				bounce++;
				break;
			}
		}	
		yield;
	}
}

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

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

	CloseScript(GetOwnScriptID());
}
 