
#Title["Animation Library Example 1"]
#Text["This sample shows a basic[r]animating example."]
#ScriptVersion[3]

#include"./AnimLib.h"

let Enemy = ID_INVALID;
let objScene = GetEnemyBossSceneObjectID;
let source = GetCurrentScriptDirectory;

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){SetScriptResult(2450);}
	case(EV_REQUEST_TIMER){SetScriptResult(-1);}
}

@Initialize{
	Enemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(Enemy);
	ObjPrim_SetTexture(Enemy, source~"boss.png"); 
	
	ObjRender_SetPosition(Enemy, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	
	ascent(x in 0..4){ObjAnim_AddFrameA1(Enemy, ANIM_IDLE, 64*x, 0, 64*(x+1), 64);}
	descent(x in 1..3){ObjAnim_AddFrameA1(Enemy, ANIM_IDLE, 64*x, 0, 64*(x+1), 64);}
	
	ascent(x in 0..4){ObjAnim_AddFrameA1(Enemy, ANIM_MOVE, 64*x, 64, 64*(x+1), 128);}
	
	descent(x in 0..4){ObjAnim_AddFrameA1(Enemy, ANIM_MOVEEND, 64*x, 64, 64*(x+1), 128);}
	
	ascent(x in 0..4){ObjAnim_AddFrameA1(Enemy, ANIM_ATTACK, 64*x, 128, 64*(x+1), 192);}
	
	descent(x in 0..4){ObjAnim_AddFrameA1(Enemy, ANIM_ATTACKEND, 64*x, 128, 64*(x+1), 192);}
	
	ObjAnim_SetLoopPointA1(Enemy, ANIM_MOVE, 3);
	ObjAnim_SetLoopPointA1(Enemy, ANIM_ATTACK, 3);
	ObjAnim_SetSpeedA1(Enemy, ANIM_IDLE, 9);
	ObjAnim_SetSpeedA1(Enemy, ANIM_MOVE, 5);
	ObjAnim_SetSpeedA1(Enemy, ANIM_ATTACK, 4);
	ObjAnim_StartAnimationA1(Enemy, 5, 1.5);
	
	ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, GetStgFrameHeight/4, 10, 3);
	BossActions;
}

@MainLoop{yield;}

task BossActions{
	let Attack_Ex = 0;
	let Enemy_Move = 0;
	while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 0){
		ObjEnemy_SetIntersectionCircleToShot(Enemy, ObjMove_GetX(Enemy), ObjMove_GetY(Enemy), 24);
		ObjEnemy_SetIntersectionCircleToPlayer(Enemy, ObjMove_GetX(Enemy), ObjMove_GetY(Enemy), 32);
		
		if(Enemy_Move>rand(345, 600) && !ObjAnim_IsAttackPlaying(Enemy)){
			ObjMove_SetDestAtWeight(Enemy, GetPlayerX+rand(-40, 40), 120+rand(-25, 25), 10, 3);
			Enemy_Move = 0;
		}
		
		if(Attack_Ex >= 400){ObjAnim_SetAttackA2(Enemy, 80);Attack_Ex = -100;}
		
		Enemy_Move++;
		Attack_Ex+=rand(0, 3);
		yield;
	}
	Obj_Delete(Enemy);
	loop(128){yield;}
	CloseScript(GetOwnScriptID);
}
