#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Nonspell 1"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]
#BGM["./../sfx/Cold Whirlwind of the Forest.ogg"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

let GetDifficulty = 2;
let GetStage = 4;

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(3000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(40); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 180, 75, 75);
	PlayEnemyHitSound(objEnemy, 1);

	let Eirin = GetCurrentScriptDirectory~"sprites/eirin.png";
	ObjPrim_SetTexture(objEnemy, Eirin); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 256+128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 256+128, 128*3);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	ObjMove_SetPosition(objEnemy, GetCenterX, GetClipMinY+120);

	SetAutoDeleteObject(true);
	attack1;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	PlaySoundEffect("end",60,GetCenterX);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
	SetShotAutoDeleteClip(32, 40, 32, 32);
}

task attack1{
	SetShotAutoDeleteClip(15, 40, 15, 15);
	let angle = 90;
	wait(15);
	ObjAnim_SetAttackA1(objEnemy, true);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 136, 69, 19, BLEND_ADD_ARGB, 25);
	wait(45);
	let waittime = [32, 28, 32, 28][GetDifficulty] - [0, 1, 1, 2, 2, 3][GetStage];
	let numsprites = [1, 1, 2, 2][GetDifficulty];
	loop(numsprites){
		LightSprite(angle, 1, [1.8, 1.8, 1.85, 1.9, 1.95, 2][GetStage], waittime, 65);
		angle+=360/numsprites;
	}
	angle+=90;
	wait(waittime*4);
	loop(numsprites){
		LightSprite(angle, -1, [2.55, 2.55, 2.6, 2.65, 2.7, 2.75][GetStage], waittime, 90);
		angle+=360/numsprites;
	}
	loop{
		wait(195);
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(45);
		ExMove(objEnemy, ObjMove_GetX(GetPlayerObjectID), rand(40, 80), rand(110, 130), 120, 3, 15);
		wait(60);
		ObjAnim_SetAttackA1(objEnemy, true);
	}	
}

task LightSprite(ang, dir, bspeed, yieldduration, radius){
	let spriteframe = 0;
	let spriteangle = 0;
	let rad = 0;
	let ang2 = ang;
	let ang3 = 0;
	let alpha = 0;
	let radacc = 2.275;
	let initradacc = radacc;
	let objSprite = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(objSprite);
	ObjMove_SetPosition(objSprite, GetEnemyX, GetEnemyY);
	ObjEnemy_SetLife(objSprite, 1);
	ObjEnemy_SetDamageRate(objSprite, 100, 100);
	PlaySoundEffect("shadow", 100, GetEnemyX);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		if(rad<radius){ rad+=radius/45; }
		loop(1){
			let obj = CreateShotA1(ObjMove_GetX(objSprite), ObjMove_GetY(objSprite), 0, 90, DOT[ORANGE], 15);
			ObjShot_SetDeleteFrame(obj, 0);
			}
		ObjMove_SetPosition(objSprite, GetEnemyX+rad*1*cos(ang2), GetEnemyY+rad*1*sin(ang2));
		ang2+=2.6333*dir;
		spriteframe++;
		if(spriteframe%yieldduration==0){
			PlaySoundEffect("shot3", 100, GetEnemyX);
			ShootShape(ObjMove_GetX(objSprite), ObjMove_GetY(objSprite), 35, bspeed, ang3, 4, [3, 3, 4, 4, 5, 5][GetStage], DIAMOND[ORANGE], 10);
		}
		yield;
		}
	Obj_Delete(objSprite);
}

function ShootShape(spawnX, spawnY, radius, v, angle, num, bside, graphic, delay) {
    ascent(i in 0..num) {
        let sx_ = spawnX+cos(angle+i*360/num);
        let sy_ = spawnY+sin(angle+i*360/num);
        let sx = spawnX+radius*cos(angle+i*360/num);
        let sy = spawnY+radius*sin(angle+i*360/num);
        let sxN_ = spawnX+cos(angle+(i+1)*360/num);
        let syN_ = spawnY+sin(angle+(i+1)*360/num);
        let sxN = spawnX+radius*cos(angle+(i+1)*360/num);
        let syN = spawnY+radius*sin(angle+(i+1)*360/num);
        CreateDNHShotA1(sx, sy, v*(((sx_-spawnX)^2+(sy_-spawnY)^2)^0.5), atan2(sy-spawnY,sx-spawnX), graphic, delay);
        ascent(j in 0..bside) {
            let toAngle = atan2(syN_-sy_,sxN_-sx_);
            let toDist_ = (((sxN_-sx_)^2+(syN_-sy_)^2)^0.5);
            let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5);
            let sx2_ = sx_+toDist_/bside*j*cos(toAngle);
            let sy2_ = sy_+toDist_/bside*j*sin(toAngle);
            let sx2 = sx+toDist/bside*j*cos(toAngle);
            let sy2 = sy+toDist/bside*j*sin(toAngle);
            CreateDNHShotA1(sx2, sy2, v*(((sx2_-spawnX)^2+(sy2_-spawnY)^2)^0.5), atan2(sy2-spawnY,sx2-spawnX), graphic, delay);
        }
    }
}