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



let objBoss;
let width = 384;
let height = 448;
let life = 6000;
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);
	SetCommonData("non1", "no");
	SetCommonData("spell", "JamesSpell");
	
	
	animSpell(objBoss, 3);
	TFinalizeSpell(scene, objBoss, false, true);
	
	
    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{
    SetForbidPlayerShot(false);
    Name(3, objBoss);
    Indicator(objBoss, 3);
    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("Power of linear[r] convergence", objBoss, 3);
    wait(240);
    ObjEnemy_SetDamageRate(objBoss, 20, 0);
	
    let dir=1; if(rand(0,1)>=1/2){dir=-1;}
	let shot=LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 80);
	let shot2=LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK5.WAV", 80);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		let angIni= rand(180, 195); if(dir==-1){angIni= rand(345, 360);}
		let las= CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angIni, 300, 40, 1, 25, 600);
		con(las, dir);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Charge.WAV", 80));
		wait(60);
		ObjEnemy_SetDamageRate(objBoss, 60, 20);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Explosion.WAV", 90));
		shotSound(shot, 260, 60, 2, objBoss, false,0);
		ascent(i in 0..260){ 
		    if(i%25==0){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				sound;
				yield;
			}
			else{
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				sound;
				ascent(j in 0..25){
					if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
					let speed = rand(1.5,9); 
					let ang=rand(0,360);
					let angle = findAngRad(GetPlayerX, ObjMove_GetX(objBoss)+ j *15 * cos(angIni+dir*i*180/260),  GetPlayerY, ObjMove_GetY(objBoss)+ j *15 * sin(angIni+dir*i*180/260))[0];
					if(speed>=4){ang = angle+25+rand(-10,20); if (rand(0,1)>=1/2){ang = angle-25+rand(-20,10);}}
					let obj= CreateShotA1(ObjMove_GetX(objBoss)+ j *20 * cos(angIni+i*dir*180/260), ObjMove_GetY(objBoss)+ j *20 * sin(angIni+dir*i*180/260),
						0, angIni+i*dir*180/260 +180, 137, 30); bulletMove(obj, ang, speed);
					ObjShot_SetSpellResist(obj, true);
				}
				yield;
			}
		}
		dir*=-1; wait(260);
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), rand(height/5, 2*height/5), 60);
		wait(340);
		
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Charge.WAV", 80));
		wait(60); let t=0; let ang= GetAngleToPlayer(objBoss)+20;
		shotSound(shot2, 240, 70, 2, objBoss, false,0);
		if(ObjMove_GetX(objBoss)>width/2){
		    while(t<60){
			   ascent(i in 0..10){
			        if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				    let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, ang+36*i, 400, 0);
					ObjShot_SetSpellResist(obj, true);
				}
				t++; wait(2); ang++;
			}
			while(t<120){
			   ascent(i in 0..10){
			        if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				    let obj =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, ang+36*i, 400, 0);
					ObjShot_SetSpellResist(obj, true);
				}
				t++; wait(2); ang+=-1;
			}
		}
		else{
		    while(t<60){
			   ascent(i in 0..10){
			        if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				    let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, ang+36*i, 400, 0);
					ObjShot_SetSpellResist(obj, true);
				}
				t++; wait(2); ang+=-1;
			}
			while(t<120){
			   ascent(i in 0..10){
			        if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				    let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, ang+36*i, 400, 0);
					ObjShot_SetSpellResist(obj, true);
				}
				t++; wait(2); ang++;
			}
		}
		ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 60);
		wait(60);
	}
}
	
task sound{
    let t=0; while(t<340){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} yield; t++;} 
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 60));
}
	
task bulletMove(obj, ang, speed){
    wait(140);
	ObjMove_SetAngle(obj,ang);
	wait(200);
	ObjMove_SetSpeed(obj,speed);
	if(speed>=2.5 && speed<4){ObjMove_SetAcceleration(obj, rand(-0.2, -0.05)); ObjMove_SetMaxSpeed(obj, rand(0.5,1));}
}

	
task con(las, dir){
    wait(60);
	let ang = ObjStLaser_GetAngle(las);
	ascent(i in 0..260){ObjStLaser_SetAngle(las,ang+dir*i*180/260); yield;}
	Obj_Delete(las);
}
	
	
	
	
	
	