//e[Single]
#ScriptVersion[3]
#ID["DT_31_RAIKO_S03"]
//System["script/default_system/Default_System.txt"]
#Title["RAIKO_S03"]
#Text[""]
//Image[""]
//Background["script/default_system/Default_Background_IceMountain.txt"]
//Player[]

#include "./../../cmn/common.txt"
#include "./../../cmn/shotConst.txt"
#include "./../../cmn/Effect.txt"
#include "./../../cmn/boss/commonBossFunc.txt"
#include "./../../cmn/commonFunc.txt"
#include "./../../cmn/lib/lib_anime_Raiko.txt"

let objEnemy;	//GIuWFNg
let pathDot = GetCurrentScriptDirectory() ~ "./../../img/raiko_dot.png";
let pathCutin = GetCurrentScriptDirectory() ~ "./../../img/raiko_cutin.png";
let pathTimer = GetCurrentScriptDirectory() ~ "./../../se/timer.wav";
let pathShot = GetCurrentScriptDirectory() ~ "./../../se/laser1.wav";
let pathShot2 = GetCurrentScriptDirectory() ~ "./../../se/shot2.wav";
let pathShot3 = GetCurrentScriptDirectory() ~ "./../../se/re.wav";

@Loading{
	if(!IsCommonDataAreaExists(nsStage)){
		LoadTexture(pathDot);
		LoadTexture(pathCutin);
		LoadSound(pathTimer);
		LoadSound(pathShot);
		LoadSound(pathShot2);
		LoadSound(pathShot3);
	}
}

@Initialize{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	SetShotAutoDeleteClip(64,64,64,64);
	
	InitializeAction(objEnemy, pathDot);
	Render();
//	BackGround(pathCutin, 450, 600);
	Schedule();
}

@MainLoop{
	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, ex, ey, 32);//蔻(e)o^
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ex, ey, 24);//蔻(̓)o^
	
	yield;
}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
//		SetScriptResult(2500);	//Ctݒ
		SetScriptResult(5000);	//Ctݒ
	}
	case(EV_REQUEST_TIMER){
		let timerBoss = 60;
		SetScriptResult(timerBoss);	//Ԑݒ
		TimerSE(timerBoss);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(10);	//XyJ[h{[iXݒ
		T_SpellCardBonus(50000);
	}
//	case(EV_REQUEST_IS_DURABLE_SPELL){
//		SetScriptResult(true);	//ϋvXyJݒ
//	}
	others{}
}

@Finalize{}

task Schedule(){
//	ObjMove_SetX(objEnemy, GetStgFrameWidth()+64);
//	ObjMove_SetY(objEnemy, GetStgFrameHeight()/2);
	ObjMove_SetDestAtFrame(objEnemy, GetStgFrameWidth()-100, GetStgFrameHeight()/2, 60);
	SetAction(ACT_MOVE, 60);
	Wait(60);
	
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	CutIn(pathCutin,180,191,700,600);
	SetAction(ACT_SPELL,120);
	Wait(180);
	
	Fire();
	End(-1);
	
	Wait(30);
	SetDamageRateType();
}

//U
task Fire(){
	let ang1 = rand(0,360);
	let ang2 = rand(0,360);
	let way = 8;
	
	Fire2();
	Fire3();
	
	loop{
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){	return;	}
		loop(way){
			Bullet(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy)-64,ang1);
			Bullet(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy)+64,ang2);
			ang1 += (360/way);
			ang2 += (360/way);
		}
		ang1 += 7;
		ang1 %= 360;
		ang2 -= 7;
		ang2 %= 360;
		
		Wait(12);
	}
}

task Bullet(let x,let y,let ang){
	let way = 3;
	let mAng = rand(0,360);
	let shot;
	let len = 8;
	
	loop(way){
		shot = CreateShotA1(x+len*cos(mAng),y+len*sin(mAng),4,ang,S_SMALL+S_PINK,10);
		Shot_Modify(shot);
		mAng += (360/way);
	}
}

task Fire2(){
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 4000){	yield;	}
	
	let way = 2;
	let tY = 0;
	let dY = GetScreenHeight()/way;
	loop{
		SetAction(ACT_SHOT_A,36);
		tY = 0;
		Wait(12);
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){	return;	}
		loop(way){
			Taiko(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),GetClipMaxX(),tY+dY/2);
			tY += dY;
		}
		Wait(24);
		Wait(60);
	}
}

task Taiko(let x,let y,let tX,let tY){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,30);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory()~"./../../img/taiko.png");
	ObjSprite2D_SetSourceRect(obj,0,0,40,48);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
//	ObjRender_SetScaleX(obj,2);
	ObjRender_SetAngleZ(obj,90);
	
	let objAng = 0;
	
	let mAng = GetAngleToObj(x,y,tX,tY);
	let mSpd = 3;
	let nowX = x;
	let nowY = y;
	
	while(nowX < GetClipMaxX()){
		ObjRender_SetAngleZ(obj,objAng);
		objAng += 6;
		nowX += mSpd * cos(mAng);
		nowY += mSpd * sin(mAng);
		ObjRender_SetPosition(obj,nowX,nowY,0);
		yield;
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){	Obj_Delete(obj);	return;	}
	}
	PlaySE(pathShot);
	mSpd = rand(4,5);
	objAng = 90;
	ObjRender_SetAngleZ(obj,objAng);
	
	let laser = CreateLooseLaserA1(nowX+24,nowY,mSpd,180,128,10,S_ZORI_N+S_YELLOW,0);
	Shot_Modify(laser);
	
	while(nowX > 10){
		nowX -= mSpd;
		ObjRender_SetPosition(obj,nowX,nowY,0);
		yield;
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){	Obj_Delete(obj);	return;	}
	}
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){	Obj_Delete(obj);	return;	}
	let ang = GetAngleToPlayer(objEnemy);	// 炷
	let way = 8;
	PlaySE(pathShot2);
	let shot;
	loop(way){
		shot = CreateShotA1(ObjMove_GetX(laser),ObjMove_GetY(laser),2,ang,S_NORMAL+S_YELLOW,0);
		Shot_Modify(shot);
		ang += (360/way);
	}
	
	Wait(10);
	ObjShot_FadeDelete(laser);
	Obj_Delete(obj);
}

task Fire3(){
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 2000){	yield;	}
	
	let clrs = [S_RED,S_ORANGE,S_YELLOW,S_GREEN,S_BGREEN,S_BLUE,S_RPURPLE];
	let clr = 0;
	let way = 20;
	let ang = 0;
	let shot;
	
	loop{
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){	return;	}
		ang = GetAngleToPlayer(objEnemy);
		clr = 0;
		PlaySE(pathShot3);
		loop(7){
			loop(way){
				shot = CreateShotA1(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),7,ang,S_NOTE+clrs[clr],0);
				Shot_Modify(shot);
				ang += (360/way);
			}
			clr++;
			ang %= 360;
			Wait(10);
		}
		Wait(30);
	}
}
