#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell1"]
#Text["Tengu Blade \"Claw of The White Wolf\""]

let cirno;
let bossX = 0;
let bossY = 0;
let imgBoss = GetCurrentScriptDirectory ~ "./img/Th125Momizi01.png";
let MagicCircle = GetCurrentScriptDirectory ~ "./img/magic.png";
let objcircle;
let isbehindscreen = 0;
let charge = GetCurrentScriptDirectory ~ "./sounds/power_logo.wav";
let shotsound1 = GetCurrentScriptDirectory ~ "./sounds/shot1.wav";
let shotsound2 = GetCurrentScriptDirectory ~ "./sounds/shot2.wav";
let shotsound3 = GetCurrentScriptDirectory ~ "./sounds/shot2.wav";
let spellcardwarn = GetCurrentScriptDirectory ~ "./sounds/spellcard.wav";
let wave = GetCurrentScriptDirectory ~ "./sounds/BulletWave.wav";
let slashsound = GetCurrentScriptDirectory ~ "./sounds/Slash.wav";
let pow = GetCurrentScriptDirectory ~ "./sounds/Eff2.wav";

#include "script\default_system\Default_Effect.txt"
#include "./Shot/AllStarShot_Const.dnh"
#include "./momijiCutin.txt"

@Initialize {
	
	cirno = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(cirno);
	ObjMove_SetDestAtWeight(cirno,192,120,6,4);
	renderBoss;
	yourmagiccircle;
	
	LoadSounds;

	mainTask;

}

@Event {

	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(2500);
	}
	case(EV_REQUEST_TIMER) {
		SetScriptResult(-1);
	}

}

@MainLoop {
	bossX = ObjMove_GetX(cirno);
	bossY = ObjMove_GetY(cirno);
	
	if(isbehindscreen == 0){
	ObjEnemy_SetIntersectionCircleToShot(cirno,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(cirno,bossX,bossY,18);
	}
	
	yield;
}

@Finalize {

}

function wait(w) { loop(w) { yield; } }

task LoadSounds {
LoadSound(charge);
LoadSound(shotsound1);
LoadSound(shotsound2);
LoadSound(shotsound3);
LoadSound(spellcardwarn);
LoadSound(wave);
LoadSound(slashsound);
LoadSound(pow);
}

task mainTask {
	
	TEnd;
	LowDamageStart;
	wait(60);
	cutinimage("Tengu Blade \"Claw of The White Wolf\"",250);
	PlaySE(spellcardwarn);
	wait(60);
	fire;
	
}

task LowDamageStart{ObjEnemy_SetDamageRate(cirno,3,10); wait(180); ObjEnemy_SetDamageRate(cirno,100,100);}


task yourmagiccircle {
	let circleframe = 0;
	let circlesize = 0.8;
	
	objcircle = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objcircle, MagicCircle);
	ObjSprite2D_SetSourceRect(objcircle,0,0,256,256);
	ObjSprite2D_SetDestCenter(objcircle);
	ObjRender_SetScaleXYZ(objcircle,circlesize,circlesize,0);
	ObjRender_SetBlendType(objcircle,BLEND_ALPHA);
	Obj_SetRenderPriorityI(objcircle, 25);

	while(!Obj_IsDeleted(cirno)) {

	ObjRender_SetPosition(objcircle, bossX, bossY, 0);
	ObjRender_SetAngleZ(objcircle, circleframe);
	circleframe+=2;

	if(ObjEnemy_GetInfo(cirno,INFO_LIFE) <= 0) {
		Obj_Delete(objcircle);
	}

	if(isbehindscreen == 0){
		ObjRender_SetAlpha(objcircle,255);
	}
	else if(isbehindscreen == 1){
		ObjRender_SetAlpha(objcircle,255/2);
	}

	yield;

	}

	
}


task renderBoss {
	let dir;
	let speed;
	
	ObjPrim_SetTexture(cirno,imgBoss);
	ObjSprite2D_SetSourceRect(cirno,0,0,39,54);
	ObjSprite2D_SetDestCenter(cirno);
	ObjRender_SetBlendType(cirno,BLEND_ALPHA);
	
	while(!Obj_IsDeleted(cirno)) {
 		dir = ObjMove_GetAngle(cirno);
  		speed = ObjMove_GetSpeed(cirno);
 
  		if(speed == 0) {ObjSprite2D_SetSourceRect(cirno,0,0,39,54); ObjRender_SetAngleXYZ(cirno,0,180,0); ObjRender_SetScaleXYZ(cirno,1,1,0);}
  		else if(cos(dir) < 0) {ObjSprite2D_SetSourceRect(cirno,0,0,39,54); ObjRender_SetAngleXYZ(cirno,0,180,0); ObjRender_SetScaleXYZ(cirno,1,1,0);}
  		else if(cos(dir) > 0) {ObjSprite2D_SetSourceRect(cirno,0,0,39,54); ObjRender_SetAngleXYZ(cirno,0,0,0); ObjRender_SetScaleXYZ(cirno,1,1,0);}
		
		if(isbehindscreen == 0){ObjRender_SetAlpha(cirno,255);}
		else if(isbehindscreen == 1){ObjRender_SetAlpha(cirno,255/2);}
 
  		yield;
	}


}


function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}

task fire {
	wait(140);
	loop{
		ObjMove_SetDestAtWeight(cirno,-100,-30,6,15);
		wait(80);
		isbehindscreen = 1;
		ObjRender_SetY(cirno,GetPlayerY);
		descent(i in 0..8){
			Sword(GetPlayerY);
			wait(i*10);
		}
		wait(120);
		isbehindscreen = 0;
		ObjMove_SetDestAtWeight(cirno,192,120,6,15);
		wait(80);
		BigSword;
		ObjMove_SetDestAtWeight(cirno,GetPlayerX,120,6,15);
		PlaySE(charge);
		wait(100);
		PlaySE(wave);
		let bullet_array = GetShotIdInCircleA2(192,224,1000,TARGET_ENEMY);
		ascent(i in 0..length(bullet_array)){
			ObjMove_SetAcceleration(bullet_array[i],0.01);
			ObjMove_SetMaxSpeed(bullet_array[i],2);
		}
		wait(180);
	}
	
}

task Sword(Y){
	
	let LaserY = Y+rand(-100,100);
	let slash = CreateStraightLaserA1(-20,LaserY,0,800,24,10,1001,60);
	let dir = GetAngleToPlayer(slash);
	ObjStLaser_SetAngle(slash,dir);
	wait(60);
	PlaySE(pow);
	if(ObjMove_GetX(cirno) < 0) {ObjMove_SetDestAtWeight(cirno,GetStgFrameWidth+100,GetPlayerY,6,40);}
	else if(ObjMove_GetX(cirno) > GetStgFrameWidth){ObjMove_SetDestAtWeight(cirno,-100,GetPlayerY,6,40);}
	let familiar = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(familiar);
	ObjMove_SetPosition(familiar,-20,LaserY);
	ObjMove_SetSpeed(familiar,15);
	ObjMove_SetAngle(familiar,dir);
	let fireframe = 0;
	while(ObjMove_GetX(familiar) != GetStgFrameWidth+5){
		if(fireframe == 1){CreateShotA1(ObjMove_GetX(familiar),ObjMove_GetY(familiar),0,rand(0,360),21,0);}
		fireframe++;
		if(fireframe == 3){fireframe = 0;}
		yield;
	}
	Obj_Delete(familiar);
	
}

task BigSword{
	
	let slash = CreateStraightLaserA1(GetPlayerX,-20,90,600,100,10,1001,60);
	let familiar = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(familiar);
	ObjMove_SetPosition(familiar,GetPlayerX,-20);
	wait(60);
	ScreenShake(30,20);
	ObjMove_SetSpeed(familiar,15);
	ObjMove_SetAngle(familiar,90);
	PlaySE(slashsound);
	let fireframe = 0;
	while(ObjMove_GetY(familiar) != GetStgFrameHeight+5){
		if(fireframe >= 1){loop(2){let bigbullet = CreateShotA2(ObjMove_GetX(familiar),ObjMove_GetY(familiar),0,rand(0,360),0.01,2,327,0); ObjRender_SetBlendType(bigbullet,BLEND_ADD_ARGB);}}
		fireframe++;
		if(fireframe == 2){fireframe = 0;}
		yield;
	}
	Obj_Delete(familiar);
	
}

task ScreenShake(distance,time){

	loop(time)
	{
		Set2DCameraFocusX(rand(-distance+GetStgFrameWidth/2,distance+GetStgFrameWidth/2));
		Set2DCameraFocusY(rand(-distance+GetStgFrameHeight/2,distance+GetStgFrameHeight/2));
		yield;
	}

	Reset2DCamera();

}

task TEnd {
	while(ObjEnemy_GetInfo(cirno,INFO_LIFE) > 0) {
		yield;
	}
	TExplosionA(bossX,bossY,10,0.5);
	Obj_Delete(cirno);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID);
}