#TouhouDanmakufu
#Title[Khayoshia's last spell -Normal-]
#Text[Moo]
#Player[FREE]
#ScriptVersion[2]
#BackGround[User(.\KhaBG.png, 0, 0)]
#BGM[.\BGM.mp3]
script_enemy_main {
    let ImgBoss = GetCurrentScriptDirectory~"KhayoshiaSprite.png";
	let shot = GetCurrentScriptDirectory~"shot.wav";
	let laser = GetCurrentScriptDirectory~"laser.wav";
	let BGM2 = GetCurrentScriptDirectory~"BGM2.mp3";
	let shot2 = GetCurrentScriptDirectory~"shot2.wav";
	let bounce = GetCurrentScriptDirectory~"bounce.wav";
	let declare = GetCurrentScriptDirectory~"cardcall.wav";
	let background = GetCurrentScriptDirectory~"KhaBG2.png";
	let spark = GetCurrentScriptDirectory~"spark.wav";
	let shot7 = GetCurrentScriptDirectory~"shot7.wav";
	let t = 0;
	let v = 255;
	let a = 0;
	let frame2 = 0;
	let angle = GetAngleToPlayer;
	let angleAcc = 0;
	let nu = 0;
	let pie = 1;
	let n = 0;
	let radius = 0;
	let w = -2;
	let num = 20;
	let b = 0;
	let blu = 1;
	let z = 0;
	let spin = 0;
    let frame = -60;
	let n = 0;
#include_function ".\lib\SHOT_REPLACE\shot_replace.dnh"
    @Initialize {
        SetLife(4000);
	LoadMusic(BGM2);
	PlayMusic(BGM2);
        SetTimer(58);
        LoadSE(shot);
	LoadSE(bounce);
	LoadSE(shot7);
	LoadSE(declare);
	LoadSE(spark);
	SetScore(6000000);
	LoadSE(shot2);
	SetShotAutoDeleteClip(150, 200, 150, 150);
	shotinit;
	PlaySE(declare);
	SetEnemyMarker(true);
	SetInvincibility(120);
	SetDamageRate(50, 0);
        SetMovePosition02(GetCenterX, GetClipMinY + 120, 10);
        CutIn(KOUMA, "Desperation "\""The Near-Perfect Attack"\", GetCurrentScriptDirectory~"KhayoshiaSprite2.png", 40, 10, 256, 256);
        LoadGraphic(ImgBoss);
	SetTexture(ImgBoss);
	LoadGraphic(background);
    	

    }

	@MainLoop{
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 30);
		frame++;
		yield;

		y+=0.5;
		n++;
		

		


		w = rand(1,2);

		if(frame == 60){
		let a = 0;
	let num = 45;
	loop(num){
	PlaySE(shot);
		CreateShotA(1, GetX, GetY, 0);
		SetShotDataA(1, 0, 0, GetAngleToPlayer+a, 3, 0.2, 3, GREEN03);
		SetShotDataA(1, 120, w, NULL, -3, 0.2, 3, RED12);
		SetShotDataA(1, 158, w, NULL, -10, -1, 3, PURPLE21);
		SetShotDataA(1, 234, w, NULL, 0, -0.1, 2, AQUA22);
		SetShotDataA(1, 245, w, NULL, 0, -0.1, -7, BLUE21);
		SetShotDataA(1, 280, w, NULL, 0.2, -0.03, 1, ORANGE21);
		SetShotDataA(1, 320, w, NULL, 0, 0.03, -10, WHITE22);
		SetShotDataA(1, 340, w, NULL, 0, 0.03, 3, RED57);
		a+=360/num;
		

FireShot(1);
}
}	
if(frame == 90 || frame == 120 || frame == 150 || frame == 180 || frame == 210 || frame == 240){
a = -2;
let num = 20;
	loop(num){
		PlaySE(shot);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+30, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+60, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+90, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+120, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+150, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+180, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+210, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+240, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+270, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+300, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+330, RED52, 0);
		CreateShot01(GetX, GetY, 3-a, GetAngleToPlayer+360, RED52, 0);
		a+=1/12;
		angle+=360/num;
}
}

if(frame == 90 && GetEnemyLife<2000){
CreateLaser01(GetClipMaxX, GetPlayerY, 3, 180, 150, 40, YELLOW21, 30);
CreateLaser01(GetClipMinX, GetPlayerY, 3, 0, 150, 40, YELLOW21, 30);
}


if(frame == 180 && GetEnemyLife<2000){
CreateLaser01(GetClipMaxX, GetPlayerY, 3, 180, 150, 40, YELLOW21, 30);
CreateLaser01(GetClipMinX, GetPlayerY, 3, 0, 150, 40, YELLOW21, 30);
}
if(frame == 270 && GetEnemyLife<2000){
CreateLaser01(GetClipMaxX, GetPlayerY, 3, 180, 150, 40, YELLOW21, 30);
CreateLaser01(GetClipMinX, GetPlayerY, 3, 0, 150, 40, YELLOW21, 30);
}

if(frame == 360 && GetEnemyLife<2000){
CreateLaser01(GetClipMaxX, GetPlayerY, 3, 180, 150, 40, YELLOW21, 30);
CreateLaser01(GetClipMinX, GetPlayerY, 3, 0, 150, 40, YELLOW21, 30);
}
if(frame == 300 && GetEnemyLife<3000){
let a = 0;
let num = 16;
	loop(num){
CreateLaser01(GetX, GetY, 8, GetAngleToPlayer+30+a, 200, 60, RED52, 0);
a+=360/num;
}
ExMove;

}
if(frame == 361){
frame=0;
}


}


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



sub ExMove { ExMove_(GetCenterY-100, 30, rand(15, 30)); }
function ExMove_(height, ud, length) {
	
	if(GetPlayerX < GetX) {
		if(GetX - length <= GetClipMinX) {
			SetMovePosition03(GetX + length, GetY, 5, 11);
		} else {
			SetMovePosition03(GetX - length, GetY, 5, 11);
		}
	} else {
		if(GetX + length >= GetClipMaxX) {
			SetMovePosition03(GetX - length, GetY, 5, 11);
		} else {
			SetMovePosition03(GetX + length, GetY, 5, 11);
		}
	}
}
	// Movement functions for the boss to move towards the player.


task Hitbox{
		let obj_id = Obj_Create(OBJ_SHOT); 
		Obj_SetPosition(obj_id, GetCenterX, GetCenterY);
		ObjShot_SetGraphic(obj_id, PURPLE03);
		Obj_SetSpeed(obj_id, 0); Obj_SetAngle(obj_id, 0); ObjShot_SetDelay(obj_id, 600000);
		ObjShot_SetBombResist(obj_id, true);
}

  @DrawLoop {
		SetTexture(ImgBoss);
                SetRenderState(ALPHA);
		SetGraphicRect(1, 1, 512, 512);
                SetAlpha(255);  
                SetGraphicScale(0.2,0.2);
                SetGraphicAngle(0,0,n);
		DrawGraphic(GetEnemyX,GetEnemyY);
               	
                      
 
               
    }
let y = 0;
@BackGround{
   SetTexture(background);
	
   SetGraphicRect(0, 0, 1066, 1031);
	SetGraphicScale(1, 1);
	SetGraphicAngle(0, 0, y);
   DrawGraphic(GetCenterX, GetCenterY);
	
  
}
let v = 0;
    @Finalize {
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
	CreateItem(ITEM_SCORE,GetX + rand(-20,20),GetY + rand(-20,20));
        DeleteGraphic(ImgBoss);
    }
}
