#TouhouDanmakufu
#ScriptVersion[3]
#Title["NS3_H"]
#System["./system/system.txt"]
#Background["./background.txt"]
#Text["Nonspell 3 Hard"]

#include"./system/function.txt"

let frame = 0;
let rad = 0;
let angle = 0;
let objBoss;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";

@Initialize {
   DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
   SetAutoDeleteObject(true);
   objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
   ObjEnemy_Regist(objBoss);
   ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-70, 10, 7);
   mainTask;
}

@Event {
   alternative(GetEventType())
   case(EV_REQUEST_LIFE){
   	SetScriptResult(3000);
   }
   case(EV_REQUEST_TIMER){
	SetScriptResult(45);
   }
}

@MainLoop {
	frame++; 
	let bossx = ObjMove_GetX(objBoss);
	let bossy = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss, bossx, bossy, 40);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, bossx, bossy, 12);
	
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){
		Obj_Delete(objBoss); 
		DeleteShotAll(TYPE_ALL,TYPE_ITEM); 
		SetAutoDeleteObject(true);
		CloseScript(GetOwnScriptID()); 
	}
	yield;   
}

@Finalize {

}

task mainTask {
	renderBoss("Cirno");
	Circle(objBoss);
	FireBoss(objBoss, 200, 255);
	moveTask;
	
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	loop(90){yield;}
	ObjEnemy_SetDamageRate(objBoss, 60, 60);
	
	while(!Obj_IsDeleted(objBoss)){
		loop{
			PlaySound("se_tan02");
			ascent(i in 0..6){
				DelayCloudBulletA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, frame*0.25 + 360/6*i, 278);
			}
			loop(60){yield;}
		}
	}
}

task DelayCloudBulletA2(x, y, speed, angle, graphic){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){ return; }
	
	let bullet = CreateShotA1(x, y, speed, angle, graphic, 0);
	let dtp = ((GetPlayerX - ObjMove_GetX(bullet))^2 + (GetPlayerY - ObjMove_GetY(bullet))^2)^0.5;
	let i = 0;
	
	ObjMove_SetSpeed(bullet, 0);
	ObjShot_SetIntersectionEnable(bullet, false);
	
	while(i<=10){
		i += 1;
		ObjRender_SetScaleXYZ(bullet, 2 - 1/10*i, 2 - 1/10*i,0);
		ObjRender_SetAlpha(bullet, 255/10*i);
		yield;
	}
	
	i = 0;
	ObjShot_SetIntersectionEnable(bullet, true);
	ObjMove_SetSpeed(bullet, speed);
	
	while(!Obj_IsDeleted(bullet)){
		i++;
		if(i % 45 == 0 && !Obj_IsDeleted(bullet)){
			ascent(a in 0..3){
				ascent(i in 0..3){
					DelayCloudBulletA0(ObjMove_GetX(bullet), ObjMove_GetY(bullet), 1 + a/6, ObjMove_GetAngle(bullet) - 360/3*i + 180, 86);
				}
			}
		}
		yield;
	}
}

task renderBoss(name){
	BossName(name, objBoss);
	
	let i = 0;
	
	ObjPrim_SetTexture(objBoss, imgBoss);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjRender_SetScaleXYZ(objBoss, 0.35, 0.35, 0);
	
	while(!Obj_IsDeleted(objBoss)){
		i++;
		yield;
	}
}

task moveTask{
	let i = 0;
	
	while(!Obj_IsDeleted(objBoss)){
		i++;
		if(i % 180 == 0){
			ObjMove_SetDestAtWeight(objBoss, rand(GetStgFrameLeft + 50, GetStgFrameWidth - 50), rand(70, GetStgFrameHeight/2 - 70), 10, 1);
		}
		yield;
	}
}