#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell1"]
#Text["First Nonspell"]

let cirno;
let bossX = 0;
let bossY = 0;
let imgBoss = GetCurrentScriptDirectory ~ "./img/Th125Momizi01.png";
let MagicCircle = GetCurrentScriptDirectory ~ "./img/magic.png";
let objcircle;
let charge = GetCurrentScriptDirectory ~ "./sounds/power_logo.wav";
let shotsound1 = GetCurrentScriptDirectory ~ "./sounds/shot1.wav";
let shotsound2 = GetCurrentScriptDirectory ~ "./sounds/shot2.wav";
let shotsound3 = GetCurrentScriptDirectory ~ "./sounds/shot3.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_SetPosition(cirno,210,-50);
	ObjMove_SetDestAtWeight(cirno,192,120,20,4);
	renderBoss;
	yourmagiccircle;
	
	LoadSounds;

	mainTask;

}

@Event {

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

}

@MainLoop {
	bossX = ObjMove_GetX(cirno);
	bossY = ObjMove_GetY(cirno);

	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);
}

task mainTask {
	
	LowDamageStart;
	fireX;
	TEnd;
	
}

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);
	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);
	}

	yield;

	}

	
}


task renderBoss {
	let dir;
	let speed;
	
	ObjPrim_SetTexture(cirno,imgBoss);
	ObjSprite2D_SetSourceRect(cirno,0,0,39,54);
	ObjSprite2D_SetDestCenter(cirno);
	
	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);}
 
  		yield;
	}


}


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

task fireX {
	
	loop{
		wait(70);
		PlaySE(charge);
		wait(80);
		let yamfam = ObjEnemy_Create(OBJ_ENEMY);
		ObjMove_SetPosition(yamfam,bossX,bossY);
		let dir = angleToPlayer;
		let fireangle = 0;
		let fireframe = 0;
		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,278,20);
			CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,dir+360/4*1,-0.5,2,278,20);
			CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,dir+360/4*2,-0.5,2,278,20);
			CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,dir+360/4*3,-0.5,2,278,20);
			if(fireframe%10 == 0){
				loop(360/18){
					CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),5,fireangle,-0.1,1,39,20);
					CreateShotA2(ObjMove_GetX(yamfam),ObjMove_GetY(yamfam),7,fireangle,-0.5,3,39,20);
					PlaySE(shotsound3);
					fireangle = fireangle+360/18;
				}
			}
			fireframe++;
			PlaySE(shotsound1);
			if(ObjEnemy_GetInfo(cirno,INFO_LIFE) <= 0){Obj_Delete(yamfam); return;}
			yield;
		}
		Obj_Delete(yamfam);
		wait(80);
		ObjMove_SetDestAtWeight(cirno,rand(192-100,192+100),rand(120-30,120+30),8,4);
		wait(40);
	}
	
}

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);
}