#TouhouDanmakufu[Player]
#ScriptVersion(3)
#ID["PERSON_B"]
#Title["Wolfie"]
#Text["Wolfie"]
#ReplayName["Wolfie"]
#Image["./protag_select.png"]
 
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 ~ "spelleffect1.png";
let spellTex2 = CSD ~ "spelleffect2.png";
 
ObjPlayer_AddIntersectionCircleA1(playerID, 0, 0, 1, 20);
 
 
@Initialize {
 TMagicCircle;
LoadSound(playerdeadSFX);
LoadSound(spellSFX);
LoadPlayerShotData(shotPerson);
 renderPlayer;
renderHitBox(GetPlayerX,GetPlayerY);
 
 
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);
 
   
 
 
 
}
 
task TExplosion{
    let path = GetCurrentScriptDirectory() ~ "player.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
    Obj_SetRenderPriority(obj, 0.60);
    ObjPrim_SetTexture(obj, path);
    ObjSprite2D_SetSourceRect(obj, 40, 0, 80, 64);
    ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
 
    let alpha = 255;
    let scale = 0;
    loop(30)
    {
        scale += 0.2;
        alpha -= 8;
        ObjRender_SetColor(obj, alpha, alpha, alpha);
        ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
        ObjRender_SetScaleXYZ(obj, scale, scale, 1);
        yield;
    }
    Obj_Delete(obj);
}
 
@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);
   TExplosion;
   wait(10);
   ObjRender_SetAlpha(playerObj,0);
   
         SetForbidPlayerShot(true);
         SetForbidPlayerSpell(true);
         
   }
 
  //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(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){
         if(count>2) {
           //location x, location y, speed, dorection, damage, penetration, ID
          CreatePlayerShotA1(GetPlayerX-3,GetPlayerY-20,20,270,2,1,1);
          CreatePlayerShotA1(GetPlayerX+3,GetPlayerY-20,20,270,2,1,1);
		  CreatePlayerShotA1(GetPlayerX-6,GetPlayerY-20,20,270,2,1,1);
          CreatePlayerShotA1(GetPlayerX+6,GetPlayerY-20,20,270,2,1,1);
          }
     }
 
       //regular shot
 
         else if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD) {
            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);
		 CreatePlayerShotA1(GetPlayerX-30,GetPlayerY-40,20,260,4,1,1);
         CreatePlayerShotA1(GetPlayerX,GetPlayerY-40,20,270,4,1,1);
         CreatePlayerShotA1(GetPlayerX+30,GetPlayerY-40,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,0.9,0.9,0);
     ObjRender_SetAlpha(playerObj,255);
     ObjSprite2D_SetSourceRect(playerObj,-3,0,40,64);
     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,180);
            ObjSprite2D_SetSourceRect(playerObj, 40, 0, 80, 64);
        }
        else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
		
        { ObjRender_SetAngleY(playerObj,0);
            ObjSprite2D_SetSourceRect(playerObj, 40, 0, 80, 64);
        }
        else
        {
           
			ObjRender_SetAngleXYZ(playerObj,0,0,0);
			if(animFrame < 5) { ObjSprite2D_SetSourceRect(playerObj,-3,0,40,64); }
			if(animFrame >= 10 && animFrame < 15) { ObjSprite2D_SetSourceRect(playerObj,120,0,161,64); }
			if(animFrame >= 20) { ObjSprite2D_SetSourceRect(playerObj,75,0,115,64); }
			
			
			
			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,0.9,0.9,0);
     ObjSprite2D_SetSourceRect(hitboxObj,8,83,51,125);
     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;
 
 
 }
}

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,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 = 1;
descent(i in 0..200/t){
ObjRender_SetPosition(circleObj,GetPlayerX+35,GetPlayerY,0);
ObjRender_SetAngleZ(circleObj,spin);
    ObjRender_SetAlpha(circleObj, i*t);
	spin+=5;
    yield;
}
}
function wait(w){
       loop(w) {
         yield;
         }
    }    
 
task spellCard {
 
PlaySE(spellSFX);
TShake();
 
       //prepare
       SetForbidPlayerShot(true);
       SetForbidPlayerSpell(true);
       SetPlayerInvincibilityFrame(300);
       SetPlayerSpeed(2,2);
       
       // a spellcard object
       let spellObject = ObjSpell_Create();
       ObjSpell_Regist(spellObject);
       ObjSpell_SetEraseShot(spellObject,true);
       
       
       // bomb
 
       loop(16) {
   
         createRingEffect;
		 createRingEffect2;
         ObjSpell_SetIntersectionCircle(spellObject,GetPlayerX,GetPlayerY,400);
         ObjSpell_SetDamage(spellObject,20);
         wait(5);
         }
     
       Obj_Delete(GetSpellManageObject);
       //cleanup
       SetForbidPlayerShot(false);
       SetForbidPlayerSpell(false);
       SetPlayerSpeed(3,2.5);
       Obj_Delete(spellObject);
       
       task TShake()
	{
		while(!Obj_IsDeleted(GetSpellManageObject))
		{
			Set2DCameraFocusX(GetStgFrameWidth / 2 + rand(-8, 8));
			Set2DCameraFocusY(GetStgFrameHeight / 2 + rand(-8, 8));
			yield;
		}

		Reset2DCamera();
	}
       
       
       
}      
   
 
 
task createRingEffect {    
     let scale = 0;
     let alpha = 200;
     let spin = 0;

	 
     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,spin);
	
     ObjSprite2D_SetSourceRect(obj,0,0,400,400);
     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,spin);
          spin+=2;
          scale+=0.4;
          alpha-=2;
         
          yield;
         
    }
   
    Obj_Delete(obj);
}

task createRingEffect2 {    
     let scale = 0;
     let alpha = 200;
     let spin = 0;

	 
     let obj = ObjPrim_Create(OBJ_SPRITE_2D);
     ObjPrim_SetTexture(obj,spellTex2);
     ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
     ObjRender_SetAlpha(obj,alpha);
     Obj_SetRenderPriorityI(obj,41);
     ObjRender_SetAngleXYZ(obj,scale,scale,spin);
	
     ObjSprite2D_SetSourceRect(obj,0,0,400,400);
     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,spin);
          spin+=-2;
          scale+=0.4;
          alpha-=2;
         
          yield;
         
    }
   
    Obj_Delete(obj);
}