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



let objBoss;
let width = 384;
let height = 448;
let life = 4600;
let scene = GetEnemyBossSceneObjectID();


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	SetCommonData("non1", "no");
	
	animNon(objBoss);
	Mario; James;
	TFinalizeNon(scene, objBoss);
	
	
    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{
    SetForbidPlayerShot(false);
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	let shot = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 60);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/Charge.WAV", 80));
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 50); let right= true; let angle; let rad; let phase =1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ascent(i in 0..3){
		    if(phase==1 || i!=0){
				if(right){
					angle = findAngRad(width/4 + 0.25*i*width, ObjMove_GetX(objBoss), height, ObjMove_GetY(objBoss))[0]; 
					rad= findAngRad(width/4 + 0.25*i*width, ObjMove_GetX(objBoss), height, ObjMove_GetY(objBoss))[1];
				}
				else{
					angle = findAngRad(3*width/4 - 0.25*i*width, ObjMove_GetX(objBoss), height, ObjMove_GetY(objBoss))[0]; 
					rad= findAngRad(3*width/4 - 0.25*i*width, ObjMove_GetX(objBoss), height, ObjMove_GetY(objBoss))[1];
				}
				ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 80));
				DelayLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), angle, rad, 25, 22, 60);
				wait(60);
				ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Explosion.WAV", 100));
				if(right){ObjMove_SetDestAtFrame(objBoss, width/4 + 0.25*i*width, height-10, 5);}
				else{ObjMove_SetDestAtFrame(objBoss, 3*width/4 - 0.25*i*width, height-10, 5);}
				wait(5);
				explosion(right, i);
				ObjMove_SetDestAtFrame(objBoss, width/2, height/5, 120);
				wait(360);
			}
		}
		right= !right; phase++;
		yield;
	}
}

task explosion(right, i){
    let t=0;
	while(t<80){
	    loop(20){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			if(right){
				let obj = CreateShotA2(width/4 + 0.25*i*width + rand(-25, 25), height, rand(5, 8), 270 +rand(-40, 40), rand(-0.05, 0), 4, rand(90,105), 0);
				Obj_SetRenderPriorityI(obj, 70); ObjShot_SetSpellResist(obj, true);
			}
			else{
			    let obj = CreateShotA2(3*width/4 - 0.25*i*width + rand(-25, 25), height, rand(5, 8), 270 +rand(-40, 40), rand(-0.05, 0), 4, rand(90,105), 0);
				Obj_SetRenderPriorityI(obj, 70); ObjShot_SetSpellResist(obj, true);
			}
		}
		yield; t++;
	}
}

task drawMario(enemy1){
    let img = GetCurrentScriptDirectory() ~ "../../img/nonMario.png";
	let i=0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjPrim_SetTexture(enemy1, img);
		if(i%2==0){ObjSprite2D_SetSourceRect(enemy1, 166, 250, 340, 770);}
		else{ObjSprite2D_SetSourceRect(enemy1, 166+512, 250, 340+512, 770);}
		ObjSprite2D_SetDestRect(enemy1, -30, -60, 30, 60);
		i++;
		wait(20);
	}
}

task Mario{
    let enemy1 = ObjPrim_Create(OBJ_SPRITE_2D);
	drawMario(enemy1);
	ObjRender_SetPosition(enemy1, width/4, 3*height/10, 60); wait(300);
	let shot = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK2.WAV", 70);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let t=0; shotSound(shot, 240, 60, 12, objBoss, false,0); let ang = GetAngleToPlayer(enemy1);
		while(t<240){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    if(rand(0,1)>=1/5){let obj= CreateShotA1(ObjRender_GetX(enemy1), ObjRender_GetY(enemy1)-20, rand(1.5, 2), 
			    ang +rand(-10, 10), 245, 0); Obj_SetRenderPriorityI(obj, 80);}
			else{let obj=CreateShotA1(ObjRender_GetX(enemy1), ObjRender_GetY(enemy1)-20, rand(1.5, 3), 
			    ang +rand(-10, 10), 411, 0); Obj_SetRenderPriorityI(obj, 80);}
			wait(12); t+=12;
		}
		wait(100);
	}
	ObjRender_SetPosition(enemy1, width/4, -300, 60);
	Obj_Delete(enemy1);
}


task drawJames(enemy2){
    let img = GetCurrentScriptDirectory() ~ "../../img/NonSpell.png";
	let i=0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjPrim_SetTexture(enemy2, img);
		if(i%2 ==0){
			ObjSprite2D_SetSourceRect(enemy2, 316, 70, 436, 410); 
			ObjSprite2D_SetDestRect(enemy2, -25, -70, 25, 70);
		}
		else{
			ObjSprite2D_SetSourceRect(enemy2, 25, 125, 180, 420); 
			ObjSprite2D_SetDestRect(enemy2, -43, -70, 43, 70);
		}
		
		i++;
		wait(30);
	}
}

task James{
    let enemy2 = ObjPrim_Create(OBJ_SPRITE_2D);
	drawJames(enemy2);
	ObjRender_SetPosition(enemy2, 3*width/4, 3*height/10, 60);
	wait(380); let ang = rand(0,360);
	let shot = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 60);
	let dir = 1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		shotSound(shot, 270, 55, 10, objBoss, false,0);
		spiral(ObjRender_GetX(enemy2), ObjRender_GetY(enemy2), 6, ang, 12, -dir, 0, 270, 1, 60, 1.4, 0, 0, 1.4, 0, 0, 152, objBoss, 0);
		spiral(ObjRender_GetX(enemy2), ObjRender_GetY(enemy2), 4, ang, 11, dir, 0, 270, 1, 60, 1.8, 0, 0, 1.8,0, 0, 147, objBoss, 0);
		spiral(ObjRender_GetX(enemy2), ObjRender_GetY(enemy2), 5, ang, 10, -dir, 0, 270, 1, 60, 2.3, 0, 0, 2.5, 0,0, 141, objBoss, 0);
		dir*=-1; wait(380);
	}
	ObjRender_SetPosition(enemy2, width/4, -300, 60);
	Obj_Delete(enemy2);
}
    

