//Main functions for the player. #include this in all player scripts.

#include "../system/functions/functions_general_stage.txt"
#include "./Shot_Const.txt"

//All these variables can be changed within the individual player scripts. These are just the default values.
let Lmin = 5; //Starting number of lives.
let Lmax = 20; //Maximum number of lives.
let Bmin = 2; //Default number of bombs.
let Bmax = 10; //Maximum number of bombs.
let FlashRate = 0; //How much flash you get per graze. Set to 0 to hide the flashbomb meter.
let PlayerSpeed = [4,2]; //How fast the player moves (unfocused, focused)
let HitBox = 0.25; //Hit box size
let GrazeBox = 30; //Graze box size
let Pmax = 4; //Maximum power. Set to 0 to hide the power meter.
let sdr = 0.5; //Shot damage rate. Increases the damage of the player's shot.
let bdr = sdr; //Bomb damage rate. Increases the damage of the player's bomb.
let RebirthFrame = 6; //Deathbomb timer.

//This renders the player's resources. Right now I'm only rendering the flashbomb and power meters though. Feel free to move that, or to add your own life and bomb rendering stuff.
task TPlayerResourceRender
{
	let posF = [GetStgFrameLeft+GetStgFrameWidth+8,GetScreenHeight*0.5+26,GetStgFrameLeft+GetStgFrameWidth+24,GetScreenHeight-20]; //The dest rects of the flashbomb meter. Change this to change the flashbomb meter's position.
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,"");
	ObjSpriteList2D_SetSourceRect(obj,0,0,1,1);
	Obj_SetRenderPriority(obj, 0.15);
	let colorF = 180;
	
	let posP = [GetStgFrameLeft+GetStgFrameWidth+36,GetScreenHeight*0.5+18,GetStgFrameLeft+GetStgFrameWidth+60,GetScreenHeight-20]; //The dest rects of the power meter. Change this to change the power meter's position.
	let colorP;

	let textP;
	if(Pmax > 0)
		{
		textP = ObjText_Create;
		ObjText_SetText(textP,"POW");
		ObjText_SetFontColorTop(textP, 255, 255, 255);
		ObjText_SetFontColorBottom(textP, 196, 196, 196);
		ObjText_SetFontBorderType(textP, BORDER_FULL);
		ObjText_SetFontBorderColor(textP,64, 64, 64);
		ObjText_SetFontBorderWidth(textP, 2);
		ObjText_SetSidePitch(textP,0);
		ObjText_SetFontSize(textP, 13);
		ObjRender_SetY(textP,posP[1]-16);
		ObjText_SetHorizontalAlignment(textP,ALIGNMENT_LEFT);
		ObjText_SetMaxWidth(textP,100);
		Obj_SetRenderPriority(textP, 0.1);
		Obj_SetVisible(textP,false);
		}

	let remStepRateS = 32;

	let objS = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objS, GetCurrentScriptDirectory() ~ "../system/img/RSS_Item.png");
	Obj_SetRenderPriority(objS, 0.15);

	let spellS = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(spellS, GetCurrentScriptDirectory() ~ "../system/img/RSS_Item.png");
	Obj_SetRenderPriority(spellS, 0.15);

	loop
		{
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(objS);
		ObjSpriteList2D_ClearVertexCount(spellS);
		//Renders bombs
		local
			{

			let countLifeS = GetPlayerSpell;
			ObjSpriteList2D_SetSourceRect(objS, 32, 0, 64, 32);
			ObjSpriteList2D_SetSourceRect(spellS, 96, 0, 128, 32);
			ascent(iStep in 0 .. 5)
				{
				ObjSpriteList2D_SetDestRect(objS, GetScreenWidth-88, GetScreenHeight-30-(iStep+1) * remStepRateS,GetScreenWidth-90 + remStepRateS, GetScreenHeight-30-(iStep) * remStepRateS);
				ObjRender_SetColor(objS,175,175,175);
				ObjSpriteList2D_AddVertex(objS);
				}
			ascent(iStep in 0 .. min(countLifeS,5))
				{
				ObjSpriteList2D_SetDestRect(spellS, GetScreenWidth-88, GetScreenHeight-30-(iStep+1) * remStepRateS,GetScreenWidth-90 + remStepRateS, GetScreenHeight-30-(iStep) * remStepRateS);
				ObjRender_SetColor(spellS,255,255,255);
				ObjSpriteList2D_AddVertex(spellS);
				}
			}

		//Renders flashbomb meter
		if(FlashRate > 0)
			{
			let flash = GetPlayerFlash;
			if(flash == 200)
				{
				colorF+=1.25;
				}
			else
				{
				colorF=180;
				}
			ObjRender_SetColorHSV(obj,0,0,20);
			ObjSpriteList2D_SetDestRect(obj,posF[0],posF[1],posF[2],posF[3]);
			ObjSpriteList2D_AddVertex(obj);

			ObjRender_SetColorHSV(obj,0,105+105*cos(colorF),255);
			ObjSpriteList2D_SetDestRect(obj,posF[0]+3,posF[3]-3-(posF[3]-posF[1]-6)*flash/200,posF[2]-3,posF[3]-3);
			ObjSpriteList2D_AddVertex(obj);
			}

		//Renders power meter
		if(Pmax > 0 && true == false)
			{
			let p = GetPlayerPower;
			if(p >= Pmax)
				{
				colorP = 25;
				}
			else
				{
				colorP = 55;
				}
			ObjRender_SetColorHSV(obj,0,0,20);
			ObjSpriteList2D_SetDestRect(obj,posP[0],posP[1],posP[2],posP[3]);
			ObjSpriteList2D_AddVertex(obj);

			let dP = posP[3]-posP[1]-6;
			ascent(i in 0..Pmax)
				{
				let yA = posP[3]-3-dP*i/Pmax;
				ObjRender_SetColorHSV(obj,0,0,100);
				ObjSpriteList2D_SetDestRect(obj,posP[0]+3,yA-dP/Pmax+2,posP[2]-3,yA);
				ObjSpriteList2D_AddVertex(obj);

				ObjRender_SetColorHSV(obj,0,colorP,255);
				ObjSpriteList2D_SetDestRect(obj,posP[0]+3,yA-(dP/Pmax-2)*(max(0,min(1,p-i))),posP[2]-3,yA);
				ObjSpriteList2D_AddVertex(obj);
				}

			if(trunc(p*100) < Pmax*100)
				{
				let num = floor(p*100);
				let numA = "";
				if(num%100 < 10)
					{
					numA = "0"~IntToString(num%100);
					}
				else
					{
					numA = IntToString(num%100);
					}
				ObjText_SetText(textP,IntToString(num/100)~"."~numA);
				ObjRender_SetX(textP,posP[0]-6);
				}
			else
				{
				ObjText_SetText(textP,"MAX");
				ObjRender_SetX(textP,posP[0]-2);
				}
			}
		yield;
		}
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
//---------Do not change beyond this point---------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
//---------Unless you really want to---------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
let PlayerPath1; //Path to the main shot's player image
let PlayerPath2; //Path to the bomb's player image
let textureO = [];
let IsFocus = false;
let ShotPermit = true;
let SpellSwitch = false;
let IsFocusForce = false;
let IsMovePermit = true;
let IsMoving = false;
let IsSubPlayerActive = false;
let PlayerColor = [0,0,255];
let IsPlayerHit = false;
let IsPlayerDown = false;
let IsGrazeEnable = true;

let ReisSwitch = false;
let ReisSpellCount = 0;

@Initialize
{
	TMain;
	LoadTexture(PlayerPath1);
	LoadTexture(PlayerPath2);

	LoadTexture(GetCurrentScriptDirectory ~ "img/Hitbox.png");
	LoadTexture(GetCurrentScriptDirectory ~ "img/Magic_Circle.png");

	SetPlayerSpell(Bmin);
	SetPlayerLife(Lmin);
	SetPlayerPower(Pmax);
	SetPlayerSpeed(0,0);
	SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);	
	SetPlayerRebirthFrame(RebirthFrame);	
	SetPlayerRebirthLossFrame(0);
	
	TImage;
	TPlayerResourceRender;
	SetDefaultBonusItemEnable(false);
	TShot;
	
	SetPlayerHitbox(HitBox);
	SetPlayerGrazebox(GrazeBox);
		
	CreatePlayerHitbox;
	//ObjPlayer_AddIntersectionCircleA1(objPlayer,0,0,HitBox,GrazeBox);

	SetAutoDeleteObject(true);
}

@MainLoop
{	
	loop(1)
		{
		if(GetKeyState(KEY_F) == KEY_PUSH)
			{
			SetPlayerFlash(200);
			}
		if(GetKeyState(KEY_V) == KEY_PUSH || GetKeyState(KEY_V) == KEY_HOLD)
			{
			SetPlayerPower(GetPlayerPower+1/30);
			}
		if(GetKeyState(KEY_B) == KEY_PUSH)
			{
			SetPlayerPower(0);
			}
		}
	if(GetPlayerLife > Lmax)
		{
		SetPlayerLife(Lmax);
		}
	if(GetPlayerSpell > Bmax)
		{
		SetPlayerSpell(Bmax);
		}
	if(GetPlayerPower > Pmax)
		{
		SetPlayerPower(Pmax);
		}
	if(IsKeyState([VK_SLOWMOVE],[KEY_PUSH,KEY_HOLD]) || IsFocusForce == true)
		{	
		IsFocus = true;
		}
	else
		{
		IsFocus = false;
		}
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
		{
		let spell = GetPlayerSpell();
		if((IsKeyState([VK_SLOWMOVE],[KEY_PUSH,KEY_HOLD]) || FlashRate == 0 || IsPlayerHit == true) && IsPlayerDown == false)
			{
			if((GetPlayerState == STATE_NORMAL || IsPlayerHit == true) && spell > 0 && IsPermitPlayerSpell == true && IsSpellActive == false && IsFlashActive == false && IsPlayerDown == false)
				{
				SetScriptResult(true);
				PlaySound("se_Spell",100);
				SetPlayerSpell(GetPlayerSpell-1);
				InitializeSpell;
				}
			else
				{
				SetScriptResult(false);
				}
			}
		else
			{
			if(GetPlayerState == STATE_NORMAL && GetPlayerFlash >= 200 && IsPermitPlayerFlash == true && IsSpellActive == false && IsFlashActive == false && IsPlayerDown == false)
				{
				//PlaySound. The flashbomb activation sound.
				InitializeFlash;
				}
			SetScriptResult(false);
			}
		}
	case(EV_HIT)
		{
		SetPlayerLife(GetPlayerLife+1);
		TPlayerHit;
		}	
	case(EV_PLAYER_REBIRTH)
		{
		SetPlayerSpell(Bmin);
		TMagicCircle(210,0);
		}	
	case(EV_GRAZE)
		{
		let g = GetEventArgument(0);
		if(g == 5)
			{
			PlaySound("se_SuperGraze",57);
			}		
		else
			{
			PlaySound("se_Graze",100);
			}
		AddGraze(g);
		AddChapterGraze(g);
		if(IsFlashActive == false)
			{
			let flash = GetPlayerFlash;
			SetPlayerFlash(min(200,flash+FlashRate));
			if(GetPlayerFlash >= 200 && flash < 200)
				{
				PlaySound("se_Boost2",100);
				}
			}
		}
}

task TImage
{	
	let wC = 8;
	let wCA = floor(wC*0.5);

	let ImageWidth = 257/8;
	let ImageHeight = 145/3;
	
	let obj = [];
	ascent(i in 0..2)
		{
		obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		if(i == 0)
			{
			ObjPrim_SetTexture(obj[i],PlayerPath1);
			}
		else
			{
			ObjPrim_SetTexture(obj[i],PlayerPath2);
			}
		Obj_SetRenderPriority(obj[i],0.37);
		}
	ObjRender_SetBlendType(obj[1],BLEND_ADD_ARGB);

	let countm = 2.5;
	let count = 4;

	let move_s = 0;
	let anim = 0;

	let move_x = 0;
	let move_y = 0;
	
	let hit = [];
	ascent(i in 0..2)
		{
		hit = hit ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(hit[i],GetCurrentScriptDirectory ~ "img/Hitbox.png");
		ObjSprite2D_SetSourceRect(hit[i],0,0,69,69);
		Obj_SetRenderPriority(hit[i],0.59-i*0.2);
		}
	ObjRender_SetBlendType(hit[1],BLEND_ADD_ARGB);
	let angle = 0;
	let scale = 4;
	let alpha = 0;
	let dirX = 0;
	let dirY = 0;
	let dirZ = 0;

	let scaleP = 1;

	let c = 0;
	
	loop
		{
		dirX = 0;	
		dirY = 0;
		dirZ = 0;
		c+=1;
		let clip = GetPlayerClip;
		ascent(i in 0..1)
			{
			Obj_SetVisible(obj[i],Obj_IsVisible(objPlayer));
			Obj_SetVisible(obj[1],false);
			}
		IsMoving = false;
		let speed = PlayerSpeed[0];
		if(IsFocus == true)
			{	
			speed = PlayerSpeed[1];
			if(scale > 1)
				{
				scale-=1/10;
				alpha+=1/10;
				}
			else
				{
				scale = 1;
				alpha = 1;
				angle+=3;
				}
			}
		else
			{
			if(scale < 2)
				{
				scale+=1/10;
				alpha-=1/10;
				angle+=3;
				}
			else
				{
				scale = 2;
				alpha = 0;
				}
			}		
		if(IsSpellActive || IsFlashActive)
			{
			if(scaleP < 1)
				{
				scaleP+=1/5;
				}
			else
				{
				scaleP = 1;
				}
			IsSubPlayerActive = true;
			}
		else
			{
			if(scaleP > 0)
				{
				scaleP-=1/10;
				}
			else
				{
				scaleP = 0;
				}
			IsSubPlayerActive = false;
			}
		if((IsKeyState([VK_RIGHT],[KEY_PUSH,KEY_HOLD]) && move_s >= 0 && IsMovePermit == true)|| move_s > 0)
			{
			if(IsKeyState([VK_RIGHT],[KEY_FREE,KEY_PULL]) || IsMovePermit == false)
				{
				move_s-=1;
				if(IsKeyState([VK_LEFT],[KEY_PUSH,KEY_HOLD]))
					{
					move_s-=1;
					}
				anim = 0;
				}
			else 
				{
				if(move_s < 4*countm)
					{
					move_s += 1;
					anim = 0;
					}
				else
					{
					anim+=1/count;
					}		
				}
			move_y = 2;
			move_x = floor(anim)%4+ceil(move_s/countm);
			}
		else if((IsKeyState([VK_LEFT],[KEY_PUSH,KEY_HOLD]) && move_s <= 0 && IsMovePermit == true) || move_s < 0)
			{
			if(IsKeyState([VK_LEFT],[KEY_FREE,KEY_PULL]) || IsMovePermit == false)
				{
				move_s+=1;
				if(IsKeyState([VK_RIGHT],[KEY_PUSH,KEY_HOLD]))
					{
					move_s+=1;
					}
				anim = 0;
				}
			else 
				{
				if(move_s > -4*countm)
					{
					move_s -= 1;
					anim = 0;
					}
				else
					{
					anim+=1/count;
					}		
				}
			move_y = 1;
			move_x = floor(anim)%4+ceil(absolute(move_s)/countm);
			}
		else
			{
			anim+=1/count;	
			move_y = 0;
			move_x = floor(anim)%8;
			}

		if(IsKeyState([VK_LEFT],[KEY_PUSH,KEY_HOLD]) && IsMovePermit == true)
			{
			dirX = -1;
			dirZ += 1;
			}
		else if(IsKeyState([VK_RIGHT],[KEY_PUSH,KEY_HOLD]) && IsMovePermit == true)
			{
			dirX = 1;
			dirZ += 1;
			}
		ascent(i in 0..2)
			{
			ObjSprite2D_SetSourceRect(obj[i],move_x*256/8,1+move_y*143.5/3,(move_x+1)*256/8,(move_y+1)*143.5/3);
			}
		if(IsKeyState([VK_DOWN],[KEY_PUSH,KEY_HOLD]) && IsMovePermit == true)
			{
			dirY = 1;
			dirZ +=1;
			}
		else if(IsKeyState([VK_UP],[KEY_PUSH,KEY_HOLD]) && IsMovePermit == true)
			{
			dirY = -1;
			dirZ +=1;
			}
		if(dirZ > 0)
			{
			IsMoving = true;
			ObjMove_SetPosition(objPlayer,min(clip[2],max(clip[0],GetPlayerX+dirX*speed*sin(90/dirZ))),min(clip[3],max(clip[1],GetPlayerY+dirY*speed*sin(90/dirZ))));
			}
		ascent(i in 0..2)
			{
			let s = scaleP;
			if(i == 0)
				{
				s = 1;
				ObjRender_SetScaleXYZ(obj[i],s,1.5-s*0.5,0);
				}	
			else
				{
				let sA = 1.125+0.125*cos(c*3);
				ObjRender_SetScaleXYZ(obj[i],s*(1.125+0.0625*cos(c*3)),(1.5-s*0.5)*(1.125+0.0625*sin(c*5)),0);
				}			
			let g = Obj_GetValueD(objPlayer,"COLORHSV",255);
			ObjRender_SetAlpha(obj[i],s*255);
			ObjRender_SetPosition(obj[i],GetPlayerX,GetPlayerY+1.5,0);
			ObjRender_SetColorHSV(obj[i],PlayerColor[0],PlayerColor[1],PlayerColor[2]*g/255);
			ObjSprite2D_SetDestCenter(obj[i]);
			}
		ObjRender_SetScaleXYZ(hit[0],0.75+HitBox/9,0.75+HitBox/8,0);
		ascent(i in 0..2)
			{
			ObjRender_SetAngleZ(hit[i],(i*2-1)*angle);
			ObjRender_SetPosition(hit[i],GetPlayerX,GetPlayerY,0);
			ObjSprite2D_SetDestCenter(hit[i]);
			if(GetPlayerState == STATE_NORMAL && Obj_IsVisible(objPlayer) == true)
				{
				ObjRender_SetAlpha(hit[i],alpha*205/(i*12+1));
				}
			else
				{
				ObjRender_SetAlpha(hit[i],0);
				}
			let g = Obj_GetValueD(objPlayer,"COLORHSV",255);	
			ObjRender_SetColorHSV(hit[i],0,0,g);
			}
		yield;
		}
	ascent(i in 0..2)
		{
		Obj_Delete(obj[i]);
		Obj_Delete(hit[i]);
		}
}

task InitializeSpell
{
	let spell = GetSpellManageObject;
	Obj_SetValue(objPlayer,"IS_PLAYER_SPELL_ACTIVE",true);
	FSpell;
	Obj_SetValue(objPlayer,"IS_PLAYER_SPELL_ACTIVE",false);
	Obj_Delete(spell);
}

task InitializeFlash
{	
	PlaySound("se_Flashbomb",90);
	let spell = GetSpellManageObject;
	Obj_SetValue(objPlayer,"IS_PLAYER_FLASH_ACTIVE",true);
	SetPlayerFlash(0);
	FFlash;
	Obj_SetValue(objPlayer,"IS_PLAYER_FLASH_ACTIVE",false);
	Obj_Delete(spell);
}

task TSpellShadow
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,"");	
	ObjSprite2D_SetSourceRect(obj,0,0,1,1);
	ObjSprite2D_SetDestRect(obj,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
	ObjRender_SetColorHSV(obj,0,0,0);
	Obj_SetRenderPriority(obj,0.29);
	let a = 0;
	loop(36)
		{
		a+=175/35;
		ObjRender_SetAlpha(obj,a);
		if(SpellSwitch == false){break;}
		yield;
		}
	while(SpellSwitch == true){yield;}
	descent(t in 0..15)
		{
		ObjRender_SetAlpha(obj,t*a/15);
		yield;
		}
	Obj_Delete(obj);
}
task TPlayerHit
{
	NotifyEventAll(EV_PLAYER_SHOOTDOWN,[]);
	if(ReisSpellCount > 0)
		{
		ReisSpellCount-=1;
		ReisSwitch = true;
		return;
		}

	let h = GetPlayerHitbox;
	let g = GetPlayerGrazebox;
	SetPlayerGrazebox(-1);
	SetPlayerHitbox(-1);
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.60);
	if(IsSubPlayerActive)
		{
		ObjPrim_SetTexture(obj, PlayerPath2);
		}
	else
		{
		ObjPrim_SetTexture(obj, PlayerPath1);
		}
	ObjSprite2D_SetSourceRect(obj,0,0,257/8,145/3);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);

	let alpha = 255;
	let scale = 0;
	let dis = 0;
	let px = GetPlayerX;
	let py = GetPlayerY;
	TExplosion;
	TDelete;

	task TExplosion
		{
		loop(30)
			{
			scale += 0.2;
			alpha -= 11;	
			let g = Obj_GetValueD(objPlayer,"COLORHSV",255);
			ObjRender_SetColorHSV(obj, 0, 0, alpha*g/255);
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetPosition(obj, px, py, 0);
			ObjRender_SetScaleXYZ(obj, scale, scale, 1);
			yield;
			}
		Obj_Delete(obj);
		}
	task TDelete
		{	
		IsMovePermit = false;
		IsPlayerHit = true;
		loop(GetPlayerRebirthFrame)
			{
			if(IsSpellActive){break;}
			yield;
			}
		if(IsSpellActive)
			{
			IsMovePermit = true;
			IsPlayerHit = false;
			SetPlayerHitbox(h);
			SetPlayerGrazebox(g);
			Obj_SetValue(objPlayer,"DAMAGE",0);
			return;
			}
		IsPlayerDown = true;
		let time = GetPlayerDownStateFrame;
		Obj_SetVisible(objPlayer,false);
		ascent(t in 0..time)
			{
			if(t < 40)
				{
				let arr = GetShotIdInCircleA2(GetStgFrameWidth/2,GetStgFrameHeight/2,600,TARGET_ENEMY);
				DeleteShotInCircle(TYPE_SHOT, TYPE_ITEM, px, py, dis);
				dis += 25;
				}
			yield;
			}
		IsPlayerDown = false;
		Obj_SetVisible(objPlayer,true);
		IsMovePermit = true;
	
		time = 105;
		TPlayerBlink(time,230);
		SetPlayerSpell(Bmin);
		SetPlayerGrazebox(g);
		loop(time){yield;}
		IsPlayerHit = false;
		SetPlayerHitbox(h);
		Obj_SetValue(objPlayer,"DAMAGE",0);
		}
}

task TMagicCircle(timeM,color)
	{
	let time = [];
	time = time ~ [min(ceil(timeM*(25/120)),25)];
	let timeB = min(ceil(timeM*(60/120)),60);
	time = time ~ [floor(timeM-time[0]-timeB)];
	time = time ~ [timeB];
	let timeA = 0;
	ascent(i in 0..length(time))
		{
		timeA+=time[i];
		}
	SetPlayerInvincibilityFrame(timeA);
	TPlayerBlink(timeA,color);
	let count = 128;

	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/Magic_Circle.png");
	Obj_SetRenderPriority(obj, 0.3);
	ObjPrim_SetVertexCount(obj, count);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ascent(i in 0..count*0.5)
		{
		ObjPrim_SetVertexUVT(obj, i*2, i*1024/(count*0.5-1), 0);
		ObjPrim_SetVertexUVT(obj, i*2+1, i*1024/(count*0.5-1), 64);
		}	
	
	let angle = 180;
	let mr = 96;
	let r = 0.9;
	let angleA = 180;
	
	ascent(t in 0..timeA)
		{
		if(t < time[0])
			{
			angle+=180/(time[0]-1);
			}
		else if(t >= time[0]+time[1])
			{
			angle+=180/(time[2]-1);
			}
		angleA+=5;
		let aa = (0.5+0.5*cos(angleA));
		let ab = 0.95+aa*0.05;
		let a = 0.5+(0.45*cos(angle)+0.05*aa);
		ascent(i in 0..count*0.5)
			{
			let i1 = i*2;
			let angle = i*360/(count*0.5-1);
			
			ObjPrim_SetVertexPosition(obj,i1+0,a*ab*(1/r+(1-a)*1)*mr*cos(angle),a*ab*(1/r+(1-a)*1)*mr*sin(angle),0);
			ObjPrim_SetVertexPosition(obj,i1+1,a*ab*(r-(1-a)*1)*mr*cos(angle),a*ab*(r-(1-a)*1)*mr*sin(angle),0);
			}	
		let g = Obj_GetValueD(objPlayer,"COLORHSV",255);
		ObjRender_SetColorHSV(obj,color,35,255*g/255);
		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
		ObjRender_SetAlpha(obj,a*255);
		ObjRender_SetAngleZ(obj,t*360/(count*0.5));
		yield;
		}
	Obj_Delete(obj);
	}

task TPlayerBlink(time,color)
{	
	let d = 0;
	ascent(t in 0..time)
		{
		if(t%9 == 0)
			{
			d = 1-d;
			}
		PlayerColor = [color,d*125,255];
		yield;
		}
	PlaySound("se_Boost",100);
	PlayerColor = [0,0,255];
}
task CreatePlayerHitbox
{
	loop
		{
		let d = 64;	
		let dam = Obj_GetValueD(objPlayer,"DAMAGE",0);
		let array = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,d,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			let shot = array[i];
			if(!Obj_IsDeleted(shot) && GetPlayerGrazebox >= 0 && Obj_GetValueD(shot,"GRAZED",0) <= 30 && ObjShot_GetIntersection(shot) == true && GetObjectDistance(objPlayer,shot) < GetPlayerGrazebox+ObjShot_GetScale(shot)*0.8)
				{	
				if(Obj_GetValueD(shot,"GRAZED",0) == 0)
					{
					NotifyEvent(GetOwnScriptID,EV_GRAZE,1);	
					}
				Obj_SetValue(shot,"GRAZED",Obj_GetValueD(shot,"GRAZED",0)+1);
				}
			else if(Obj_GetValueD(shot,"LASER",false) == false)
				{
				}
			if(!Obj_IsDeleted(shot) && GetPlayerHitbox >= 0 && ObjShot_GetIntersection(shot) == true && GetPlayerInvincibilityFrame <= 0 && GetObjectDistance(objPlayer,shot) < d && GetObjectDistance(objPlayer,shot) < GetPlayerHitbox+ObjShot_GetHitbox(shot)*0.8 && Obj_GetValueD(shot,"LASER",false) == false)
				{
				d = GetObjectDistance(objPlayer,shot);
				dam = ObjShot_GetStrength(shot);
				}
			}
		if(!IsPlayerHit && dam > 0 && ReisSwitch == false)
			{
			NotifyEventAll(EV_HIT,true);
			}
		yield;
		}

}