//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Mouth"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 4700;
let time= 70;
let scene = GetEnemyBossSceneObjectID();

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(time);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetCommonData("non1", "no");
	SetCommonData("spell", "Mouth");
	
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, height/5, 60);
	Main;
}


@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}


task Main{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/SPELLCARD.WAV", 100));
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTIONSHORT.WAV", 100));
	Spell("Chaos sign:[r]Dynamical Maw", objBoss, 1);
    wait(240);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjEnemy_SetDamageRate(objBoss, 20, 0);
	    let colour = rand(77, 84);
		ascent(i in 1..5){mouthform(80, i, 90, colour);}
		wait(180);
		ObjEnemy_SetDamageRate(objBoss, 60, 20);
		wait(140);
		bulletSpew(1);
		bulletSpew(2);
		let short= 120; let long = 250;
		let las=1; if(rand(0,1)>=1/2){las=-1;}
		if(las>0){
			let obj1= CreateStraightLaserA1(0,0, 0, 550, 30, short, 17, 60); lasCon(obj1,1);
			let obj2= CreateStraightLaserA1(width,0, 0, 550, 30, long, 22, 60); lasCon(obj2, -1);
		}
		else{
			let obj1= CreateStraightLaserA1(0,0, 0, 550, 30, long, 22, 60); lasCon(obj1,1);
			let obj2= CreateStraightLaserA1(width,0, 0, 550, 30, short, 17, 60); lasCon(obj2, -1);
		}
		wait(860);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 80));
		wait(120);
		let col = [317,318,320, 321,322,323];
        ascent(j in 0..6){
		    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 70));
		    let ang= rand(0,360);
		    let xr= rand(width*0.2, 0.8*width); let yr= rand(height/4, 0.5*height); let s = rand(2,3); let radi = rand(80, 120); let r; let t=0;
			ascent(i in 0..20){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				r = 4*radi *cos(t)*(sin(t))^2;
				let obj = CreateShotA1(xr+ r *cos(t), yr+ r/s *sin(t), 0, t, col[j], 20);
				t+= 180/20;
				rotate(obj, ang, 60, 1, xr, yr, true, true);
				ObjMove_AddPatternA2(obj, 200-20*j -i, 0, NO_CHANGE, rand(0.01, 0.02), 0, 4);
			}
			if(j<=2){wait(50-j);} else{wait(20);}
		}
		wait(240);
	}
}


	
task com(obj, ang, xcentre, ycentre){
    wait(60);
	rotate(obj, 45, 60, ang/absolute(ang), xcentre, ycentre, false, true);
}
	


task mouthform(numBul, n, radius, colour) {
    let r; let t=0; let ang = rand(20,40); let shot = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 70);
	shotSound(shot, 2*numBul, 70, 2, objBoss, false, 0);
	alternative(n)
	case(1){
		ascent(i in 0..numBul){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			r = 4*radius *cos(t)*(sin(t))^2;
			let obj = CreateShotA1( r *cos(t), r/3 *sin(t), 0, t, colour, 0);
			ObjShot_SetSpellResist(obj, true);
			com(obj, 45, 0,0);
			wait(2); t+= 180/numBul;
			movement(obj, i, -1, 240, 0,0, ang);
		}
	}
	case(2){
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			r = 4*radius *cos(t)*(sin(t))^2;
			let obj = CreateShotA1( width + r *cos(t+180), r/3 *sin(t+180), 0, t, colour, 0);
			com(obj, -45, width,0);
			ObjShot_SetSpellResist(obj, true);
			wait(2); t+= 180/numBul;
			movement(obj, i, 1, 240, width, 0, ang);
		}
	}
	case(3){
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			r = 4*radius *cos(t)*(sin(t))^2;
			if(i%2==0){
				let obj = CreateShotA1( r *cos(t), height + r/3 *sin(t), 0, t, colour, 0);
				com(obj, -45, 0, height);
				ObjShot_SetSpellResist(obj, true);
			    t+= 180/(numBul *3/2);
				ObjMove_AddPatternA2(obj, 860, 0, -45 + rand(-40, 40), rand(0.01, 0.02), 0, rand(1.2,2));
			}
			else{
			    loop(2){
				    let obj = CreateShotA1( r *cos(t), height + r/3 *sin(t), 0, t, colour, 0);
					com(obj, -45, 0, height);
					ObjShot_SetSpellResist(obj, true);
					t+= 180/(numBul *3/2);
					del(obj);
				}
			}
			wait(2);
			sound;
		}
	}
	case(4){
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			r = 4*radius *cos(t)*(sin(t))^2;
			if(i%2==0){
				let obj = CreateShotA1( width + r *cos(t+180), height + r/3 *sin(t+180), 0, t, colour, 0);
				com(obj, 45, width, height);
				ObjShot_SetSpellResist(obj, true);
				t+= 180/(numBul*3/2);
				ObjMove_AddPatternA2(obj, 860, 0, 225 + rand(-40, 40), rand(0.01, 0.02), 0, rand(1.2,2));
			}
			else{
			    loop(2){
				    let obj = CreateShotA1( width + r *cos(t+180), height + r/3 *sin(t+180), 0, t, colour, 0);
					com(obj, 45, width, height);
					ObjShot_SetSpellResist(obj, true);
					t+= 180/(numBul*3/2);
					del(obj);
				}
			}
			wait(2);
		}
	}
}

task del(obj){
    wait(860); Obj_Delete(obj);
}

task sound{
    wait(860); if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/TWINKLE.WAV", 60));
}

task movement(obj, i, dir, delay, xcentre, ycentre, ang){
    let warp = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 80);
    wait(260-2*i);
	ObjSound_Play(warp);
	rotate(obj, 45, 60, dir, xcentre, ycentre, false, true);
	wait(200);
	ObjSound_Play(warp);
	rotate(obj, 80, 140, -dir, xcentre, ycentre, false, true);
	wait(180);
	ObjSound_Play(warp);
	rotate(obj, ang, 60, dir, xcentre, ycentre, false, true);
	ObjMove_AddPatternA2(obj, 120, rand(3,5.5), 90+ dir*(20+ang) +rand(-30, 30), rand(-0.1, -0.02), 0, rand(0.8,2));
	wait(120);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK5.WAV", 70));
}

task lasCon(obj, dir){
    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 80));
    loop(60){
		yield;
	}
	MasterSparkSound(250, objBoss);
	loop(80){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(obj);}
	    if(Obj_IsDeleted(obj)){return;}
		yield;
	}
	ascent(i in 0..140){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(obj);}
	    if(Obj_IsDeleted(obj)){return;} 
		ObjStLaser_SetAngle(obj, 90-90*dir +dir*i*80/140); yield;
	}
}

task bulletSpew(num){
    let twinkle = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/TWINKLE2.WAV", 70);
    wait(140);
    let t=0;
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	shotSound(twinkle, 230, 70, 10, objBoss, false, 0);
	while(t<230){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		if (num==1){CreateShotA1(0, height, rand(1,4), -45+rand(-20, 20), 231, 0);}
		else{CreateShotA1(width, height, rand(1,4), 225+rand(-20, 20), 231, 0);}
		wait(10); t+=10;
	}
}
    






