
#Title["NonSpell 2"]
#Text["2nd NonSpell Attack"]
#include"./Index.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let AnimFrame = 0;
let AnimFrame2 = 0;

let imgBoss = CSD ~ "/Texture/Beatrix.png";
let AttackSFX = CSD ~ "/SFX/TWINKLE.wav";

@Initialize {

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	ObjMove_SetPosition(bossObj,190,-100);
	ObjMove_SetDestAtWeight(bossObj,190,120,8,5);
	
	LoadSound(AttackSFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(3500);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(50);
	}	
 }

@MainLoop {
	
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,15);
	
	yield;
 }

@Finalize {

 }

task mainTask{
	renderBoss;
	callbackboss;
	TEnd;
	loop{
		Fire;
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),rand(90,131),120);
		wait(120);
		Fire2;
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),rand(90,131),120);
		wait(120);
		yield;
	}
 }
 
task renderBoss{
	let Z=0;
	let X=0;
	let dir;
	
	ObjPrim_SetTexture(bossObj, imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,250,250);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,0.5,0.5,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		dir = ObjMove_GetAngle(bossObj);
		
		if(cos(dir) > 0) {
			loop(90){
				ObjRender_SetScaleXYZ(bossObj,0.4+0.02*sin(Z),0.4-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X>0){X-=2;}
				wait(1);
			}
		}
		else if(cos(dir) < 0) {
			loop(90){
				ObjRender_SetScaleXYZ(bossObj,0.4+0.02*sin(Z),0.4-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X<180){X+=2;}
		wait(1);
			}}
		yield;
	}

 }
 
task Fire{
	wait(120);
	let B = rand(360,0);
	let C = 30;
	let angle= 0;
	PlaySound(AttackSFX);
	loop(50){
		while (angle<360){
			CreateShotA2(bossX+C*sin(angle+B),bossY-C*cos(angle+B),0,angle+B-90,0.05,10,35,120);
			angle+=360/6;
		}
		angle=0;
		B+=1.6*8;
		C+=10;
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
}

task Fire2{
	wait(120);
	let B = rand(360,0);
	let C = 30;
	let angle= 0;
	PlaySound(AttackSFX);
	loop(50){
		while (angle<360){
			CreateShotA2(bossX+C*sin(angle+B),bossY-C*cos(angle+B),0,angle+B-90,0.05,10,39,120);
			angle+=360/6;
		}
		angle=0;
		B-=1.6*8;
		C+=10;
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
}

 task TEnd {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0) {
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	Obj_Delete(bossObj);
	ResetShaderI(0, 100);
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	CloseScript(GetOwnScriptID);
}