#東方弾幕風[Player]	//自機スクリプトであることを示します
#ScriptVersion[2]	//スクリプトバージョン2が必須です
#Menu[魔理沙]	//自機選択画面で表示されるメニューです
#Text[レーザー強化タイプ
攻撃範囲　狭い
移動速度　速い]
#ReplayName[marisa]	//リプレイ選択時に表示される文字です。8文字以内です。

script_player_main
{
	let current = GetCurrentScriptDirectory();//カレントディレクトリを取得
	let imgplayer = current~"marisa.png";
	let system = current~"system.png";
	let imgRumiaCutIn = current~"RumiaCutIn.png";
	let senshot = current~"nshot.wav";
	let sesshot = current~"sshot.wav";
	let sekakusei = current~"kakusei.wav";
	let sezessi = current~"zessi.wav";
	let shotCount = 1;//弾を連続で発射するための変数
	let bNextShot = false;//ちょん押しでも自弾を連続発射するための変数(説明しづらい)
	let objShot = NULL;//誘導弾
        let grad = 0;
        let atk = 1;
        let time = 0;
      let kakusei = false;
      let zessi = 0;
      let kirikae = 0;
      let gensyo = 0;
      SetCommonData("kakusei",0);


      let option = 0;
      let teisoku = false;
      let shotnum = 0;
      let auranum = 0;


	task miss{
           SetPlayerInvincibility(180);
        }

        task sshot1(let x,let y,let gr){
	   let objShot= Obj_Create(OBJ_SHOT);
	   Obj_SetX(objShot, GetPlayerX+x);
	   Obj_SetY(objShot, y);
           ObjShot_SetGraphic(objShot, gr);
	   ObjShot_SetDamage(objShot, 2.2);    
	   ObjShot_SetPenetration(objShot, 2);
	   Obj_SetAngle(objShot, 270);
	   Obj_SetSpeed(objShot,15);
	   while( !Obj_BeDeleted(objShot) && teisoku==true && kakusei==true && bNextShot == true){
		Obj_SetX(objShot,GetPlayerX+x);
		yield;
	   }
	   Obj_Delete(objShot);
	}
        task sshot2(let x,let y,let gr){
	   let objShot= Obj_Create(OBJ_SHOT);
	   Obj_SetX(objShot, GetPlayerX+x);
	   Obj_SetY(objShot, y);
           ObjShot_SetGraphic(objShot, gr);
	   ObjShot_SetDamage(objShot, 2.2);    
	   ObjShot_SetPenetration(objShot, 2);
	   Obj_SetAngle(objShot, 270);
	   Obj_SetSpeed(objShot,15);
	   while( !Obj_BeDeleted(objShot) && teisoku==true && kakusei==false && bNextShot == true){
		Obj_SetX(objShot,GetPlayerX+x);
		yield;
	   }
	   Obj_Delete(objShot);
	}
        task nshot(let x,let y,let ang,let gr){
           shotnum+=1;
	   let objShot= Obj_Create(OBJ_SHOT);
	   Obj_SetX(objShot, GetPlayerX+x);
	   Obj_SetY(objShot, y);
           ObjShot_SetGraphic(objShot, gr);
	   ObjShot_SetDamage(objShot, 0.8);    
	   ObjShot_SetPenetration(objShot, 2);
	   Obj_SetAngle(objShot, ang);
	   Obj_SetSpeed(objShot,15);
	   while( !Obj_BeDeleted(objShot) ){
		yield;
	   }
	   Obj_Delete(objShot);
           shotnum-=1;
	}
        task aura1(let x,let y,let ang,let gr){
           auranum+=1;
	   let objShot= Obj_Create(OBJ_SHOT);
	   Obj_SetX(objShot, GetPlayerX+x);
	   Obj_SetY(objShot, GetPlayerY+y);
           ObjShot_SetGraphic(objShot, gr);
	   ObjShot_SetDamage(objShot, 1);    
	   ObjShot_SetPenetration(objShot, 100000);
	   Obj_SetAngle(objShot, ang);
	   Obj_SetSpeed(objShot,0);
	   while( !Obj_BeDeleted(objShot) && teisoku==true && kakusei==false && bNextShot == true){
		Obj_SetX(objShot,GetPlayerX+x);
		Obj_SetY(objShot,GetPlayerY+y);
		yield;
	   }
	   Obj_Delete(objShot);
           auranum-=1;
	}
        task aura2(let x,let y,let ang,let gr){
           auranum+=1;
	   let objShot= Obj_Create(OBJ_SHOT);
	   Obj_SetX(objShot, GetPlayerX+x);
	   Obj_SetY(objShot, GetPlayerY+y);
           ObjShot_SetGraphic(objShot, gr);
	   ObjShot_SetDamage(objShot, 3);    
	   ObjShot_SetPenetration(objShot, 100000);
	   Obj_SetAngle(objShot, ang);
	   Obj_SetSpeed(objShot,0);
	   while( !Obj_BeDeleted(objShot) && kakusei==true && bNextShot == true){
		Obj_SetX(objShot,GetPlayerX+x);
		Obj_SetY(objShot,GetPlayerY+y);
		yield;
	   }
	   Obj_Delete(objShot);
           auranum-=1;
	}




	@Initialize
	{	
		//自機の初期化時に一回だけ呼び出される部分です
		SetPlayerLifeImage(imgplayer, 0, 0, 50, 50);//残機数の画像
		LoadGraphic(imgplayer);
		LoadGraphic(system);
		SetSpeed(4.5, 2.2);//移動速度を設定
		LoadPlayerShotData(current~"ShotData.txt");
		SetItemCollectLine(120);//アイテム回収ラインを設定
                SetInitialBombCount(3);
                SetGrazeCircle(40);
                SetRebirthFrame(0);
	}
	
	@MainLoop
	{	

      if(GetKeyState(VK_USER)==KEY_PUSH&&kakusei==true&&kirikae==0){
          SetCommonData("kakusei",0);
          SetCommonData("zessi",0);
          zessi=0;
          kakusei=false;
          AddPoint(-30);
          kirikae=10;
          PlaySE(sekakusei);
      }
      else if(GetKeyState(VK_USER)==KEY_PUSH&&kakusei==false&&kirikae==0&&GetCommonData("kakuseikinsi")==false){
         SetCommonData("kakusei",1);
         kakusei=true;
         kirikae=5;
         PlaySE(sekakusei);
      }
      if(kakusei==true){
         if(GetKeyState(VK_USER)==KEY_HOLD&&GetCommonData("zessi")==0){
            zessi++;
            if(zessi>10&&GetCommonData("ruikei")>0){SetCommonData("zessi",1);PlaySE(sezessi);}
         }
	 SetSpeed(7.5, 4.5);
         gensyo++;
         if(gensyo>=3){AddPoint(-1);gensyo=0;}
         if(GetCommonData("zessi")==1){
            SetCommonData("ruikei",GetCommonData("ruikei")-1);
            if(GetCommonData("ruikei")==0){SetCommonData("zessi",0)}
         }
      }
      else{
	 SetSpeed(4.5, 2.7);
      }

      if(kakusei==true&&GetPoint>0){Slow(1);}
      else{Slow(0);SetCommonData("zessi",0)}

      if(GetPoint>500){AddPoint(500-GetPoint)}
      if(kirikae>0){kirikae-=1;}

      if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
         teisoku=true;
         if(option<10&&kakusei==false){option+=1;}
         if(option>0&&kakusei==true){option-=1;}
      }
      else{
         teisoku=false;
         if(option>0&&kakusei==false){option-=1;}
         if(option<10&&kakusei==true){option+=1;}
      }

		//毎フレーム実行される部分です
		//
		if(teisoku==true)
		{	//低速移動時の自弾
			if(shotCount == 0)
			{
				if(kakusei==true&&option==0){
					if(auranum<1){
						aura2(0, 0,270,10);
					}
					sshot1(+30, GetPlayerY()-10,4);
					sshot1(-0, GetPlayerY()-20,4);
					sshot1(-30, GetPlayerY()-10,4);
					PlaySE(sesshot);
				}
				if(kakusei==false&&option==10){
					if(auranum<2){
						aura1(-15, -20,270,9);
						aura1(+15, -20,270,9);
					}
					sshot2(+15, GetPlayerY()-30,3);
					sshot2(-15, GetPlayerY()-30,3);
					PlaySE(sesshot);
				}
				shotCount=2;
			}
		}
		
		//高速移動時の自弾発射
		if(shotCount == 0)
		{
			if(kakusei==true&&shotnum<=20&&option>3){
				if(auranum<1){
					aura2(0, 0,270,10);
				}
				nshot(-30+option*1.5, GetPlayerY()-option*2,270-grad/2,2);
				nshot(+30-option*1.5, GetPlayerY()-option*2,270-grad/2,2);
				PlaySE("sePlayerShot01.wav");
			}
			if(kakusei==false&&shotnum<=30&&option<7){
					let a=0;
					let b=0;
				if(grad>0){
					a=1;
				}
				if(grad<0){
					b=1;
				}
				nshot(-30+option*1.5, GetPlayerY()-option*2,270-grad*a*1.0,1);
				nshot(-0, GetPlayerY()-16,270-grad*0.5,1);
				nshot(+30-option*1.5, GetPlayerY()-option*2,270-grad*b*1.0,1);
				PlaySE("sePlayerShot01.wav");
			}
			shotCount=2;
		}		
		

		if(GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD)
		{
			if(shotCount > 0){shotCount-=1;}
			bNextShot = true;
		}
		else{bNextShot = false;}

                //ショット振り
		if(teisoku==true){grad=0;}
		else if(GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD){
			if(GetKeyState(VK_LEFT)==KEY_PUSH || GetKeyState(VK_LEFT)==KEY_HOLD)
			{
				grad+=1.5;
				if(grad<0){grad+=1.5}
			}
			else if(GetKeyState(VK_RIGHT)==KEY_PUSH || GetKeyState(VK_RIGHT)==KEY_HOLD)
			{
				grad-=1.5;
				if(grad>0){grad-=1.5}
			}
			else 
			{
				if(grad<0){grad+=2}if(grad>0){grad-=2}
				if(grad<4&&grad>-4){grad=0}
			}
		}
		else{
			if(grad<0){grad+=3}if(grad>0){grad-=3}
			if(grad<4&&grad>-4){grad=0}
		}
		if(grad>30){grad=30}if(grad<-30){grad=-30}
		

		SetIntersectionCircle(GetPlayerX, GetPlayerY, 0);//当たり判定登録	
		yield;time++;
	}
	@Missed
	{
            if(GetPlayerBomb>0&&GetCommonData("autobomb")==true){
            	if(kakusei==false && GetPlayerBomb>1){
            		AddBomb(-1);
            	}
               CallSpellCard;
            }
            else{
            bNextShot = false;
            if(kakusei==true){
                kakusei=false;
                SetCommonData("kakusei",0);
                Slow(0);
	        SetSpeed(4.5, 2.2);
            }
            AddPoint(-GetPoint);
            AddPoint(250);
            kirikae=10;
            SetPlayerInvincibility(180);
            }
	    yield;
        }

	@SpellCard
	{
		//スペルカードを発動した瞬間に呼ばれる部分です
		//ここでGetKeyStateで移動状態を判断したりして
		//呼び出すスペルを選びます	
                if(OnMissed==true){UseSpellCard("Demarcation", 10);}
                else{UseSpellCard("Demarcation", 130);}
			
		
	}
	
	@DrawLoop
	{
		//描画ループ。スキップされない限り1フレームに1回実行されます

		if(GetCommonData("zessi")==0){SetColor(255, 255, 255);}
		else{SetColor(255, 155, 155);}
		SetRenderState(ALPHA);
		SetTexture(imgplayer);
		if(GetKeyState(VK_LEFT)==KEY_PUSH || GetKeyState(VK_LEFT)==KEY_HOLD)
		{
		   SetGraphicRect(0, 48, 48, 96);
		}
		else if(GetKeyState(VK_RIGHT)==KEY_PUSH || GetKeyState(VK_RIGHT)==KEY_HOLD)
		{
		   SetGraphicRect(0, 96, 48, 144);
		}
                else{
		   SetGraphicRect(0, 0, 48, 48);
                }
                SetGraphicAngle(0,0,0);
		DrawGraphic(GetPlayerX(), GetPlayerY());
		SetColor(255, 255, 255);
	
		SetGraphicRect(0, 160, 20, 180);
                SetGraphicAngle(0,0,time*4);
		DrawGraphic(GetPlayerX()+30-option*1.5, GetPlayerY()-option*2);	
		DrawGraphic(GetPlayerX()-30+option*1.5, GetPlayerY()-option*2);	

           if(GetCommonData("kakusei")>0){
              SetTexture( system );
              SetGraphicAngle(0,0,0);
              let a=GetPoint/10;
              SetGraphicRect( 0, 48, 50, 64 );
              DrawGraphic( GetPlayerX, GetPlayerY+20 );
              SetGraphicRect( 0, 64, a, 80 );
              DrawGraphic( GetPlayerX+a/2-25, GetPlayerY+20 );
           }
	}
	
	@Finalize
	{
		//スクリプト終了時に一回だけ呼び出される部分です
		DeleteGraphic(imgplayer);
	}
}

script_spell Demarcation
{
	//低速移動時は、敵の座標に直に出現
	//高速移動時は、自機の周囲に出現
	let koukazikan = GetArgument();
	let current = GetCurrentScriptDirectory();
	let imgEffect = current~"Spell.png";
	let sebomb = current~"bomb.wav";

	@Initialize
	{
		LoadGraphic(imgEffect);
		SetPlayerInvincibility(koukazikan+40);//無敵時間を設定
                PlaySE(sebomb);
		run();
	}
	@MainLoop
	{
		yield;
	}
	@Finalize
	{
		DeleteGraphic(imgEffect);
	}
	task run()
	{
		effect(0);
		effect(1);
		loop(koukazikan){;yield;}
		End();
	}

	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;}
			loop{alpha-=8; yield;}
		}
		task lineEffect
		{
			loop(120){concentrationLine();loop(1){yield;}}
		}
		alphaState;
		
		let obj = Obj_Create(OBJ_SPELL);//スペルオブジェクトを作成
		ObjEffect_SetTexture(obj, imgEffect);//テクスチャを設定
		ObjEffect_SetRenderState(obj, ADD);//描画方法を設定
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);//プリミティブタイプを設定
		ObjEffect_CreateVertex(obj, 10);//頂点を10個つくる
		
		ascent(i in 0..5)
		{
			let angle = -45+i*90;
			let x;
			let y;
				//高速移動時は広範囲
				x = 512*cos(angle);
				y = 512*sin(angle);
			ObjEffect_SetVertexXY(obj, i*2, x, y);//頂点の座標を設定
			ObjEffect_SetVertexXY(obj, i*2+1, 0, 0);
			ObjEffect_SetVertexColor(obj, i*2, 0, 255, 255, 255);//頂点カラーを設定
		}
		
		while(!Obj_BeDeleted(obj))
		{
			tv-=1;
			if(num == 0){angle+=1;}
			else if(num == 1){angle-=1;}
			ObjEffect_SetAngle(obj, 0, 0, angle);//描画角度を設定
				Obj_SetPosition(obj, GetCenterX, GetCenterY);//座標を設定
				SetIntersectionCircle(GetCenterX, GetCenterY, 300, 1.2, true);//当たり判定を登録
			ascent(i in 0..5)
			{
				ObjEffect_SetVertexUV(obj, i*2,   16*i,tv);//テクスチャの座標を設定
				ObjEffect_SetVertexUV(obj, i*2+1 ,32,  64+tv);
				
				ObjEffect_SetVertexUV(obj, i*2,   16*i, tv);	
				ObjEffect_SetVertexUV(obj, i*2+1, 32,   64+tv);
				
				ObjEffect_SetVertexColor(obj, i*2+1, alpha, 255, 255, 255);		
			}
			yield;
		}
		
	}
	
	task concentrationLine()
	{	//線が集まる感じのエフェクト
		let angle=rand(0, 360);
		let alpha=0;	
		let scale=3;
		
		let obj = Obj_Create(OBJ_SPELL);//スペルオブジェクトを作成
		ObjEffect_SetTexture(obj, imgEffect);//テクスチャを設定
		ObjEffect_SetRenderState(obj, ADD);//描画方法を設定
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);//プリミティブタイプを設定
		ObjEffect_CreateVertex(obj, 3);//頂点を3個つくる
		ObjEffect_SetAngle(obj, 0, 0, angle);//描画角度を設定
		
		//頂点座標を設定
		ObjEffect_SetVertexXY(obj, 0,  0,  10);
		ObjEffect_SetVertexXY(obj, 1, -10, 50);
		ObjEffect_SetVertexXY(obj, 2, 10,  50);
		
		//頂点のUV値を設定
		ObjEffect_SetVertexXY(obj, 0, 10,  10);
		ObjEffect_SetVertexXY(obj, 1,  0,  50);
		ObjEffect_SetVertexXY(obj, 2, 20,  50);
		
		//色設定
		ObjEffect_SetVertexColor(obj, 0, alpha, 255, 0, 255);
		ObjEffect_SetVertexColor(obj, 1, 0, 255, 0, 255);
		ObjEffect_SetVertexColor(obj, 2, 0, 255, 0, 255);
		
		loop(60)
		{
			scale-=3/60;
			alpha+=5;
			ObjEffect_SetVertexColor(obj, 0, alpha, 255, 0, 255);
			ObjEffect_SetScale(obj, scale, scale*3);	
			Obj_SetPosition(obj, GetEnemyX, GetEnemyY);	
			yield;	
		}
		Obj_Delete(obj);
	}


}
