#TouhouDanmakufu
#Title[Halloween 2009 - Illusion Phantom darkness (Hard)]
#Text[Wait, weren't these fake ghosts? What is going on?]
#Player[FREE]
#ScriptVersion[2]
#Image[.\system\afro01.png]
#BGM[.\Music\spook.mp3]

script_enemy_main {
    let CSD        = GetCurrentScriptDirectory;

    let ImgBoss = CSD ~ "system\spook.png";
    let ShotData   = CSD ~ "system\supershot.txt";

    let cut = CSD ~ "system\spookcut.png";
    let bg = CSD ~ "system\bg.png";
    let bg2 = CSD ~ "system\bg2.png";
    let bg3 = CSD ~ "system\bg3.png";
    let bg4 = CSD ~ "system\bg4.png";
    let bg5 = CSD ~ "system\bg5.png";
    let lay = CSD ~ "system\lay.png";

    let dark = CSD ~ "system\darkness.png";

    let schijn = 0;    
    let schijn2 = 0;
    let schijn3 = 0;
    let schijn4 = 0;
    let schijn5 = 0;
    let schuif = 0;
    let framer = 1;
    let alph = 255;

    let spoken = 1;
    let verdwijn = 0;
    let moving = 0;

    @Initialize {
        SetLife(400);
        SetTimer(80);
	SetScore(350000);
	SetDamageRateEx(10,4,5,5);
	MagicCircle(false);
        SetInvincibility(240);
	SetMovePosition01(GetCenterX,GetCenterY-100,5);
	SetEnemyMarker(true);

        LoadGraphic(ImgBoss);
        LoadGraphic(bg);
	LoadGraphic(bg2);
	LoadGraphic(bg3);
	LoadGraphic(bg4);
	LoadGraphic(bg5);
	LoadGraphic(lay);
	LoadGraphic(dark);
	LoadGraphic(cut);

	LoadUserShotData(ShotData);

	CutIn(YOUMU,"Illusion Phantom darkness",cut,0,0,0,0);
	cutin("KANAKO",cut,0,0,350,348);
	ConcentrationX;
	hpBar;
	hitboxer;
	Hoofd;	
	darkness;
    }

    @MainLoop {
	if(verdwijn==1){}
	if(verdwijn==0){
		SetCollisionA(GetX, GetY, 32);
        	SetCollisionB(GetX, GetY, 32);
	}
	yield;
    }

    @DrawLoop {

	if(verdwijn==1){ if(alph>=0){alph-=10;} alph = 0;}
	if(verdwijn==0){ DiscoCircle; if(alph<250){alph+=10;} }

	SetTexture(ImgBoss);
	SetAlpha(alph);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.7,0.7);
	SetRenderState(ALPHA);
	SetGraphicRect(0,0,128,128);
       	DrawGraphic(GetX, GetY);

	// Focus anim
	Focus;

	DrawText("Unknown Spirit",35,30,12,255);
    }

    @BackGround{
	SetTexture(bg);
	SetGraphicRect(0,0,768,768);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.7,0.7);
	SetRenderState(ALPHA);
	SetAlpha(schijn);
        DrawGraphic(GetCenterX, GetCenterY);

	SetTexture(bg2);
	SetGraphicRect(0,0,768,768);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.7,0.7);
	SetRenderState(ALPHA);
	SetAlpha(schijn2);
        DrawGraphic(GetCenterX, GetCenterY);

	SetTexture(bg3);
	SetGraphicRect(0,0,768,768);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.7,0.7);
	SetRenderState(ALPHA);
	SetAlpha(schijn3);
        DrawGraphic(GetCenterX, GetCenterY);

	SetTexture(bg4);
	SetGraphicRect(0,0,768,768);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.7,0.7);
	SetRenderState(ALPHA);
	SetAlpha(schijn4);
        DrawGraphic(GetCenterX, GetCenterY);

	SetTexture(bg5);
	SetGraphicRect(0,0,768,768);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(0.7,0.7);
	SetRenderState(ALPHA);
	SetAlpha(schijn5);
        DrawGraphic(GetCenterX, GetCenterY);

	if(framer==1){
		if(schijn<250){schijn+=5;}
		if(schijn==250){framer++;}
	}
	if(framer==2){
		if(schijn<250){schijn+=5; schijn5-=5;}
		if(schijn==250){framer++;}
	}
	if(framer==3){
		if(schijn2<250){schijn2+=5; schijn-=5;}
		if(schijn2==250){framer++;}
	}
	if(framer==4){
		if(schijn3<250){schijn3+=5; schijn2-=5;}
		if(schijn3==250){framer++;}
	}
	if(framer==5){
		if(schijn4<250){schijn4+=5; schijn3-=5;}
		if(schijn4==250){framer++;}
	}
	if(framer==6){
		if(schijn5<250){schijn5+=5; schijn4-=5;}
		if(schijn5==250){framer=2;}
	}

	SetTexture(lay);
	SetGraphicRect(0,0,256,20000);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(2,2);
	SetRenderState(ALPHA);
	SetAlpha(105);
        DrawGraphic(GetCenterX, GetCenterY+schuif);

	SetTexture(lay);
	SetGraphicRect(0,0,20000,256);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(2,2);
	SetRenderState(ALPHA);
	SetAlpha(85);
        DrawGraphic(GetCenterX+schuif, GetCenterY);

	schuif+=2;
    }

    @Finalize {	
	PlaySE(CSD ~"SFX\se_spookdood.wav");
	if(GotSpellCardBonus){
                PlaySE(CSD ~ "SFX\se_cardget.wav");
        }
    }

    task Hoofd{
	wait(120);
	PlaySE(CSD ~"SFX\se_spooklaugh.wav");
	ConcentrationH;
	floating;
	ghosts;
	beweeg;
	vuur3;
	pijn;
	tijd;
	yield;
    }

    task beweeg { 
	let x = 0;
	loop{	
		moving = 1;
		wait(30);
		SetMovePosition03(GetCenterX-120,GetCenterY-120,30,80);
		while(x<8){wait(10); vuur; x++;}
		x = 0;
		wait(30);
		SetMovePosition03(GetCenterX+120,GetCenterY-120,30,80);
		while(x<8){wait(10); vuur; x++;}
		x = 0;
		wait(30);
		SetMovePosition03(GetCenterX,GetCenterY-80,30,80);
		wait(120);
		moving = 1;

		vuur2;
		wait(180);
		yield;
	}
    }

    task vuur3{
	let speed = 1.0;
	loop{
		wait(15);
		CreateShotA(98,rand(GetClipMinX,GetCenterX+30),GetClipMaxY,40);
		SetShotDataA(98,0,0,rand(260,280),0,0.50,0.5,rand(55,57));
		FireShot(98);

		CreateShotA(99,rand(GetCenterX-30,GetClipMaxX),GetClipMaxY,40);
		SetShotDataA(99,0,0,rand(260,280),0,0.50,0.5,rand(55,57));
		FireShot(99);

		yield;
	}
    }

    task vuur{
	let x = 1;
	let speed = 1.0;

	while(x<8){
		PlaySE(CSD ~"SFX\se_tan00.wav");
		CreateShotA(x,GetEnemyX,GetEnemyY,10);
		SetShotDataA(x,0,0,GetAngleToPlayer,0,0.50,speed,202);
		FireShot(x);
		speed+=0.2;
		x++;
	}
    }

    task vuur2{
	let x = 0;
	let dir = 0;
	loop(6){
		while(x<20){
			PlaySE(CSD ~"SFX\se_kira00.wav");
			CreateShot01(GetEnemyX+40*cos(dir),GetEnemyY+40*sin(dir),2,dir,232,0);
			dir+=360/20;
			x++;
		}
		dir+=11;
		x=0;
		wait(30);
	}
    }
    // darkness like Mystia.
    task darkness{
	let obj = Obj_Create(OBJ_EFFECT);
	let alphret = 0;


	ObjEffect_SetTexture(obj,dark);
	
	Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
	ObjEffect_SetRenderState(obj,ALPHA);
	ObjEffect_SetLayer(obj,5);

	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj, 4);

          // Second vertex (number 0)
          ObjEffect_SetVertexXY(obj, 0, -600, -600);
          ObjEffect_SetVertexUV(obj, 0, 0, 0);

          // Second vertex (number 1)
          ObjEffect_SetVertexXY(obj, 1, 600, -600);
          ObjEffect_SetVertexUV(obj, 1, 1200, 0);
 
          // Third vertex (number 2)
          ObjEffect_SetVertexXY(obj, 2, -600, 600);
          ObjEffect_SetVertexUV(obj, 2, 0, 1200);

          // Fourth vertex (number 3)
          ObjEffect_SetVertexXY(obj, 3, 600, 600);
          ObjEffect_SetVertexUV(obj, 3, 1200, 1200);

	while(!Obj_BeDeleted(obj)){
		if(alphret<255){alphret++;}

	  	ObjEffect_SetVertexColor(obj,0,alphret,255,255,255);
	 	ObjEffect_SetVertexColor(obj,1,alphret,255,255,255);
	  	ObjEffect_SetVertexColor(obj,2,alphret,255,255,255);
	  	ObjEffect_SetVertexColor(obj,3,alphret,255,255,255);

		Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
		yield;
	}
 
    }

    task floating{
	wait(120);
	loop{
		if(moving==1){yield;}
		if(moving==0){
			loop(18){ wait(2); SetY(GetY-1.2); }
			wait(10);
			loop(18){ wait(2); SetY(GetY+1.2); }	
		}	
		yield;
	}
    }

    task ghosts{
	let r = 0;
	let s = 0;
	let dir = 0;
	let x = 0;
	let t = 0;

	loop{
		if(spoken==0){yield;}
			if(spoken==1){
			while(x<36){
				wait(4);
				CreateShotA(97,GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir-45),30);
				SetShotDataA(97,0,0,dir,0,0,0,WHITE01);
				SetShotKillTime(97,0);
				FireShot(97);

				CreateShotA(98,GetEnemyX+r*cos(dir-180),GetEnemyY+r*sin(dir),30);
				SetShotDataA(98,0,0,dir,0,0,0,WHITE01);
				SetShotKillTime(98,0);
				FireShot(98);

				CreateShotA(99,GetEnemyX+r*cos(-dir+45),GetEnemyY+r*sin(dir+180),30);
				SetShotDataA(99,0,0,-dir,0,0,0,WHITE01);
				SetShotKillTime(99,0);
				FireShot(99);

				if(s==0){r+=10; if(r==80){s=1;}}
				if(s==1){r-=1; if(r==40){s=2;}}
				if(s==2){r+=1; if(r==120){s=1;}}

				dir+=360/36;
				x++;
			}
			x = 0;
			dir = 0;
		}
		yield;
	}
    }

    // brr geluid
    task pijn{	
    let brr = GetEnemyLife / 100 * 7;	
    	loop{
   		let hp = GetEnemyLife;
		wait(3.3);
		let nhp = GetEnemyLife;
		if(nhp<hp && nhp>brr){PlaySE(GetCurrentScriptDirectory~"SFX\se_damage00.wav");}
		if(nhp<hp && nhp<brr){PlaySE(GetCurrentScriptDirectory~"SFX\se_damage01.wav");}
		yield;
 	}
    }
 
    // timeout sfx
    task tijd{
	loop{
		wait(60);
		if(GetTimer<=10){PlaySE(CSD~"SFX\se_timeout.wav");}
		if(GetTimer<=5){PlaySE(CSD~"SFX\se_timeout2.wav");}
		yield;
	}

    }

    #include_function ".\hitboxer.txt"
    #include_function ".\spellcircle.txt"
    #include_function ".\Hwfunctions.txt"
    #include_function ".\hudeffects.txt"
    #include_function ".\cutin.txt"
}