let CSD = GetCurrentScriptDirectory;

let MinX=32;
let MaxX=380;
let MinY=0;
let MaxY=464;
let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;

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

function Distance(x1, y1, x2, y2){
	return ((x1-x2)^2 + (y1-y2)^2)^0.5;
}

function SetScore(let v) { if(GetEventType == EV_REQUEST_SPELL_SCORE) { SetScriptResult(v); } }
function SetLife(let v) { if(GetEventType == EV_REQUEST_LIFE) { SetScriptResult(v); } }
function SetTimer(let v) { if(GetEventType == EV_REQUEST_TIMER) { SetScriptResult(v); } }
function GetAngleTo(let object1, let object2) { return atan2(ObjMove_GetY(object2)-ObjMove_GetY(object1), ObjMove_GetX(object2)-ObjMove_GetX(object1));}

task renderNazrinUFO(obj){

	let wi = 64;
	let he = 80;
	
	let dir;
	let speed;
	let whichway = 0;
	let frame = 0;
	let idleframe = 0;
	let castframe = 0;
	Obj_SetValue(obj,"cast",0);

	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/NazrinUFOSprite.png");
	ObjSprite2D_SetSourceRect(obj,0,0,wi,he);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetAngleXYZ(obj,0,180,0);
	
	while(!Obj_IsDeleted(obj)){
		
		dir = ObjMove_GetAngle(obj);
		speed = ObjMove_GetSpeed(obj);

		if(speed == 0 && frame > 0){whichway = -1;}
		else if(speed == 0 && frame < 0){whichway = 1;}
		else if(speed == 0 && frame == 0){whichway = 0;}

		else if(cos(dir) < 0){whichway=-1;}
		else if(cos(dir) > 0){whichway=1;}
		
		if(Obj_GetValueD(obj,"cast",0) == 0 && speed == 0 && frame == 0){

			castframe = 0;
			if(idleframe >= 0 && idleframe < 10){ObjSprite2D_SetSourceRect(obj,0,0,wi,he);}
			else if(idleframe >= 10 && idleframe < 20){ObjSprite2D_SetSourceRect(obj,wi,0,wi*2,he);}
			else if(idleframe >= 20 && idleframe < 30){ObjSprite2D_SetSourceRect(obj,wi*2,0,wi*3,he);}
			else if(idleframe >= 30 && idleframe < 40){ObjSprite2D_SetSourceRect(obj,wi*3,0,wi*4,he);}
			
			if(idleframe < 40){idleframe++;}
			else if(idleframe == 40){idleframe=0;}
			
		}
		
		else if(Obj_GetValueD(obj,"cast",0) == 1 && speed == 0 && frame == 0){
			
			idleframe=0;
			frame=0;
			if(castframe >= 0 && castframe < 7){ObjSprite2D_SetSourceRect(obj,0,he*2,wi,he*3);}
			if(castframe >= 7 && castframe < 14){ObjSprite2D_SetSourceRect(obj,wi,he*2,wi*2,he*3);}
			if(castframe >= 14 && castframe < 21){ObjSprite2D_SetSourceRect(obj,wi*2,he*2,wi*3,he*3);}
			if(castframe >= 21 && castframe < 22){ObjSprite2D_SetSourceRect(obj,wi*3,he*2,wi*4,he*3);}
			if(castframe < 22){castframe++;}
		}

		else if(speed != 0 || frame != 0){
		
			if(frame < 0 && frame >= -8){ObjSprite2D_SetSourceRect(obj,0,he,wi,he*2); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame < -8 && frame >= -16){ObjSprite2D_SetSourceRect(obj,wi,he,wi*2,he*2); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame < -16 && frame >= -24){ObjSprite2D_SetSourceRect(obj,wi*2,he,wi*3,he*2); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame < -24 && frame >= -25){ObjSprite2D_SetSourceRect(obj,wi*3,he,wi*4,he*2); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame > 0 && frame <= 8){ObjSprite2D_SetSourceRect(obj,0,he,wi,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 8 && frame <= 16){ObjSprite2D_SetSourceRect(obj,wi,he,wi*2,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 16 && frame <= 24){ObjSprite2D_SetSourceRect(obj,wi*2,he,wi*3,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 24 && frame <= 25){ObjSprite2D_SetSourceRect(obj,wi*3,he,wi*4,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			idleframe=0;
			castframe=0;
			
		}
		
		frame=frame+whichway;
		if(frame > 25){frame=25;}
		if(frame < -25){frame=-25;}
		yield;
		
	}
	
}

task RunReflectCode(obj)
{
	while(!Obj_IsDeleted(obj))
	{		
		if(trunc(ObjMove_GetX(obj))>GetStgFrameWidth)
		{
			ObjMove_SetX(obj,GetStgFrameWidth-ObjMove_GetX(obj)+GetStgFrameWidth);
			ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
			return;
		}
    
		if(trunc(ObjMove_GetX(obj))<0)
		{	
			ObjMove_SetX(obj,-ObjMove_GetX(obj));
			ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
			return;
		}
        if(trunc(ObjMove_GetY(obj))>GetStgFrameHeight)
		{
			ObjMove_SetY(obj,448-ObjMove_GetY(obj)+448);
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
			return;
        }
        if(trunc(ObjMove_GetY(obj))<0)
		{
			ObjMove_SetY(obj,-ObjMove_GetY(obj));
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
			return;
        }
		yield;
	}
}