
let anime_state = 0;
let anime_count = 0;

let ACT_AUTOSET	= 0;
let ACT_STAND	= 0;
let ACT_MOVE_LP	= 1;
let ACT_MOVE_L	= 2;
let ACT_MOVE_RP	= 3;
let ACT_MOVE_R	= 4;

let anime_start = [0, 4, 5, 9, 10];
let anime_start2 = [0, 4, 5, 7, 8];
let anime_pattern  = [0, 1, 2, 1, 8, 9, 10, 11, 10, 12, 13, 14, 15, 14];
let anime_pattern2 = [0, 1, 2, 1, 7, 6, 6, 6, 6, 4, 5, 5, 5, 5];
let anime_wait = 7;
let anime_frame = 1;

let fairy_type = 0;

function SetFairyType(type)
{
	fairy_type = type;
}

function InitializeAction()
{
	T_Anime();
}

task T_Anime()
{
	let bx = GetX();

	loop{
		if( bx < GetX() - 0.1 ){
			if( anime_state == ACT_MOVE_RP ){
				anime_state = ACT_MOVE_R;
				anime_frame = 4;
			}
			else if( anime_state != ACT_MOVE_R ){
				anime_state = ACT_MOVE_RP;
				anime_frame = 1;
			}
		}
		else if( bx > GetX() + 0.1 ){
			if( anime_state == ACT_MOVE_LP ){
				anime_state = ACT_MOVE_L;
				anime_frame = 4;
			}
			else if( anime_state != ACT_MOVE_L ){
				anime_state = ACT_MOVE_LP;
				anime_frame = 1;
			}
		}
		else{
			anime_state = ACT_STAND;
			anime_frame = 4;
		}

		if( fairy_type < 3 ){
			let p = anime_pattern[trunc(anime_count / anime_wait) % anime_frame + anime_start[anime_state]];
			let x1 = p % 8 * 32;
			let y1 = trunc(p / 8) * 32 + fairy_type * 64 + 128;
			SetGraphicRect(x1, y1, x1 + 32, y1 + 32);
		}
		else{
			let p = anime_pattern2[trunc(anime_count / anime_wait) % anime_frame + anime_start[anime_state]];
			let x1 = p % 4 * 64 + 256;
			let y1 = trunc(p / 4) * 64 + (fairy_type - 3) * 128;
			SetGraphicRect(x1, y1, x1 + 64, y1 + 64);
		}

		bx = GetX();

		if( anime_state == ACT_MOVE_LP || anime_state == ACT_MOVE_RP ){
			anime_count += 5;
			Wait(5);
		}
		else{
			anime_count ++;
			yield;
		}
	}
}
