#TouhouDanmakufu[Player]
#ScriptVersion(3)
#ID["PERSON_A"]
#Title["Person A"] 
#Text["Person A"]
#Image["./protag_select.png"]
#ReplayName["PersonA"]

let CSD = GetCurrentScriptDirectory;
let playerID = GetPlayerObjectID;
let playerObj = ObjPrim_Create(OBJ_SPRITE_2D);
let hitboxObj = ObjPrim_Create(OBJ_SPRITE_2D);
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);
let count = -1;
let isFocus = false;
let playerdeadSFX = CSD ~ "DEAD.wav";
let spellSFX = CSD ~ "LASER2.wav";
let animFrame = 0; 
let animFrame2 = 0;
//shot sheet
let shotPerson = CSD ~ "playershot.txt";

//texture sheet
let playerSprite = CSD ~ "player.png";

//spell card effect texture 
let spellTex = CSD ~ "spelleffect.png";

ObjPlayer_AddIntersectionCircleA1(playerID, 0, 0, 1, 20);


@Initialize { 

TMagicCircle;

LoadSound(playerdeadSFX);
LoadSound(spellSFX);
LoadPlayerShotData(shotPerson);

SetPlayerSpeed (5,2.5);

//set hitbox
ObjPlayer_AddIntersectionCircleA1(playerID,0,0,2,16);

//set how far your player can move in the feild 
SetPlayerClip(0+6,0+24,384-6,448-16);

//set how long your player is gone when dead
SetPlayerDownStateFrame(60);

//set how long you have to deathbomb 
SetPlayerRebirthFrame(2);

//set auto collection line
SetPlayerAutoItemCollectLine(120);

   
renderPlayer;
renderHitBox(GetPlayerX,GetPlayerY);



}
@Event {
     alternative(GetEventType)
    
    //spell card
    case(EV_REQUEST_SPELL) {
    if (GetPlayerSpell > 0) {
        SetScriptResult(true);
		SetPlayerSpell(GetPlayerSpell - 1);
		spellCard;
      } 
     else  {
       SetScriptResult(false);
    }
   
  
}

  //player is dead
   case(EV_PLAYER_SHOOTDOWN){
   PlaySE(playerdeadSFX);
         SetForbidPlayerShot(true);
         SetForbidPlayerSpell(true);
         ObjRender_SetAlpha(playerObj,0);
   }

  //player is reviving 
   case(EV_PLAYER_REBIRTH) {
         SetForbidPlayerShot(false);
         SetForbidPlayerSpell(false);
         ObjRender_SetAlpha(playerObj,255);
         SetPlayerInvincibilityFrame(120);
         SetPlayerSpell(3);
  }

}

@MainLoop{ 


  if(IsPermitPlayerShot) {  
        if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD) {
           count++;
             if(count>=5) {
                count = 0;
          }
     }
     if(GetVirtualKeyState(VK_SHOT) == KEY_PULL) {
       count = -1;
     }


      //focus shot
     
       if(isFocus) { 
         if(count>3) {
           //location x, location y, speed, dorection, damage, penetration, ID
          CreatePlayerShotA1(GetPlayerX-3,GetPlayerY-20,20,270,5,1,1);
          CreatePlayerShotA1(GetPlayerX+3,GetPlayerY-20,20,270,5,1,1);
          }
     }

       //regular shot 

         else { 
            if(count>3) {
         CreatePlayerShotA1(GetPlayerX-8,GetPlayerY-20,20,260,4,1,1);
         CreatePlayerShotA1(GetPlayerX,GetPlayerY-20,20,270,4,1,1);
         CreatePlayerShotA1(GetPlayerX+8,GetPlayerY-20,20,280,4,1,1);
        }
		
          

 
   yield;
     }
}
}


task renderPlayer {
     ObjPrim_SetTexture(playerObj,playerSprite);
     Obj_SetRenderPriorityI(playerObj,31);
     ObjRender_SetBlendType(playerObj,BLEND_ALPHA);
     ObjRender_SetAngleXYZ(playerObj,0,0,0);
     ObjRender_SetScaleXYZ(playerObj,1,1,0);
     ObjRender_SetAlpha(playerObj,255);
     ObjSprite2D_SetSourceRect(playerObj,0,12,50,70);
     ObjSprite2D_SetDestCenter(playerObj);
     ObjRender_SetPosition(playerObj,GetPlayerX,GetPlayerY,0);

     while(!Obj_IsDeleted(playerObj)){
            ObjRender_SetPosition(playerObj,GetPlayerX,GetPlayerY,0);
            
            if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD) {
             ObjRender_SetAngleY(playerObj,0);
             ObjSprite2D_SetSourceRect(playerObj,90,0,50,70);
              }
            else if (GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD) {
              ObjRender_SetAngleY(playerObj,180);
              ObjSprite2D_SetSourceRect(playerObj,90,0,50,70);
			 }
            else {
                
		 
			ObjRender_SetAngleXYZ(playerObj,0,0,0);
			if(animFrame < 5) { ObjSprite2D_SetSourceRect(playerObj,0,12,50,70); }
			if(animFrame >= 10 && animFrame < 15) { ObjSprite2D_SetSourceRect(playerObj,103,0,140,70); }
			if(animFrame >= 20) { ObjSprite2D_SetSourceRect(playerObj,195,0,252,98); }
			
			animFrame++;		// count animFrame. (++ is +1)
		
		if(animFrame > 25) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
		
             }

      yield;

 }
 
}


task renderHitBox(x,y) {
      let alpha = 0;
      let spin = 0;

     ObjPrim_SetTexture(hitboxObj,playerSprite);
     ObjRender_SetBlendType(hitboxObj,BLEND_ALPHA);
     Obj_SetRenderPriorityI(hitboxObj,50);
     ObjRender_SetAlpha(hitboxObj,0);
     ObjRender_SetScaleXYZ(hitboxObj,1,1,0);
     ObjSprite2D_SetSourceRect(hitboxObj,0,90,50,137);
     ObjSprite2D_SetDestCenter(hitboxObj);
     
   while (!Obj_IsDeleted(hitboxObj)) {
       ObjRender_SetPosition(hitboxObj,GetPlayerX,GetPlayerY,0);
       ObjRender_SetAngleZ(hitboxObj,spin);
       ObjRender_SetAlpha(hitboxObj,alpha);

   if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){
         alpha = 255;
      }
    else { 
         alpha = 0;
      }
        
      spin+=2;
    
       yield;

  
 }
}
function wait(w){
       loop(w) {
	     yield;
		 }
	}

task TMagicCircle
{      let alpha = 0;
      let spin = 0;
	  let path = GetCurrentScriptDirectory ~ "magiccircle.png";
	  let frameInvOld = 0;

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,255);
     ObjRender_SetAlpha(circleObj,0);
     ObjRender_SetScaleXYZ(circleObj,0.8,0.8,0);
     ObjSprite2D_SetSourceRect(circleObj,0,0,400,406);
     ObjSprite2D_SetDestCenter(circleObj);
     
   while(true)
	{   
		let frameInv = GetPlayerInvincibilityFrame();

		if(frameInv <= 0)
		{
			Obj_SetVisible(circleObj, false);
			frameInvOld = 0;
			
		}
		
   
   else {
   Obj_SetVisible(circleObj, true);
       ObjRender_SetPosition(circleObj,GetPlayerX-35,GetPlayerY,0);
       ObjRender_SetAngleZ(circleObj,spin);
       ObjRender_SetAlpha(circleObj,alpha);

         alpha = 120;
		 spin+=3;
		 fade;
        }
    
       yield;
  
 }
Obj_Delete(circleObj);
}

function fade{
let spin = 0;
let t = 0.5;
descent(i in 0..200/t){
ObjRender_SetPosition(circleObj,GetPlayerX+35,GetPlayerY,0);
ObjRender_SetAngleZ(circleObj,spin);
    ObjRender_SetAlpha(circleObj, i*t);
	spin+=3;
    yield;
}
}

task spellCard {

PlaySE(spellSFX);
       let current = GetCurrentScriptDirectory();
	let imgEffect = current ~ "spelleffect.png";

	SetPlayerInvincibilityFrame(300);//無敵時間を設定
	let bSlowMove = GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD;
	let objManage = GetSpellManageObject();
	
	let enemyX = 0;
	let enemyY = 0;

	TShake();
	effect(0);
	effect(1);
	loop(220)
	{
		enemyX = GetPlayerX();
		enemyY = GetPlayerY();
		let pos = GetEnemyIntersectionPosition(enemyX, enemyY, 1);
		if(length(pos) > 0)
		{
			enemyX = pos[0][0];
			enemyY = pos[0][1];
		}
		yield;
	}
	Obj_Delete(objManage);


	task effect(num)
	{	
		//エフェクト用タスク
		let tv=0;
		let angle=0;
		let alpha=0;	
		task alphaState
		{	//α値を変更するためのタスク
			alpha=0;
			loop(256/3){alpha+=3; yield;}
			loop(90){yield;}
			while(!Obj_IsDeleted(objManage)){alpha-=8; yield;}
		}
		task lineEffect
		{
			loop(120){concentrationLine();loop(1){yield;}}
		}
		alphaState;
		if(bSlowMove){lineEffect;}
		
		let obj = ObjSpell_Create();//スペルオブジェクトを作成
		ObjPrim_SetVertexCount(obj, 10);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriority(obj, 0.60);
		ObjPrim_SetTexture(obj, imgEffect);
		ObjSpell_Regist(obj);

		ascent(i in 0..5)
		{
			let angle = -45+i*90;
			let x;
			let y;
			if(bSlowMove)
			{
				//低速移動時は範囲が狭い
				x = 192*cos(angle);
				y = 192*sin(angle);
			}
			else
			{
				//高速移動時は広範囲
				x = 512*cos(angle);
				y = 512*sin(angle);
			}
			ObjPrim_SetVertexPosition(obj, i*2, x, y, 0);
			ObjPrim_SetVertexPosition(obj, i*2+1, 0, 0, 0);
			ObjPrim_SetVertexColor(obj, i*2, 0, 0, 0);
		}
		
		while(!Obj_IsDeleted(objManage))
		{
			tv-=1;
			if(num == 0){angle+=1;}
			else if(num == 1){angle-=1;}
			ObjRender_SetAngleZ(obj, angle);//描画角度を設定

			if(bSlowMove)
			{	//低速移動時
				ObjRender_SetPosition(obj, enemyX, enemyY, 0);
				ObjSpell_SetIntersectionCircle(obj, enemyX, enemyY, 81);//当たり判定を登録
				ObjSpell_SetDamage(obj, 5);
			}
			else 
			{	//高速移動時
				ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);//座標を設定
				ObjSpell_SetIntersectionCircle(obj, GetPlayerX, GetPlayerY, 256);//当たり判定を登録
				ObjSpell_SetDamage(obj, 2);
			}
			ascent(i in 0..5)
			{
				ObjPrim_SetVertexUVT(obj, i*2,   16*i, tv);//テクスチャの座標を設定
				ObjPrim_SetVertexUVT(obj, i*2+1 ,32,  64+tv);
				
				ObjPrim_SetVertexColor(obj, i*2+1, alpha, alpha, alpha);
			}
			yield;
		}
		Obj_Delete(obj);
	}

	task concentrationLine()
	{	//線が集まる感じのエフェクト
		let angle = rand(0, 360);
		let alpha=0;	
		let scale=3;
		
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);//オブジェクトを作成
		ObjPrim_SetVertexCount(obj, 3);//頂点を3個つくる
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);//プリミティブタイプを設定
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);//描画方法を設定
		Obj_SetRenderPriority(obj, 0.60);
		ObjPrim_SetTexture(obj, imgEffect);//テクスチャを設定
		ObjRender_SetAngleZ(obj, angle);//描画角度を設定
		
		//頂点座標を設定
		ObjPrim_SetVertexPosition(obj, 0,  0,  10, 0);
		ObjPrim_SetVertexPosition(obj, 1, -10, 50, 0);
		ObjPrim_SetVertexPosition(obj, 2, 10,  50, 0);
		
		//頂点のUV値を設定
		ObjPrim_SetVertexUVT(obj, 0, 10,  10);
		ObjPrim_SetVertexUVT(obj, 1,  0,  50);
		ObjPrim_SetVertexUVT(obj, 2, 20,  50);
		
		//色設定
		ObjPrim_SetVertexColor(obj, 0,  alpha, 0, alpha);
		ObjPrim_SetVertexColor(obj, 1,  0, 0, 0);
		ObjPrim_SetVertexColor(obj, 2,  0, 0, 0);
		
		loop(60)
		{
			scale-=3/60;
			alpha+=5;
			ObjPrim_SetVertexColor(obj, 0,  alpha, 0, alpha);
			ObjRender_SetScaleXYZ(obj, scale, scale*3, 1);	
			ObjRender_SetPosition(obj, enemyX, enemyY, 0);	
			yield;	
		}
		Obj_Delete(obj);
	}

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

		Reset2DCamera();
	}


task createRingEffect {		
     let scale = 0;
	 let alpha = 200;
	 
	 let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	 ObjPrim_SetTexture(obj,spellTex);
	 ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	 ObjRender_SetAlpha(obj,alpha);
	 Obj_SetRenderPriorityI(obj,41);
	 ObjRender_SetAngleXYZ(obj,scale,scale,0);
	 ObjSprite2D_SetSourceRect(obj,0,0,200,200);
	 ObjSprite2D_SetDestCenter(obj);
	 ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
	 
	 ObjRender_SetColor(obj,truncate(rand(128,255)),truncate(rand(128,255)),truncate(rand(128,255)));
	 
	 while(alpha > 0) {
	      ObjRender_SetAlpha(obj,alpha);
		  ObjRender_SetAngleXYZ(obj,scale,scale,0);
		  
		  scale+=0.4;
		  alpha-=2;
		  
		  yield;
		  
		  
	}
	
	Obj_Delete(obj);
}	
}