/* 
SPECTRAL NEXUS SHOT FUNCTIONS!!! (MOSTLY ADAPTED FROM ULTIMA XD)
*/

function CreateDNHShotA1(x,y,s,a,g,delay){

	let obj = ObjShot_Create(OBJ_SHOT);
	ObjShot_Regist(obj);
	ObjRender_SetBlendType(obj,GetShotDataInfoA1(g,TARGET_ENEMY,INFO_BLEND));
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetGraphic(obj,g);	
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,s);
	ObjMove_SetAngle(obj,a);
	checkhitbox;
	ObjShot_SetIntersectionEnable(obj,false);
	
	task checkhitbox{
		while(!Obj_IsDeleted(obj)){
			if(IsPlayerSpellActive){
				ObjShot_SetIntersectionEnable(obj,true);
			}
			yield;
		}
	}
	
	return obj;
}

function CreateDNHShotA2(x,y,s,a,acc,mspeed,g,delay){

	let obj = ObjShot_Create(OBJ_SHOT);
	ObjRender_SetBlendType(obj,GetShotDataInfoA1(g,TARGET_ENEMY,INFO_BLEND));
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetGraphic(obj,g);	
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj,s);
	ObjMove_SetAngle(obj,a);
	ObjMove_SetAcceleration(obj,acc);
	ObjMove_SetMaxSpeed(obj,mspeed);
	checkhitbox;
	
	ObjShot_SetIntersectionEnable(obj,false);
	
	task checkhitbox{
		while(!Obj_IsDeleted(obj)){
			if(IsPlayerSpellActive){
				ObjShot_SetIntersectionEnable(obj,true);
			}
			yield;
		}
	}
	
	return obj
}

function CreateDNHShotA3(x,y,xspd,yspd,xacc,yacc,xmax,ymax,g,delay){

	let obj = ObjShot_Create(OBJ_SHOT);
	ObjRender_SetBlendType(obj,GetShotDataInfoA1(g,TARGET_ENEMY,INFO_BLEND));
	ObjShot_SetDelay(obj,delay);
	ObjShot_SetGraphic(obj,g);	
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetSpeed(obj, sqrt( power(xspd,2) + power(yspd,2) ) );
	ObjMove_SetAngle(obj,atan2( x + y, (x+xspd) + (y+yspd) ));

	ObjMove_SetAcceleration(obj,0);
	ObjMove_SetMaxSpeed(obj,0);
	ObjMove_AddPatternB2(obj,delay,xspd,yspd,xacc,yacc,xmax,ymax);
	checkhitbox;
	
	ObjShot_SetIntersectionEnable(obj,false);
	
	task checkhitbox{
		while(!Obj_IsDeleted(obj)){
			if(IsPlayerSpellActive){
				ObjShot_SetIntersectionEnable(obj,true);
			}
			yield;
		}
	}
	
	return obj;
}

function CreateDNHLooseLaserA1(x,y,s,a,l,w,g,delay){

	let obj = CreateLooseLaserA1(x,y,s,a,l,w,g,delay);
	ObjLaser_SetGrazeInvalidFrame(obj,4);
	ObjLaser_SetInvalidLength(obj,5,30);
	Obj_SetValue(obj,"RENDER WIDTH",w);
	Obj_SetValue(obj,"INTERSECT WIDTH",w*0.95);
	checkhitbox;
	
	ObjShot_SetIntersectionEnable(obj,false);
	
	task checkhitbox{
		while(!Obj_IsDeleted(obj)){
			if(IsPlayerSpellActive){
				ObjShot_SetIntersectionEnable(obj,true);
			}
			yield;
		}
	}
	
	return obj;
}

function CreateDNHStraightLaserA1(x,y,a,l,w,g,deltime,delay){

	if(deltime==0)
	{
	
		deltime = 1000000;
	}
	
	let obj = CreateStraightLaserA1(x,y,a,l,w,deltime,g,delay);	
	
	ObjLaser_SetGrazeInvalidFrame(obj,4);
	ObjLaser_SetInvalidLength(obj,5,30);
	Obj_SetValue(obj,"RENDER WIDTH",w);
	Obj_SetValue(obj,"INTERSECT WIDTH",w*0.95);
	checkhitbox;
	
	ObjShot_SetIntersectionEnable(obj,false);
	
	task checkhitbox{
		while(!Obj_IsDeleted(obj)){
			if(IsPlayerSpellActive){
				ObjShot_SetIntersectionEnable(obj,true);
			}
			yield;
		}
	}
	return obj;
}

function CreateDNHCurveLaserA1(x,y,s,a,l,w,g,delay){

	let obj = CreateCurveLaserA1(x,y,s,a,l,w,g,delay);
	ObjLaser_SetGrazeInvalidFrame(obj,4);
	ObjCrLaser_SetTipDecrement(obj,0);
	Obj_SetValue(obj,"RENDER WIDTH",w);
	Obj_SetValue(obj,"INTERSECT WIDTH",w*0.95);
	checkhitbox;
	
	ObjShot_SetIntersectionEnable(obj,false);
	
	task checkhitbox{
		while(!Obj_IsDeleted(obj)){
			if(IsPlayerSpellActive){
				ObjShot_SetIntersectionEnable(obj,true);
			}
			yield;
		}
	}
	return obj;
}

/* 
The following 2 tasks were made by ULTIMA, and their purpose is to greatly reduce lag.
This is done by disabling their hitbox when far away from the player.
Thanks!!! :)
*/

task ManageAllShotCollision{
	let obj = [];
	let IsDebug = false;
	let base_size = 24;
	
	loop{
		if(GetPlayerInvincibilityFrame <= 0){
			obj = GetShotIdInCircleA2(GetPlayerX, GetPlayerY, 40, TARGET_ENEMY);
			
			ascent(i in 0..length(obj)){
			
				if(Obj_GetValueD(obj[i], "AUTO COLLISION", true) && Obj_GetType(obj[i]) == OBJ_SHOT){
				
					if(GetDistance(GetPlayerX, GetPlayerY, ObjMove_GetX(obj[i]), ObjMove_GetY(obj[i])) <= base_size){
					
						if(IsDebug){
							ObjRender_SetColor(obj[i],0,255,0);
						}
						
						ObjShot_SetIntersectionEnable(obj[i],true);
					}
					
					else{
						if(IsDebug){
							ObjRender_SetColor(obj[i],255,0,0);
						}
						
						ObjShot_SetIntersectionEnable(obj[i],false);
					}
				}
			}
		}
		yield;
	}
	
}

task ManageAllLaserCollision{
	let obj = [];
	let IsDebug = false;
	let base_size = 24;
	
	loop{
		if(GetPlayerInvincibilityFrame <= 0){
			obj = GetShotIdInCircleA2(GetPlayerX, GetPlayerY, 40, TARGET_ENEMY);
			
			ascent(i in 0..length(obj)){
			
				if(Obj_GetValueD(obj[i], "AUTO COLLISION", true) && Obj_GetType(obj[i]) == OBJ_LOOSE_LASER){
				
					if(GetDistance(GetPlayerX, GetPlayerY, ObjMove_GetX(obj[i]), ObjMove_GetY(obj[i])) <= base_size + ObjLaser_GetLength(obj[i])){
					
						if(IsDebug){
							ObjRender_SetColor(obj[i],0,255,0);
						}
						
						ObjShot_SetIntersectionEnable(obj[i],true);
					}
					
					else{
						if(IsDebug){
							ObjRender_SetColor(obj[i],255,0,0);
						}
						
						ObjShot_SetIntersectionEnable(obj[i],false);
					}
				}
				
				else if(Obj_GetValueD(obj[i], "AUTO COLLISION",true) && Obj_GetType(obj[i]) == OBJ_CURVE_LASER){
				
					if(GetDistance(GetPlayerX, GetPlayerY, ObjMove_GetX(obj[i]), ObjMove_GetY(obj[i])) <= base_size + (ObjLaser_GetLength(obj[i]) * ObjMove_GetSpeed(obj[i]))){
						
						if(IsDebug){
							ObjRender_SetColor(obj[i],0,255,0);
						}
						
						ObjShot_SetIntersectionEnable(obj[i],true);
					}
					
					else{
					
						if(IsDebug){
							ObjRender_SetColor(obj[i],255,0,0);
						}
						
						ObjShot_SetIntersectionEnable(obj[i],false);
					}				
				}
			}
		}
		yield;
	}
	
}