#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Ballerina non-spell 01 normal"]
#Text["Ballerina non-spell 01 normal"]

let freeze = false;

#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(1);
	setAura(bossObj,255,64,255);
	SetCommonData("bossAttackType",1);
	SetCommonData("bossCircleType",0);
	setInvulnerableTime(bossObj,120);	
	
	ObjMove_SetDestAtSpeed(bossObj,192,120,3);
	SetAutoDeleteObject(true);
	mainTask;
} 

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

@MainLoop { yield; } 
@Finalize { } 


task mainTask { 
	yield;
	scriptEnding;	
	wait(90);
	move;
} 

// movement task
task move {
	let dir = 0;
	while(!Obj_IsDeleted(bossObj)) { 
		dir = rand_int(0,359);
		bossAttack = 1;
		ObjMove_SetDestAtWeight(bossObj,GetCenterX+120*cos(dir),120+40*sin(dir),5,10);
		wait(30);
		bossAttack = 0;		
		freeze = true;
		fire;
		effect;
		ascent(i in 0..90) { 
			wait(1);
			ObjMove_SetX(bossObj,GetCenterX+120*cos(dir));
			ObjMove_SetY(bossObj,120+40*sin(dir));
			dir+=4;
		}
		ObjMove_RandomPosWeight(bossObj,80,30,10,30,GetCenterX-100,GetCenterY-60,GetCenterX+100,GetCenterY);
		wait(30);
		PlaySFX(SFX_POWER1);
		freeze = false;
		wait(120);
	}
}

task fire {
	let speed = 0; 
	loop(4) {
		PlaySFX(SFX_TAN00);
		ascent(i in 0..20) {
			speed = rand(4,5);
			sprayShot(bossX,bossY,speed*cos(rand(240,270)),speed*sin(rand(240,270)),0.1,58,10);
		}
		wait(5);
		ascent(i in 0..20) {
			speed = rand(4,5);
			sprayShot(bossX,bossY,speed*cos(rand(270,300)),speed*sin(rand(270,300)),0.1,58,10);

		}
		wait(10);
	}
}

task effect { 
	PlaySFX(SFX_ICE);
	loop(4) {
		exploEffect(bossX,bossY,20,0.5);
		wait(20);
	}
}

task sprayShot(x,y,xv,yv,accely,kleur,delay){
	let obj = ObjShot_Create(OBJ_SHOT);
	let ac = accely;
	
	ObjShot_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,0);
	ObjShot_SetGraphic(obj,kleur);
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetSpellResist(obj,true);

	loop(60) {      		
		yv += ac;
		ObjMove_SetAngle(obj,180 + atan(yv/xv));
		ObjMove_SetPosition(obj,ObjMove_GetX(obj)+xv,ObjMove_GetY(obj) + yv);
		yield;
	}

	ObjShot_SetGraphic(obj,54);
	while(freeze) { yield; }
	ObjShot_SetGraphic(obj,58);

	ac = rand(0.001,0.01);

	while(!Obj_IsDeleted(obj)) {
		ObjMove_SetPosition(obj,ObjMove_GetX(obj) + xv,ObjMove_GetY(obj) + yv);
		ObjMove_SetAngle(obj,180 + atan(yv/xv));
		yv += ac;
		yield;
	}

}

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

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

	CloseScript(GetOwnScriptID());
}
 