# core/animation (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/animation/

애니메이션 처리기.
- 원본 core/animation.js 를 옮긴 것이다. 프레임(Frame) 목록을 초당 프레임 수(animation_speed)로 넘기고,
  반복하지 않으면 마지막 프레임에서 멈추고 완료 콜백을 부른다.
- 원본도 컴포넌트가 아니라 그냥 객체다. 노드에 붙지 않고, 들고 있는 쪽(컴포넌트 등)이 tick 을 불러 주고
  get_current_image / get_current_rect 로 지금 프레임을 꺼내 Sprite 에 넣는다.
- 원본의 set_frames 계열은 배열이 아니거나 비면 그냥 돌아간다. 러스트 판은 타입이 정해져 있어서 빈 목록만 거른다.
  (set_frames 는 원본처럼 빈 목록도 받는다)
- 프레임 번호는 usize 다. 원본 gotoAndPlay(-1) 처럼 음수를 0 으로 맞추는 일은 타입이 대신한다.

## Animation (struct) (Default)

애니메이션 처리기.
- 프레임(Frame) 목록을 초당 프레임 수로 넘긴다. 노드에 붙지 않으므로 들고 있는 쪽이 tick 을 불러 주고 지금 프레임을 Sprite 에 건다.
- 처음은 프레임 없음, 초당 10 프레임, 반복, 정지 상태.

예제: 스프라이트 시트 네 칸을 돌려 Sprite 에 걸기

```rust
use vanilla::{Animation, Image, Rect, Sprite};

let sheet = Image::create_from_pixels(64, 16, vec![255u8; 64 * 16 * 4]);
let rects: Vec<Rect> = (0..4).map(|frame_index| Rect::create(16.0 * frame_index as f64, 0.0, 16.0, 16.0)).collect();
let mut animation = Animation::new();
animation.set_frames_from_rects(sheet, &rects);
animation.set_animation_speed(8.0);
animation.play();
animation.tick(0.25);
assert_eq!(animation.get_current_frame_index(), 2);

let mut sprite = Sprite::new();
sprite.set_frame(animation.get_current_frame());
assert_eq!(sprite.get_image_rect(), Rect::create(32.0, 0.0, 16.0, 16.0));
```

- `pub fn new() -> Animation` (constructor, since 0.3.2): 생성. (초당 10 프레임, 반복, 정지 상태)
  - returns Animation: 프레임이 없는 새 처리기.
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): 갱신. (쌓인 시간만큼 프레임을 넘긴다. 한 번에 여러 프레임을 넘길 수 있다)
  - 재생 중이 아니거나, 프레임이 없거나, 초당 프레임 수가 0 이하면 아무것도 하지 않는다. 한 프레임에 못 미친 시간은 다음 tick 으로 넘긴다.
  - 끝을 지나면 반복이면 처음으로 돌고, 아니면 마지막 프레임에서 멈추고 완료 콜백을 부른다.
  - `time_delta` f64: 지난 시간. (초)

  예제: 한 번에 두 프레임 넘기고 처음으로 돌기

  ```rust
  use vanilla::{Animation, Image, Rect};
  
  let sheet = Image::create_from_pixels(48, 16, vec![255u8; 48 * 16 * 4]);
  let rects = [Rect::create(0.0, 0.0, 16.0, 16.0), Rect::create(16.0, 0.0, 16.0, 16.0), Rect::create(32.0, 0.0, 16.0, 16.0)];
  let mut animation = Animation::new();
  animation.set_frames_from_rects(sheet, &rects);
  animation.set_animation_speed(4.0);
  animation.play();
  animation.tick(0.125);
  assert_eq!(animation.get_current_frame_index(), 0);
  animation.tick(0.375);
  assert_eq!(animation.get_current_frame_index(), 2);
  animation.tick(0.25);
  assert_eq!(animation.get_current_frame_index(), 0);
  ```

- `pub fn play(&mut self)` (method, since 0.3.2): 재생. (이미 재생 중이면 아무것도 하지 않는다)
- `pub fn pause(&mut self)` (method, since 0.3.2): 일시정지. (지금 프레임과 쌓인 시간을 그대로 둔다)
- `pub fn resume(&mut self)` (method, since 0.3.2): 재개. (멈춘 프레임에서 이어 간다)
- `pub fn stop(&mut self)` (method, since 0.3.2): 정지. (첫 프레임으로 되돌린다. 멈춰 있어도 되돌린다)
- `pub fn set_frames(&mut self, frames: Vec<Frame>)` (method, since 0.3.2): 프레임 설정. (설정한 뒤 정지한다)
  - `frames` Vec<Frame>: 차례대로 보일 프레임 목록. (빈 목록도 받는다)
- `pub fn set_frames_from_images(&mut self, images: &[Rc<Image>])` (method, since 0.3.2): 프레임 설정. (낱장의 스프라이트 이미지 목록, 비면 무시한다)
  - `images` &[Rc<Image>]: 프레임마다 쓸 그림 목록. (프레임마다 그 그림 전체가 영역이다)
- `pub fn set_frames_from_rects(&mut self, image: Rc<Image>, rects: &[Rect])` (method, since 0.3.2): 프레임 설정. (스프라이트 시트 한 장과 칸 영역 목록, 비면 무시한다)
  - `image` Rc<Image>: 스프라이트 시트 그림.
  - `rects` &[Rect]: 프레임마다 쓸 시트 안의 칸 영역 목록. (픽셀, 시트 왼쪽 위 기준)
- `pub fn set_animation_speed(&mut self, animation_speed: f64)` (method, since 0.3.2): 초당 프레임 수 설정. (예: 60 이면 초당 이미지가 60 번 바뀐다. 0 이하면 넘기지 않는다)
  - `animation_speed` f64: 초당 프레임 수. (처음 10)
- `pub fn set_loop(&mut self, is_loop: bool)` (method, since 0.3.2): 반복 여부 설정.
  - `is_loop` bool: true 면 끝에서 처음으로 돌고, false 면 마지막 프레임에서 멈춘다. (처음 true)
- `pub fn set_on_complete(&mut self, callback: impl FnMut() + 'static)` (method, since 0.3.2): 애니메이션 완료 콜백 설정. (반복하지 않을 때 마지막 프레임에 닿으면 불린다)
  - 다시 부르면 앞의 콜백을 바꾼다. 멈춘 뒤에는 다시 재생해 끝에 닿을 때까지 불리지 않는다.
  - `callback` impl FnMut() + 'static: 끝에 닿을 때 부를 함수. (tick 안에서 불린다)

  예제: 한 번 돌고 끝난 것 알기

  ```rust
  use vanilla::{Animation, Image};
  use std::cell::Cell;
  use std::rc::Rc;
  
  let images = [Image::create_from_pixels(8, 8, vec![0u8; 8 * 8 * 4]), Image::create_from_pixels(8, 8, vec![0u8; 8 * 8 * 4])];
  let mut animation = Animation::new();
  animation.set_frames_from_images(&images);
  animation.set_loop(false);
  let complete_count = Rc::new(Cell::new(0));
  let complete_count_in_callback = complete_count.clone();
  animation.set_on_complete(move || {
  	complete_count_in_callback.set(complete_count_in_callback.get() + 1);
  });
  animation.play();
  animation.tick(1.0);
  assert_eq!(complete_count.get(), 1);
  assert!(!animation.is_playing());
  assert_eq!(animation.get_current_frame_index(), 1);
  ```

- `pub fn goto_and_play(&mut self, index: usize)` (method, since 0.3.2): 특정 프레임으로 이동하여 재생한다. (번호는 프레임 수 안으로 맞춘다)
  - `index` usize: 옮겨 갈 프레임 번호. (0 부터, 프레임 수 - 1 보다 크면 마지막, 프레임이 없으면 0)
- `pub fn goto_and_stop(&mut self, index: usize)` (method, since 0.3.2): 특정 프레임으로 이동하여 정지한다. (번호는 프레임 수 안으로 맞춘다)
  - `index` usize: 옮겨 갈 프레임 번호. (0 부터, 프레임 수 - 1 보다 크면 마지막, 프레임이 없으면 0)
- `pub fn is_playing(&self) -> bool` (method, since 0.3.2): 재생 중인지 여부 반환.
  - returns bool: 재생 중이면 true.
- `pub fn get_current_frame(&self) -> Option<&Frame>` (method, since 0.3.2): 현재 프레임 반환. (없으면 None)
  - returns Option<&Frame>: 지금 프레임. 프레임이 없으면 None.
- `pub fn get_current_image(&self) -> Option<&Rc<Image>>` (method, since 0.3.2): 현재 이미지 반환.
  - returns Option<&Rc<Image>>: 지금 프레임의 그림. 프레임이 없으면 None.
- `pub fn get_current_rect(&self) -> Option<Rect>` (method, since 0.3.2): 현재 영역 반환.
  - returns Option<Rect>: 지금 프레임의 그림 안 영역. (픽셀) 프레임이 없으면 None.
- `pub fn get_current_frame_index(&self) -> usize` (method, since 0.3.2): 현재 프레임 번호 반환.
  - returns usize: 지금 프레임 번호. (0 부터)
- `pub fn get_total_frame_count(&self) -> usize` (method, since 0.3.2): 모든 프레임 수 반환.
  - returns usize: 프레임 수.
