# animation/action (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/animation/action/

액션.

## Action extends Object

액션.
- 순차적으로 스텝을 실행하는 스크립팅 방식의 액션 클래스.
- 사용 예시:
    const action = new Action()
        .wait(1.0, {
            started:   (target) => { },
            updated:   (progress, target) => { target.setLocalOpacity(progress); },
            completed: (target) => { }
        })
        .condition((target) => gameState.isReady())
        .call((target) => { target.playAnimation(); })
        .repeat(3, new Action().wait(0.5, { updated: (p, t) => { ... } }))
        .forever(new Action().wait(2.0));

    action.start(myNode);

    // 매 프레임:
    action.step(timeDelta);

예제: 기다리고 부르고 반복하기

```js
import { Action } from "./build/vanilla.js";

const target = { opacity: 0, isReady: false };
const blinkAction = new Action();
blinkAction.wait(0.1);
const action = new Action();
action.wait(0.5, {
	updated: (progress, actionTarget) => {
		actionTarget.opacity = progress;
	}
});
action.call((actionTarget) => {
	actionTarget.isReady = true;
});
action.repeat(2, blinkAction);
action.start(target);
for (let frame = 0; frame < 60; ++frame) {
	action.step(1 / 60);
}
console.log(target.opacity, target.isReady, action.isDone());
```

- `constructor()` (constructor, since 0.0.9): 생성.
- `wait(duration, callbacks = {})` (method, since 0.0.9): 일정 시간 대기.
  - callbacks.started(target)              : 스텝 시작 시 호출.
  - callbacks.updated(progress, target)    : 매 프레임 호출. progress = 0~1.
  - callbacks.completed(target)            : 스텝 완료 시 호출.
  - `duration` number: 기다릴 시간. (초, 0 이하이면 다음 step 에서 바로 끝난다)
  - `callbacks` { started?: Function, updated?: Function, completed?: Function }: { started, updated, completed } 콜백. (모두 생략할 수 있다)
  - returns Action: 이어 쓸 수 있게 이 액션.

  예제: 진행률 콜백 받기

  ```js
  import { Action } from "./build/vanilla.js";
  
  const action = new Action();
  action.wait(1, {
  	started: (target) => {
  		console.log("started", target.name);
  	},
  	updated: (progress, target) => {
  		target.progress = progress;
  	},
  	completed: (target) => {
  		console.log("completed", target.progress);
  	}
  });
  const target = { name: "fade", progress: 0 };
  action.start(target);
  action.step(0.5);
  action.step(0.5);
  console.log(action.isDone());
  ```

- `condition(conditionFn, callbacks = {})` (method, since 0.0.9): 조건이 참이 될 때까지 대기. (조건 함수는 대상을 받는다)
  - callbacks.started(target)              : 스텝 시작 시 호출.
  - callbacks.updated(target)              : 조건 충족 전 매 프레임 호출.
  - callbacks.completed(target)            : 조건 충족 시 호출.
  - `conditionFn` Function: 대상을 받아 참을 돌려주면 다음 스텝으로 넘어가는 함수. (step 마다 부른다)
  - `callbacks` { started?: Function, updated?: Function, completed?: Function }: { started, updated, completed } 콜백. (모두 생략할 수 있다)
  - returns Action: 이어 쓸 수 있게 이 액션.
- `call(fn)` (method, since 0.0.9): 즉시 콜백 호출.
  - `fn` Function: 대상을 받는 함수. (스텝이 시작되면 바로 부르고 다음 스텝으로 넘어간다)
  - returns Action: 이어 쓸 수 있게 이 액션.
- `repeat(times, innerAction)` (method, since 0.0.9): 내부 액션을 N회 반복. (0 이하이면 돌지 않고 넘어간다)
  - `times` number: 반복 수. (0 이하이면 돌지 않고 넘어간다)
  - `innerAction` Action: 반복할 액션. (끝날 때마다 같은 대상으로 다시 start 한다)
  - returns Action: 이어 쓸 수 있게 이 액션.
- `forever(innerAction)` (method, since 0.0.9): 내부 액션을 무한 반복.
  - `innerAction` Action: 끝날 때마다 다시 start 할 액션.
  - returns Action: 이어 쓸 수 있게 이 액션.
- `repeatWhile(conditionFn, innerAction)` (method, since 0.4.0): 조건이 참인 동안 내부 액션을 반복. (조건 함수는 대상을 받는다)
  - `conditionFn` Function: 대상을 받아 참이면 한 번 더 도는 함수. (스텝을 시작할 때와 내부 액션이 끝날 때마다 부른다)
  - `innerAction` Action: 반복할 액션.
  - returns Action: 이어 쓸 수 있게 이 액션.

  예제: 조건이 참인 동안 반복하기

  ```js
  import { Action } from "./build/vanilla.js";
  
  const counter = { count: 0 };
  const incrementAction = new Action();
  incrementAction.call((target) => {
  	target.count += 1;
  });
  const action = new Action();
  action.repeatWhile((target) => {
  	return target.count < 3;
  }, incrementAction);
  action.start(counter);
  for (let frame = 0; frame < 10; ++frame) {
  	action.step(1 / 60);
  }
  console.log(counter.count, action.isDone());
  ```

- `start(target)` (method, since 0.0.9): 실행 시작. (처음 스텝부터)
  - 대상을 들고 있다가 step 이 그 대상을 콜백에 넘기고 getTarget 이 돌려준다. (러스트 판 start(node_tree, Some(node_id)))
  - `target` System.Object: 콜백과 조건 함수에 넘길 대상. (undefined 이면 전의 대상을 그대로 쓴다)
  - returns Action: 이어 쓸 수 있게 이 액션.
- `startWith(target)` (method, since 0.4.0): 대상을 들지 않고 실행 시작. (처음 스텝부터)
  - 첫 스텝의 시작 콜백까지 이 자리에서 target 으로 부르고, 대상은 들지 않는다. (getTarget 은 그대로)
  - 뒤로는 stepWith 에 매번 대상을 넘긴다. repeat, forever, loop 의 안쪽 액션도 이렇게 돈다.
  - `target` System.Object: 이번에 콜백과 조건 함수에 넘길 대상.
  - returns Action: 이어 쓸 수 있게 이 액션.

  예제: 대상을 매번 넘겨 돌리기

  ```js
  import { Action } from "./build/vanilla.js";
  
  const target = { opacity: 0 };
  const action = new Action();
  action.wait(1, {
  	updated: (progress, actionTarget) => {
  		actionTarget.opacity = progress;
  	}
  });
  action.startWith(target);
  action.stepWith(target, 0.5);
  console.log(target.opacity, action.getTarget());
  ```

- `stop()` (method, since 0.0.9): 실행 중단.
- `step(timeDelta)` (method, since 0.0.9): 갱신. (매 프레임 호출)
  - start 로 든 대상을 콜백에 넘긴다. (러스트 판 step(node_tree, time_delta))
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `stepWith(target, timeDelta)` (method, since 0.4.0): 대상을 넘겨 갱신. (매 프레임 호출)
  - start 전이거나 끝났으면 아무것도 하지 않는다. 든 대상 대신 target 을 콜백에 넘긴다.
  - `target` System.Object: 이번에 콜백과 조건 함수에 넘길 대상.
  - `timeDelta` number: 지난 프레임부터의 시간. (초)

  예제: 장면 갱신에서 대상을 넘겨 돌리기

  ```js
  import { Action } from "./build/vanilla.js";
  
  const logo = { opacity: 0 };
  const action = new Action();
  action.wait(2, {
  	updated: (progress, actionTarget) => {
  		actionTarget.opacity = progress;
  	}
  });
  action.startWith(logo);
  action.stepWith(logo, 1);
  action.stepWith(logo, 1);
  console.log(logo.opacity, action.isDone());
  ```

- `isDone()` (method, since 0.0.9): 완료 여부 반환.
  - returns boolean: 모든 스텝을 마쳤거나 stop 했으면 참.
- `isRunning()` (method, since 0.0.9): 실행 중 여부 반환.
  - returns boolean: start 한 뒤 아직 끝나지 않았으면 참.
- `getTarget()` (method, since 0.0.9): 타겟 반환.
  - returns System.Object: start 에 넘긴 대상. 없으면 null.
