#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Last Word? (Mlimlemel)"]
#Text["Fun with Mlimlemel"]
#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 BGM = GetCurrentScriptDirectory ~ "/SFX/Tiamat Magna.mp3";

@Initialize {

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

	ObjMove_SetPosition(bossObj,0,-100);
	ObjMove_SetDestAtWeight(bossObj,190,120,8,5);
	
	LoadSound(BGM);
	PlayBGM(BGM,1,397);
	Cutin(bossObj," ???? ~ Last Word",4);
	LoadSound(AttackSFX);
	mainTask;
}

@Event {
	
	alternative(GetEventType())
	
	case(EV_REQUEST_IS_LAST_SPELL){
		SetScriptResult(true)
	}
	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{
	renderBG("MIMLE");
	wait(120);
	//movement;
	callbackboss;
	renderBoss;
	Fire;
	TEnd;
 }
 
task movement{
	let A=0;
	wait(70);
	loop{
		ObjMove_SetDestAtWeight(bossObj,190+100*sin(A),225-120*cos(A),4,2.5);
		A+=2;
		wait(6);
		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(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(120);
	let B = rand(360,0);
	let C = 0;
	let A = 0;
	let angle= 0;
	loop{
	PlaySound(AttackSFX);
	while (angle<360){
		CreateShotA2(bossX+30*sin(angle+GetAngleToPlayer(bossObj)+90),bossY-30*cos(angle+GetAngleToPlayer(bossObj)+90),-1,angle+A,0.05,5,190,30);
		let obj = CreateShotA2(bossX+30*sin(angle+GetAngleToPlayer(bossObj)+90),bossY-30*cos(angle+GetAngleToPlayer(bossObj)+90),-1,angle+A,0.05,5,308,30);
		bullet(obj);
		angle+=360/15;
	}
	A+=1.618*2;
	angle=0;
	wait(12);
	PlaySound(AttackSFX);
	while (angle<360){
		CreateShotA2(bossX+30*sin(angle+GetAngleToPlayer(bossObj)+90),bossY-30*cos(angle+GetAngleToPlayer(bossObj)+90),-1,angle+A,0.05,5,187,30);
		let obj = CreateShotA2(bossX+30*sin(angle+GetAngleToPlayer(bossObj)+90),bossY-30*cos(angle+GetAngleToPlayer(bossObj)+90),-1,angle+A,0.05,5,309,30);
		bullet(obj);
		angle+=360/15;
	}
	A+=1.618*2;
	angle=0;
	wait(12);
	PlaySound(AttackSFX);
	while (angle<360){
		CreateShotA2(bossX+30*sin(angle+GetAngleToPlayer(bossObj)+90),bossY-30*cos(angle+GetAngleToPlayer(bossObj)+90),-1,angle+A,0.05,5,188,30);
		let obj = CreateShotA2(bossX+30*sin(angle+GetAngleToPlayer(bossObj)+90),bossY-30*cos(angle+GetAngleToPlayer(bossObj)+90),-1,angle+A,0.05,5,310,30);
		bullet(obj);
		angle+=360/15;
	}
	A+=1.618*2;
	angle=0;
	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);}
	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);
 }