#include"./AllStarShot_Const.txt"
#include"./functions_event.txt"
#include"./Sound.txt"
#include"./Music.txt"

let objEnemy = ID_INVALID;
let objPlayer = GetPlayerObjectID;
let enmx = -64;
let enmy = -64;
let timer;
let spellLife = 3;
let Effect = GetCommonData("EFFECT",0);
let Cheat = GetCommonData("CHEAT",false);

let TYPE_POINT = 6;
let TYPE_SPELL = 5;
let TYPE_LIFE = 2;

task ObjShot_SetIceBreak(shot,value)
	{
	Obj_SetValue(shot,"ICE_BREAK",value);
	}
function ObjShot_GetIceBreak(shot)
	{
	return Obj_GetValueD(shot,"ICE_BREAK",false);
	}
function CreateBulletA1(x,y,speed,angle,graph,delay,scale)
{	
	let shot = CreateShotA1(x,y,speed,angle,graph,delay);
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetScaleXYZ(shot,scale,scale,0);
	ObjShot_SetScale(shot,scale);	
	return shot;
}
function CreateBulletOA1(obj,speed,angle,graph,delay,scale)
{
	return CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),speed,angle,graph,delay,scale);
}
function CreateBulletA2(x,y,speed,angle,accel,velo,max,graph,delay,scale)
{
	let shot = CreateBulletA1(x,y,speed,angle,graph,delay,scale);
	ObjMove_SetAcceleration(shot,accel);
	ObjMove_SetAngularVelocity(shot,velo);
	ObjMove_SetMaxSpeed(shot,max);
	return shot;
}

function CreateBulletOA2(obj,speed,angle,accel,velo,max,graph,delay,scale)
{
	let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),speed,angle,graph,delay,scale);
	ObjMove_SetAcceleration(shot,accel);
	ObjMove_SetAngularVelocity(shot,velo);
	ObjMove_SetMaxSpeed(shot,max);
	return shot;
}
function CreateBulletB1(x,y,xS,yS,graph,delay,scale)
{
	let shot = CreateBulletA1(x,y,0,0,graph,delay,scale);;
	ObjMove_AddPatternB1(shot,0,xS,yS);
	return shot;
}
function CreateBulletB2(x,y,xS,yS,xA,yA,xM,yM,graph,delay,scale)
{
	let shot = CreateBulletA1(x,y,0,0,graph,delay,scale);
	ObjMove_AddPatternB2(shot,0,xS,yS,xA,yA,xM,yM);
	return shot;
}
function CreateLaserA1(x,y,angle,dist,width,del,graph,delay)
{
	let laserObj = CreateStraightLaserA1(x,y,angle,dist,width,del,graph,delay);	
	ObjShot_SetIntersectionEnable(laserObj,false);
	ObjShot_SetScale(laserObj,width);
	TIntersect;
	return laserObj;

	task TIntersect
		{
		loop(delay){yield;}
		ascent(t in 0..del)
			{
			if(Obj_IsDeleted(laserObj)){return;}
			let x = ObjMove_GetX(laserObj);
			let y = ObjMove_GetY(laserObj);
			let dist = ObjLaser_GetLength(laserObj);
			let angle = ObjStLaser_GetAngle(laserObj);
			let width = ObjShot_GetScale(laserObj)*0.5;
			ObjLaser_SetRenderWidth(laserObj,width);
			if(!Obj_IsDeleted(laserObj) && GetPlayerInvincibilityFrame() <= 0 && ObjShot_GetIntersection(laserObj) == true && IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width*0.25,GetPlayerX,GetPlayerY,Obj_GetValueD(GetPlayerObjectID,"PLAYER_GRAZEBOX",0)))
				{
				if(IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width*0.25,GetPlayerX,GetPlayerY,Obj_GetValueD(GetPlayerObjectID,"PLAYER_HITBOX",0)))
					{
					Obj_SetValue(GetPlayerObjectID,"DAMAGE",true);
					}
				if(t%3 == 0)
					{
					Obj_SetValue(objPlayer,"GRAZE",Obj_GetValueD(objPlayer,"GRAZE",0)+1);
					}
				}
			yield;
			}
		}
}
function CreateLaserA2(x,y,speed,angle,dist,width,del,graph,delay)
{
	let laserObj = CreateStraightLaserA1(x,y,angle,dist,width,del,graph,delay);	
	ObjShot_SetIntersectionEnable(laserObj,false);
	ObjShot_SetScale(laserObj,width);
	ObjShot_SetIceBreak(laserObj,true);
	TIntersect;
	TGrow;
	return laserObj;
	
	task TGrow
		{
		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 TIntersect
		{
		loop(delay+1){yield;}
		ascent(t in 0..del)
			{
			if(Obj_IsDeleted(laserObj)){return;}
			let x = ObjMove_GetX(laserObj);
			let y = ObjMove_GetY(laserObj);
			let dist = ObjLaser_GetLength(laserObj);
			let angle = ObjStLaser_GetAngle(laserObj);
			let width = ObjShot_GetScale(laserObj)*0.5;
			ObjLaser_SetRenderWidth(laserObj,width);
			if(!Obj_IsDeleted(laserObj) && GetPlayerInvincibilityFrame() <= 0 && ObjShot_GetIntersection(laserObj) == true && IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width*0.25,GetPlayerX,GetPlayerY,Obj_GetValueD(GetPlayerObjectID,"PLAYER_GRAZEBOX",0)))
				{
				if(IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width*0.25,GetPlayerX,GetPlayerY,Obj_GetValueD(GetPlayerObjectID,"PLAYER_HITBOX",0)))
					{
					Obj_SetValue(GetPlayerObjectID,"DAMAGE",true);
					}
				if(t%3 == 0)
					{
					Obj_SetValue(objPlayer,"GRAZE",Obj_GetValueD(objPlayer,"GRAZE",0)+1);
					}
				}
			if(!Obj_IsDeleted(laserObj) && ObjShot_GetIceBreak(laserObj)==true)
				{
				let array = GetShotIdInCircleA2(x+dist*0.5*cos(angle),y+dist*0.5*sin(angle),dist*0.5,TARGET_PLAYER);
				ascent(i in 0..length(array))
					{
					let shot = array[i];
					let xS = ObjMove_GetX(shot);
					let yS = ObjMove_GetY(shot);
					if(!Obj_IsDeleted(shot) && ObjShot_GetIceBreak(shot) == true && IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width,xS,yS,ObjShot_GetScale(shot)*32))
						{
						NotifyEventAll(EV_ICE_BREAK,[xS,yS]);
						Obj_Delete(shot);
						}
					}
				}
			yield;
			}
		}
}

function CreateLaserA3(x,y,speed,angle,dist,width,del,graph,delay,delete)
{
	let widthA = 0.2;
	while(width*widthA < 5)
		{
		widthA+=0.01;
		}
	let widthB = 1-widthA;
	let laserObj = CreateStraightLaserA1(x,y,angle,0,widthA*width,10^10,graph,0);	
	ObjShot_SetIntersectionEnable(laserObj,false);
	ObjShot_SetScale(laserObj,width);
	TGrow;
	TWidth;
	return laserObj;
	
	task TWidth
		{
		loop(delay+1){if(Obj_IsDeleted(laserObj)){return;}yield;}
		let t = 0;
		loop(10)
			{
			widthA+=widthB/10;
			width = ObjShot_GetScale(laserObj)*0.5;
			ObjLaser_SetRenderWidth(laserObj,widthA*width);
			ObjLaser_SetIntersectionWidth(laserObj,widthA*width*0.45);
			if(Obj_IsDeleted(laserObj)){return;}
			TIntersect(t);
			t+=1;
			yield;
			}
		loop(del-20)
			{
			if(Obj_IsDeleted(laserObj)){return;}
			width = ObjShot_GetScale(laserObj)*0.5;
			ObjLaser_SetRenderWidth(laserObj,width);
			TIntersect(t);
			t+=1;
			yield;
			}
		loop(delete)
			{
			width = ObjShot_GetScale(laserObj)*0.5;
			widthA-=1/delete;
			ObjLaser_SetRenderWidth(laserObj,widthA*width);
			if(Obj_IsDeleted(laserObj)){return;}
			TIntersect(t);
			t+=1;
			yield;
			}
		Obj_Delete(laserObj);
		task TIntersect(t)
			{
			if(Obj_IsDeleted(laserObj)){return;}
			let x = ObjMove_GetX(laserObj);
			let y = ObjMove_GetY(laserObj);
			let dist = ObjLaser_GetLength(laserObj);
			let width = widthA*ObjShot_GetScale(laserObj)*0.5;
			angle = ObjStLaser_GetAngle(laserObj);
			ObjLaser_SetRenderWidth(laserObj,width*widthA);
			if(!Obj_IsDeleted(laserObj) && GetPlayerInvincibilityFrame() <= 0 && ObjShot_GetIntersection(laserObj) == true && IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width*0.25,GetPlayerX,GetPlayerY,Obj_GetValueD(GetPlayerObjectID,"PLAYER_GRAZEBOX",0)))
				{
				if(IsIntersected_Line_Circle(x,y,x+dist*cos(angle),y+dist*sin(angle),width*0.25,GetPlayerX,GetPlayerY,Obj_GetValueD(GetPlayerObjectID,"PLAYER_HITBOX",0)))
					{
					Obj_SetValue(GetPlayerObjectID,"DAMAGE",true);
					}
				if(t%3 == 0)
					{
					Obj_SetValue(objPlayer,"GRAZE",Obj_GetValueD(objPlayer,"GRAZE",0)+1);
					}
				}
			}
		}

	task TGrow
		{
		let distA = 0;
		while(distA < dist)
			{
			if(Obj_IsDeleted(laserObj))
				{
				distA = dist;
				}
			else
				{
				distA+=speed;
				ObjLaser_SetLength(laserObj,distA);
				}
			if(Obj_IsDeleted(laserObj)){return;}
			yield;
			}
		ObjLaser_SetLength(laserObj,dist);
		}
}
function IsBeyondClip(obj,clip)
	{
	let value = false;
	if(ObjMove_GetX(obj) < -clip || ObjMove_GetX(obj) > GetStgFrameWidth+clip || ObjMove_GetY(obj) < -clip || ObjMove_GetY(obj) > GetStgFrameHeight+clip)
		{
		value = true;
		}
	return value;
	}
task CreateItem1(x,y,type)
	{
	if(type != TYPE_POINT)
		{
		PlaySound("se_Bonus",90);
		}
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/RSS_ITEM.png");
	ObjSprite2D_SetSourceRect(obj,type*32,0,(type+1)*32,32);	
	Obj_SetRenderPriority(obj,0.35);
	let yA = -2;
	let angle = 90;
	let scale = 0;
	let scaleM = 28;
	let switch = false;
	if(type == TYPE_POINT)
		{
		scaleM = 16;	
		ObjRender_SetColorHSV(obj,0,0,225);
		Obj_SetRenderPriority(obj,0.34);
		}
	while(!Obj_IsDeleted(obj) && y < GetStgFrameHeight+32)
		{
		if(angle > 0)
			{
			angle-=90/20;
			scale+=scaleM/20;
			}
		else
			{
			angle = 0;
			scale=scaleM;
			}
		ObjRender_SetPosition(obj,x,y,0);
		ObjRender_SetAngleZ(obj,angle);
		if(GetDistance(x,y,GetPlayerX,GetPlayerY) < 96 || GetPlayerY < GetStgFrameHeight/2-90 || switch == true)
			{
			switch = true;
			let angle = atan2(GetPlayerY-y,GetPlayerX-x);
			x+=10*cos(angle);
			y+=10*sin(angle);
			}
		else
			{
			y+=yA;
			yA+=1/32;
			}
		if(GetDistance(x,y,GetPlayerX,GetPlayerY) < 16)
			{
			Obj_Delete(obj);
			alternative(type)
				case(TYPE_LIFE)
					{
					PlaySound("se_Extend",100);
					SetPlayerHealth(GetPlayerHealth+100);
					}
				case(TYPE_SPELL)
					{
					PlaySound("se_Item2",90);
					SetPlayerSpell(GetPlayerSpell+1);
					}
				others
					{
					PlaySound("se_Item",120);
					AddScore(floor(GetGraze/10)+300);
					AddPoint(1);
					}
			return;
			}	
		ObjSprite2D_SetDestRect(obj,-scale/2,-scale/2,scale/2,scale/2);
		yield;
		}	
	Obj_Delete(obj);
	}
task ObjMove_AddPatternA5(obj,time,speed,angle,accel,velo,max,graph,scale)
{
	loop(time){yield;}
	if(Obj_IsDeleted(obj)){return;}
	ObjMove_AddPatternA2(obj,0,speed,angle,accel,velo,max);
	ObjShot_SetGraphic(obj,graph);
	ObjShot_SetScale(obj,scale);
}
task ObjMove_AddPatternA6(obj,time,speed,angle,accel,velo,max,target,graph,scale)
{
	loop(time){yield;}
	if(Obj_IsDeleted(obj)){return;}
	ObjMove_AddPatternA2(obj,0,speed,0,accel,velo,max);
	ObjShot_SetGraphic(obj,graph);
	ObjMove_SetAngle(obj,atan2(ObjMove_GetY(target)-ObjMove_GetY(obj),ObjMove_GetX(target)-ObjMove_GetX(obj))+angle);
	ObjShot_SetScale(obj,scale);
}
task ObjShot_SetIntersection(shot,value)
{
	Obj_SetValue(shot,"INTERSECTION_ENABLED",value);
}
function ObjShot_GetIntersection(shot)
{
	return Obj_GetValueD(shot,"INTERSECTION_ENABLED",true);
}
task ObjShot_SetScale(shot,scale)
{
	if(shot == ID_INVALID || Obj_IsDeleted(shot)){return;}
	ObjRender_SetScaleXYZ(shot,scale,scale,0);
	Obj_SetValue(shot,"SHOT_HITBOX",GetShotDataInfoA1(ObjShot_GetImageID(shot),TARGET_ENEMY,INFO_COLLISION)*scale);
}
function ObjShot_GetScale(shot)
{
	if(Obj_IsDeleted(shot)){return ID_INVALID;}
	return Obj_GetValueD(shot,"SHOT_HITBOX",GetShotDataInfoA1(ObjShot_GetImageID(shot),TARGET_ENEMY,INFO_COLLISION));
}
task SetPlayerHealth(numb)
{
	Obj_SetValue(GetPlayerObjectID,"PLAYER_LIFE",numb);
}
function GetPlayerHealth
{
	return Obj_GetValueD(GetPlayerObjectID,"PLAYER_LIFE",0);
}
task SetPlayerFlash(numb)
{
	Obj_SetValue(GetPlayerObjectID,"PLAYER_FLASH",numb);
}
function GetPlayerFlash
{
	return Obj_GetValueD(GetPlayerObjectID,"PLAYER_FLASH",0);
}
task SetPlayerIntersectionEnable(value)
{
	Obj_SetValue(GetPlayerObjectID,"INTERSECTION_ENABLE",value);
}
function IsPlayerIntersectionEnable
{
	return Obj_GetValueD(GetPlayerObjectID,"INTERSECTION_ENABLE",true);
}
task SetPlayerSpellEnable(numb)
{
	Obj_SetValue(GetPlayerObjectID,"PLAYER_SPELL_ENABLE",numb);
}
function GetPlayerSpellEnable
{
	return Obj_GetValueD(GetPlayerObjectID,"PLAYER_SPELL_ENABLE",true);
}
function GetDistance(x,y,x2,y2)
{
	return (((x2-x)^2)+((y2-y)^2))^(1/2);
}
task ObjShot_ScaleDelete(shot,time)
{
	let scaleM = 1.125*ObjShot_GetScale(shot)/GetShotDataInfoA1(ObjShot_GetImageID(shot),TARGET_ENEMY,INFO_COLLISION);
	let x = ObjMove_GetX(shot);
	let y = ObjMove_GetY(shot);
	descent(t in 0..time)
		{	
		let scale = t*scaleM/time;
		ObjMove_SetPosition(shot,x,y);
		ObjShot_SetScale(shot,scale);	
		yield;
		}
	Obj_Delete(shot);
}
task ObjShot_AlphaDelete(shot,time)
{
	let x = ObjMove_GetX(shot);
	let y = ObjMove_GetY(shot);
	descent(t in 0..time)
		{	
		let alpha = t*255/time;
		ObjMove_SetPosition(shot,x,y);
		ObjRender_SetAlpha(shot,alpha);	
		ObjShot_SetIntersection(shot,false);
		yield;
		}
	Obj_Delete(shot);
}

task Obj_DelayDelete(target,time)
{
	loop(time){yield;}
	Obj_Delete(target);
}
	
task ObjEffect_Explosion(x,y)
{
	NotifyEventAll(EV_EFFECT_EXPLOSION,[x,y]);
}


task ObjEffect_Charge(x,y)
{
	NotifyEventAll(EV_EFFECT_CHARGE,[x,y]);
}
task TShake
{
	let xM = Get2DCameraX;
	let yM = Get2DCameraY;
	let dist = 3;
	loop(30)
		{
		dist+=0.5;
		Set2DCameraFocusX(xM+rand(-1,1)*dist);
		Set2DCameraFocusY(yM+rand(-1,1)*dist);
		yield;
		}
	Set2DCameraFocusX(xM);
	Set2DCameraFocusY(yM);
}
