#include"./system/ZUN_ShotConst.txt"
#include"./system/Default_EnemyConst.txt"
#include"./system/Sound.txt"
#include"./system/Music.txt"

let objEnemy;
let enmx;
let enmy;
let timer;
let EnemyMotion = true;
let EnemySpell = false;
let EnemySpell2 = false;
let EnemySpell3 = false;
let shadowS = false;
let shadowT = [60,3];

let DS_NUKE_RED = 0;
let DS_NUKE_PURPLE = 1;
let DS_NUKE_BLUE = 2;
let DS_NUKE_SKY = 3;
let DS_NUKE_GREEN = 4;
let DS_NUKE_YELLOW = 5;
let DS_NUKE_ORANGE = 6;
let DS_NUKE_WHITE = 7;

function GetDistance(x,y,x2,y2)
{
	return (((x2-x)^2)+((y2-y)^2))^(1/2);
}

task Obj_DelayDelete(target,time)
{
	loop(time){yield;}
	Obj_Delete(target);
}

task TXY
{
	let obj = [
			CreateStraightLaserA2(0,0,10,0,GetStgFrameHeight*2,48,0,DS_BEAM_WHITE,10^10),
			CreateStraightLaserA2(0,0,10,90,GetStgFrameHeight*2,48,0,DS_BEAM_WHITE,10^10),
			CreateStraightLaserA2(0,0,10,180,GetStgFrameHeight*2,48,0,DS_BEAM_WHITE,10^10),
			CreateStraightLaserA2(0,0,10,270,GetStgFrameHeight*2,48,0,DS_BEAM_WHITE,10^10),
			];
	ascent(i in 0..4)
		{
		ObjRender_SetAlpha(obj[i],150);
		ObjRender_SetColorHSV(obj[i],0,255,255);
		ObjStLaser_SetSource(obj[i],false);
		Obj_SetRenderPriorityI(obj[i],25);
		}
	while(!Obj_IsDeleted(objEnemy))
		{
		enmx = ObjMove_GetX(objEnemy);
		enmy = ObjMove_GetY(objEnemy);
		ascent(i in 0..4)
			{
			ObjMove_SetPosition(obj[i],enmx,enmy);
			}
		yield;
		}
}

function GetAngleToObject(target1,target2)
{
	let angle = atan2(ObjMove_GetY(target2)-ObjMove_GetY(target1),ObjMove_GetX(target2)-ObjMove_GetX(target1));
	return angle;
}

function CreateShotC1(x,y,speed,angle,graph,del)
{
	let bulletObj = CreateShotA1(x,y,0,angle,graph,0);
	TMove;
	return bulletObj;
	task TMove
		{
		let alph = 0;
		let scale = 2;
		ObjShot_SetIntersectionEnable(bulletObj,false);
		loop(del)
			{
			alph+=255/del;
			scale-=1/del;
			ObjRender_SetAlpha(bulletObj,alph);
			ObjRender_SetScaleXYZ(bulletObj,scale,scale,0);
			yield;
			}
		ObjMove_SetSpeed(bulletObj,speed);
		ObjShot_SetIntersectionEnable(bulletObj,true);
		ObjShot_SetSpellResist(bulletObj,false);
		}
}

function CreateShotC2(x,y,speed,angle,accel,max,graph,del)
{
	let bulletObj = CreateShotA1(x,y,0,angle,graph,0);
	TMove;
	return bulletObj;
	task TMove
		{
		let alph = 0;
		let scale = 2;
		ObjShot_SetIntersectionEnable(bulletObj,false);
		loop(del)
			{
			alph+=255/del;
			scale-=1/del;
			ObjRender_SetAlpha(bulletObj,alph);
			ObjRender_SetScaleXYZ(bulletObj,scale,scale,0);
			yield;
			}
		ObjMove_SetSpeed(bulletObj,speed);
		ObjMove_SetAcceleration(bulletObj,accel);
		ObjMove_SetMaxSpeed(bulletObj,max);
		ObjShot_SetIntersectionEnable(bulletObj,true);
		ObjShot_SetSpellResist(bulletObj,false);
		}
}


function CreateShotOA2(target,speed,angle,accel,max,graph,del)
{
	let bulletObj = CreateShotA2(ObjMove_GetX(target),ObjMove_GetY(target),speed,angle,accel,max,graph,del);
	return bulletObj;
}

function CreateStraightLaserA2(x,y,speed,angle,dist,width,del,graph,delay)
{
	let laserObj = CreateStraightLaserA1(x,y,angle,0,width,del,graph,delay);
	TFGrow;
	return laserObj;
	
	task TFGrow
		{
		let distA = 0;
		while(distA < dist)
			{
			if(Obj_IsDeleted(laserObj))
				{
				distA = dist;
				}
			else
				{
				distA+=speed;
				ObjLaser_SetLength(laserObj,distA);
				}
			yield;
			}
		ObjLaser_SetLength(laserObj,dist);
		}
}


task TSpellName(name)
{
	let x = 0;
	let y = 350;
	let z = 0;
	let spell = ObjText_Create;
	ObjText_SetText(spell, name);
	ObjText_SetFontColorTop(spell,100,250,150);
	ObjText_SetFontColorBottom(spell,50,125,75);
	ObjText_SetFontBorderWidth(spell,1);
	ObjText_SetFontBorderColor(spell,0,0,0);
	ObjText_SetFontBorderType(spell,BORDER_SHADOW);
	ObjText_SetHorizontalAlignment(spell, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(spell, GetStgFrameWidth);
	ObjText_SetFontSize(spell,12);
	Obj_SetRenderPriorityI(spell,75);
	ObjText_SetFontType(spell,0);
	ObjText_SetFontBold(spell,true);
	loop(20)
		{
		ObjRender_SetPosition(spell,x,y,0);
		ObjRender_SetAlpha(spell,z);
		z+=255/19;
		yield;
		}
	loop(30){yield;}	
	loop(60)
		{
		ObjRender_SetPosition(spell,x,y,0);
		y-=330/60;
		yield;
		}
	while(!Obj_IsDeleted(spell))
		{
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0)
			{
			Obj_Delete(spell);
			}
		yield;
		}
}

task TSongName(name)
{
	let x = 180;
	let y = 430;
	let z = 0;
	let song = ObjText_Create;
	ObjText_SetText(song, name);
	ObjText_SetFontBorderWidth(song,0.5);
	ObjText_SetFontBorderColor(song,0,0,0);
	ObjText_SetFontBorderType(song,BORDER_SHADOW);
	ObjText_SetHorizontalAlignment(song, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(song, GetStgFrameWidth);
	ObjText_SetFontSize(song,14);
	Obj_SetRenderPriorityI(song,75);
	ObjText_SetFontType(song,0);
	loop(90)
		{
		ObjRender_SetPosition(song,GetStgFrameWidth-GetStgFrameWidth*cos(x),y,0);
		x+=2;
		yield;
		}
	loop(60){yield;}
	loop(90)
		{
		ObjRender_SetPosition(song,GetStgFrameWidth-GetStgFrameWidth*cos(x),y,0);
		x+=2;
		yield;
		}
	Obj_Delete(song);
	
}

task ObjEnemy_SetIntersectionToPlayer(obj,value)
{
	Obj_SetValue(obj,"ENEMY_INTERSECTION_PLAYER_ENABLE",value);
}

function ObjEnemy_IsIntersectionToPlayer(obj)
{
	return Obj_GetValueD(obj,"ENEMY_INTERSECTION_PLAYER_ENABLE",true);
}

task ObjEnemy_SetIntersectionToShot(obj,value)
{
	Obj_SetValue(obj,"ENEMY_INTERSECTION_SHOT_ENABLE",value);
}

function ObjEnemy_IsIntersectionToShot(obj)
{
	return Obj_GetValueD(obj,"ENEMY_INTERSECTION_SHOT_ENABLE",true);
}

task TLife(target,delay,hit1,hit2,damage1,bomb1,damage2,bomb2)
{
	let counter = 0;
	while(!Obj_IsDeleted(target))
		{
		counter+=1;
		if(counter >=delay)
			{
			ObjEnemy_SetDamageRate(target,damage2,bomb2);	
			}
		else
			{
			ObjEnemy_SetDamageRate(target,damage1,bomb1);	
			}
		if(hit1 > 0 && ObjEnemy_IsIntersectionToShot(target) == true)
			{
			ObjEnemy_SetIntersectionCircleToShot(target,ObjMove_GetX(target),ObjMove_GetY(target),hit1);
			}
		if(hit2 > 0 && ObjEnemy_IsIntersectionToShot(target) == true)
			{
			ObjEnemy_SetIntersectionCircleToPlayer(target,ObjMove_GetX(target),ObjMove_GetY(target),hit2);
			}
		yield;
		}
}

task TEnd
{
	while(!Obj_IsDeleted(objEnemy))
		{
		if(timer <= 0 || ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			Obj_Delete(objEnemy);
			loop(1){yield;}
			CloseScript(GetOwnScriptID);
			DeleteShotAll(TYPE_ALL,TYPE_ITEM);
			}
		yield;
		}
}

function CreateNukeA1(x,y,speed,angle,accel,max,graph,scaleM,delay)
{
	let obj = CreateShotA2(x,y,speed,angle,accel,max,0,delay);
	TShotRender;
	return obj;
	task TShotRender
		{
		let objRend = 
			[
			ObjPrim_Create(OBJ_SPRITE_2D),
			ObjPrim_Create(OBJ_SPRITE_2D)	
			];
		let xM = ObjMove_GetX(obj);
		let yM = ObjMove_GetY(obj);
		let angleM = ObjMove_GetAngle(obj);
		let move1 = 0;
		let moveA = 0;
		let angle2 = [0,0];
		let scale = [scaleM,scaleM*1.1];
		let alpha = [200,75];
		let rect = [0,0];

		alternative( graph )
			case( DS_NUKE_RED ){ rect = [ 0, 0 ]; }
			case( DS_NUKE_ORANGE ){ rect = [ 1, 0 ]; }
			case( DS_NUKE_YELLOW ){ rect = [ 2, 0 ]; }
			case( DS_NUKE_GREEN ){ rect = [ 3, 0 ]; }
			case( DS_NUKE_SKY ){ rect = [ 0, 1 ]; }
			case( DS_NUKE_BLUE ){ rect = [ 1, 1 ]; }
			case( DS_NUKE_PURPLE ){ rect = [ 2, 1 ]; }
			case( DS_NUKE_WHITE ){ rect = [ 3, 1 ]; }

		loop(delay){yield;}
		ascent(i in 0..2)
			{
			ObjPrim_SetTexture(objRend[i],GetCurrentScriptDirectory ~ "./system/img/NUCLEAR.png");
			Obj_SetRenderPriorityI(objRend[i],51);		
			ObjRender_SetBlendType(objRend[i],BLEND_ADD_ARGB);		
			ObjSprite2D_SetSourceRect(objRend[i],rect[0]*256,rect[1]*256,256+rect[0]*256,256+rect[1]*256);
			ObjSprite2D_SetDestCenter(objRend[i]);
			}

		while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(objEnemy,INFO_LIFE) > 0)
			{
			angleM = ObjMove_GetAngle(obj);
			angle2[0] = angle2[0] - 64;
			angle2[1] = angle2[1] + 64;
			move1 = sin(moveA);
			ascent(i in 0..2)
				{
				ObjRender_SetPosition(objRend[i],ObjMove_GetX(obj),ObjMove_GetY(obj),0);
				ObjRender_SetAngleZ(objRend[i],angle2[i]);
				ObjRender_SetScaleXYZ(objRend[i],move1*scale[i],move1*scale[i],0);
				ObjRender_SetAlpha(objRend[i],move1*alpha[i]);
				}
			ObjShot_SetIntersectionCircleA1(obj,move1*(96*scaleM));
			if( move1 < 1 ){ moveA += 2; }
			yield;
			}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			ascent(i in 0..2)
				{
				Obj_Delete(objRend[i]);
				}	
			}
		else
			{
			move1 = 0;
			moveA = 0;
			while(move1 < 1)
				{
				move1 = sin(moveA);
				angle2[0] = angle2[0] - 64;
				angle2[1] = angle2[1] + 64;
				ascent(i in 0..2)
					{
					ObjRender_SetAngleZ(objRend[i],angle2[i]);
					ObjRender_SetScaleXYZ(objRend[i],scale[i]+move1*scale[i]/2,scale[i]+move1*scale[i]/2,0);
					ObjRender_SetAlpha(objRend[i],alpha[i]-move1*alpha[i]);
					}
				moveA+=10;
				yield;
				}
			ascent(i in 0..2)
				{
				Obj_Delete(objRend[i]);
				}
			}
		}	
}