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



let objBoss;
let width = 384;
let height = 448;
let life = 3500;
let time= 62;
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);
	
	
	animSpell(objBoss, 2);
	TFinalizeSpell(scene, objBoss, false, true);
	SetCommonData("non2", "no");
	SetCommonData("spell", "MarioSpell");
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, height/2, 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(2, objBoss);
    Indicator(objBoss, 2);
    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));
	let shot = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 70);
	Spell("Tough Love", objBoss, 2);
    wait(240);
    ObjEnemy_SetDamageRate(objBoss, 20, 0);

    wait(60);
	let t=0; let ang = rand(40, 140); let col = [410,411,414,415,416];
	while(t<500){
	    let angle = (ang+rand(-20,20))%360; if(angle>180){angle-=360;}
		heart(width/2, height/2,80, col[truncate(rand(0,5))], 0, 6, 7.5, 6, angle); wait(80-70*t/500);
        ang+=90	; t+= 80-70*t/500; if(t>=250){ObjEnemy_SetDamageRate(objBoss, 60, 20);}
	}
	wait(120);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/Horn.WAV", 80));
	wait(60);
	t=0;
	hearts(1); hearts(2); ang= rand(0,360);
	shotSound(shot, -1, 45, 3, objBoss, false,0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	   ascent(i in 0..7){let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, ang+360/7 *i, 407, 0); ObjShot_SetSpellResist(obj, true);}
		wait(3); ang++;
	}
}

task hearts(n){
    let shot = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK2.WAV", 70);
    if(n==1){
	    wait(260); let t=0; let ang= rand(0,360);
		shotSound(shot, -1, 70, 12, objBoss, false,0);
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	        ascent(i in 0..5){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5, ang+360/5 *i, 385, 0); Obj_SetRenderPriorityI(obj, 60);
				ObjShot_SetSpellResist(obj, true);
			}
		    wait(13); ang+=-12;
		}
	}
	else{
	    wait(500);
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	        heart(rand(width/8, 7*width/8), 0, 40, 414, 30, 1, 1.5, 2, 0);
			wait(90);
		}
	}
}


task heart(x, y, numBul,col, del, radmin, radmax, speed, ang){
    let shot = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK5.WAV", 70);
    let t=0; let rad= rand(radmin, radmax);
    ObjSound_Play(shot);
	ascent(i in 0..numBul){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	    let obj = CreateShotA1(x+ rad *16*(sin(t))^3, y - rad*(13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*(t))), 0, 90, col, del); 
		Obj_SetRenderPriorityI(obj, 70); ObjShot_SetSpellResist(obj, true);
	    con(obj, ang, speed);
		t+=360/numBul;
	}
}

	
task con(obj, ang, speed){
    wait(10); let dir= 1; if(ang<0){dir=-1;} let t=0; let angIni = ObjMove_GetAngle(obj);
	if(absolute(ang)<20){
	    rotate(obj, ang, 60, dir, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), false, false);
		while(t<60){ObjMove_SetAngle(obj, angIni + dir*ang*t/60); t++; yield;}
	}
	else{
	    let angle = absolute(ang)-20;
		rotate(obj, angle, 60, dir, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), false, false);
        while(t<60){ObjMove_SetAngle(obj, angIni + dir*angle*t/60); t++; yield;}
		t=0; angIni = ObjMove_GetAngle(obj);
		rotate(obj, 20, 60, dir, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), false, false);
		while(t<60){ObjMove_SetAngle(obj, angIni + dir*t*40/60); t++; yield;}
	}
	wait(30);
	ObjMove_SetSpeed(obj, speed);
}
		
	