let isPlayerDead = false;
let isPlayerDeadB = false;
let isPlayerHit = false;
let isPlayerActive = true;
let isPlayerImmobile = false;
let isPlayerInvincible = false;
let canPlayerFire = true;

let powerState = 0;

let flash = 0;

let displayUsesMain = 0;
let loc_distance = 10000;

let GetAngleToCenter = atan2(GetPlayerY - GetStgFrameHeight/2, GetPlayerX - GetStgFrameWidth/2);

let flag_stopTimer = false;

let GCX = GetStgFrameWidth/2.5;
let GCY = GetStgFrameHeight/2.5;

let blink_counter = 0;

let player_basicShotDamage = 3.45 * player_DamageMultiplier;

let playerDebugEnabled = false;
let playerDebugTimer = 0;

task InitPlayer()
{
	RenderPlayer();
	RenderHitbox();
	PlayerFireLoop();
	PlayerDebug();
}

function FP_DeleteShotInCircle_th10(x, y, radius)
{
	let loc_a = GetShotIdInCircleA2(x, y, radius, TARGET_ENEMY);
	ascent(i in 0..length(loc_a))
	{
		if(!ObjShot_IsSpellResist(loc_a[i]))
		{
			if(showBreakEffect)
			{
				BulletDelete_th10(ObjMove_GetX(loc_a[i]), ObjMove_GetY(loc_a[i]), ObjMove_GetSpeed(loc_a[i]), ObjMove_GetAngle(loc_a[i]), ObjShot_GetImageID(loc_a[i]));
			}
			ObjShot_SetDeleteFrame(loc_a[i], 0);
		}
	}
}

task FP_ClearAllShots_th10(x, y, radiusMod)
{
	let loc_radius = 0;
	while(loc_radius <= 700)
	{
		FP_DeleteShotInCircle_th10(x, y, loc_radius);
		loc_radius += radiusMod;
		yield;
	}
}

task BulletDelete_th10(x, y, speed, angle, graphic)
{	
	function CreateBreakObject(x, y, blend)
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let colorArray = GetShotDataInfoA1(graphic, TARGET_ENEMY, INFO_DELAY_COLOR);
		ObjPrim_SetTexture(obj, img_etbreak);
		Obj_SetRenderPriorityI(obj, 46);
		ObjSprite2D_SetSourceRect(obj, 0, 0, 16, 16);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetBlendType(obj, blend);
		ObjRender_SetAlpha(obj, 100);
		ObjRender_SetAngleZ(obj, rand(0,360));
		ObjRender_SetColor(obj,100 + colorArray[0],100 + colorArray[1],100 + colorArray[2]);
		//GraphicCheck(obj, color);
		
		return obj;
	}
	
	if(x < GetStgFrameWidth + 30 && x > -30 && y < GetStgFrameHeight + 30 && y > -30)
	{
		let x_vel = speed/2 * cos(angle);
		let y_vel = speed/2 * sin(angle);
		
		let frameTimer = 0;
		//let effectObj;
		let scale = 1;
		let scaleB = 1;
		let yMod = 0;
		let effectObj = CreateBreakObject(x, y, BLEND_ADD_ARGB);
		let effectObjB = CreateBreakObject(x, y, BLEND_ADD_ARGB);
		//ObjRender_SetScaleXYZ(effectObj, scale, scale, 0);
		while(scale > 0)
		{
			x += x_vel;
			y += y_vel;
			if(scaleB >= 1.4)
			{
				Obj_Delete(effectObjB);
			}
			ObjRender_SetScaleXYZ(effectObj, scale, scale, 0);
			ObjRender_SetScaleXYZ(effectObjB, scaleB, scaleB, 0);
			ObjRender_SetY(effectObjB, y - yMod);
			ObjRender_SetPosition(effectObj, x, y, 0);
			scale -= 0.045;
			scaleB += 0.045;
			yMod += 1;
			frameTimer++;
			yield;
		}
		Obj_Delete(effectObj);
		
	}
}

task DeathExplosion()
{
	task CreateEffectParticle(effType)
	{
		let effScale = rand(1.75, 2);
		let effScaleMod = effScale / 25;
		let eff_X = GetPlayerX;
		let eff_Y = GetPlayerY;
		
		let eff_X_vel = rand(-6, 6);
		let eff_Y_vel = rand(-6, 6);
		
		let objParticle = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(objParticle, BLEND_ADD_RGB);
		ObjPrim_SetTexture(objParticle, img_effect_base);
		ObjSprite2D_SetSourceRect(objParticle, 0, 0, 16, 16);
		ObjSprite2D_SetDestCenter(objParticle);
		Obj_SetRenderPriorityI(objParticle, 60);
		ObjRender_SetPosition(objParticle, eff_X, eff_Y, 0);
		alternative(effType)
		case(0)
		{
			ObjRender_SetBlendType(objParticle, BLEND_ADD_RGB);
			ObjRender_SetColor(objParticle, 150, 150, 150);
		}
		case(1)
		{
			ObjRender_SetBlendType(objParticle, BLEND_MULTIPLY);
			ObjPrim_SetTexture(objParticle, img_effect_base_mult);
		}
		
		while(effScale > 0)
		{
			
			eff_X += eff_X_vel;
			eff_Y += eff_Y_vel;
			effScale -= effScaleMod;
			ObjRender_SetPosition(objParticle, eff_X, eff_Y, 0);
			ObjRender_SetScaleXYZ(objParticle, effScale, effScale, 0);
			yield;
		}
		Obj_Delete(objParticle);
	}
	
	loop(20)
	{
		CreateEffectParticle(0);
	}
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, img_eff01);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 60);
	ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
	
	let alpha = 150;
	let scale = 0;
	let scalemod = 0.55;
	loop(40)
	{
		scale += scalemod;
		alpha -= 5;
		if(scalemod > 0)
		{
			scalemod = scalemod^1.07;
		}
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		yield;
	}
	Obj_Delete(obj);
}

task ClearEnemyBullets(x, y, Tsize, Tspd, Tmaxrad, delay)
{
	let stopclear = true;
	loop(delay){yield;}
	while(stopclear==true && GetPlayerState==STATE_DOWN)
	{
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,x,y,Tsize);
		Tsize += Tspd;
		if(Tsize>=Tmaxrad)
		{
			stopclear = false;
		}
		yield;
		DeleteShotInCircle(TYPE_ALL,TYPE_FADE,x,y,Tsize);
	}
}

task ClearEnemyBulletsB(x, y, Tsize, Tspd, Tmaxrad, delay)
{
	let stopclear = true;
	loop(delay){yield;}
	while(stopclear==true)
	{
		DeleteShotInCircle(TYPE_SHOT,TYPE_IMMEDIATE,x,y,Tsize);
		Tsize += Tspd;
		if(Tsize >= Tmaxrad)
		{
			stopclear = false;
		}
		yield;
		DeleteShotInCircle(TYPE_ALL,TYPE_IMMEDIATE,x,y,Tsize);
	}
}

task CreateBasicPlayerShot(x, y, speed, dir, texture, x1, y1, x2, y2, x3, y3, x4, y4, dmg, pene)
{
	if(!IsPermitPlayerShot)
	{
		return;
	}
	
	function ShotFade
	{
		let frame = 0;
		
		let alpha = 250;
		let angmod = 0;
		let scalex = 1;
		let scaley = 1;
		let inx = loc_x;
		let iny = loc_y - 20;
		
		let fadeEff = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(fadeEff, img_player);
		ObjSprite2D_SetSourceRect(fadeEff, 0,146,15,157);
		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, 270);
		
		Obj_SetRenderPriority(fadeEff, 0.47);
		
		while(alpha > 0)
		{
			if(frame >= 0 && frame < 3)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 0,146,15,157);
			}
			if(frame >= 6 && frame < 9)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 16,146,31,157);
			}
			if(frame >= 9 && frame < 12)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 32,146,47,157);
			}
			if(frame >= 12 && frame < 15)
			{
				ObjSprite2D_SetSourceRect(fadeEff, 48,146,63,157);
			}
			
			ObjRender_SetAngleZ(fadeEff, 270 + angmod);	
			ObjRender_SetScaleXYZ(fadeEff, scalex, scaley, 1);
			ObjRender_SetColor(fadeEff, alpha, alpha, alpha);
			ObjRender_SetPosition(fadeEff, loc_x, iny, 1);
			
			frame++;
			alpha -= 15;
			iny -= 5;
			angmod += 20;
			yield;
		}
		Obj_Delete(fadeEff);
	}
	
	let loc_x;
	let loc_y;
	
	let obj = CreatePlayerShotA1(x, y, speed, dir, dmg, 1, 1);
	Obj_SetRenderPriorityI(obj, 47);
	
	while(!Obj_IsDeleted(obj))
	{
		loc_x = ObjMove_GetX(obj);
		loc_y = ObjMove_GetY(obj);
		yield;
	}
	if(loc_y <= 0)
	{
		yield;
	}
	else
	{
		ShotFade;
	}
}

task PlayerFireLoop()
{
	let shotCount = 0;
	let shotCountOptionA = 0;
	let shotCountOptionB = 0;
	loop
	{
		if(isPlayerDead == false && canPlayerFire == true)
		{
			if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && shotCount==-1)
			{
				shotCount = 0;
			}
			if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{	
				if(shotCount == 2 && canPlayerFire == true && isPlayerImmobile == false)
				{
					CreateBasicPlayerShot(round(GetPlayerX) - 10, round(GetPlayerY) + 15, 24, 270, img_player, 0, 144, 32, 160, 0, 0, 0, 0, player_basicShotDamage, 1);
					CreateBasicPlayerShot(round(GetPlayerX) + 10, round(GetPlayerY) + 15, 24, 270, img_player, 0, 144, 32, 160, 0, 0, 0, 0, player_basicShotDamage, 1);
					PlaySE(se_shot);
				}	
			}
			else
			{
				if(shotCount == 2 && canPlayerFire == true && isPlayerImmobile == false)
				{
					CreateBasicPlayerShot(round(GetPlayerX) - 10, round(GetPlayerY) + 15, 24, 270, img_player, 0, 144, 32, 160, 0, 0, 0, 0, player_basicShotDamage, 1);
					CreateBasicPlayerShot(round(GetPlayerX) + 10, round(GetPlayerY) + 15, 24, 270, img_player, 0, 144, 32, 160, 0, 0, 0, 0, player_basicShotDamage, 1);
					PlaySE(se_shot);
				}	
			}

			if(shotCount >= 0)
			{
				shotCount++;
			}
			if(shotCount == 3)
			{
				shotCount=-1;
			}
			yield;
		}
		else
		{
			yield;
		}
	}
}

task ShiftCheck()
{
	if(useAlternateBombType)
	{
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD || GetCommonData("isDebugEnabled", false) == true)
		{
			PlaySE(se_extend);
			playerDebugEnabled = true;
			playerPower = 500;
			PowerCheck(2, 0);
		}
		else
		{
			PowerCheck(2, 0);
		}
	}
	else
	{
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD || GetCommonData("isDebugEnabled", false) == true)
		{
			PlaySE(se_extend);
			playerDebugEnabled = true;
			playerPower = 500;
			PowerCheck(2, 0);
		}
		else
		{
			PowerCheck(2, 0);
		}
	}
}

task SpellcardBomb_th10
{
	let objManage = GetSpellManageObject;
	let spellTime = 0;
	let spellHitSize = 0;
	
	let spellX = GetPlayerX;
	let spellY = GetPlayerY;
	
	let mainAlpha = 255;
	
	let spellRise = 1;      //Reimu - 1    Marisa - 1.5
	let spellSize = 180;    //Reimu - 180  Marisa - 130
	let maxSpellTime = 90;
	
	let objSpell = ObjSpell_Create();
	ObjSpell_Regist(objSpell);
	ObjSpell_SetDamage(objSpell, player_BombDamage * player_BombDamageMultiplier);
	ObjSpell_SetEraseShot(objSpell, false);
	
	function CreateRing(x, y, vertexCount, width, maxRadius)
	{
		let loc_obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(loc_obj, img_effect_line);
		ObjPrim_SetPrimitiveType(loc_obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(loc_obj, vertexCount+2);
		ObjRender_SetBlendType(loc_obj, BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(loc_obj, 70);
		
		Obj_SetValue(loc_obj, "a", 0);
		Obj_SetValue(loc_obj, "b", 0);
		Obj_SetValue(loc_obj, "c", 0);
		
		task ManageRing()
		{
			let calcAngle = 0;
			let calcUVT = 384/(vertexCount/2);
			ascent(i in 0..(vertexCount/2) + 2)
			{
				ObjPrim_SetVertexUVT(loc_obj, 0 + i*2, 79, 0 + calcUVT*i);
				ObjPrim_SetVertexUVT(loc_obj, 1 + i*2, 64, 0 + calcUVT*i);
			}
			
			while(!Obj_IsDeleted(loc_obj))
			{
				ascent(i in 0..(vertexCount/2) + 2)
				{
					calcAngle = i * (360/(vertexCount/2));
					ObjPrim_SetVertexPosition(loc_obj, 0 + i*2, 
					0 + (Obj_GetValue(loc_obj, "b") * cos(calcAngle + Obj_GetValue(loc_obj, "a"))), 
					0 + (Obj_GetValue(loc_obj, "b") * sin(calcAngle + Obj_GetValue(loc_obj, "a"))), 1);
					
					ObjPrim_SetVertexPosition(loc_obj, 1 + i*2, 
					0 + ((Obj_GetValue(loc_obj, "b") - Obj_GetValue(loc_obj, "c")) * cos(calcAngle + Obj_GetValue(loc_obj, "a"))), 
					0 + ((Obj_GetValue(loc_obj, "b") - Obj_GetValue(loc_obj, "c")) * sin(calcAngle + Obj_GetValue(loc_obj, "a"))), 1);
				}
				yield;
			}
		}
		ObjRender_SetPosition(loc_obj, 100, 100, 1);
		ManageRing();
		return loc_obj;
	}
	
	task CreateMoFBombRing(obj, type, width, maxRadius)
	{
		let ring = CreateRing(GetPlayerX, spellY, 32, 18, 50);
		
		let spellRadiusSpeed = 2.5;
		let spellRadiusWeight = 8;
		let spellMaxMaxRadius = 150;
		let spellMaxRadius = maxRadius;
		let spellDistance = spellMaxMaxRadius - spellMaxRadius;
		
		let spellWidth = width;
		let spellRad = spellSize;
		let spellAlpha = 255;
		let spellAngleZ = 0;
		
		alternative(type)
		case(0) //Main Ring
		{
			spellRadiusSpeed = 2.5;
			spellRadiusWeight = 8;
			spellMaxMaxRadius = spellRad;
			spellMaxRadius = maxRadius;
			spellDistance = spellMaxMaxRadius - spellMaxRadius;
		
			spellWidth = width;
			spellAlpha = 255;
			spellAngleZ = 0;
		}
		case(1) //Sub Ring
		{
			spellRadiusSpeed = 2;
			spellRadiusWeight = 8;
			spellMaxMaxRadius = spellRad - 8;
			spellMaxRadius = maxRadius;
			spellDistance = spellMaxMaxRadius - spellMaxRadius;
		
			spellWidth = width;
			spellAlpha = 255;
			spellAngleZ = 0;
		}
		case(2) //Purple Ring A
		{
			spellRadiusSpeed = 2;
			spellRadiusWeight = 22;
			spellMaxMaxRadius = spellRad + 30;
			spellMaxRadius = maxRadius;
			spellDistance = spellMaxMaxRadius - spellMaxRadius;
		
			spellWidth = width;
			spellAlpha = 255;
			spellAngleZ = 0;
		}
		case(3) //Purple Ring B
		{
			spellRadiusSpeed = 2;
			spellRadiusWeight = 22;
			spellMaxMaxRadius = spellRad + 30;
			spellMaxRadius = maxRadius;
			spellDistance = spellMaxMaxRadius - spellMaxRadius;
		
			spellWidth = width;
			spellAlpha = 255;
			spellAngleZ = 0;
		}
		
		while(!Obj_IsDeleted(ring))
		{
			alternative(type)
			case(0) //Main Ring
			{
				ObjRender_SetAlpha(ring, mainAlpha);
				ObjRender_SetPosition(ring, spellX, spellY, 1);
				Obj_SetValue(ring, "a", spellAngleZ);
				Obj_SetValue(ring, "b", spellMaxRadius);
				Obj_SetValue(ring, "c", spellWidth);
				
				if(spellDistance > 1)
				{
					spellDistance = spellMaxMaxRadius - spellMaxRadius;
					let spellRadiusSpeed = spellDistance / spellRadiusWeight;
					
					if(spellRadiusSpeed > 5)
					{
						spellRadiusSpeed = 5;
					}
					spellMaxRadius += spellRadiusSpeed;
				}
				
				if(mainAlpha <= 0)
				{
					Obj_Delete(ring);
				}
				spellHitSize = spellMaxRadius - 8;
				spellAngleZ += 32;
			}
			case(1) //Sub Ring
			{
				ObjRender_SetAlpha(ring, mainAlpha);
				ObjRender_SetPosition(ring, spellX, spellY, 1);
				Obj_SetValue(ring, "a", spellAngleZ);
				Obj_SetValue(ring, "b", spellMaxRadius);
				Obj_SetValue(ring, "c", spellWidth);
				
				if(spellDistance > 1)
				{
					spellDistance = spellMaxMaxRadius - spellMaxRadius;
					let spellRadiusSpeed = spellDistance / spellRadiusWeight;
					
					if(spellRadiusSpeed > 4)
					{
						spellRadiusSpeed = 4;
					}
					spellMaxRadius += spellRadiusSpeed;
				}
				
				if(mainAlpha <= 0)
				{
					Obj_Delete(ring);
				}
				
				spellAngleZ -= 13;
			}
			case(2) //Purple Ring A
			{
				ObjRender_SetColor(ring, 255, 0, 255);
				ObjRender_SetAlpha(ring, spellAlpha);
				ObjRender_SetPosition(ring, spellX, spellY, 1);
				Obj_SetValue(ring, "a", spellAngleZ);
				Obj_SetValue(ring, "b", spellMaxRadius);
				Obj_SetValue(ring, "c", spellWidth);
				
				if(spellDistance > 1)
				{
					spellDistance = spellMaxMaxRadius - spellMaxRadius;
					let spellRadiusSpeed = spellDistance / spellRadiusWeight;
					
					if(spellRadiusSpeed > 5)
					{
						spellRadiusSpeed = 5;
					}
					spellMaxRadius += spellRadiusSpeed;
				}
				if(spellDistance < 20)
				{
					if(spellAlpha <= 0)
					{
						Obj_Delete(ring);
					}
					else
					{
						spellAlpha -= 10;
					}
				}
				spellAngleZ += 7;
			}
			case(3) //Purple Ring B
			{
				ObjRender_SetColor(ring, 255, 0, 255);
				ObjRender_SetAlpha(ring, spellAlpha);
				ObjRender_SetPosition(ring, spellX, spellY, 1);
				Obj_SetValue(ring, "a", spellAngleZ);
				Obj_SetValue(ring, "b", spellMaxRadius);
				Obj_SetValue(ring, "c", spellWidth);
				
				if(spellDistance > 1)
				{
					spellDistance = spellMaxMaxRadius - spellMaxRadius;
					let spellRadiusSpeed = spellDistance / spellRadiusWeight;
					
					if(spellRadiusSpeed > 5)
					{
						spellRadiusSpeed = 5;
					}
					spellMaxRadius += spellRadiusSpeed;
				}
				if(spellDistance < 20)
				{
					if(spellAlpha <= 0)
					{
						Obj_Delete(ring);
					}
					else
					{
						spellAlpha -= 10;
					}
				}
				spellAngleZ -= 7;
			}
			yield;
		}
	}
	
	task ManageHitBox()
	{
		while(spellTime < maxSpellTime)
		{
			ObjSpell_SetIntersectionCircle(objSpell, spellX, spellY, spellHitSize);
			FP_DeleteShotInCircle_th10(spellX, spellY, spellHitSize);
			yield;
		}
		Obj_Delete(objSpell);
	}
	
	task ManageParticles()
	{
		task CreateEffectParticle(speed, angle, type)
		{
			let loc_x = spellX + ((spellSize - 18) * cos(angle));
			let loc_y = spellY + ((spellSize - 18) * sin(angle));
			let loc_scale = rand(0.75, 1.5);
			let loc_scaleMod = loc_scale/20;
			let loc_angle = rand(0, 360);
			let loc_angMod = rand(-5, 5);
			
			let x_vel = speed * cos(angle);
			let y_vel = speed * sin(angle);
			
			let effParticle = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(effParticle, img_eff_leaf);
			ObjRender_SetBlendType(effParticle, BLEND_ADD_ARGB);
			ObjSprite2D_SetSourceRect(effParticle, 0, 0, 32, 32);
			ObjSprite2D_SetDestCenter(effParticle);
			
			alternative(type)
			case(0)
			{
				ObjRender_SetColor(effParticle, 255, 0, 0);
			}
			case(1)
			{
				ObjRender_SetColor(effParticle, 0, 0, 255);
			}
			
			while(loc_scale > 0)
			{
				ObjRender_SetPosition(effParticle, loc_x, loc_y, 1);
				ObjRender_SetScaleXYZ(effParticle, loc_scale, loc_scale, 1);
				ObjRender_SetAngleZ(effParticle, loc_angle);
				loc_x += x_vel;
				loc_y += y_vel - spellRise;
				loc_scale -= loc_scaleMod;
				loc_angle += loc_angMod;
				yield;
			}
			Obj_Delete(effParticle);
		}
		
		loop(30){yield;}
		loop(maxSpellTime)
		{
			CreateEffectParticle(2.75, rand(0, 360), 0);
			CreateEffectParticle(2.75, rand(0, 360), 0);
			CreateEffectParticle(-2.75, rand(0, 360), 1);
			CreateEffectParticle(-2.75, rand(0, 360), 1);
			yield;
		}
	}
	
	PlaySE(se_slash);
	SetPlayerInvincibilityFrame(300);
	ManageHitBox();
	ManageParticles();
	CreateMoFBombRing(objManage, 0, 28, 50);
	CreateMoFBombRing(objManage, 1, 18, 50);
	
	CreateMoFBombRing(objManage, 2, 20, 50);
	loop(12){spellY -= spellRise;yield;}
	CreateMoFBombRing(objManage, 3, 20, 50);
	loop(12){spellY -= spellRise;yield;}
	CreateMoFBombRing(objManage, 2, 20, 50);
	loop(12){spellY -= spellRise;yield;}
	CreateMoFBombRing(objManage, 3, 20, 50);
	
	while(mainAlpha > 0)
	{
		if(spellTime > maxSpellTime)
		{
			mainAlpha -= 15;
			spellRise -= 0.1;
		}
		spellY -= spellRise;
		spellTime++;
		yield;
	}
	Obj_Delete(objManage);
}

task RenderPlayer()
{
	let f = 0;
	let scaleX = 1;
	let scaleY = 1;
	let flashframe = 0;
	let flashframe_eff = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img_player);
	ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	Obj_SetRenderPriorityI(obj, 31);
	
	let objHit = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objHit, img_player);
	ObjSprite2D_SetSourceRect(objHit, 192, 144, 208, 160);
	ObjSprite2D_SetDestCenter(objHit);
	Obj_SetVisible(objHit, false);
	ObjRender_SetScaleXYZ(objHit, 1, 1, 1);
	Obj_SetRenderPriorityI(objHit, 32);

	loop
	{
		if(isPlayerHit == false)
		{
			if (GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_PULL && isPlayerImmobile == false)
			{
				f = 0;
			}
			if (GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_PULL && isPlayerImmobile == false)
			{
				if(GetVirtualKeyState(VK_RIGHT)!=KEY_PUSH && GetVirtualKeyState(VK_RIGHT)!=KEY_HOLD)
				{
					f = 0;
				}
			}
			//Moving right
			if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD && GetVirtualKeyState(VK_LEFT) == KEY_FREE && isPlayerImmobile == false)
			{
				if (f >= 25)
				{
					f = 5;
				}
				if(f >= 0 && f < 1) { ObjSprite2D_SetSourceRect(obj, 0,96,32,144); }
				if(f >= 1 && f < 2) { ObjSprite2D_SetSourceRect(obj, 32,96,64,144); }
				if(f >= 2 && f < 3) { ObjSprite2D_SetSourceRect(obj, 64,96,96,144); }
				if(f >= 3 && f < 4) { ObjSprite2D_SetSourceRect(obj, 96,96,128,144); }
				
				if(f >= 5 && f < 10) { ObjSprite2D_SetSourceRect(obj, 128,96,160,144); }
				if(f >= 10 && f < 15) { ObjSprite2D_SetSourceRect(obj, 160,96,192,144); }
				if(f >= 15 && f < 20) { ObjSprite2D_SetSourceRect(obj, 192,96,224,144); }
				if(f >= 20 && f < 25) { ObjSprite2D_SetSourceRect(obj, 224,96,256,144); }
				f++;
			}
			//Moving left
			else if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD && GetVirtualKeyState(VK_RIGHT) == KEY_FREE && isPlayerImmobile == false)
			{
				if (f >= 25)
				{
					f = 5;
				}
				if(f >= 0 && f < 1) { ObjSprite2D_SetSourceRect(obj, 0,48,32,96); }
				if(f >= 1 && f < 2) { ObjSprite2D_SetSourceRect(obj, 32,48,64,96); }
				if(f >= 2 && f < 3) { ObjSprite2D_SetSourceRect(obj, 64,48,96,96); }
				if(f >= 3 && f < 4) { ObjSprite2D_SetSourceRect(obj, 96,48,128,96); }
				
				if(f >= 5 && f < 10) { ObjSprite2D_SetSourceRect(obj, 128,48,160,96); }
				if(f >= 10 && f < 15) { ObjSprite2D_SetSourceRect(obj, 160,48,192,96); }
				if(f >= 15 && f < 20) { ObjSprite2D_SetSourceRect(obj, 192,48,224,96); }
				if(f >= 20 && f < 25) { ObjSprite2D_SetSourceRect(obj, 224,48,256,96); }
				f++;
			}
			//Idle
			else
			{
				if(f >= 0  && f < 5) { ObjSprite2D_SetSourceRect(obj, 0,0,32,48); }
				if(f >= 5  && f < 10) { ObjSprite2D_SetSourceRect(obj, 32,0,64,48); }
				if(f >= 10 && f < 15) { ObjSprite2D_SetSourceRect(obj, 64,0,96,48); }
				if(f >= 15 && f < 20) { ObjSprite2D_SetSourceRect(obj, 96,0,128,48); }
				if(f >= 20 && f < 25) { ObjSprite2D_SetSourceRect(obj, 128,0,160,48); }
				if(f >= 25 && f < 30) { ObjSprite2D_SetSourceRect(obj, 160,0,192,48); }
				if(f >= 30 && f < 35) { ObjSprite2D_SetSourceRect(obj, 192,0,224,48); }
				if(f >= 35 && f < 40) { ObjSprite2D_SetSourceRect(obj, 224,0,256,48); }
				if(f >= 40){f = 0;}
				f++;
			}
			
			if(GetPlayerInvincibilityFrame > 0)
			{
				flash = 1;
			}
			if(GetPlayerInvincibilityFrame == 0)
			{
				flash = 0;
			}
			if(flash == 1)
			{
				flashframe++;
			}
			
			if(flashframe == 0)
			{
				ObjRender_SetColor(obj, 50,50,255);
			}
			else if(flashframe == 2)
			{
				ObjRender_SetColor(obj, 255,255,255);
				flashframe = -1;
			}
			
			if(flash == 0)
			{
				ObjRender_SetColor(obj, 255,255,255);
			}
		
			ObjSprite2D_SetDestCenter(obj);
			ObjSprite2D_SetDestRect(obj, -16,-24,16,24);
			ObjRender_SetPosition(obj, round(ObjMove_GetX(GetPlayerObjectID)), round(ObjMove_GetY(GetPlayerObjectID)), 0);
			ObjRender_SetPosition(objHit, round(ObjMove_GetX(GetPlayerObjectID)), round(ObjMove_GetY(GetPlayerObjectID)), 0);
			ObjRender_SetScaleXYZ(obj, scaleX, scaleY, 1);
			scaleX = 1;
			scaleY = 1;
		}
		else if(isPlayerHit == true && isPlayerDead && scaleX > 0)
		{
			ObjRender_SetScaleXYZ(obj, scaleX, scaleY, 1);
			scaleX -= 0.1;
			scaleY = 0;
		}
		
		if(scaleX <= 0)
		{
			Obj_SetVisible(obj, false);
		}
		
		if(flashframe_eff == 0)
		{
			ObjRender_SetColor(objHit, 200,150,150);
		}
		else if(flashframe_eff == 4)
		{
			ObjRender_SetColor(objHit, 255,255,255);
			
		}
		else if(flashframe_eff == 7)
		{
			flashframe_eff = -1;
		}
		flashframe_eff++;
		yield;
	}
}

task RenderHitbox()
{
	let hitbox2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(hitbox2, img_hitbox);
	ObjSprite2D_SetSourceRect(hitbox2, 0, 0, 63, 63);
	ObjSprite2D_SetDestRect(hitbox2, -32, -32, 32, 32);
	
	let hitbox = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(hitbox, img_hitbox);
	ObjSprite2D_SetSourceRect(hitbox, 0, 0, 63, 63);
	ObjSprite2D_SetDestRect(hitbox, -32, -32, 32, 32);

	let hitboxscale = 0;

	let count = 0;

	let hitboxangle = 0;


	sub DrawHitbox
	{
		if (count > 0.6)
		{
			count = 0.6;
		}

		if (count >= 0.045*10)
		{
			hitboxscale = 1;
			hitboxangle += 3;
		}

		if (hitboxscale < 0)
		{
			hitboxscale = 0;
		}

		if (count < 0.045*10)
		{
			hitboxangle += 16;
		}

		if (GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH)
		{
			hitboxscale = 0;
			count = 0;
		}

		if ((GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD) && count < 0.045*10 && isPlayerImmobile == false)
		{
			if(isPlayerDead == false)
			{
				hitboxscale += 0.32-count;
				count += 0.045;
			}
		}

		if (!(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD) && hitboxscale > 0 || isPlayerDead == true || isPlayerImmobile == true)
		{
			count = 0;
			hitboxscale -= 0.2;
		}
		if(hitboxscale < 0)
		{
			hitboxscale = 0;
		}
		ObjRender_SetPosition(hitbox,GetPlayerX-0.5,GetPlayerY-0.5,0);
		ObjRender_SetAngleZ(hitbox,hitboxangle);
		ObjRender_SetScaleXYZ(hitbox, hitboxscale, hitboxscale, hitboxscale);
	}

	sub DrawHitbox2
	{
		if (count != 0)
		{
			ObjRender_SetPosition(hitbox2,GetPlayerX-0.5,GetPlayerY-0.5,0);
			ObjRender_SetAngleZ(hitbox2,-hitboxangle);
			ObjRender_SetScaleXYZ(hitbox2, hitboxscale, hitboxscale, hitboxscale);
		}
		else
		{
			ObjRender_SetPosition(hitbox2,GetPlayerX-0.5,GetPlayerY-0.5,0);
			ObjRender_SetAngleZ(hitbox2,hitboxangle);
			ObjRender_SetScaleXYZ(hitbox2, hitboxscale, hitboxscale, -hitboxscale);
		}
	}
	
	task DrawTheBox
	{
		loop
		{	
			DrawHitbox;
			DrawHitbox2;
			yield;
		}
	}
	DrawTheBox;
}

task TItemCreateA2(type,x,y,gox,goy,score)
{
	let objitem = CreateItemA2(type, x, y, gox, goy, score);
	let speed = 5.7;
	let falling = 0;
	let time = 0;
	let gotoplayer = 0;
	while(!Obj_IsDeleted(objitem))
	{
		if(time > 30)
		{
			if(((ObjMove_GetY(objitem) - GetPlayerY)^2 + (ObjMove_GetX(objitem) - GetPlayerX)^2) < (50^2) && gotoplayer == 0 && GetPlayerState != STATE_DOWN || GetPlayerY < 120 && gotoplayer == 0 && GetPlayerState != STATE_DOWN)
			{
				gotoplayer = 1;
			}
			else if (gotoplayer == 1)
			{
				ObjMove_SetDestAtSpeed(objitem, GetPlayerX, GetPlayerY, speed);
				speed += 0.1;
			}
		}
		time++;
		yield;
	}
	return;
}

task PlayerDebug()
{
	loop
	{
		if(GetKeyState(KEY_4) == KEY_PUSH && playerDebugEnabled)
		{
			if(playerPower < 500)
			{
				playerPower += 100;
				PowerCheck(1, 0);
			}
			else
			{
				playerPower = 0;
				PowerCheck(1, 2);
			}
		}
		yield;
	}
}

task PlayerRebirth()
{
	let val_y = 0;
	isPlayerImmobile = true;
	ObjMove_SetPosition(GetPlayerObjectID, GetStgFrameWidth/2, GetStgFrameHeight + 30);
	loop(39)
	{
		ObjMove_SetPosition(GetPlayerObjectID, GetStgFrameWidth/2, GetStgFrameHeight + 30 - val_y);
		val_y += 2;
		yield;
	}
	ForcePowerCheck();
	isPlayerImmobile = false;
}

function ForcePowerCheck()
{
	powerState = -1;
	PowerCheck(2, 0);
}

