#TouhouDanmakufu
#ScriptVersion[3]
#Title["S4_L"]
#System["./system/system.txt"]
#Background["./background.txt"]
#Text["Spell 4 Lunatic"]

#include"./system/function.txt"

let frame = 0;
let angle = 0;
let count = 0;
let objBoss;
let objScene = GetEnemyBossSceneObjectID;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";
let bossx = 0;
let bossy = 0;

@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(800);
   }
   case(EV_REQUEST_TIMER){
	SetScriptResult(90);
   }
   case(EV_REQUEST_SPELL_SCORE){
	SetScriptResult(10000000);
   }
}

@MainLoop {
	frame++; 
	bossx = ObjMove_GetX(objBoss);
	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);
	
	ObjEnemy_SetDamageRate(objBoss, 1, 1);
	loop(120){yield;}
	ObjEnemyBossScene_StartSpell(objScene);
	PlaySound("se_spell");
	
	renderSpellBG;
	SpellText("Frost Sign \"Frozen Fauna\"", objBoss, objScene);
	
	loop(120){yield;}
	moveTask;
	ObjEnemy_SetDamageRate(objBoss, 10, 10);
	
	while(!Obj_IsDeleted(objBoss)){
		PlaySound("se_ch00");
		loop(50){loop(3){charge2(objBoss, 150, 128, 255);} yield;}
		loop(120){yield;}
		loop{
			PlaySound("se_lgods1");
			ascent(i in 0..18){DelayCloudBulletA2(0, 0, 1, 90/18*i, 292); DelayCloudBulletA2(0, 0, 2, 90/18*i, 292);}
			loop(60){yield;}
			
			PlaySound("se_lgods2");
			ascent(i in 0..18){DelayCloudBulletA2(GetStgFrameWidth, 0, 1, 90 + 90/18*i, 293); DelayCloudBulletA2(GetStgFrameWidth, 0, 2, 90 + 90/18*i, 293);}
			loop(60){yield;}
			
			loop(60){
				PlaySound("se_tan01");
				loop(3){DelayCloudBulletA3(bossx + rand(-100, 100), bossy + rand(-100, 100), 0.5, rand(0, 360), 261);}
				yield;
			}
			loop(60){yield;}
		}
	}
}

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

task DelayCloudBulletA2(x, y, speed, angle, graphic){
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) == 0){break;}
	let bullet = CreateShotA1(x, y, speed, angle, graphic, 0);
	let saveSpeed = speed;
	let i = 0;
	let time = round(rand(120, 180));
	let velocity = 0;
	let gravity = 0.025;
	
	ObjMove_SetSpeed(bullet, 0);

	while(i <= 10){
		i += 1;
		ObjShot_SetIntersectionEnable(bullet, false);
		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 == time){
			PlaySound("se_kira01");
		}
		if(i >= time){
			velocity += gravity;
			ObjMove_SetY(bullet, ObjMove_GetY(bullet) + velocity);
			ObjShot_SetGraphic(bullet, 291);
		}
		yield;
	}
	
	yield;
	
}

task DelayCloudBulletA3(x, y, speed, angle, graphic){
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) == 0){break;}
	let bullet = CreateShotA1(x, y, speed, angle, graphic, 0);
	let saveSpeed = speed;
	let i = 0;
	let time = 120;
	
	ObjMove_SetSpeed(bullet, 0);

	while(i <= 10){
		i += 1;
		ObjShot_SetIntersectionEnable(bullet, false);
		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 == time){
			PlaySound("se_kira00");
			ObjMove_SetAngle(bullet, atan2(GetPlayerY - ObjMove_GetY(bullet), GetPlayerX - ObjMove_GetX(bullet)));
			ObjMove_SetMaxSpeed(bullet, 4);
			ObjMove_SetAcceleration(bullet, 0.05);
		}
		if(i >= time){
			ObjShot_SetGraphic(bullet, 259);
		}
		yield;
	}
	
	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 renderSpellBG{
	let ct = 0;
	
	let texture1 = GetCurrentScriptDirectory~"img/sbg1.png";
	let bg1 = ObjPrim_Create(OBJ_SPRITE_2D);
	let texture2 = GetCurrentScriptDirectory~"img/sbg2.png";
	let bg2 = ObjPrim_Create(OBJ_SPRITE_2D);
	
	Obj_SetRenderPriority(bg1, 0.23);
	ObjPrim_SetTexture(bg1, texture1);
	ObjSprite2D_SetSourceRect(bg1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(bg1);
	ObjRender_SetPosition(bg1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	
	Obj_SetRenderPriority(bg2, 0.23);
	ObjPrim_SetTexture(bg2, texture2);
	ObjSprite2D_SetSourceRect(bg2, 0, 0, 1024, 1024);
	ObjSprite2D_SetDestCenter(bg2);
	ObjRender_SetPosition(bg2, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	
	while(!Obj_IsDeleted(objBoss)){
		ct++;
		ObjSprite2D_SetSourceRect(bg2, ct, ct, 1024+ct, 1024+ct);
		ObjRender_SetAngleXYZ(bg2, 0, 0, ct/6);
		ObjRender_SetScaleXYZ(bg2, 1+0.25*sin(ct/4), 1+0.25*sin(ct/4), 1);
		if(ct<60){ObjRender_SetAlpha(bg1, ct*(255/60)); ObjRender_SetAlpha(bg2, ct*(255/60));}
		yield;
	}
	
	ct = 0;
	
	loop(60){ct++; ObjRender_SetAlpha(bg1, 255-ct*(255/60)); yield;}
	Obj_Delete(bg1); Obj_Delete(bg2);
}