/* ---------------------------------------

--------------------------------------- */

let optionX1 = 64;
let optionY1 = 144;
let optionX2 = 80;
let optionY2 = 160;

let option_shotDamage = 1.95 * player_DamageMultiplier;

let opPos_levelOne   = [ 
	[0, 32, 0, 20], 
	[0, 0, 0, 0], 
	[0, 0, 0, 0], 
	[0, 0, 0, 0] ];
	
let opPos_levelTwo   = [ 
	[-32, 10, -16, 20], 
	[32, 10, 16, 20], 
	[0, 0, 0, 0], 
	[0, 0, 0, 0] ];
	
let opPos_levelThree = [ 
	[-32, 10, -16, 20], 
	[32, 10, 16, 20], 
	[0, 32, 0, 30], 
	[0, 0, 0, 0] ];
	
let opPos_levelFour  = [ 
	[-32, 10, -16, 20], 
	[32, 10, 16, 20], 
	[-16, 32, -8, 30], 
	[16, 32, 8, 30] ];

let player_maxPower = 500;

/* ---------------------------------------

--------------------------------------- */

let lockX = 0;
let lockY = 0;
let lockTime = 0;

let home_enemyArray = [];
let home_closestObj = 0;

let opPos_none   = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ];
let opPos_current = opPos_none;
let opPos_Array = [opPos_none, opPos_levelOne, opPos_levelTwo, opPos_levelThree, opPos_levelFour];

let optionOne = 0;
let optionTwo = 0;
let optionThree = 0;
let optionFour = 0;

let optionArray = [0, 0, 0, 0];

task InitOptions()
{
	optionOne = CreateOptionObject();
	optionTwo = CreateOptionObject();
	optionThree = CreateOptionObject();
	optionFour = CreateOptionObject();
	
	OptionMoveLoop(optionOne, 0);
	OptionMoveLoop(optionTwo, 1);
	OptionMoveLoop(optionThree, 2);
	OptionMoveLoop(optionFour, 3);
	
	OptionDrawLoop(optionOne, 0);
	OptionDrawLoop(optionTwo, 0);
	OptionDrawLoop(optionThree, 0);
	OptionDrawLoop(optionFour, 0);
	
	OptionFireLoop(optionOne, 5, false);
	OptionFireLoop(optionTwo, 5, false);
	OptionFireLoop(optionThree, 5, false);
	OptionFireLoop(optionFour, 5, false);
}

task CreateHomingPlayerShot(x, y, speed, angle, dmg, pene)
{
	if(!IsPermitPlayerShot)
	{
		return;
	}
		
	function ShotFade(angle)
	{
		let frame = 0;
		
		let alpha = 250;
		let angmod = 0;
		let scalex = 1;
		let scaley = 1;
		
		let x_vel = 3 * cos(angle);
		let y_vel = 3 * sin(angle);
		
		let inx = loc_x + (x_vel*5);
		let iny = loc_y + (y_vel*5);
		
		let xOffset = 16;
		
		let fadeEff = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(fadeEff, img_player);
		ObjSprite2D_SetSourceRect(fadeEff, 0, 160, 16, 176);
		ObjSprite2D_SetDestCenter(fadeEff);
		
		ObjRender_SetBlendType(fadeEff, BLEND_ADD_ARGB);
		ObjRender_SetAlpha(fadeEff, 150);
		ObjRender_SetPosition(fadeEff, loc_x, iny, 1);
		ObjRender_SetScaleXYZ(fadeEff, 1.1, 1.1, 1);
		ObjRender_SetAngleZ(fadeEff, angle + 270);
		
		Obj_SetRenderPriority(fadeEff, 0.47);
		
		while(alpha > 0)
		{
			if(frame >= 0 && frame < 3)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 0, 160, 16, 176);
			}
			if(frame >= 6 && frame < 9)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 16, 160, 32, 176);
			}
			if(frame >= 9 && frame < 12)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 32, 160, 48, 176);
			}
			if(frame >= 12 && frame < 15)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 48, 160, 64, 176);
			}
			
			ObjRender_SetAngleZ(fadeEff, angle + angmod);	
			ObjRender_SetScaleXYZ(fadeEff, scalex, scaley, 1);
			ObjRender_SetColor(fadeEff, alpha, alpha, alpha);
			ObjRender_SetPosition(fadeEff, inx, iny, 1);
			
			frame++;
			alpha -= 15;
			inx += x_vel;
			iny += y_vel;
			angmod += 20;
			yield;
		}
		Obj_Delete(fadeEff);
	}
	
	let rframe = 0;
	let ar = GetAllEnemyID;
	let loc_x;
	let loc_y;
	let loc_angle;
	
	let obj = CreatePlayerShotA1(x, y, speed, angle, dmg, 1, 2);
	Obj_SetRenderPriorityI(obj, 47);
	
	let home_doHome = false;
	let home_angMod = 5;
	let home_obj = 0;
	let home_dir = 270;
	
	let home_diff = 0;
	let home_diffAbs = 0;
	
	if(length(home_enemyArray) != 0)
	{
		if(ObjEnemy_GetInfo(home_closestObj, INFO_LIFE) > 0)
		{
			home_doHome = true;
			home_obj = home_closestObj;
		}
	}
	
	while(!Obj_IsDeleted(obj))
	{
		loc_x = ObjMove_GetX(obj);
		loc_y = ObjMove_GetY(obj);
		loc_angle = ObjMove_GetAngle(obj);
		
		if(home_doHome && !Obj_IsDeleted(home_obj))
		{
			home_dir = atan2(ObjMove_GetY(home_obj) - loc_y, ObjMove_GetX(home_obj) - loc_x);
			home_diff = home_dir - angle;
			while(home_diff >= 180) { home_diff -= 360; }
			while(home_diff < -180) { home_diff += 360; }
			home_diffAbs = (|home_diff|);
			
			home_angMod = home_diffAbs/10;
			
			if(home_diffAbs <= 2)
			{
				angle = home_dir;
			}
			else if(home_diffAbs > 2)
			{
				angle += home_angMod * home_diff / (home_diffAbs/2);
			}
			
		}

		ObjMove_SetAngle(obj, angle);
		yield;
	}
	if(loc_y >= 0)
	{
		ShotFade(loc_angle);
	}
}

function CreateOptionObject()
{
	let op_obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjRender_SetPosition(op_obj, GetPlayerX, GetPlayerY, 0);
	Obj_SetValue(op_obj, "ACTIVE", 0);
	Obj_SetValue(op_obj, "ANIM", 0);
	return op_obj;
}

task OptionMoveLoop(op_obj, loc_pos)
{
	let distancetopoint = 0;
	let directiontopoint = 0;
	let orbit = 0;
	let count = 0;
	let dir1 = 270;
	let dir2 = 270;
	let warpOffset = 0;

	let destinationx = GetPlayerX;
	let destinationy = GetPlayerY;
	
	while(!Obj_IsDeleted(op_obj))
	{
		alternative(Obj_GetValueD(op_obj, "ACTIVE", 0))
		case(-1)
		{
			if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{
				destinationx = GetPlayerX + opPos_current[loc_pos][2];
				destinationy = GetPlayerY + opPos_current[loc_pos][3];
			}
			else
			{
				destinationx = GetPlayerX + opPos_current[loc_pos][0];
				destinationy = GetPlayerY + opPos_current[loc_pos][1];
			}
			ObjRender_SetPosition(op_obj, destinationx, destinationy, 1);
			Obj_SetValue(op_obj, "ACTIVE", 1);
		}
		case(0)
		{
			
		}
		case(1)
		{
			distancetopoint = ((destinationx - ObjRender_GetX(op_obj))^2 + (destinationy - ObjRender_GetY(op_obj))^2)^0.5;
			directiontopoint = atan2(destinationy - ObjRender_GetY(op_obj),destinationx - ObjRender_GetX(op_obj));
			if(distancetopoint >= 1)
			{
				ObjRender_SetPosition(op_obj, ObjRender_GetX(op_obj) + cos(directiontopoint) * distancetopoint * 0.4, ObjRender_GetY(op_obj) + sin(directiontopoint) * distancetopoint * 0.4, 1);
			}
			else
			{
				ObjRender_SetPosition(op_obj, destinationx, destinationy, 1);
			}
			
			if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{
				destinationx = GetPlayerX + opPos_current[loc_pos][2];
				destinationy = GetPlayerY + opPos_current[loc_pos][3];
			}
			else
			{
				destinationx = GetPlayerX + opPos_current[loc_pos][0];
				destinationy = GetPlayerY + opPos_current[loc_pos][1];
			}
		}
		yield;
	}
}

task OptionDrawLoop(op_obj, angle)
{
	let orbit = 0;
	let orbitB = 0;
	let count = 0;
	let animFrame = 0;
	let animState = 0;
	let animScale = 0.25;
	let animScaleMod = 0.32;
	
	let optionDraw = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(optionDraw, img_player);
	ObjSprite2D_SetSourceRect(optionDraw, optionX1, optionY1, optionX2, optionY2);
	ObjSprite2D_SetDestRect(optionDraw, -7.5, -7.5, 8.5, 8.5);
	ObjRender_SetBlendType(optionDraw, BLEND_ALPHA);
	Obj_SetRenderPriorityI(optionDraw, 25);
	
	let optioneffect = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(optioneffect, img_player);
	ObjSprite2D_SetSourceRect(optioneffect, optionX1, optionY1, optionX2, optionY2);
	ObjSprite2D_SetDestRect(optioneffect, -7.5, -7.5, 8.5, 8.5);
	ObjRender_SetBlendType(optioneffect, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(optioneffect, 24);
	
	while(!Obj_IsDeleted(op_obj))
	{
		if(playerPower != player_maxPower)
		{
			ObjRender_SetAlpha(optioneffect, 0);
		}
		else
		{
			ObjRender_SetAlpha(optioneffect, 150);
		}
		
		if(Obj_GetValueD(op_obj, "RESET", false) == true)
		{
			animState = 0;
			animFrame = 0;
			animScale = 0.25;
			animScaleMod = 0.2;
			orbitB = 0;
			Obj_SetValue(op_obj, "RESET", false);
		}
		
		alternative(Obj_GetValueD(op_obj, "ACTIVE", 0))
		case(-1, 0)
		{
			ObjRender_SetAlpha(optionDraw, 0);
			ObjRender_SetAlpha(optioneffect, 0);
		}
		case(1)
		{
			if(animState == 0)
			{
				if(animFrame == 14)
				{
					animState = 1;
				}
				count = 0;
				animScale += animScaleMod;
				animScaleMod -= 0.021;
				orbitB += 13;
				ObjRender_SetScaleXYZ(optionDraw, animScale, animScale, 1);
				ObjRender_SetBlendType(optionDraw, BLEND_ADD_ARGB);
				animFrame++;
			}
			else
			{
				animScale = 1;
				ObjRender_SetScaleXYZ(optionDraw, 0.95, 0.95, 1);
				ObjRender_SetBlendType(optionDraw, BLEND_ALPHA);
				count++;
			}
			ObjRender_SetAlpha(optionDraw, 255);
		}
		
		ObjRender_SetAngleZ(optioneffect, orbit);
		ObjRender_SetAngleZ(optionDraw, orbitB);
		
		ObjRender_SetScaleXYZ(optioneffect, 1.52, 1.52, 1);
		
		ObjRender_SetPosition(optionDraw, ObjRender_GetX(op_obj), ObjRender_GetY(op_obj), 1);
		ObjRender_SetPosition(optioneffect, ObjRender_GetX(op_obj), ObjRender_GetY(op_obj), 1);
		
		orbit -= 6;
		orbitB += 6;
		yield;
	}
	let scale = 1;
	while(scale > 0)
	{
		scale -= 0.25;
		ObjRender_SetScaleY(optionDraw, scale/2);
		ObjRender_SetScaleX(optioneffect, scale);
		yield;
	}
	Obj_Delete(optionDraw);
	Obj_Delete(optioneffect);
}

task OptionDeathEffect(op_obj)
{
	let eff_scaleX = 1;
	let eff_scaleY = 1;
	let eff_angle = 0;
	let optionEffect = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(optionEffect, img_player);
	ObjSprite2D_SetSourceRect(optionEffect, optionX1, optionY1, optionX2, optionY2);
	ObjSprite2D_SetDestRect(optionEffect, -7.5, -7.5, 8.5, 8.5);
	ObjRender_SetBlendType(optionEffect, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(optionEffect, 25);
	ObjRender_SetPosition(optionEffect, ObjRender_GetX(op_obj), ObjRender_GetY(op_obj), 1);
	
	while(eff_scaleX > 0)
	{
		eff_scaleX -= 0.1;
		eff_scaleY += 0.1;
		eff_angle += 4;
		ObjRender_SetScaleXYZ(optionEffect, eff_scaleX, eff_scaleY, 1);
		ObjRender_SetAngleZ(optionEffect, eff_angle);
		yield;
	}
	Obj_Delete(optionEffect);
}
	
function OptionFinishFlags(op_obj)
{
	Obj_SetValue(op_obj, "ACTIVE", 0);
}

function OptionResetFlags(op_obj)
{
	Obj_SetValue(op_obj, "ACTIVE", -1);
	Obj_SetValue(op_obj, "RESET", true);
}

task OptionFireLoop(op_obj, firerate, noFocusOnly)
{
	let countA = 0;
	let countB = 20;

	while(!Obj_IsDeleted(op_obj))
	{
		if(countB >= firerate && canPlayerFire == true && Obj_GetValueD(op_obj, "ACTIVE", 0) != 0)
		{
			if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD)
			{
				if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
				{

				}
				else
				{
					
				}
				CreateHomingPlayerShot(ObjRender_GetX(op_obj), ObjRender_GetY(op_obj), 14, 270, option_shotDamage, 1);
				countB = 0;
			}
		}
		countB++;
		yield;
	}
}

task PowerCheck(didGet, op_deleteType)
{
	if(playerPower >= 0 && playerPower < 100 && powerState != 0)
	{
		powerState = 0;
		opPos_current = opPos_Array[powerState];
		alternative(op_deleteType)
		case(0)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(1)
		{
			OptionDeathEffect(optionArray[0]);
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(2)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionDeathEffect(optionArray[i]);
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		optionArray = [0, 0, 0, 0];
	}
	else if(playerPower >= 100 && playerPower < 200 && powerState != 1)
	{
		powerState = 1;
		opPos_current = opPos_Array[powerState];
		if(didGet == 1)
		{
			PlaySE(se_powerup1);
		}
		alternative(op_deleteType)
		case(0)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(1)
		{
			OptionDeathEffect(optionArray[1]);
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(2)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionDeathEffect(optionArray[i]);
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		OptionResetFlags(optionOne);
		optionArray = [optionOne, 0, 0, 0];
	}
	else if(playerPower >= 200 && playerPower < 300 && powerState != 2)
	{
		powerState = 2;
		opPos_current = opPos_Array[powerState];
		if(didGet == 1)
		{
			PlaySE(se_powerup1);
		}
		alternative(op_deleteType)
		case(0)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(1)
		{
			OptionDeathEffect(optionArray[2]);
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(2)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionDeathEffect(optionArray[i]);
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		OptionResetFlags(optionOne);
		OptionResetFlags(optionTwo);
		optionArray = [optionOne, optionTwo, 0, 0];
	}
	else if(playerPower >= 300 && playerPower < 400 && powerState != 3)
	{
		powerState = 3;
		opPos_current = opPos_Array[powerState];
		if(didGet == 1)
		{
			PlaySE(se_powerup1);
		}
		alternative(op_deleteType)
		case(0)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(1)
		{
			OptionDeathEffect(optionArray[3]);
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		case(2)
		{
			ascent(i in 0..length(optionArray))
			{
				if(optionArray[i] != 0)
				{
					OptionDeathEffect(optionArray[i]);
					OptionFinishFlags(optionArray[i]);
				}
			}
		}
		OptionResetFlags(optionOne);
		OptionResetFlags(optionTwo);
		OptionResetFlags(optionThree);
		optionArray = [optionOne, optionTwo, optionThree, 0];
	}
	else if(playerPower >= 400 && playerPower < 500 && powerState != 4)
	{
		powerState = 4;
		opPos_current = opPos_Array[powerState];
		if(didGet == 1)
		{
			PlaySE(se_powerup1);
		}
		
		if(didGet != -1)
		{
			alternative(op_deleteType)
			case(0)
			{
				ascent(i in 0..length(optionArray))
				{
					if(optionArray[i] != 0)
					{
						OptionFinishFlags(optionArray[i]);
					}
				}
			}
			case(2)
			{
				ascent(i in 0..length(optionArray))
				{
					if(optionArray[i] != 0)
					{
						OptionDeathEffect(optionArray[i]);
						OptionFinishFlags(optionArray[i]);
					}
				}
			}
			OptionResetFlags(optionOne);
			OptionResetFlags(optionTwo);
			OptionResetFlags(optionThree);
			OptionResetFlags(optionFour);
			optionArray = [optionOne, optionTwo, optionThree, optionFour];
		}
	}
	else if(playerPower >= 500 && powerState != 5)
	{
		powerState = 5;
		opPos_current = opPos_Array[4];
		playerPower = 500;
		if(didGet == 1)
		{
			PlaySE(se_powerup1);
		}
		if(didGet == 2)
		{
			alternative(op_deleteType)
			case(0)
			{
				ascent(i in 0..length(optionArray))
				{
					if(optionArray[i] != 0)
					{
						OptionFinishFlags(optionArray[i]);
					}
				}
			}
			OptionResetFlags(optionOne);
			OptionResetFlags(optionTwo);
			OptionResetFlags(optionThree);
			OptionResetFlags(optionFour);
			optionArray = [optionOne, optionTwo, optionThree, optionFour];
		}
	}
}
