#include "./EffectFunctions.dnh"

function GetClipMaxX{ return GetStgFrameWidth; }
function GetClipMaxY{ return GetStgFrameHeight; }

function GetCenterX{ return GetStgFrameWidth/2; }
function GetCenterY{ return GetStgFrameHeight/2; }

function GetClipMinX{ return 0; }
function GetClipMinY{ return 0; }

function randInt(min, max){ return floor(rand(min, max + 1)); }

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

function NormalizeAngle(angle){
  angle %= 360;
  if(angle<0){ angle += 360; }
  return angle;
}

function AngleBetweenPoints(x1, y1, x2, y2){ //get angle from (x1, y1) to (x2, y2)
	return atan2(y2-y1, x2-x1);
}

function GetAngleToPoint(sourceX,sourceY,targetX,targetY){
	return atan2(targetY-sourceY,targetX-sourceX);
}

function GetLineBorderPoint(px, py, tx, ty){
	let ta = atan2(ty-py, tx-px);
	
	if( ta>= atan2(GetClipMinY - py, GetClipMinX - px) && ta<=  atan2(GetClipMinY - py, GetClipMaxX - px)){
		return [px - (GetClipMinY-py)*tan(ta+90), GetClipMinY];
	}
	if( ta>= atan2(GetClipMinY - py, GetClipMaxX - px) && ta<=  atan2(GetClipMaxY - py, GetClipMaxX - px)){
		return [GetClipMaxX, py + (GetClipMaxX-px)*tan(ta)];
	}
	if( ta>= atan2(GetClipMaxY - py, GetClipMaxX - px) && ta<=  atan2(GetClipMaxY - py, GetClipMinX - px)){
		return [px - (GetClipMaxY-py)*tan(ta-90), GetClipMaxY];
	}
	if( ta> atan2(GetClipMaxY - py, GetClipMinX - px) || ta<  atan2(GetClipMinY - py, GetClipMinX - px)){
		return [GetClipMinX, py + (GetClipMinX-px)*tan(ta+180)];
	}
}

function GetLife(obj){
	if(!Obj_IsDeleted(obj)){ return ObjEnemy_GetInfo(obj,INFO_LIFE); }
	else{ return 0; }
}

function GetTimer{
	return ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID,INFO_TIMERF);
}

task Invuln(target, duration, shot, bomb){ 
	ObjEnemy_SetDamageRate(target, 0, 0);
	wait(duration);
	ObjEnemy_SetDamageRate(target,shot,bomb);
}

function ExMove(obj, tarx, length, height, border, speed, weight) { //object moves towards target x coord, maintaining height and moving length distance, keeping border units from left/right
	if(tarx < ObjMove_GetX(obj)) {
			if(ObjMove_GetX(obj) - length <= 0+border) {
				ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)+length, height, weight, speed);
			} else {
				ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)-length, height, weight, speed);
			}
		} else {
			if(ObjMove_GetX(obj) + length >= GetStgFrameWidth-border) {
				ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)-length, height, weight, speed);
			} else {
				ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)+length, height, weight, speed);
			}
		}
}

function CreateSprite ( let texture, let priorityInt, let rects, let center ) {
		// rects = 1-dimensional, size 4
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, texture);
		Obj_SetRenderPriorityI(obj, priorityInt);
		ObjSprite2D_SetSourceRect(obj, rects[0], rects[1], rects[2], rects[3]);
		ObjSprite2D_SetDestRect(obj, -center[0], -center[1], rects[2]-rects[0]-center[0], rects[3]-rects[1]-center[1]);
		return obj;
}

let TIME_NEVERDELETE = 2^20;
let VALUE_SHOT_TYPE = "Object Type";
let VALUE_SHOT_LASERWIDTH = "Laser Width";
let VALUE_ITEM_MAKEITEM = "Object Delete Make Item";

function CreateDNHShotA1 ( let x, let y, let v, let angle, let graphic, let delay ) {
	if(GetCommonData("bullettype", 1)==0){
	CreateShotA1(x, y, v, angle, graphic, delay);
	}
	else{
	let obj = CreateShotA1(x, y, 0, angle, graphic, 0);
	Obj_SetValue(obj, VALUE_SHOT_TYPE, Obj_GetType(obj));
	CreateDelayCloud(obj, v, angle, 0, 0, graphic, delay);
	ObjRender_SetAlpha(obj, 0);
	return obj;
	}
}

function CreateDNHShotA2 ( let x, let y, let v, let angle, let vAcc, let vF, let graphic, let delay ) {
if(GetCommonData("bullettype", 1)==0){
	CreateShotA2(x, y, v, angle, vAcc, vF, graphic, delay);
	} else{
	let obj = CreateShotA2(x, y, 0, angle, 0, 0, graphic, 0);
	Obj_SetValue(obj, VALUE_SHOT_TYPE, Obj_GetType(obj));
	CreateDelayCloud(obj, v, angle, vAcc, vF, graphic, delay);
	ObjRender_SetAlpha(obj, 0);
	return obj;
	}
}

function CreateMDNHShotA2 ( let x, let y, let v, let angle, let vAcc, let vF, let graphic, let delay ) {
	let obj = CreateShotA2(x, y, v, angle, vAcc, vF, graphic, 0);
	Obj_SetValue(obj, VALUE_SHOT_TYPE, Obj_GetType(obj));
	CreateMovingDelayCloud(obj, v, angle, vAcc, vF, graphic, delay);
	//ObjRender_SetAlpha(obj, 0);
	return obj;
}
function CreateDNHShotA3 ( let x, let y, let v, let angle, let vAcc, let vF, let graphic, let delay ) {
	let obj = CreateShotA2(x, y, 0, angle, 0, 0, graphic, 0);
	Obj_SetValue(obj, VALUE_SHOT_TYPE, Obj_GetType(obj));
	CreateDelayCloud(obj, v, angle, vAcc, vF, graphic, delay);
	ObjRender_SetAlpha(obj, 0);
	return obj;
}

task CreateMovingDelayCloud ( let obj, let v, let angle, let vAcc, let vF, let graphic, let delay ) {
	let rects = GetShotDataInfoA1(graphic, TARGET_ENEMY, INFO_RECT);
	if(delay > 0) {
		ObjRender_SetBlendType(obj, GetShotDataInfoA1(graphic, TARGET_ENEMY, INFO_BLEND));
		//ObjRender_SetAngleZ(obj, angle);
		descent(i in 0..delay) {
			ObjRender_SetPosition(obj, ObjMove_GetX(obj), ObjMove_GetY(obj), NULL);
			ObjRender_SetAlpha(obj, 255*cos(i*90/delay));
			ObjRender_SetScaleXYZ(obj, 2-cos(i*90/delay), 2-cos(i*90/delay), NULL);
			if(Obj_IsDeleted(obj)==true) { yield; return; }
			wait(1);
		}
	} else {
		yield;
	}
	//ObjRender_SetAlpha(obj, 255);
	//Obj_Delete(objCloud);
	//ObjMove_SetSpeed(obj, v);
	//ObjMove_SetAcceleration(obj, vAcc);
	//ObjMove_SetMaxSpeed(obj, vF);
}

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