#東方弾幕風[Player]
#ScriptVersion[3]	//スクリプトバージョン3が必須です
#ID["Marisa"]
#Title["霊夢"]
#Text["プレイヤーサンプルです。闇符"]
#ReplayName["Reimu"]

let objPlayer = GetPlayerObjectID();
let objOption =[];
let OptionX=[0,0];
let OptionY=[0,0];
let current = GetCurrentScriptDirectory();
@Initialize
{
	let path = current ~ "Default_Player_RumiaShotData.txt";
	LoadPlayerShotData(path); //自弾画像ロード

	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 0, 30); //当たり判定登録
	SetPlayerSpeed(4.0,1.5);

	TImage();//自機描画用タスク起動
	OptionMove;
	TRenderOption;
	TShot();//弾発射用タスク起動
	TShot2();//弾発射用タスク起動
//	TLaser(0);
//	TLaser(1);
	TMagicCircle();//無敵時間魔法陣タスク起動
	SetPlayerRebirthFrame(15);
	SetPlayerDownStateFrame(120);
	LoadSound(current ~ "se/seGraze.wav");
	LoadSound(current ~ "se/sePlayerCollision.wav");
}

@MainLoop
{
	if(GetPlayerSpell==0)
	{
	SetPlayerRebirthFrame(0);
	}
	else
	{
	SetPlayerRebirthFrame(15);
	}
	yield;
}

@Finalize
{
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
		//スペルカード要求
		let spell = GetPlayerSpell();//残りスペル数
		if(spell >= 1)
		{
			SetScriptResult(true);//スペル発動可能
			SetPlayerSpell(spell-1);//スペル数を1減らす
			TSpell();//スペルタスク起動
		}
		else
		{
			SetScriptResult(false);//スペル発動不可
		}
	}
	case(EV_HIT)
	{
		//被弾
		PlaySE(current ~ "se/sePlayerCollision.wav");
		TExplosion();
	}
	case(EV_PLAYER_REBIRTH)
	{
		//復帰
		SetPlayerSpell(3);
		SetPlayerInvincibilityFrame(240);
	}
	case(EV_GET_ITEM)
	{
		//アイテム取得通知
		let itemType = GetEventArgument(0);
		alternative(itemType)
		case(ITEM_SPELL)
		{
			WriteLog("ITEM_SPELL");
		}
		case(ITEM_SPELL_S)
		{
			WriteLog("ITEM_SPELL_S");
		}
		case(ITEM_POWER)
		{
			WriteLog("ITEM_POWER");
		}
		case(ITEM_POWER_S)
		{
			WriteLog("ITEM_POWER_S");
		}
	}
	case(EV_GRAZE)
	{
		//グレイズ通知
		GrazeEffect(GetEventArgument(1));
		PlaySE(current ~ "se/seGraze.wav");
	}
}

task GrazeEffect(BulletArray)
{
	ascent(i in 0..length(BulletArray))
	{
		if(!Obj_IsDeleted(BulletArray[i]))
		{
		Effect(BulletArray[i]);
		}
	}

	task Effect(Bobj)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Light.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -32, -32, -0);
		ObjPrim_SetVertexPosition(obj, 1, 32, -32, -0);
		ObjPrim_SetVertexPosition(obj, 2, 32, 32, 0);
		ObjPrim_SetVertexPosition(obj, 3, -32, 32, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 64, 0);
		ObjPrim_SetVertexUVT(obj, 2, 64, 64);
		ObjPrim_SetVertexUVT(obj, 3, 0, 64);

		let x=(GetPlayerX+ObjRender_GetX(Bobj))/2;
		let y=(GetPlayerY+ObjRender_GetY(Bobj))/2;
		ObjRender_SetPosition(obj,x,y,0);

		let Alpha=1.0;
		let R=255;
		let G=255;
		let B=255;
		let speed=5;
		let angle=atan2(GetPlayerY-ObjRender_GetY(Bobj),GetPlayerX-ObjRender_GetX(Bobj))+rand(-5,5);
		angle+=90*(floor(rand(0,2-0.001))*2-1);
		let Scale=0.1;
		let Zangle=angle+90;
		let color=GetShotDataInfoA1(ObjShot_GetImageID(Bobj),TARGET_ENEMY,INFO_DELAY_COLOR);
		R=(color[0]+255)/2;
		G=(color[1]+255)/2;
		B=(color[2]+255)/2;
		loop(15)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale*4,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha-=1.0/15;
			yield;
		}
		Obj_Delete(obj);
	}
}

task OptionMove
{
let HiOpX=[24,-24];
let HiOpY=[-8,-8];
let LowOpX=[10,-10];
let LowOpY=[-24,-24];
let LowCount=0;
loop
{
	if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
	{
		LowCount+=1;
		if(LowCount>10){LowCount=10;}
	}
	else
	{
		LowCount-=1;
		if(LowCount<0){LowCount=0;}
	}
	ascent(i in 0..2)
	{
	OptionX[i]=HiOpX[i]*(10-LowCount)/10+LowOpX[i]*(LowCount)/10;
	OptionY[i]=HiOpY[i]*(10-LowCount)/10+LowOpY[i]*(LowCount)/10;
	}
	yield;
}
}

task TImage
{
	let path = GetCurrentScriptDirectory() ~ "img/STG_Player01.png";
	ObjPrim_SetTexture(objPlayer, path);
	//ObjRender_SetBlendType(objPlayer, BLEND_ADD_RGB);
	loop
	{
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0, 48, 48, 96);
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0, 96, 48, 144);	
		}
		else
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0, 0, 48, 48);
		}
		ObjSprite2D_SetDestCenter(objPlayer);
 		if(GetPlayerState==STATE_NORMAL)
		{
				ObjRender_SetAlpha(objPlayer,255);
				ObjRender_SetColor(objPlayer,255,255,255);
		}
 		else if(GetPlayerState==STATE_HIT)
		{
				ObjRender_SetAlpha(objPlayer,255);
				ObjRender_SetColor(objPlayer,150,150,255);
		}
/*
 		if(MainShotCount>=15)
		{
				ObjRender_SetAlpha(objPlayer,255);
				ObjRender_SetColor(objPlayer,255,255,255);
		}
 		else if(MainShotCount>=10)
		{
				ObjRender_SetAlpha(objPlayer,255);
				ObjRender_SetColor(objPlayer,100,255,100);
		}
 		else
		{
				ObjRender_SetAlpha(objPlayer,255);
				ObjRender_SetColor(objPlayer,255,100,100);
		}
*/
		yield;
	}
}

task TRenderOption
{
	DrawOption(0);
	DrawOption(1);
	task DrawOption(num)
	{
		let path = GetCurrentScriptDirectory() ~ "img/STG_Player01.png";
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		objOption=objOption~[obj];
		ObjPrim_SetTexture(obj, path);
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		let OpX=[0,0,0,0,0];
		let OpY=[0,0,0,0,0];
		let Zangle=0;
		loop
		{
			ObjRender_SetAngleZ(obj,Zangle);
			Zangle+=3;
			//ObjMove_SetDestAtWeight(obj, GetPlayerX,GetPlayerY, 10,5);
			ObjRender_SetPosition(obj, OpX[0],OpY[0], 0);
			ObjSprite2D_SetSourceRect(obj, 0, 160, 20, 178);
			ObjSprite2D_SetDestCenter(obj);
 			if(GetPlayerState==STATE_NORMAL)
			{
				ObjRender_SetAlpha(obj,255);
				ObjRender_SetColor(obj,255,255,255);
			}
 			else if(GetPlayerState==STATE_HIT)
			{
			//	ObjRender_SetAlpha(obj,255);
			//	ObjRender_SetColor(obj,150,150,255);
			}
			else
			{
				ObjRender_SetAlpha(obj,0);
			}
			ascent(i in 0..4)
			{
				OpX[i]=OpX[i+1];
				OpY[i]=OpY[i+1];
			}
			OpX[4]=GetPlayerX+OptionX[num];
			OpY[4]=GetPlayerY+OptionY[num];
			yield;
		}
	}
}

task TShot
{
	let pShot=false;
	ShotCheck;
	let Speed=12.5;
	let MainDamage_L=9.0;
	let MainDamage=2.0;
	let DamageRate=1;
	loop
	{
		while(!pShot){yield;}
		if(IsPlayerSpellActive){DamageRate=0.5;}
		else{DamageRate=1.0;}
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
		{
		MainShot2(GetPlayerX()+8*cos(270), GetPlayerY()+16, Speed, 270, MainDamage_L, 1, 2);
		}
		else
		{
		MainShot(GetPlayerX()+8*cos(270), GetPlayerY()+16, Speed, 270, MainDamage*1.0, 1, 4);
		MainShot(GetPlayerX()+8*cos(262), GetPlayerY()+16, Speed, 262, MainDamage, 0.95, 4);
		MainShot(GetPlayerX()+8*cos(278), GetPlayerY()+16, Speed, 278, MainDamage, 0.95, 4);
		MainShot(GetPlayerX()+8*cos(254), GetPlayerY()+16, Speed, 254, MainDamage*0.85, 1, 4);
		MainShot(GetPlayerX()+8*cos(286), GetPlayerY()+16, Speed, 286, MainDamage*0.85, 1, 4);
		}

		loop(3){yield;}
		yield;
	}

	task ShotCheck
	{
		loop
		{
			if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot)
			{
			pShot=true;

			}
			else
			{
			pShot=false;

			}
			yield;
		}
	}

	task MainShot(x,y,speed,angle,damage,pene,id)
	{
		let obj=CreatePlayerShotA1(x,y,speed,angle,damage,pene,id);
		Obj_SetRenderPriorityI(obj,20);
		let scale=0.75;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		while(!Obj_IsDeleted(obj)){yield;}
	}

	task MainShot2(x,y,speed,angle,damage,pene,id)
	{
		let obj=CreatePlayerShotA1(x,y,speed,angle,damage,pene,id);
		Obj_SetRenderPriorityI(obj,20);
		let scale=0.75;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		while(!Obj_IsDeleted(obj)){yield;}
	}
}

task TShot2
{
	let pShot=false;
	ShotCheck;
	let HomingDamage_L=3.5;
	let HomingDamage=2.5;
	let HomingAngle=[-75,-45,-15];
	let HomingNum=0;
	let DamageRate=1;
	loop
	{
		while(!pShot){yield;}
		if(IsPlayerSpellActive){DamageRate=0.5;}
		else{DamageRate=1.0;}

		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
		{
		HomingShot_L(GetPlayerX+OptionX[0], GetPlayerY+OptionY[0],270+15,HomingDamage_L);
		HomingShot_L(GetPlayerX+OptionX[1], GetPlayerY+OptionY[1],270-15,HomingDamage_L);
		}
		else
		{
		HomingShot_S(GetPlayerX+OptionX[0], GetPlayerY+OptionY[0],0+HomingAngle[HomingNum%3],HomingDamage);
		HomingShot_S(GetPlayerX+OptionX[1], GetPlayerY+OptionY[1],180-HomingAngle[HomingNum%3],HomingDamage);
		}
		HomingNum+=1;
		loop(5){yield;}
		yield;
	}

	task ShotCheck
	{
		loop
		{
			if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot)
			{
			pShot=true;

			}
			else
			{
			pShot=false;

			}
			yield;
		}
	}
}

task HomingShot_S(x,y,angle,damage)
{
	let EnemyObjectArray=GetIntersectionRegistedEnemyID;
	let Dis=1000000;
	let nearObj;
	if(length(EnemyObjectArray)!=0)
	{
		ascent(i in 0..length(EnemyObjectArray))
		{
		let dis=(ObjRender_GetY(EnemyObjectArray[i])-y)^2+(ObjRender_GetX(EnemyObjectArray[i])-x)^2;
		if(dis<Dis)
		{
			nearObj=EnemyObjectArray[i];
		}
		}
	}

	let speed=7;
	let obj=CreatePlayerShotA1(x,y,speed,angle,damage,1,3);
	Obj_SetRenderPriorityI(obj,20);
	let scale=0.4;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	loop(240)
	{
		if(length(EnemyObjectArray)>0)
		{
		let ToPlayerAngle=atan2(ObjRender_GetY(nearObj)-ObjRender_GetY(obj),ObjRender_GetX(nearObj)-ObjRender_GetX(obj));
		let judgangle=sin(ToPlayerAngle-angle);
	//	let ShiftAngle=absolute(ToPlayerAngle-angle);

		if(judgangle>0)
		{
			angle+=4.0;
		}
		else
		{
			angle-=4.0;
		}
		ObjMove_SetAngle(obj,angle);
		}
		yield;
	}
	while(!Obj_IsDeleted(obj))
	{
		yield;
	}
	Obj_Delete(obj);
}

task HomingShot_L(x,y,angle,damage)
{
	let EnemyObjectArray=GetIntersectionRegistedEnemyID;
	let Dis=1000000;
	let nearObj;
	if(length(EnemyObjectArray)!=0)
	{
		ascent(i in 0..length(EnemyObjectArray))
		{
		let dis=(ObjRender_GetY(EnemyObjectArray[i])-y)^2+(ObjRender_GetX(EnemyObjectArray[i])-x)^2;
		if(dis<Dis)
		{
			nearObj=EnemyObjectArray[i];
		}
		}
	}

	let speed=10;
	let obj=CreatePlayerShotA1(x,y,speed,angle,damage,1,3);
	Obj_SetRenderPriorityI(obj,20);
	let scale=0.8;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	loop(120)
	{
		if(length(EnemyObjectArray)>0)
		{
		let ToPlayerAngle=atan2(ObjRender_GetY(nearObj)-ObjRender_GetY(obj),ObjRender_GetX(nearObj)-ObjRender_GetX(obj));
		let judgangle=sin(ToPlayerAngle-angle);
	//	let ShiftAngle=absolute(ToPlayerAngle-angle);

		if(judgangle>0)
		{
			angle+=2.0;
		}
		else
		{
			angle-=2.0;
		}
		ObjMove_SetAngle(obj,angle);
		}

		yield;
	}
	while(!Obj_IsDeleted(obj))
	{
		yield;
	}
	Obj_Delete(obj);
}

task TMagicCircle
{
	let Scale=0;
	let Zangle=0;
	let count=0;
	loop
	{
		if(GetPlayerInvincibilityFrame>30)
		{
			if(Scale<1.5){Scale+=1.5/30;}
		}
		else if(GetPlayerState==STATE_DOWN)
		{
			count+=1;
			if(count>=60)
			{
			if(Scale<1.5){Scale+=1.5/30;}
			}
		}
		else
		{
			count=0;
			if(Scale>0){Scale-=1.5/30;}
		}
		Zangle+=3;
		InvEffectBlur(Scale,Zangle);
		yield;
	}

	task InvEffectBlur(Scale,Zangle)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 128);
		ObjPrim_SetVertexUVT(obj, 1, 128, 128);
		ObjPrim_SetVertexUVT(obj, 2, 128, 256);
		ObjPrim_SetVertexUVT(obj, 3, 0, 256);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let Alpha=0.4;
		let R=255;
		let G=255;
		let B=255;
		loop(15)
		{
		//	ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha-=0.4/15;
			yield;
		}
		Obj_Delete(obj);
	}
}


task TExplosion
{
	ReBirthEffect;
	let MS=15.0;
	loop(60){E_Dust;}
	loop(30)
	{
		if(GetPlayerState==STATE_NORMAL){break;}
		E_Effect(MS);
		MS-=0.5;
		yield;
	}

	task E_Effect(MaxScale)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let Alpha=0.2;
		let Scale=0;
		let R=255;
		let G=255;
		let B=255;

		let Xangle=rand(0,360);
		let Yangle=rand(0,360);
		let Zangle=rand(0,360);

		loop(20)
		{
			Alpha-=0.2/20;
			Scale+=MaxScale/20;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleXYZ(obj,Xangle,Yangle,Zangle);
			yield;
		}
		Obj_Delete(obj);
	}

	task E_Dust
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let speed=(rand(1,15));
		let Angle=rand(0,360);
		let Alpha=0.6;
		let Scale=0.2;
		let Zangle=0;
		let ZangleRotate=rand(-10,10);
		let R=255;
		let G=255;
		let B=255;
		loop(45)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha-=0.6/45;
			Scale-=0.2/45;
			Zangle+=ZangleRotate;
			yield;
		}
		Obj_Delete(obj);
	}

	task ReBirthEffect
	{
		while(GetPlayerState!=STATE_DOWN){yield;}
		loop(60){yield;}
		DrawPlayer;
		task DrawPlayer
		{
		let path = GetCurrentScriptDirectory() ~ "img/STG_Player01.png";
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, path);
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		let X=GetStgFrameWidth/2;
		let Y=474;
		let Zangle=0;
		loop(60)
		{
			ObjRender_SetPosition(objPlayer, X,Y, 0);
			ObjRender_SetPosition(obj, X,Y, 0);
			ObjSprite2D_SetSourceRect(obj, 0, 0, 48, 48);
			ObjSprite2D_SetDestCenter(obj);
			Y-=1;
			yield;
		}
		Obj_Delete(obj);
		}
	}
}

task TSpell
{
	let EndFrag=false;
	SetPlayerInvincibilityFrame(360);//無敵時間を設定
	let bSlowMove = GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD;
	let objManage = GetSpellManageObject();
	
	Move;
	Thunder;
	Dark;
	loop(60){yield;}
	Star;
	TShake();
	loop(270){yield;}
	Obj_Delete(objManage);

task Move
{
	loop(60){yield;}
	SetPlayerSpeed(2.5,1.0);
	loop(240)
	{
		yield;
	}
	SetPlayerSpeed(5.0,2.0);
}

task Star
{
let t=floor(rand(0,7));
ascent(i in 0..7)
{
	StarEfect(9+(t+i)%7,i*360/7);
}
	//StarEfect(16,0);

loop(48)
{
BorderEffect(GetPlayerX,GetPlayerY);
loop(5){yield;}
}

}

task StarEfect(graphic,angle)
{
		function XYR{return(rand(0,0));}
		let R=255;;let G=255;let B=255;
		if(graphic==9){R=255;G=0+50;B=0+50;}
		if(graphic==10){R=255;G=100+50;B=0+50;}
		if(graphic==11){R=255;G=255;B=0+50;}
		if(graphic==12){R=0+50;G=255;B=0+50;}
		if(graphic==13){R=0+50;G=255;B=255;}
		if(graphic==14){R=0+50;G=0+50;B=255;}
		if(graphic==15){R=160+50;G=32+50;B=240;}

		let Xangle=rand(0,360);
		let Yangle=rand(0,360);
		let Zangle=rand(0,360);

		let XanglePlus=rand(-10,10);
		let YanglePlus=rand(-10,10);
		let ZanglePlus=rand(-10,10);

		let Scale=0.1;

	//	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		let obj=ObjSpell_Create;
		ObjSpell_Regist(obj);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Spark.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -128+XYR, -512+XYR, -0);
		ObjPrim_SetVertexPosition(obj, 1, 128+XYR, -512+XYR, -0);
		ObjPrim_SetVertexPosition(obj, 2, 128+XYR, 0+XYR, 0);
		ObjPrim_SetVertexPosition(obj, 3, -128+XYR, 0+XYR, 0);

		let UVR=256*0;

		ObjPrim_SetVertexUVT(obj, 0, 0+UVR, 0);
		ObjPrim_SetVertexUVT(obj, 1, 256+UVR, 0);
		ObjPrim_SetVertexUVT(obj, 2, 256+UVR, 512);
		ObjPrim_SetVertexUVT(obj, 3, 0+UVR, 512);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let Alpha=0;

		ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);

		let speed=2;
	//	let angle=rand(260,280);
	//	Obj_SetSpeed(obj,speed);
	//	Obj_SetAngle(obj,angle);

		let ShadowCount=0;
		let scx=0;
		let scy=1.0;
		let wide=0;
		let wideangle=0;
		let time=0;
		if(graphic==16){time=300;}
		ObjSpell_SetDamage(obj,0.75*7);
		loop(240)
		{
			if(Alpha<0.3){Alpha+=0.04;}
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleXYZ(obj,0,Yangle,cos(angle)*3);
			Yangle=rand(-30,30);
		//	ObjSpell_SetIntersecrionLine(obj,GetPlayerX,GetClipMinY,GetPlayerX,GetPlayerY,64*scx,2.3,true);
			if(graphic==9)
			{
			ObjSpell_SetIntersectionLine(obj,GetPlayerX,0,GetPlayerX,GetPlayerY,96*scx);
			}
			if(scx<0.8){scx+=0.05;}
			ObjRender_SetScaleXYZ(obj,scx,scy,1);
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY+40,0);
			yield;
			angle+=6;
		}
		EndFrag=true;
		loop(15)
		{
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			Alpha-=0.3/15;
		//	ObjSpell_SetIntersecrionLine(obj,GetPlayerX,GetClipMinY,GetPlayerX,GetPlayerY,64*scx,2.0,true);
			if(graphic==9)
			{
			ObjSpell_SetIntersectionLine(obj,GetPlayerX,0,GetPlayerX,GetPlayerY,96*scx);
			}
			if(scx>0){scx-=0.1;}
			ObjRender_SetScaleXYZ(obj,scx,scy,1);
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY+40,0);
			yield;
		}
	Obj_Delete(obj);
}


task Dark
{
		let X=GetPlayerX;
		let Y=GetPlayerY;

		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/CircleEffect.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -24, -24, -0);
		ObjPrim_SetVertexPosition(obj, 1, 24, -24, -0);
		ObjPrim_SetVertexPosition(obj, 2, 24, 24, 0);
		ObjPrim_SetVertexPosition(obj, 3, -24, 24, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 48, 0);
		ObjPrim_SetVertexUVT(obj, 2, 48, 48);
		ObjPrim_SetVertexUVT(obj, 3, 0, 48);

		ObjRender_SetPosition(obj,X,Y,0);
		let Alpha=0;
		let Scale=0;
		ObjRender_SetColor(obj,255,255,255);

		loop(60)
		{
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,255*Alpha,255*Alpha,255*Alpha);
			Scale+=1;
			Alpha+=0.2/60;
			yield;
		}
		loop{yield;if(EndFrag){break;}}
		loop(15)
		{
			ObjRender_SetColor(obj,255*Alpha,255*Alpha,255*Alpha);
			Alpha-=0.2/15;
			yield;
		}
		Obj_Delete(obj);
}
task BorderEffect(x,y)
{
		let r1=0;
		let r2=50;
		let randvertex=floor(rand(0,5));
		let VertexNum=16;
		let Radius=[r1,r2];

		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/SampleEC01a.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, VertexNum);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ascent(i in 0+randvertex..VertexNum+randvertex)
		{
			ObjPrim_SetVertexPosition(obj,i-randvertex,Radius[i%2]*cos(360/(VertexNum-2)*i),Radius[i%2]*sin(360/(VertexNum-2)*i),0);
		}
		ascent(i in 0+randvertex..VertexNum+randvertex)
		{	
			ObjPrim_SetVertexUVT(obj, i-randvertex,  256/(VertexNum)*i,  16*(i%2));
		}

		let Scale=1;
		ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
		ObjRender_SetPosition(obj,x,y,0);
		let Alpha=0.4;
		let angle=0;

		let StandardX=192;
		let StandardY=224+32;

		let Xangle=rand(0,360);
		let Yangle=rand(0,360);
		let Zangle=rand(0,360);

		ObjRender_SetAngleXYZ(obj,Xangle*0+60,Yangle*0,Zangle);

	//	Obj_SetSpeed(obj,12);
	//	Obj_SetAngle(obj,270);

		loop(60)
		{
			if(r1<64)
			{
			r1+=8;
			r2+=8;
			Radius=[r1,r2];
			}
			ObjRender_SetX(obj,GetPlayerX);
			ObjRender_SetY(obj,ObjRender_GetY(obj)-12);
			ascent(i in 0..VertexNum)
			{
				ObjPrim_SetVertexPosition(obj, i, Radius[i%2]*cos(360/(VertexNum-2)*i+angle), Radius[i%2]*sin(360/(VertexNum-2)*i+angle),0);
			}
			ascent(i in 0..VertexNum)
			{			
				ObjPrim_SetVertexColor(obj, i,255*Alpha,255*(i%3)*Alpha,0);
			}
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			angle+=2;
			yield;
			if(EndFrag){break;}
		}
		if(EndFrag)
		{
			loop(15)
			{
			r1-=64/15;
			r2-=64/15;
			Radius=[r1,r2];
			ObjRender_SetX(obj,GetPlayerX);

			ObjRender_SetY(obj,ObjRender_GetY(obj)-12);
			ascent(i in 0..VertexNum)
			{
			ObjPrim_SetVertexPosition(obj, i, Radius[i%2]*cos(360/(VertexNum-2)*i+angle), Radius[i%2]*sin(360/(VertexNum-2)*i+angle),0);
			}
			ascent(i in 0..VertexNum)
			{			
				ObjPrim_SetVertexColor(obj, i,255*Alpha,255*(i%3)*Alpha,0);
			}
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			angle+=2;
			yield;
			}
		}
		Obj_Delete(obj);
}

task Thunder
{
	loop(30)
	{
		let rangle=rand(0,360);
		PrismLaserShot(0,0);
		loop(2){yield;}
	}
	let R=0;
	loop(30)
	{
		R+=4;
		let rangle=rand(0,360);
		PrismLaserShot(1,R);
		loop(2){yield;}
	}
	loop(60)
	{
		let rangle=rand(0,360);
		PrismLaserShot(1,R);
		loop(2){yield;}
	}
	loop(30)
	{
		R-=4;
		let rangle=rand(0,360);
		PrismLaserShot(1,R);
		loop(2){yield;}
	}
	task PrismLaserShot(type,R)
	{
		let ra=rand(0,360);
		let x=ObjRender_GetX(objPlayer)+R*cos(ra)*type;
		let y=ObjRender_GetY(objPlayer)+R/2*sin(ra)*type;
		let speed=rand(5,10);
		let angle=rand(0,360);
		let angleplus=rand(-10,10);
		if(type==0)
		{
		x+=120*cos(angle+180);
		y+=120*sin(angle+180);
		}
		loop(12)
		{
			x+=speed*cos(angle);
			y+=speed*sin(angle);
			Lightning(x,y,rand(-1.5,1.5),angle+180,rand(10,15),rand(6,10), [150,150,0]);
			angle+=angleplus*type;
			yield;
		}

		task Lightning(x,y,speed,angle,leng,width,color)
		{
		//	x+=cos(angle)*leng*0.5;
		//	y+=sin(angle)*leng*0.5;

			let imgBer = GetCurrentScriptDirectory() ~ "img/thunder.png";
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, imgBer);
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		//	Obj_SetRenderPriorityI(obj,90);
			ObjPrim_SetVertexPosition(obj, 0, -width/2, -leng/2, -0);
			ObjPrim_SetVertexPosition(obj, 1, width/2, -leng/2, -0);
			ObjPrim_SetVertexPosition(obj, 2, width/2, leng/2, 0);
			ObjPrim_SetVertexPosition(obj, 3, -width/2, leng/2, 0);

			ObjPrim_SetVertexUVT(obj, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj, 1, 48, 0);
			ObjPrim_SetVertexUVT(obj, 2, 48, 48);
			ObjPrim_SetVertexUVT(obj, 3, 0, 48);

			ObjRender_SetPosition(obj,x,y,0);

			let Alpha=0;
			let frame=0;
			let graphic=0;

			loop(5)
			{
			LightSet;
			Alpha+=1/5;
			ObjRender_SetColor(obj,255*Alpha,255*Alpha,150*Alpha);
			yield;
			}

			loop(30)
			{
			LightSet;
			Alpha-=1/30;
			ObjRender_SetColor(obj,255*Alpha,255*Alpha,150*Alpha);
			yield;
			}
			Obj_Delete(obj);
	
			sub LightSet
			{
				if(frame%3==0)
				{
				graphic=floor(rand(0,256));
				ObjRender_SetAngleZ(obj,angle-90*(floor(rand(0,2))*2-1));

				ObjPrim_SetVertexUVT(obj, 0, 0, 0+graphic);
				ObjPrim_SetVertexUVT(obj, 1, 48, 0+graphic);
				ObjPrim_SetVertexUVT(obj, 2, 48, 48+graphic);
				ObjPrim_SetVertexUVT(obj, 3, 0, 48+graphic);
				ObjRender_SetScaleXYZ(obj,(floor(rand(0,2))*2-1)/2,(floor(rand(0,2))*2-1),1);
				}
				frame++;
				ObjRender_SetPosition(obj,x,y,0);
				x+=speed*cos(angle);
				y+=speed*sin(angle);
			}
		}

	}
}
	task TShake()
	{/*
		while(!Obj_IsDeleted(objManage))
		{
			Set2DCameraFocusX(GetStgFrameWidth / 2 + rand(-6, 6));
			Set2DCameraFocusY(GetStgFrameHeight / 2 + rand(-6, 6));
			yield;
		}

		loop(60)
		{
		Reset2DCamera();
		yield;
		}*/
	}

}
