#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 4 (Mimlemel)"]
#Text["4th SpellCard"]
#include"./Index.txt"

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

let imgBoss = GetCurrentScriptDirectory ~ "/Texture/Mimlemel.png";
let AttackSFX = GetCurrentScriptDirectory ~ "/SFX/TWINKLE3.wav";
let Attack2SFX = GetCurrentScriptDirectory ~ "/SFX/ATTACK5.wav";
let Attack3SFX = GetCurrentScriptDirectory ~ "/SFX/TWINKLE.wav";

@Initialize {
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

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

	ObjMove_SetDestAtWeight(bossObj,190,120,8,5);
	
	Cutin(bossObj," ????",4);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	LoadSound(Attack3SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(5500);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(70);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(400000);
	}
 }

@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{
	callbackboss;
	renderBoss;
	renderBG("MIMLE");
	TEnd;
	wait(120);
	let angle= 0;
	loop(20){TChargeEffect(bossX,bossY);}
	while (angle<360){
		PlaySound(Attack3SFX);
		let obj = CreateShotA2(bossX+50*sin(angle),bossY-50*cos(angle),0,angle-90,0.1,10,308,18);
		bullet(obj);
		angle+=360/100;
	}
	angle=0;
	loop(20){TChargeEffect(bossX,bossY);}
	wait(12);
	while (angle<360){
		PlaySound(Attack3SFX);
		let obj = CreateShotA2(bossX+40*sin(angle),bossY-40*cos(angle),0,angle-90,0.1,10,309,18);
		bullet(obj);
		angle+=360/100;
	}
	angle=0;
	loop(20){TChargeEffect(bossX,bossY);}
	wait(12);
	while (angle<360){
		PlaySound(Attack3SFX);
		let obj = CreateShotA2(bossX+30*sin(angle),bossY-30*cos(angle),0,angle-90,0.1,10,310,18);
		bullet(obj);
		angle+=360/100;
	}
	angle=0;
	Fire;
	Fire2;
 }
 
task movement{
	wait(70);
	loop{
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),rand(90,131),50);
		wait(100);
	}
}
 
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(9){
				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-=20;}
				wait(1);
			}
		}
		else if(cos(dir) > 0) {
			loop(9){
				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+=20;}
		wait(1);
			}}
		yield;
	}
 }
 
task Fire{
	wait(100);
	let B = rand(360,0);
	let C = 0;
	let angle= 0;
	movement;
	loop(5){TChargeEffect(bossX,bossY);}
	wait(20);
	loop{
		PlaySound(AttackSFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),5,angle+B-90,-0.1,2,37,18);
			angle+=360/5;
		}
		angle=0;
		C-=1.6;
		B+=20*sin(C);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(3);
		yield;
	}
}

task Fire2{
	wait(80);
	let X=0;
	loop{
		let obj= CreateShotA1(X,0,rand(1,3),90,rand(308,311),40);
		bullet(obj);
		X=rand(0,380);
		wait(12);
		yield;
	}
}

task bullet(obj){
	while(((GetPlayerX-ObjMove_GetX(obj))^2+(GetPlayerY-ObjMove_GetY(obj))^2)^0.5>100){
		if(Obj_IsDeleted(obj)){break;}
		yield;
	}
	if(!Obj_IsDeleted(obj)){CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,GetAngleToPlayer(obj),0.01,5,328,30); PlaySound(Attack2SFX);}
	Obj_Delete(obj);
}

 task TEnd {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0) {
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(1){
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.3,255,255,10);
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.2,255,10,255);
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.1,255,100,0);
		wait(1);
	}
	Obj_Delete(bossObj);
	ResetShaderI(0, 100);
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	CloseScript(GetOwnScriptID);
}