let CSD = GetCurrentScriptDirectory;


let MinX=32;
let MaxX=380;
let MinY=0;
let MaxY=464;

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));}
function GetAngleTwoPoints(x1,y1,x2,y2){ return atan2(y2-y1,x2-x1);}

function CO_GetAngleToObjectMove(x,y,obj){
	let angle = atan2(ObjMove_GetY(obj)-y,ObjMove_GetX(obj)-x);
	return angle;
}


task PlaytestInvincibility{
	
	AddVirtualKey(VK_USER1,KEY_SPACE,KEY_INVALID);
	
	loop{
		if(GetVirtualKeyState(VK_USER1)==KEY_HOLD){
		SetPlayerInvincibilityFrame(30);}
	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)
		{
			return;// comment out to have bottom screen bouncing
			ObjMove_SetY(obj,448-ObjMove_GetY(obj)+448);
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
        }
        if(trunc(ObjMove_GetY(obj))<0)
		{
			ObjMove_SetY(obj,-ObjMove_GetY(obj));
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
			return;
        }
		*/
		yield;
	}
}

task RunReflectCode2(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)
		{
			return;// comment out to have bottom screen bouncing
			ObjMove_SetY(obj,448-ObjMove_GetY(obj)+448);
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
        }
		*/
		
        if(trunc(ObjMove_GetY(obj))<0)
		{
			ObjMove_SetY(obj,-ObjMove_GetY(obj));
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
			return;
        }
		
		yield;
	}
}

task widescreen{
	let left = 32;
	let top = 16;
	let right = 416;
	let bottom = 464;
	
	loop{
	if(left>0){left-=0.51;}
	if(top>0){top-=0.25;}
	if(right<GetScreenWidth){right+=4;}
	if(bottom<GetScreenHeight){bottom+=0.26;}
	SetStgFrame(left,top,right,bottom,20,80);
	SetPlayerClip(left,top,right,bottom);
	
	yield;}
}

task ExpandFrame(speed){
    let left;
    let top;
    let right;
    let bottom;
    let t = 0;
    let F = [[32,0],[16,0],[416,640],[464,480]];
     
    while(t<1){
    left = F[0][0]+t*(F[0][1]-F[0][0]);
    top = F[1][0]+t*(F[1][1]-F[1][0]);
    right = F[2][0]+t*(F[2][1]-F[2][0]);
    bottom = F[3][0]+t*(F[3][1]-F[3][0]);
    SetStgFrame(left,top,right,bottom,20,80);
    SetPlayerClip(left,top,right,bottom);
    t+=speed/100;
    yield;
    }
    SetStgFrame(floor(left),floor(top),F[2][1],ceil(bottom),20,80);
    SetPlayerClip(floor(left),floor(top),F[2][1],ceil(bottom));
}

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

task BottomEnemyMarker(obj, bossobj){					 //Don't murder me please..
  let imgnumber = GetCurrentScriptDirectory~"img/marker.png";
  let objMarker = ObjPrim_Create(OBJ_SPRITE_2D);
  ObjRender_SetPosition(objMarker, GetCenterX, GetStgFrameHeight+32, 1); 
  ObjPrim_SetPrimitiveType(objMarker, PRIMITIVE_TRIANGLESTRIP);
  ObjRender_SetBlendType(objMarker, BLEND_ALPHA);
  Obj_SetRenderPriority(objMarker, 0.85);
  ObjPrim_SetTexture(objMarker, imgnumber);
  ObjRender_SetAlpha(objMarker, 255);
  ObjSprite2D_SetSourceRect(objMarker, 0, 0, 48, 16);
  ObjSprite2D_SetDestRect(objMarker, -25, -8, 25, 8);
  while(ObjEnemy_GetInfo(bossobj,INFO_LIFE)>0){
    ObjRender_SetPosition(objMarker, ObjMove_GetX(obj)+32, GetStgFrameHeight+24, 1);
    yield;
  }
 loop(12){yield;}
  Obj_Delete(objMarker);
}



	//let obj = CurvyLaser(GetX,GetY,speed-7,angle,60,50,373,373,0);
	//ObjMove_AddPatternA2(obj,5,NO_CHANGE,NO_CHANGE,0.5,7*rot,speed);
	//ObjMove_AddPatternA2(obj,75,NO_CHANGE,NO_CHANGE,-0.3,0.7*rot,speed-5);

function CurvyLaser(xpos,ypos,speed,angle,length,width,bulletgraphic,trailgraphic,delay) {
	let obj = CreateShotA1(xpos,ypos,speed,angle,bulletgraphic,delay);
	LaserTrail;
	return obj;
	
	task LaserTrail {
		let del = length;
		let speed = 0;
		while(!Obj_IsDeleted(obj)) {
			speed = ObjMove_GetSpeed(obj);
			if(speed>0) {
				if(10/speed>=0) {
					FadeLaser(width,del);
					loop(10/speed) { yield; }
				}
			}
			else { yield; }
		}
	}
	task FadeLaser(initwidth,del) {
		let objTrail = CreateStraightLaserA1(ObjMove_GetX(obj)-40*cos(ObjMove_GetAngle(obj)),ObjMove_GetY(obj)-40*sin(ObjMove_GetAngle(obj)),ObjMove_GetAngle(obj),120,initwidth,1200,trailgraphic,0);
		
		let initlength = 120;
		let width = initwidth;
		let length = initlength;
		let alpha = 255;
		loop(del) {
			//if(alpha<255) { alpha+=255/8; }
			width-=initwidth/del;
			length-=initlength/del;
			ObjRender_SetColor(objTrail,alpha,alpha,alpha);
			ObjLaser_SetIntersectionWidth(objTrail,width);
			ObjLaser_SetRenderWidth(objTrail,width);
			ObjLaser_SetLength(objTrail,length);
			yield;
		}
		Obj_Delete(objTrail);
	}
}

function PagodaLaser(x,y,xmov,ymov,scale,angle,prerotation,postrotation,maxwidth,color,delay,del) {
	let objHitbox = CreateStraightLaserA1(x,y,angle,500,maxwidth*(5/4),del+45,1,delay);
	PagodaBeam;
	return objHitbox;
	
	task PagodaBeam {
		let objLaser1 = ObjPrim_Create(OBJ_SPRITE_2D);
		let objLaser2 = ObjPrim_Create(OBJ_SPRITE_2D);
		let objDetail = ObjPrim_Create(OBJ_SPRITE_2D);
		let objFire = ObjPrim_Create(OBJ_SPRITE_2D);
		let objFire2 = ObjPrim_Create(OBJ_SPRITE_2D);
		
		let width = 2;
		let length = 0;
		let fire_scale = 1.2*scale;
		let fire_scale2 = 0.8*scale;
		let fire_count = 0;
		let detail_count = 0;
		let newangle = angle;
		let newX = x;
		let newY = y;
				
		Obj_SetVisible(objHitbox,false);
		
		ObjRender_SetBlendType(objLaser1, BLEND_ALPHA);
		Obj_SetRenderPriorityI(objLaser1, 28);
		ObjPrim_SetTexture(objLaser1, GetCurrentScriptDirectory ~ "img/PagodaLaser.png");
		ObjSprite2D_SetSourceRect(objLaser1, 0, 37, 256, 69);
		ObjSprite2D_SetDestRect(objLaser1, 0.5, -16, 256.5, 16);
		ObjRender_SetColor(objLaser1,color[0],color[1],color[2]);
		
		ObjRender_SetBlendType(objLaser2, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(objLaser2, 29);
		ObjPrim_SetTexture(objLaser2, GetCurrentScriptDirectory ~ "img/PagodaLaser.png");
		ObjSprite2D_SetSourceRect(objLaser2, 0, 77, 256, 109);
		ObjSprite2D_SetDestRect(objLaser2, 0.5, -16, 256.5, 16);
		
		ObjRender_SetBlendType(objDetail, BLEND_ALPHA);
		Obj_SetRenderPriorityI(objDetail, 29);
		ObjPrim_SetTexture(objDetail, GetCurrentScriptDirectory ~ "img/PagodaLaser.png");
		ObjSprite2D_SetSourceRect(objDetail, 0, 0, 256, 30);
		ObjSprite2D_SetDestRect(objDetail, 0.5, -18, 256.5, 18);
		
		ObjRender_SetBlendType(objFire, BLEND_ALPHA);
		Obj_SetRenderPriorityI(objFire, 27);
		ObjPrim_SetTexture(objFire, GetCurrentScriptDirectory ~ "img/PagodaLaser_Fire.png");
		ObjRender_SetColor(objFire,color[0],color[1],color[2]);
		ObjRender_SetScaleXYZ(objFire,fire_scale,fire_scale,1);
		
		ObjRender_SetBlendType(objFire2, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(objFire2, 30);
		ObjPrim_SetTexture(objFire2, GetCurrentScriptDirectory ~ "img/PagodaLaser_Fire.png");
		ObjRender_SetScaleXYZ(objFire2,fire_scale2,fire_scale2,1);
		
		loop(delay) {
			newangle+=prerotation;
			ObjStLaser_SetAngle(objHitbox,newangle);
			newX+=xmov;
			newY+=ymov;
			ObjMove_SetX(objHitbox,newX);
			ObjMove_SetY(objHitbox,newY);
		
			ObjRender_SetScaleXYZ(objLaser1,2,width/25,0);
			ObjRender_SetPosition(objLaser1,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser1,ObjStLaser_GetAngle(objHitbox));
			
			ObjRender_SetScaleXYZ(objLaser2,2,width/32,0);
			ObjRender_SetPosition(objLaser2,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser2,ObjStLaser_GetAngle(objHitbox));
			
			
			ObjRender_SetScaleXYZ(objDetail,2,width/32,0);
			ObjRender_SetPosition(objDetail,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objDetail,ObjStLaser_GetAngle(objHitbox));
			ObjSprite2D_SetSourceRect(objDetail, 0-detail_count, 0, 256-detail_count, 30);
			ObjSprite2D_SetDestRect(objDetail, 1.5, -18, 257.5, 18);
			
			
			ObjRender_SetPosition(objFire,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire, 1, 1, 123, 127); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire, 129, 1, 255, 127); }
			ObjSprite2D_SetDestRect(objFire, 0.5, -61, 128.5, 61);
			
			ObjRender_SetPosition(objFire2,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire2,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire2, 1, 129, 123, 255); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire2, 129, 129, 255, 255); }
			ObjSprite2D_SetDestRect(objFire2, 0.5, -61, 128.5, 61);
			
			if(Obj_IsDeleted(objHitbox)) {
				Obj_Delete(objLaser1);
				Obj_Delete(objLaser2);
				Obj_Delete(objDetail);
				Obj_Delete(objFire);
				Obj_Delete(objFire2);
			}
			
			fire_count+=0.5;
			detail_count+=12;
			yield;
		}
		loop(20) {
			newangle+=postrotation;
			ObjStLaser_SetAngle(objHitbox,newangle);
			newX+=xmov;
			newY+=ymov;
			ObjMove_SetX(objHitbox,newX);
			ObjMove_SetY(objHitbox,newY);
		
			ObjRender_SetScaleXYZ(objLaser1,2*(length/500),width/25,1);
			ObjRender_SetPosition(objLaser1,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser1,ObjStLaser_GetAngle(objHitbox));
			
			ObjRender_SetScaleXYZ(objLaser2,2*(length/500),width/32,0);
			ObjRender_SetPosition(objLaser2,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser2,ObjStLaser_GetAngle(objHitbox));
			
			
			ObjRender_SetScaleXYZ(objDetail,2,width/32,0);
			ObjRender_SetPosition(objDetail,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objDetail,ObjStLaser_GetAngle(objHitbox));
			ObjSprite2D_SetSourceRect(objDetail, 0-detail_count, 0, 256-detail_count, 30);
			ObjSprite2D_SetDestRect(objDetail, 0.5, -18, 256.5, 18);
			
			
			ObjRender_SetPosition(objFire,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire, 1, 1, 123, 127); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire, 129, 1, 255, 127); }
			ObjSprite2D_SetDestRect(objFire, 0.5, -61, 128.5, 61);
			
			ObjRender_SetPosition(objFire2,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire2,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire2, 1, 129, 123, 255); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire2, 129, 129, 255, 255); }
			ObjSprite2D_SetDestRect(objFire2, 0.5, -61, 128.5, 61);
			
			if(Obj_IsDeleted(objHitbox)) {
				Obj_Delete(objLaser1);
				Obj_Delete(objLaser2);
				Obj_Delete(objDetail);
				Obj_Delete(objFire);
				Obj_Delete(objFire2);
			}
			
			length+=500/20;
			width+=(maxwidth-2)/20;
			fire_count+=0.5;
			detail_count+=12;
			yield;
		}
		
		loop(del) {
			newangle+=postrotation;
			ObjStLaser_SetAngle(objHitbox,newangle);
			newX+=xmov;
			newY+=ymov;
			ObjMove_SetX(objHitbox,newX);
			ObjMove_SetY(objHitbox,newY);
		
			ObjRender_SetScaleXYZ(objLaser1,2*(length/500),width/25,1);
			ObjRender_SetPosition(objLaser1,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser1,ObjStLaser_GetAngle(objHitbox));
			
			ObjRender_SetScaleXYZ(objLaser2,2*(length/500),width/32,0);
			ObjRender_SetPosition(objLaser2,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser2,ObjStLaser_GetAngle(objHitbox));
			
			
			ObjRender_SetScaleXYZ(objDetail,2,width/32,0);
			ObjRender_SetPosition(objDetail,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objDetail,ObjStLaser_GetAngle(objHitbox));
			ObjSprite2D_SetSourceRect(objDetail, 0-detail_count, 0, 256-detail_count, 30);
			ObjSprite2D_SetDestRect(objDetail, 0.5, -18, 256.5, 18);
			
			
			ObjRender_SetPosition(objFire,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire, 1, 1, 123, 127); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire, 129, 1, 255, 127); }
			ObjSprite2D_SetDestRect(objFire, 0.5, -61, 128.5, 61);
			
			ObjRender_SetPosition(objFire2,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire2,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire2, 1, 129, 123, 255); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire2, 129, 129, 255, 255); }
			ObjSprite2D_SetDestRect(objFire2, 0.5, -61, 128.5, 61);
			
			if(Obj_IsDeleted(objHitbox)) {
				Obj_Delete(objLaser1);
				Obj_Delete(objLaser2);
				Obj_Delete(objDetail);
				Obj_Delete(objFire);
				Obj_Delete(objFire2);
			}
			
			fire_count+=0.5;
			detail_count+=12;
			yield;
		}
		
		loop(30) {
			newangle+=postrotation;
			ObjStLaser_SetAngle(objHitbox,newangle);
			newX+=xmov;
			newY+=ymov;
			ObjMove_SetX(objHitbox,newX);
			ObjMove_SetY(objHitbox,newY);
		
			ObjShot_SetIntersectionEnable(objHitbox,false);
		
			ObjRender_SetScaleXYZ(objLaser1,2,width/25,1);
			ObjRender_SetPosition(objLaser1,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser1,ObjStLaser_GetAngle(objHitbox));
			
			ObjRender_SetScaleXYZ(objLaser2,2,width/32,0);
			ObjRender_SetPosition(objLaser2,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objLaser2,ObjStLaser_GetAngle(objHitbox));
			
			
			ObjRender_SetScaleXYZ(objDetail,2,width/32,0);
			ObjRender_SetPosition(objDetail,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objDetail,ObjStLaser_GetAngle(objHitbox));
			ObjSprite2D_SetSourceRect(objDetail, 0-detail_count, 0, 256-detail_count, 30);
			ObjSprite2D_SetDestRect(objDetail, 0.5, -18, 256.5, 18);
			
			
			ObjRender_SetScaleY(objFire,fire_scale*(width/32));
			ObjRender_SetPosition(objFire,ObjMove_GetX(objHitbox),ObjMove_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire, 1, 1, 123, 127); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire, 129, 1, 255, 127); }
			ObjSprite2D_SetDestRect(objFire, 0.5, -61, 128.5, 61);
			
			ObjRender_SetScaleY(objFire2,fire_scale2*(width/32));
			ObjRender_SetPosition(objFire2,ObjMove_GetX(objHitbox),ObjRender_GetY(objHitbox),0);
			ObjRender_SetAngleZ(objFire2,ObjStLaser_GetAngle(objHitbox));
			if(fire_count%2==0) { ObjSprite2D_SetSourceRect(objFire2, 1, 129, 123, 255); }
			else if(fire_count%2==1) { ObjSprite2D_SetSourceRect(objFire2, 129, 129, 255, 255); }
			ObjSprite2D_SetDestRect(objFire2, 0.5, -61, 128.5, 61);
			
			if(Obj_IsDeleted(objHitbox)) {
				Obj_Delete(objLaser1);
				Obj_Delete(objLaser2);
				Obj_Delete(objDetail);
				Obj_Delete(objFire);
				Obj_Delete(objFire2);
			}
			
			width-=(maxwidth+2)/30;
			fire_count+=0.5;
			detail_count+=12;
			yield;
		}
		Obj_Delete(objLaser1);
		Obj_Delete(objLaser2);
		Obj_Delete(objDetail);
		Obj_Delete(objFire);
		Obj_Delete(objFire2);
	}
}