#TouhouDanmakufu
#Title["Ritual of Jealousy"]
#Text[]
#Image[]
#BackGround[Default]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
	#include_function ".\spellcard.txt";
	let type=0;
	let frame=0;
    let array=[26, 41, 163, 231];
    let cutin=GetCurrentScriptDirectory~"img\face02ct.png";
	#include_function ".\parseeanim.txt";
    
task thing{
    let x=GetX;
    let y=GetY;
    let angle=0;
    let counter=0;
    loop{
        angle=atan2(GetPlayerY-y, GetPlayerX-x);
        x+=2*cos(angle);
        y+=2*sin(angle);
        counter++;
        if(counter%13==0){PlaySE(GetCurrentScriptDirectory~"sound\Shot1.wav"); shot(x+rand(-30, 30), y+rand(-30, 30));}
        yield;
    }
}
task shot(x, y){
    let counter=0;
    let obj=Obj_Create(OBJ_SHOT);
    Obj_SetPosition(obj, x, y);
    Obj_SetSpeed(obj, 0);
    Obj_SetAngle(obj, 0);
    ObjShot_SetGraphic(obj, array[rand_int(0, 3)]);
    ObjShot_SetDelay(obj, 30);
    while(Obj_BeDeleted(obj)==false){
        counter++;
        if(counter==60){ascent(i in 0..6){shotb(x, y, GetAngleToPlayer+i*60, i); Obj_Delete(obj);}}
        yield;
    }
}
task shotb(x, y, angle, type){
    let counter=0;
    let obj=Obj_Create(OBJ_SHOT);
    Obj_SetPosition(obj, x+8*cos(angle), y+8*sin(angle));
    Obj_SetSpeed(obj, 0);
    Obj_SetAngle(obj, angle);
    ObjShot_SetGraphic(obj, 77);
    ObjShot_SetDelay(obj, 15);
    while(Obj_BeDeleted(obj)==false){
        counter++;
        if(counter>=30){
            Obj_SetSpeed(obj, 3);
            if(Obj_GetX(obj)>GetClipMaxX+5 || Obj_GetX(obj)<GetClipMinX-5 || Obj_GetY(obj)>GetClipMaxY+5 || Obj_GetY(obj)<GetClipMinY-5){
                if(type%3==0){
                PlaySE(GetCurrentScriptDirectory~"sound\BulletWave.wav");
                CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 0.1, 180+Obj_GetAngle(obj)+rand(-10, 10), 0.01, 1, 240, 0);
                }
                Obj_Delete(obj);
            }
        }
        yield;
    }
}
task shots{
    loop{
        PlaySE(GetCurrentScriptDirectory~"sound\Shot1.wav");
        ascent(i in 0..6){
        CreateShot01(GetX, GetY, 7, GetAngleToPlayer+i*60+30, 227, 5);
        }
        loop(3){yield;}
    }
}

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

		SetMovePosition02(GetCenterX,GetCenterY,20);
		Concentration01(150);
		CutIn(YOUMU, "''Ritual of Jealousy''",cutin,0,0,256,512);
		LoadUserShotData(GetCurrentScriptDirectory~"supershot.txt");

		SetCommonData("ONSPELL", "TRUE");
		spellbonus(10000000);
		timer;
		SetCommonData("timercount", 60*30);
        lastspell;
        PlayMusic(GetCurrentScriptDirectory~"bgm.mp3");
	}
	
	@MainLoop
	{
        survival=1;
		frame++;
		yield;
        if(frame==100){thing;}
        if(frame==200){shots;}
}
	
	@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;
        animcast;
	}
}