#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell2"]
#Text["Tengu Sign \"White Wolf, Black 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";
let mirror;
let imgmirror = GetCurrentScriptDirectory ~ "./img/Mirror.png";

#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(4400);
	}
	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 Sign \"White Wolf, Black Wolf\"",230);
	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;
	}


}

task renderMirror {
	let dir;
	let speed;
	
	ObjPrim_SetTexture(mirror,imgmirror);
	ObjSprite2D_SetSourceRect(mirror,0,0,39,54);
	ObjSprite2D_SetDestCenter(mirror);
	ObjRender_SetBlendType(mirror,BLEND_ADD_ARGB);
	ObjRender_SetColorHSV(mirror,359,0,255);
	
	while(!Obj_IsDeleted(mirror)) {
 		dir = ObjMove_GetAngle(mirror);
  		speed = ObjMove_GetSpeed(mirror);
 
  		if(speed == 0) {ObjSprite2D_SetSourceRect(mirror,0,0,39,54); ObjRender_SetAngleXYZ(mirror,0,180,0); ObjRender_SetScaleXYZ(mirror,1,1,0);}
  		else if(cos(dir) < 0) {ObjSprite2D_SetSourceRect(mirror,0,0,39,54); ObjRender_SetAngleXYZ(mirror,0,180,0); ObjRender_SetScaleXYZ(mirror,1,1,0);}
  		else if(cos(dir) > 0) {ObjSprite2D_SetSourceRect(mirror,0,0,39,54); ObjRender_SetAngleXYZ(mirror,0,0,0); ObjRender_SetScaleXYZ(mirror,1,1,0);}
 
  		yield;
	}


}

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

function mirrorToPlayer {
	let dir = atan2(GetPlayerY-ObjMove_GetY(mirror),GetPlayerX-ObjMove_GetX(mirror));
	return dir;
}

task fire {
	wait(130);
	ascent(i in 1..80){ScreenShake(i,1); yield;}
	mirror = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(mirror);
	renderMirror;
	ObjRender_SetPosition(mirror,bossX,bossY,0);
	ObjMove_SetDestAtWeight(cirno,112,110,6,6);
	ObjMove_SetDestAtWeight(mirror,192+80,110,6,6);

	loop{
		wait(60);
		loop(2){
			let familiar = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar);
			let familiar2 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar2);
			let familiar3 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar3);
			let familiar4 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar4);
			familiarmove(familiar,0);
			familiarmove(familiar2,90);
			familiarmove(familiar3,180);
			familiarmove(familiar4,270);
			mirrorattack;
			wait(180);
		}
		ObjMove_SetDestAtWeight(mirror,112,110,6,6);
		ObjMove_SetDestAtWeight(cirno,192+80,110,6,6);
		wait(60);
		loop(2){
			let familiar = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar);
			let familiar2 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar2);
			let familiar3 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar3);
			let familiar4 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar4);
			familiarmove(familiar,0);
			familiarmove(familiar2,90);
			familiarmove(familiar3,180);
			familiarmove(familiar4,270);
			mirrorattack;
			wait(180);
		}
		ObjMove_SetDestAtWeight(cirno,112,110,6,6);
		ObjMove_SetDestAtWeight(mirror,112,310,6,6);
		wait(60);
		loop(2){
			let familiar = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar);
			let familiar2 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar2);
			let familiar3 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar3);
			let familiar4 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar4);
			familiarmove(familiar,0);
			familiarmove(familiar2,90);
			familiarmove(familiar3,180);
			familiarmove(familiar4,270);
			mirrorattack2;
			wait(180);
		}
		ObjMove_SetDestAtWeight(cirno,192+80,110,6,6);
		ObjMove_SetDestAtWeight(mirror,192+80,310,6,6);
		wait(60);
		loop(2){
			let familiar = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar);
			let familiar2 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar2);
			let familiar3 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar3);
			let familiar4 = ObjEnemy_Create(OBJ_ENEMY);
			ObjEnemy_Regist(familiar4);
			familiarmove(familiar,0);
			familiarmove(familiar2,90);
			familiarmove(familiar3,180);
			familiarmove(familiar4,270);
			mirrorattack2;
			wait(180);
		}
		ObjMove_SetDestAtWeight(cirno,112,110,6,6);
		ObjMove_SetDestAtWeight(mirror,192+80,110,6,6);
	}
	
}

task mirrorattack{
	
	let dir = mirrorToPlayer;
	ascent(i in 0..30){
		CreateLooseLaserA1(ObjMove_GetX(mirror),ObjMove_GetY(mirror),3,dir+i*7,50,16,3,20);
		CreateLooseLaserA1(ObjMove_GetX(mirror),ObjMove_GetY(mirror),3,dir-i*7,50,16,3,20);
		wait(4);
	}
	
}

task mirrorattack2{
	
	loop(3){
		CreateLooseLaserA1(ObjMove_GetX(mirror),ObjMove_GetY(mirror),2,mirrorToPlayer,50,30,299,40);
		CreateLooseLaserA1(ObjMove_GetX(mirror),ObjMove_GetY(mirror),2,mirrorToPlayer+20,50,30,299,40);
		CreateLooseLaserA1(ObjMove_GetX(mirror),ObjMove_GetY(mirror),2,mirrorToPlayer-20,50,30,299,40);
		wait(60);
	}
}

task familiarmove(obj,angle){

	let dir = angleToPlayer;
	let fireframe = 0;
	let fireangle = 0;
	ascent(i in 0..40){
		ObjMove_SetPosition(obj,bossX+2*i*cos(i*1.5+angle),bossY+2*i*sin(i*1.5+angle));
		ascent(j in 0..4){
			if(fireframe == 1){
				CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),4,360/4*j+dir,-0.1,2,278,20);
				PlaySE(shotsound1);
			}
		}
		fireframe++;
		if(fireframe == 4){fireframe = 0;}
		yield;
	}
	loop(360/18){
		CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),5,fireangle,-0.1,1,39,20);
		CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),7,fireangle,-0.5,3,39,20);
		fireangle = fireangle+360/18;
	}
	PlaySE(shotsound3);
	Obj_Delete(obj);
	
}

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;
	}
	SetCommonData("X",bossX);
	SetCommonData("Y",bossY);
	TExplosionA(bossX,bossY,10,0.5);
	Obj_Delete(cirno);
	Obj_Delete(mirror);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID);
}