//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["CW comp"]
#System["./../../system/NSystem.dnh"]
#Player["./../../player/Reimu by plasmaflame22/Reimu_A.txt", "./../../player/Reimu by plasmaflame22/Reimu_C.txt"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncSaul.dnh"


let objBoss;
let width = 444;
let height = 448;
let life = 3000;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";


@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", "yes");
	SetCommonData("spell", "CW");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, 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);
	sound;
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Mind twist: CW complex", objBoss, 1);
	let shot = LoadEffect(path~"ATTACK4.WAV", 70);
    wait(240);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	CW(4, 1.6, 60);
	move(0.3*width, 0.7*width, height/10, 3*height/10, 120, 180, 300, objBoss, 0, scene, 0); 
	wait(40);
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 90));
	wait(100);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>2*life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 2*time/3){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	wait(30);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(shot);
	    ascent(i in 0..8){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, GetAngleToPlayer(objBoss) +45*i, 291,0);}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/3){wait(140);} else{wait(100);}
	}
}	

task sound{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/3){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
}
	

task CW(num, speed, pause){
    let shot = LoadEffect(path~ "ATTACK5.WAV", 80);
    let posi = []; let posin = [];
	let objID = []; let objnID= [];
	ascent(i in 0.. num){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		posi= posi~ [50 + i*350/(num-1) +rand(-40,40)];
	}
	ascent(i in 0.. num){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		posin= posin~ [50 + i*350/(num-1) +rand(-40,40)];
	}
	ObjSound_Play(shot);
	ascent(i in 0.. num){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	    let obj= CreateShotA1(posi[i], 10, 0, 90, 356, pause); 
		objID= objID~ [obj];
		controlSpeed(objID[i], 0, speed, pause);
	}
	wait(60+pause);
	ObjSound_Play(shot);
	ascent(i in 0.. num){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	    let obj= CreateShotA1(posin[i], 10, 0, 90, 356, pause);
		objnID= objnID~ [obj];
		controlSpeed(obj, 0, speed, pause);
	}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		bullets(num, objID, objnID, speed, pause);	
		posi = posin; posin = [];
		objID = objnID; objnID= [];
		ascent(i in 0.. num){posin= posin~ [50 + i*350/(num-1) +rand(-40,40)];}
		wait(60+pause);
		ascent(i in 0.. num){ObjMove_SetSpeed(objID[i], 0);}
		ObjSound_Play(shot);
		ascent(i in 0.. num){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj= CreateShotA1(posin[i], 10, speed, 90, 356, pause);
			objnID= objnID~ [obj];
			controlSpeed(obj, 0, speed, pause);
		}
	    yield;
	}
}
	
	
task bullets(num, objID, objnID, speed, pause){
    if(Obj_IsDeleted(objID) || Obj_IsDeleted(objnID)){return;}
    let x; let xdest1; let xdest2; let result1; let result2; let xdest3; let result3; let y;
	ascent(i in 0..num){
		x = ObjMove_GetX(objID[i]); xdest1 = ObjMove_GetX(objnID[i]);
		y = ObjMove_GetY(objID[i]);
		result1= findAngRad(xdest1,x,10, y);
		if(i==0){spittle(objID[0], 180);} if(i==num-1){spittle(objID[num-1], 0);}	
		spawn(x,y, result1, speed, pause);
		if(i>0){
			xdest1 = ObjMove_GetX(objnID[i-1]);
			result1= findAngRad(xdest1,x,10, y);
			spawn(x,y, result1, speed, pause);
		}
		if(i<num-1){
			xdest1 = ObjMove_GetX(objnID[i+1]);
			result1= findAngRad(xdest1,x,10, y);
			spawn(x,y, result1, speed, pause);  
		}
	}
}


task spittle(obj, angle){
    let shot = LoadEffect(path~"ATTACK2.WAV", 60);
    shotSound(shot, -1, 60, 4, obj, true, 0, scene, 0);
    while(!Obj_IsDeleted(obj)){
	    if(Obj_IsDeleted(obj)){return;}
	    CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(8,10), angle+rand(-80,80), 241,0);
		CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(8,10), angle+rand(-80,80), 241,0);
		yield;
	}
}
	
	
	
task spawn(x,y,result, speed, pause){
    let twinkle = LoadEffect(path~"TWINKLE.WAV", 80);
    shotSound(twinkle, 12, 80, 2, objBoss, true, 0, scene, 0);
	ascent(j in 0..4){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		let obj=CreateShotA1(x + j/4 * result[1] * cos(result[0]), y + j/4 * result[1] * sin(result[0]), 0,90,217, 0);
		controlSpeed(obj, j, speed, pause );
		if(rand(0,1)<0.1){explode(obj);}
		wait(6);
	}
}

task explode(obj){
    while(ObjMove_GetY(obj)<height/2){yield;}
	ObjShot_SetGraphic(obj, 218);
	while(ObjMove_GetY(obj)<height){yield;}
	let res = findAngRad(GetPlayerX, ObjMove_GetX(obj), GetPlayerY, ObjMove_GetY(obj));
	loop(2){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 0.3, rand(180, 360), rand(-0.01,-0.005), 0.05, 241,0);
	}
	Obj_Delete(obj);
}


task controlSpeed(obj, j, speed, pause){
	wait(pause-6*j);
	while(!Obj_IsDeleted(obj)){
		ObjMove_SetSpeed(obj, speed);
		wait(60);
		ObjMove_SetSpeed(obj, 0);
		wait(pause);
	}
}
		
