//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["MVT: Slash and Rolle"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncJames.dnh"


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


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	
	animSpell(objBoss);
	TFinalizeSpell(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 3*height/10, 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{
    Indicator(objBoss);
    takeDamage(objBoss, scene, life);
    superSaiyan(objBoss);
    SetForbidPlayerShot(false);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/SPELLCARD.WAV", 100));
	Spell("MVT: Slash and Rolle", scene, objBoss);
    wait(240);
	ObjEnemy_SetDamageRate(objBoss, 20, 10);
	mvt;
	snakes;
	wait(240);
	ObjEnemy_SetDamageRate(objBoss, 60, 30);
}


task snakes{
    let s =LoadEffect(GetCurrentScriptDirectory~ "./../SFX/snakehiss.WAV", 90);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2){yield;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(s);
	    if(rand(0,1)>=1/2){
		    let ang= rand(30, 60);
			ascent(i in 0.. truncate(rand(4, 7))){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} let obj = CreateShotA1(0, 0, 4, ang, 333, 4*i); controlWvel(obj, 3, 20, 600, 3);}
		}
		else{
		    let ang= rand(120, 150);
		    ascent(i in 0.. truncate(rand(4, 7))){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} let obj = CreateShotA1(width, 0, 4, ang, 333, 4*i); controlWvel(obj, -3, 20, 600, 3);}
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3){wait(120);}
		else{wait(60);}
	}
}


task mvt {
    let dir=1;
	if(rand(0,1)>=1/2){dir=-1;}
	let l = dir;
    let i =0;
    let col;
	let flash = LoadEffect(GetCurrentScriptDirectory~"./../SFX/swordFlash.WAV", 70);
	let shot = LoadEffect(GetCurrentScriptDirectory~"./../SFX/ATTACK4.WAV", 50);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    col = rand(93, 105);
	    let x; let y; let t= -dir*width/2;
		shotSound(shot, 200, 30, 2, objBoss,0);
		loop(200){
			loop(2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				x = width/2 +t;
				y = height/2 - ((dir*t)^3 -(192^2)*(dir*t))/20000;
				let obj = CreateShotA1(x,y, 0, 0, col, 0);
				ObjMove_AddPatternA1(obj, 400, 1, rand(0,360));
				t+= dir*width/400;
			}
			yield;
		}
		wait(60);
		t = -dir*width/2;
		ObjSound_Play(flash);
		if(dir==1){
			loop(10){
				loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					x = width/2 +t;
					y = 0.46*x +46.7;
					let obj = CreateShotA1(x,y, 0, rand(0,360), 137, 0);
					ObjMove_AddPatternA2(obj, 120, 0, NO_CHANGE, rand(0.02, 0.04), 0, 2);
					t+= dir*width/60;
				}
				yield;
			}
		}
		else{
			loop(10){
				loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					x = width/2 +t;
					y = -0.46*x +224.5;
					let obj = CreateShotA1(x,y, 0, rand(0,360), 137, 0);
					ObjMove_AddPatternA2(obj, 120, 0, NO_CHANGE, rand(0.01, 0.02), 0, 2);
					t+= dir*width/60;
				}
				yield;
			}
		}
		wait(20);
		t = dir*width/2;
		ObjSound_Play(flash);
		if(dir==1){
			loop(10){
				loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					x = width/2 +t;
					y = dir* 0.46*x +133.4;
					let obj = CreateShotA1(x,y, 0, rand(0,360), 137, 0);
					ObjMove_AddPatternA2(obj, 120, 0, NO_CHANGE, rand(0.01, 0.02), 0, 2);
					t-= dir*width/60;
				}
				yield;
			}
		}
		else{
			loop(10){
				loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					x = width/2 +t;
					y = dir* 0.46*x +310.7;
					let obj = CreateShotA1(x,y, 0, rand(0,360), 137, 0);
					ObjMove_AddPatternA2(obj, 120, 0, NO_CHANGE, rand(0.01, 0.02), 0, 2);
					t-= dir*width/60;
				}
				yield;
			}
		}
		wait(20);
		t = -dir*width/2;
		ObjSound_Play(flash);
		if(dir == 1){
			loop(10){
				loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					x = width/2 +t;
					y = 0.46*x +224.5;
					let obj = CreateShotA1(x,y, 0, rand(0,360), 137, 0);
					ObjMove_AddPatternA2(obj, 120, 0, NO_CHANGE, rand(0.01, 0.02), 0, 2);
					t+= dir*width/60;
				}
				yield;
			}
		}
		else{
			loop(10){
				loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					x = width/2 +t;
					y = -0.46*x +401.8;
					let obj = CreateShotA1(x,y, 0, rand(0,360), 137, 0);
					ObjMove_AddPatternA2(obj, 120, 0, NO_CHANGE, rand(0.01, 0.02), 0, 2);
					t+= dir*width/60;
				}
				yield;
			}
		}
		dir*=-1;
		i++;
		if(i%2==1){wait(60);}
		else{
		    wait(80);
			DelayLaser(width/2, 0, 90, 500, 25, 13, 60);
			DelayLaser(0, height/2, 0, 500, 25, 13, 60);
			wait(60);
			ObjMove_SetDestAtFrame(objBoss, 0, height/2, 30);
			wait(30);
			ObjSound_Play(flash);
			let x = 0;
			loop(10){
			    loop(6){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				    let obj = CreateShotA1(x,height/2, 0, rand(0, 360), 137, 0); ObjMove_AddPatternA2(obj, 80, 0, NO_CHANGE, rand(0.02, 0.04), 0, 1.5); 
					x+=width/60;
				}
				yield;
			}
			ObjMove_SetDestAtFrame(objBoss, width/2, height/5, 30);
			wait(30);
			ObjSound_Play(flash);
			x=0;
			loop(10){
			    loop(8){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
					let obj = CreateShotA1(width/2 ,x, 0, 90 -20 *l + l * rand(0,220), 137, 0); ObjMove_AddPatternA2(obj, 40, 0, NO_CHANGE, rand(0.02, 0.04), 0, 4); 
					x+= height/80;
				}
				yield;
			}
		    ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 60);
		    wait(30);
		}
	}
}
	
	