#TouhouDanmakufu
#Title["Subterranean Nuclear Fission"]
#Text[]
#Image[]
#BackGround[Default]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
	#include_function ".\spellcard.txt";
	let dtype=0;
	let frame=0;
    let cutin=GetCurrentScriptDirectory~"img\face06ct.png";
    let imgWarning=GetCurrentScriptDirectory~"img\stg6enm2.png";
    let ball=GetCurrentScriptDirectory~"img\stg6enm3.png";
	#include_function ".\utsuhoanim.txt";

function CreateEffect(tex, layer){
    let obj=Obj_Create(OBJ_EFFECT);
    ObjEffect_SetTexture(obj, tex);
    ObjEffect_SetLayer(obj, layer);
    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_CreateVertex(obj, 4);
    return obj;
}
function EffectXY(id, left, top, right, bottom){
    ObjEffect_SetVertexXY(id, 0, left, top);
    ObjEffect_SetVertexXY(id, 1, right, top);
    ObjEffect_SetVertexXY(id, 2, left, bottom);
    ObjEffect_SetVertexXY(id, 3, right, bottom);
}
function EffectUV(id, left, top, right, bottom){
    ObjEffect_SetVertexUV(id, 0, left, top);
    ObjEffect_SetVertexUV(id, 1, right, top);
    ObjEffect_SetVertexUV(id, 2, left, bottom);
    ObjEffect_SetVertexUV(id, 3, right, bottom);
}
function EffectColor(id, vertex, alpha, r, g, b){
    ascent(i in 0..vertex){ObjEffect_SetVertexColor(id, i, alpha, r, g, b);}
}
task Warning {
    PlaySE(GetCurrentScriptDirectory~"sound\warning.wav");
    let symbol=CreateEffect(imgWarning, 7);
    EffectXY(symbol, -256, -64, 256, 64);
    Obj_SetPosition(symbol, GetCenterX, GetCenterY);
    
    let warning1=CreateEffect(imgWarning, 7);
    EffectXY(warning1, -256, -20, 256, 20);
    Obj_SetPosition(warning1, GetCenterX, GetCenterY+80);
    
    let warning2=CreateEffect(imgWarning, 7);
    EffectXY(warning2, -256, -20, 256, 20);
    Obj_SetPosition(warning2, GetCenterX, GetCenterY-80);
    
    let counter = 0;
    ascent(i in 0..31){
      EffectUV(symbol, counter, 0, counter+512, 128);
      EffectUV(warning1, -counter, 216, -counter+512, 256);
      EffectUV(warning2, -counter, 216, -counter+512, 256);
      EffectColor(symbol, 4, 255/30*i, 255, 255, 255);
      EffectColor(warning1, 4, 255/30*i, 255, 255, 255);
      EffectColor(warning2, 4, 255/30*i, 255, 255, 255);
      counter++;
      yield;
    }
    loop(88){
      EffectUV(symbol, counter, 0, counter+512, 128);
      EffectUV(warning1, -counter, 216, -counter+512, 256);
      EffectUV(warning2, -counter, 216, -counter+512, 256);
      counter++;
      yield;
    }
    descent(i in 0..31){
      EffectUV(symbol, counter, 0, counter+512, 128);
      EffectUV(warning1, -counter, 216, -counter+512, 256);
      EffectUV(warning2, -counter, 216, -counter+512, 256);
      EffectColor(symbol, 4, 255/30*i, 255, 255, 255);
      EffectColor(warning1, 4, 255/30*i, 255, 255, 255);
      EffectColor(warning2, 4, 255/30*i, 255, 255, 255);
      counter++;
      yield;
    }
}
task shotball(angle){
    PlaySE(GetCurrentScriptDirectory~"sound\kaboom.wav");
    let counter=-1;
    let scale=0.2;
    let angleb=angle;
    let obj=CreateEffect(ball, 7);
    Obj_SetPosition(obj, GetX, GetY);
    ObjEffect_SetRenderState(obj, ADD);
    EffectUV(obj, 0, 0, 256, 256);
    EffectXY(obj, -128, -128, 128, 128);
    while(Obj_BeDeleted(obj)==false){
        yield;
            if( ((Obj_GetX(obj)-GetPlayerX)^2+(Obj_GetY(obj)-GetPlayerY)^2)^0.5<128*scale*0.7){ShootDownPlayer;}
        ObjEffect_SetScale(obj, scale, scale);
        if(Obj_GetX(obj)<GetClipMinX || Obj_GetX(obj)>GetClipMaxX){
            if(counter==-1){
            counter=0;
            PlaySE(GetCurrentScriptDirectory~"sound\kaboom.wav");
            }
        }
        if(counter==-1){Obj_SetPosition(obj, Obj_GetX(obj)+cos(angle), Obj_GetY(obj)+sin(angle));}
        if(counter>=0){
            if(scale<1){scale+=0.01;} 
            counter++;
            if(counter%60==0){
                angleb+=angle*7;
                PlaySE(GetCurrentScriptDirectory~"sound\Shot1.wav");
                ascent(i in 0..20){ CreateShot01(Obj_GetX(obj)+20*cos(angleb+i*18), Obj_GetY(obj)+20*sin(angleb+i*18), 1, angleb+i*18, 239, 20);}
            }
        }
    }
}
task ballb{
    PlaySE(GetCurrentScriptDirectory~"sound\kaboom.wav");
    let counter=-1;
    let scale=0.1;
    let v=rand(2, 4);
    let obj=CreateEffect(ball, 7);
    Obj_SetPosition(obj, rand(GetClipMinX+25, GetClipMaxX-25), GetClipMinY+rand(50, 140));
    ObjEffect_SetRenderState(obj, ADD);
    EffectUV(obj, 0, 0, 256, 256);
    EffectXY(obj, -128, -128, 128, 128);
    while(Obj_BeDeleted(obj)==false){
        yield;
            if( ((Obj_GetX(obj)-GetPlayerX)^2+(Obj_GetY(obj)-GetPlayerY)^2)^0.5<128*scale*0.7){ShootDownPlayer;}
        ObjEffect_SetScale(obj, scale, scale);
        if(counter==-1){
        if(scale<0.8){scale+=0.1;}else{counter=0;}
        }
        if(counter>=0){
        Obj_SetY(obj, Obj_GetY(obj)+v);
            if(scale>0.2){scale-=0.005;} 
            counter++;
        }
    }
}

	@Initialize
	{
		SetInvincibility(200);
		SetLife(500);
		SetDamageRate(10,10);
        LoadGraphic(imgWarning);
        LoadGraphic(ball);
        LoadGraphic(cutin);

		SetMovePosition02(GetCenterX,GetCenterY-100,20);
		Concentration01(150);
		CutIn(YOUMU, "''Subterranean Nuclear Fission''",cutin,0,0,425,512);
		LoadUserShotData(GetCurrentScriptDirectory~"supershot.txt");

		SetCommonData("ONSPELL", "TRUE");
		spellbonus(10000000);
		timer;
		SetCommonData("timercount", 60*120);
        lastspell;
        PlayMusic(GetCurrentScriptDirectory~"bgm.mp3");
	}
	
	@MainLoop
	{
		SetCollisionA(GetX(),GetY(),24);
		SetCollisionB(GetX(),GetY(),24);
		frame++;
		yield;
        if(frame==50){Warning;}
        if(frame==50){
            shotball(-30);
            shotball(30);
            shotball(150);
            shotball(210);
        }
        if(frame>300 && frame%30==0){ballb; ballb;}
}
	
	@Finalize
	{
		AddScore(spellbonustotal);
		if(spellbonustotal>0)
		{
		PlaySE(GetCurrentScriptDirectory~"sound\se_cardget.wav");
		loop(50)
		{
			CreateItem(ITEM_SCORE,GetX+rand(-100,100),GetY+rand(20,100));
		}
		}else{
		loop(15)
		{
			CreateItem(ITEM_SCORE,GetX+rand(-100,100),GetY+rand(20,100));
		}
		}
		SetCommonData("ONSPELL", "FALSE");
        if(spellbonustotal==0){SetCommonData("REDO", "TRUE");}
	}
	
	@DrawLoop
	{
        bg;
        if(dtype==1){animcast;}else{anim2;}
	}
}