#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell3 (YAM)"]
#Text["I Had To Do This \"YAM of The Wolf -I'm So Sorry!-\""]

let cirno;
let tarumi;
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 charge2 = GetCurrentScriptDirectory ~ "./sounds/Charge3.wav";
let shotsound1 = GetCurrentScriptDirectory ~ "./sounds/shot1.wav";
let shotsound2 = GetCurrentScriptDirectory ~ "./sounds/shot2.wav";
let shotsound3 = GetCurrentScriptDirectory ~ "./sounds/shot3.wav";
let shot3 = ObjSound_Create;
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 caution = GetCurrentScriptDirectory ~ "./sounds/caution.wav";
let boon = GetCurrentScriptDirectory ~ "./sounds/Boon1.wav";
let famsound = GetCurrentScriptDirectory ~ "./sounds/fam.wav";
let explode = GetCurrentScriptDirectory ~ "./sounds/stars.wav";
let sploosh = GetCurrentScriptDirectory ~ "./sounds/water.wav";
let defeat = GetCurrentScriptDirectory ~ "./sounds/enemoff.wav";
let yamdelete = 0;
let shotlock = 0;

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

@Initialize {
	
	cirno = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(cirno);
	ObjMove_SetPosition(cirno,GetCommonData("X",0),GetCommonData("Y",0));
	ObjMove_SetDestAtWeight(cirno,192,120,6,4);
	renderBoss;
	yourmagiccircle;
	CheckFor00;
	
	LoadSounds;

	mainTask;

}

task CheckFor00{loop(60){DeleteShotAll(TYPE_ALL,TYPE_ITEM); yield;}}

@Event {

	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(4300);
	}
	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(spellcardwarn);
LoadSound(wave);
LoadSound(slashsound);
LoadSound(pow);
LoadSound(caution);
LoadSound(charge2);
LoadSound(boon);
LoadSound(famsound);
LoadSound(explode);
LoadSound(sploosh);
LoadSound(defeat);
ObjSound_Load(shot3,shotsound3);
ObjSound_SetSoundDivision(shot3,SOUND_SE);
ObjSound_SetVolumeRate(shot3,50);
}

task mainTask {
	
	TEnd;
	LowDamageStart;
	wait(60);
	memewarning;
	PlaySE(caution);
	wait(60*3+30);
	SetCommonData("musicswap",2);
	cutinimage("I Had To Do This \"YAM of The Wolf -I'm So Sorry!-\"",300);
	PlaySE(spellcardwarn);
	wait(180);
	fire;
	
}

task LowDamageStart{ObjEnemy_SetDamageRate(cirno,3,10); wait(60*10); 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 {
	
	tarumi = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(tarumi);
	renderTarumi(tarumi);
	ObjMove_SetPosition(tarumi,-50,50);
	ObjMove_SetDestAtWeight(cirno,GetStgFrameWidth/4*3,100,10,6);
	ObjMove_SetDestAtWeight(tarumi,GetStgFrameWidth/4*1,100,10,6);
	fillerbullets;
	loop{
	wait(100);
	ObjMove_SetDestAtWeight(tarumi,GetPlayerX,100,10,6);
	yamdelete=0;
	wait(60);
	SetCommonData("CastAnimation",1);
	if(ObjEnemy_GetInfo(cirno,INFO_LIFE) > 0){PlaySE(charge2);}
	else{return;}
	if(ObjMove_GetX(tarumi) <= 192){ObjMove_SetDestAtWeight(cirno,ObjMove_GetX(tarumi)+150,150,8,4);}
	else{ObjMove_SetDestAtWeight(cirno,ObjMove_GetX(tarumi)-150,150,8,4);}
	wait(120);
	SetCommonData("CastAnimation",2);
	wait(10);
	if(ObjEnemy_GetInfo(cirno,INFO_LIFE) > 0){yam(ObjMove_GetX(tarumi),ObjMove_GetY(tarumi)); PlaySE(boon);}
	else{return;}
	wait(70);
	if(ObjEnemy_GetInfo(cirno,INFO_LIFE) > 0){momijiyam; PlaySE(famsound);}
	else{return;}
	wait(40);
	SetCommonData("CastAnimation",0);
	ObjMove_SetDestAtWeight(tarumi,rand(192-140,192+140),rand(50,150),10,6);
	wait(60*3);
	yamdelete=1;
	yield;
	}
	
}

task yam(x,y){
	
	let shot = CreateShotA2(x,y,0,90,0.05,4,1,0);
	let yam = ObjPrim_Create(OBJ_SPRITE_2D);
	let imgyam = GetCurrentScriptDirectory ~ "./img/Yam.png";
	Obj_SetVisible(shot,false);
	ObjShot_SetAutoDelete(shot,false);
	ObjShot_SetSpellResist(shot,true);
	Obj_SetRenderPriorityI(yam,50);

	ObjPrim_SetTexture(yam,imgyam);
	ObjSprite2D_SetSourceRect(yam,0,0,279,221);
	ObjSprite2D_SetDestCenter(yam);
	ObjRender_SetAngleZ(yam,-90);
	yamshake(shot);
	
	let soundlock = 0;
	while(yamdelete == 0){
		ObjRender_SetPosition(yam,ObjMove_GetX(shot),ObjMove_GetY(shot),0);
		ObjShot_SetIntersectionCircleA1(shot,60);
		if(ObjMove_GetY(shot) >= GetStgFrameHeight){
			if (soundlock == 0){PlaySE(explode); soundlock++;}
			ObjMove_SetSpeed(shot,0);
			ObjMove_SetMaxSpeed(shot,0);
			ObjMove_SetAcceleration(shot,0);
		}
		yield;
	}
	Obj_Delete(shot);
	Obj_Delete(yam);
	
}

task momijiyam{
	
	let yamfam = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(yamfam);
	let imgyam = GetCurrentScriptDirectory ~ "./img/Yam.png";
	ObjPrim_SetTexture(yamfam,imgyam);
	ObjSprite2D_SetSourceRect(yamfam,0,0,279,221);
	ObjSprite2D_SetDestCenter(yamfam);
	ObjRender_SetScaleXYZ(yamfam,0.2,0.2,0);
	yamspin;
	if(ObjEnemy_GetInfo(cirno,INFO_LIFE) <= 0){Obj_Delete(yamfam); return;}
	ObjMove_SetPosition(yamfam,ObjMove_GetX(tarumi),ObjMove_GetY(tarumi));
	ObjMove_SetDestAtWeight(yamfam,bossX,bossY,3,3);
	wait(70);
	let dir = angleToPlayer;
	if(ObjEnemy_GetInfo(cirno,INFO_LIFE) <= 0){Obj_Delete(yamfam); return;}
	ascent(i in 1..90){
		ObjMove_SetPosition(yamfam,bossX+2*i*0.8*cos(i*6),bossY+2*i*0.8*sin(i*6));
		CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,dir,-0.5,2,282,0);
		CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,dir+360/3*1,-0.5,2,282,0);
		CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,dir+360/3*2,-0.5,2,282,0);
		PlaySE(shotsound1);
		if(ObjEnemy_GetInfo(cirno,INFO_LIFE) <= 0){Obj_Delete(yamfam); return;}
		yield;
	}
	Obj_Delete(yamfam);

	task yamspin{
		let a = 0;
			while(!Obj_IsDeleted(yamfam)){
			ObjRender_SetAngleZ(yamfam,a);
			a=a+7;
			yield;
		}
	}
	
}

task yamshake(obj){
	while(ObjMove_GetY(obj) <= GetStgFrameHeight){yield;}
	shotlock++;
	ScreenShake(5,10);
	let yamfamiliar1 = ObjEnemy_Create(OBJ_ENEMY);
	let yamfamiliar2 = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(yamfamiliar1);
	ObjEnemy_Regist(yamfamiliar2);
	ObjMove_SetPosition(yamfamiliar1,ObjMove_GetX(obj),ObjMove_GetY(obj));
	ObjMove_SetPosition(yamfamiliar2,ObjMove_GetX(obj),ObjMove_GetY(obj));
	ObjMove_SetAngle(yamfamiliar2,180);
	ObjMove_SetSpeed(yamfamiliar1,3);
	ObjMove_SetSpeed(yamfamiliar2,3);
	loop(40){
		CreateShotB2(ObjMove_GetX(yamfamiliar1),ObjMove_GetY(yamfamiliar1),rand(-1,1),-3,0,0.05,1,3,47,0);
		CreateShotB2(ObjMove_GetX(yamfamiliar2),ObjMove_GetY(yamfamiliar2),rand(-1,1),-3,0,0.05,1,3,47,0);
		PlaySE(sploosh);
		wait(2);
	}
	Obj_Delete(yamfamiliar1);
	Obj_Delete(yamfamiliar2);
	
}

task fillerbullets{

	let soundlock = 0;
	while(shotlock == 0){yield;}
	while(shotlock > 0){
		while(ObjEnemy_GetInfo(cirno,INFO_LIFE) >= 1433*2 && ObjEnemy_GetInfo(cirno,INFO_LIFE) <= 4300){
			CreateShotA2(rand(0,GetStgFrameWidth),rand(0,120),0,rand(85,95),0.05,2,337,30);
			ObjSound_Play(shot3);
			wait(10);
		}
		while(ObjEnemy_GetInfo(cirno,INFO_LIFE) >= 1433 && ObjEnemy_GetInfo(cirno,INFO_LIFE) < 1433*2){
			CreateShotA2(rand(0,GetStgFrameWidth),rand(0,120),0,rand(85,95),0.05,2,337,30);
			ObjSound_Play(shot3);
			wait(8);
		}
		while(ObjEnemy_GetInfo(cirno,INFO_LIFE) < 1433 && ObjEnemy_GetInfo(cirno,INFO_LIFE) > 0){
			CreateShotA2(rand(0,GetStgFrameWidth),rand(0,120),0,rand(85,95),0.05,2,337,30);
			ObjSound_Play(shot3);
			wait(5);
		}
	}

}

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 TossFlowerPetal(x,y,angle,speed){
	let mx = speed*cos(angle);
	let my = speed*sin(angle);
	let f = 0;
	let s = 0;
	let e = 0;
	let ins = 0;
	let a = 255;
	let cl= ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(cl,BLEND_ALPHA);
	ObjRender_SetAlpha(cl,a);
	Obj_SetRenderPriority(cl,0.80);
	ObjPrim_SetTexture(cl,GetCurrentScriptDirectory ~ "./img/FlowerPetal.png");
	ObjSprite2D_SetSourceRect(cl,0,0,23,24);
	ObjSprite2D_SetDestCenter(cl);
	//ObjRender_SetColor(cl,255,0,0);
	ObjRender_SetX(cl,x);
	ObjRender_SetY(cl,y);
	while(!Obj_IsDeleted(cl)){
	f++;
	ObjRender_SetPosition(cl,ObjRender_GetX(cl)+mx,ObjRender_GetY(cl)+my,0);
		ObjRender_SetScaleXYZ(cl,1+sin(e)*0.2,1+sin(e)*0.2,1);
		e+=1.90;
	ObjRender_SetAlpha(cl,a);
	if(a > 0){a=a-1;}
	yield;
	}
	e+=2.10;
	ins+=0.01;
	s+=0.01;
	if(f==620){Obj_Delete(cl);}
	if(Obj_IsDeleted(cirno)){Obj_Delete(cl);}
	yield;
}

task TEnd {
	while(ObjEnemy_GetInfo(cirno,INFO_LIFE) > 0) {
		yield;
	}
	shotlock=0;
	TExplosionA(bossX,bossY,10,0.5);
	yamdelete = 1;
	PlaySE(defeat);
	loop(10){TossFlowerPetal(ObjMove_GetX(tarumi),ObjMove_GetY(tarumi),rand(0,360),3);}
	Obj_Delete(tarumi);
	loop(100){TossFlowerPetal(ObjMove_GetX(cirno),ObjMove_GetY(cirno),rand(0,360),3); DeleteShotAll(TYPE_ALL,TYPE_ITEM); yield;}
	loop(20){TossFlowerPetal(ObjMove_GetX(cirno),ObjMove_GetY(cirno),rand(0,360),3);}
	Obj_Delete(cirno);
	PlaySE(defeat);
	loop(240){DeleteShotAll(TYPE_ALL,TYPE_ITEM); yield;}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID);
}