#BackGround[Default]
#Player[FREE]
#ScriptVersion[2]
script_enemy_main{
	#include_function ".\lib\lib_anime_Sunnymilk.txt"

	let imgBoss=GetCurrentScriptDirectory~"img\Boss\dot_sunnymilk.png";
        #include_function ".\lib\lib_Bosscommon.txt"
        #include_function ".\lib\lib_SpellBouns.txt"
	let bounus=3000000;		//スペル基本得点

    	@Initialize
        {
	  SetLife(500);
	  SetDamageRate(0,0);
	  SetMovePosition03(cx,cy-120,10,10);
	  SetAction(ACT_MOVE,60);
	  SetCountDown;
          Tmain;
	}
	
	@MainLoop
	{  
		if(OnEvent()==true){break;}
                yield;
                SetCollisionA(GetX(),GetY(),32);
		SetCollisionB(GetX(),GetY(),16);
		if(GetHitCount<=0){return;}
		if(Hitframe%4==0){
			PlaySE(Hit);
		}
		Hitframe++;
        } 
	
	@Finalize
	{       
		let Power=int(GetCommonData("Power")/100);
		loop(20){
			CreateShotFromScript("I_Item",GetX+rand(-70,70),GetY+rand(-70,70),0,0,0,1);
		}
		if(Power>=5){
			loop(10){
				CreateShotFromScript("TenItem",GetX+rand(50,-50),GetY+rand(50,-50),0,0,0,0);
			}
		}
		else if(Power>=3){
			loop(10){
				CreateShotFromScript("P_Item" ,GetX+rand(50,-50),GetY+rand(50,-50),0,0,0,0);
			}
			loop(4){
				CreateShotFromScript("TenItem",GetX+rand(50,-50),GetY+rand(50,-50),0,0,0,0);
			}
		}
		else{
			loop(20){
				CreateShotFromScript("P_Item" ,GetX+rand(50,-50),GetY+rand(50,-50),0,0,0,0);
			}
			loop(2){
				CreateShotFromScript("TenItem",GetX+rand(50,-50),GetY+rand(50,-50),0,0,0,0);
			}
		}

		SetCommonData("SpellCade",false);
		if(GetMissCountInThisSpell == 0 && GetBombCountInThisSpell == 0 && IsTimeOut == false){//スペル取得した時
			PlaySE("seGetSpellCardBonus.wav");			//効果音
			SetCommonData("GetSpell",true);				//スペルを取得したかどうか
		}
	}

	@DrawLoop
	{//描画
                SetColor(255,255,255);
		SetRenderState(ALPHA);
		DrawBoss( imgBoss );
	}

        task Tmain
        {
		yield;
		SpellCutName(0,128,bounus);		//カード名
		SetMovePosition03(cx,cy-100,10,10);
		SetInvincibility(60);
		SetTimer(41);
		loop(60){yield;}
	 	SetDamageRate(10,1);
		SetAction(ACT_SPELL,60);
		loop(60){yield;}
		let Pangle=0;
		loop(20){yield;}
		loop{//3回やると長い溜め
			loop(3){
				SetAction(ACT_SHOT_A,90);//溜め
				Shot01(12);
				SetAction(ACT_SHOT_B,90);//発射
				loop(4){yield}
				Shot02;
				loop(80){yield}			
				Tmove();
				loop(40){yield;}
			}
			SetAction(ACT_SHOT_A,120);//溜め
			Shot01(25);
			loop(3){
				SetAction(ACT_SHOT_B,90);//発射
				loop(5){yield}
				Shot02();
				Tmove();
				loop(10){yield}
			}
			loop(60){yield}
			Tmove();
			loop(60){yield}			
		}



		function Shot01(wait){//光エフェクト
			PlaySE(Lazer3);
			let X = GetEnemyX-7;
			let Y = GetEnemyY-20;
			let L = 0;
			let MaxL = Length(cx, X, cmy-10, Y);
			let obj = Obj_Create(OBJ_LASER);  
			Obj_SetX(obj, cx);//画面外
			Obj_SetY(obj, cmy-10);
			Obj_SetAngle(obj,atan2(Y - cmy-10, X - cx) );//サニーの手へ
			ObjShot_SetGraphic(obj,186);
        	        Obj_SetAlpha(obj,220);
			ObjLaser_SetLength(obj,L);
			ObjLaser_SetWidth(obj,10);
			ObjLaser_SetSource(obj,false);
			loop(120){
				ObjLaser_SetLength(obj,L);
				L += 4;
				if(MaxL <= L){break;}
				yield;
			}
			loop(wait){
				effectshot(ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj),10,2);
				Effect(ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj));
				Effect(ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj));
				loop(4){yield;}
			}
			Obj_Delete(obj);
		}

		function Length(x1,x2,y1,y2){//二点間の距離
			let X = (x2 - x1) ^ 2;
			let Y = (y2 - y1) ^ 2;
			return (X + Y) ^ 0.5;
		}

		task Effect(X,Y){//集中エフェクト
			let angle = rand(0,360);
			let obj = Obj_Create(OBJ_LASER);  
			Obj_SetX(obj, X + 120 * cos(angle) );
			Obj_SetY(obj, Y + 120 * sin(angle) );
			Obj_SetAngle(obj,angle);//サニーの手へ
			ObjShot_SetGraphic(obj,2);
        	        Obj_SetAlpha(obj,220);
			ObjLaser_SetLength(obj,20);
			ObjLaser_SetWidth(obj,10);
			ObjLaser_SetSource(obj,false);
			let L =Length(ObjLaser_GetEndX(obj),X,ObjLaser_GetEndY(obj),Y);
			while( !Obj_BeDeleted(obj) ){
				Obj_SetX(obj, X + L * cos(angle) );
				Obj_SetY(obj, Y + L * sin(angle) );		
				if(L<=10){break;}
				L -= 6;
				yield;
			}
			Obj_Delete(obj);
		}

		task Shot02(){//ショット
			PlaySE(Lazer2);
			let X = GetEnemyX - 7;
			let Y = GetEnemyY - 5;
			if(Level == 1){Shot02aa(X,Y);}
			else{Shot02ab(X,Y);}
			Shot02b(X,Y);
		}

		task Shot02aa(X,Y){//hard
			let speed = 6;
			loop(5){
				let angle = atan2(GetPlayerY - Y, GetPlayerX - X) - 80;
				loop(11){
					CreateShot01(X,Y,speed,angle,35,12);
					angle += 160 / 10;
				}
				speed -= 1.0;
			}
			let speed = 6;
			loop(5){
				let angle = atan2(GetPlayerY - Y, GetPlayerX - X) - 3;
				loop(2){
					CreateShot01(X,Y,speed,angle,33,12);
					angle += 6;
				}
				speed -= 1.0;
			}
			speed = 5.5;
			loop(5){
				let angle = atan2(GetPlayerY - Y, GetPlayerX - X) - 80;
				loop(10){
					CreateShot01(X,Y,speed,angle,34,12);
					angle += 160 / 9;
				}
				speed -= 1.0;
			}
		}
		task Shot02ab(X,Y){//easy
			let speed = 4;
			loop(3){
				let angle = atan2(GetPlayerY - Y, GetPlayerX - X) - 80;
				loop(7){
					CreateShot01(X,Y,speed,angle,34,12);
					angle += 160 / 6;
				}
				speed -= 1.0;
			}
			speed = 3.5;
			loop(3){
				let angle = atan2(GetPlayerY - Y, GetPlayerX - X) - 80;
				loop(6){
					CreateShot01(X,Y,speed,angle,34,12);
					angle += 160 / 5;
				}
				speed -= 1.0;
			}
		}


		task Shot02b(X,Y){//レーザー
			let angle = atan2(GetPlayerY - Y, GetPlayerX - X) - 1;
			loop(3){
				CreateLaser01(X, Y, 8, angle, 190, 10, 9, 0);
				angle += 1;
			}
		}
		

	}

        task Tmove(){
                 if(GetX>=cx+100){SetMovePositionRandom01(40,20,1.5,GetX-40,cy-120,GetX-41,cy-60);}
                 else if(GetX<=cx-100){SetMovePositionRandom01(40,20,1.5,GetX+40,cy-120,GetX+41,cy-60);}
                 else if(GetX>=GetPlayerX){SetMovePositionRandom01(40,20,1.5,GetX-40,cy-120,GetX-41,cy-60);}
                 else if(GetX<=GetPlayerX){SetMovePositionRandom01(40,20,1.5,GetX+40,cy-120,GetX+41,cy-60);}
                 SetAction( ACT_MOVE, 190 );
        }

        task effectshot(X,Y,delay,col){
                  CreateShotA(1,X,Y,delay);
                  SetShotDataA(1,0,0,0,0,0,0,col);
                  SetShotKillTime(1,0);
                  FireShot(1);
        }

}
#include_script".\Item01.txt"
#include_script".\Item02.txt"
#include_script".\Item04.txt"

