#include "./default_system/Default_Effect.txt"
#include "./default_system/AllStarShot_Const.txt"
#include"./functions.txt"
#include "./cutin.txt"
let objScene = GetEnemyBossSceneObjectID();
@Initialize {
	// define a boss in bossObj and register it
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	ObjEnemyBossScene_StartSpell(objScene);
	
	// move boss to desired x y location at desired speed
	ObjMove_SetDestAtSpeed(bossObj, 192,100,3);
	

	mainTask;	// run mainTask
	
} 

@Event { 
	// setting the boss timer and life
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(3500);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(46);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(30000);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
	SetScriptResult(true);
	}
} 

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	// collision for the shots and player
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,10);
	yield;
} 

@Finalize {
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)
        +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
		AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
	}

} 

task mainTask { 
	RenderBoss;
	movement;
	CutinA1("Flying Sign \"Air Slash\"",20);
	setOverlay;
	TEnd;
	yield;
} 

//ObjMove_SetDestAtSpeed(bossObj,x,y,speed);		

task movement {
	wait(180);
	loop{
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0) {return;}
		Fire;
		wait(240);
		Circle(786,36);
		wait(60);
	}
}
task Fire{
	let angle;
	angle=rand(0,360);
	SE_Play(laserSFX,40);
	loop(6){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0) {return;}
		Laser(angle);
		angle+=360/6;
	}
}

task Laser(angle){
	let shot = CreateLooseLaserA1(bossX,bossY, 4, angle, 150, 24, 1022, 0);
	let angle2;
	wait(5);
	while(ObjMove_GetX(shot)<GetStgFrameWidth && ObjMove_GetX(shot)>0 && ObjMove_GetY(shot)<GetStgFrameHeight && ObjMove_GetY(shot)>0){
		angle2 = rand(0,360);
		loop(3){
			SE_Play(shoot2,60);
			Aftershot(shot,angle2);
			angle2+=360/3;
		}
		wait(5);
		}
}
task Retaliation{
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){
	if(ObjEnemy_GetInfo(bossObj,INFO_SHOT_HIT_COUNT)>0){
		SE_Play(shoot,60);
		laserAngle;
		}
	wait(5);
	}
}
task laserAngle{
	let randomX = bossX+rand(-50,50);
	let randomY = bossY+rand(-50,50);
	let angle = atan2(GetPlayerY-randomY, GetPlayerX-randomX);
	let shot = CreateShotA1(randomX,randomY,rand(2,3),angle,round(rand(510,512)),0);
	yield;
}
task Aftershot(ID,angle){
	let shot = CreateShotA1(ObjMove_GetX(ID)+6*cos(angle),ObjMove_GetY(ID)+6*sin(angle),1,angle,214,15);
	
}

task Circle(let graphic,let amount){
	let angleX = rand(0,360);
	SE_Play(shoot,45);
	loop(amount){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0) {return;}
		CreateShotA1(bossX,bossY,1.5,angleX,graphic,0);
		angleX+=360/amount ;
	}
}