//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ZT Non-Spell 7"]
#Text["Non-Spell 7"]
#include "script/default_system/Default_ShotConst.txt"
#include "script/ZT/Library.txt"

let objBoss;
let bossX = 0;
let bossY = 0;
let angleT = 0;

@Event {
	alternative(GetEventType)
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(4000); 
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(46);
	}
}

@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	BottomEnemyMarker(objBoss, objBoss);
        ObjEnemy_Regist(objBoss);
        let ZeroTwoPixel = GetCurrentScriptDirectory() ~ "img/ZeroTwoPixel.png";
        ObjPrim_SetTexture(objBoss, ZeroTwoPixel);
        ObjSprite2D_SetSourceRect(objBoss, 0, 0, 20, 49);
        ObjSprite2D_SetDestCenter(objBoss);
        ObjMove_SetDestAtWeight(objBoss, GetCenterX(), 100, 5, 4);
		mainTask;
}

@MainLoop {
	bossX = ObjMove_GetX(objBoss);
	bossY = ObjMove_GetY(objBoss);
	angleT = GetAngleToPlayer(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,bossX,bossY,32);
	yield;
}

task mainTask {
	mainShot;
	bossInvincibility;
	Aura;
	angleSource;
	TEnd;
}

task Aura {
	let frameAura = 0;
	let objAura = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objAura, GetCurrentScriptDirectory~"./img/spellcircle.png");
	ObjSprite2D_SetSourceRect(objAura,0, 0, 132, 132);
	ObjRender_SetScaleXYZ(objAura,1,1,1);
	Obj_SetRenderPriorityI(objAura,25);
	ObjSprite2D_SetDestCenter(objAura);
	ObjRender_SetBlendType(objAura,BLEND_ADD_ARGB);
	ObjRender_SetPosition(objAura,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	ObjRender_SetAngleXYZ(objAura,0,0,0);
	while(!Obj_IsDeleted(objBoss)){
		frameAura++;
		ObjRender_SetScaleXYZ(objAura,1.5+0.2*cos(frameAura*3),1.5+0.2*cos(frameAura*3),1);
		ObjRender_SetAngleXYZ(objAura,0,0,frameAura*3);
		ObjRender_SetAlpha(objAura,205-50*cos(frameAura*3));
		ObjRender_SetPosition(objAura,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
		yield;
	}
	Obj_Delete(objAura);
}

task angleSource {
	let dir;
	let speed;
	while(!Obj_IsDeleted(objBoss)) {
	dir = ObjMove_GetAngle(objBoss);
	speed = ObjMove_GetSpeed(objBoss);
	if(speed == 0) { ObjSprite2D_SetSourceRect(objBoss,0,0,20,49); }
	else if(cos(dir) < 0) { ObjSprite2D_SetSourceRect(objBoss,20,0,0,49); }
	else if(cos(dir) > 0) { ObjSprite2D_SetSourceRect(objBoss,0,0,20,49); }
		yield;
	}
}

task mainShot {
	wait(120);
	shotSound;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	CreateShotA1(bossX, bossY, rand(3, 6), rand(0, 360), 65, 20);
	CreateShotA1(bossX, bossY, rand(2, 4), rand(0, 360), 162, 20);
	CreateShotA1(bossX, bossY, rand(2, 4), rand(0, 360), 162, 20);
	wait(3);
	}
}

task shotSound {
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	SE_Play(cus,60);
	wait(5);
	}
}

task bossInvincibility {
	ObjEnemy_SetDamageRate(objBoss,0,0);
	wait(125);
	ObjEnemy_SetDamageRate(objBoss,95,95);
}

task TEnd {
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		yield;
	}
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	wait(5);
	CloseScript(GetOwnScriptID());
}