# core/tween (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/tween/

트윈.

## Tween extends Object

트윈.
- setTarget(node) 로 대상 노드를 정하면 값 이름이 TweenProperty 이름인 값을 tick 마다 그 노드에 바로 쓴다.
  대상 노드가 트윈이 처음 돈 때의 노드 트리에서 떨어지면(removeChild 등) 콜백 없이 조용히 끝난다.
- duration 이 0 이하이면 바로 끝(진행률 1)으로 친다.

예제: 값 두 개를 0.5 초 동안 바꾸기

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

const tween = new Tween({ x: 0, alpha: 1 });
tween.to({ x: 100, alpha: 0 }, 0.5);
tween.easing(Tween.easingFunction.quadratic.easeOut);
tween.setUpdate((values) => {
	console.log(values.x, values.alpha);
});
tween.setComplete(() => {
	console.log("complete");
});
tween.start();
for (let step = 0; step < 5; ++step) {
	tween.tick(0.1);
}
console.log(tween.isFinished());
```

- `constructor(initialValues)` (constructor, since 0.0.8): 생성.
  - `initialValues` Object: 시작 값 객체. (이름마다 숫자나 Vector2, 얕게 복사해 둔다)
- `static createNodeTween(node, tweenProperty, startValue, endValue, duration)` (static, since 0.4.0): 노드 속성 하나를 움직이는 트윈 생성. (정적)
  - new Tween(시작값).to(목표값, duration).setTarget(node) 와 같다. 값 이름은 속성 이름이다.
  - `node` WorldNode: 움직일 노드.
  - `tweenProperty` string: TweenProperty 의 값.
  - `startValue` number | Vector2: 시작 값.
  - `endValue` number | Vector2: 목표 값.
  - `duration` number: 걸리는 시간. (초, 0 이하이면 바로 끝)
  - returns Tween: 만든 트윈. (아직 돌지 않는다, Scene.startTween 이나 start 로 시작한다)

  예제: 씬에서 노드 위치를 움직이기

  ```js
  import { Engine, EngineConfiguration, Scene, WorldNode, Tween, TweenProperty, Vector2 } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	initialize(engine) {
  		super.initialize(engine);
  		const node = new WorldNode();
  		const root = this.getRoot();
  		root.addChild(node);
  		const tween = Tween.createNodeTween(node, TweenProperty.localPosition, Vector2.create(0, 0), Vector2.create(200, 100), 0.5);
  		tween.easing(Tween.easingFunction.cubic.easeInOut);
  		tween.setComplete(() => {
  			const localPosition = node.getLocalPosition();
  			console.log(localPosition.x, localPosition.y);
  		});
  		this.startTween(tween);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `static findRootNode(node)` (static, since 0.4.0): 노드의 맨 위 노드 반환.
  - `node` Node: 시작 노드.
  - returns Node: 부모를 끝까지 따라 올라간 노드. (부모가 없으면 그 노드)
- `tick(timeDelta)` (method, since 0.0.8): 갱신.
  - `timeDelta` number: 지난 시간. (초, 시작했고 끝나지 않았을 때만 넘긴다)
- `to(properties, duration)` (method, since 0.0.8): 목표값 설정.
  - `properties` System.Object: 목표 값 객체. (시작 값에 같은 이름이 있고 꼴이 같은 것만 바뀐다, 숫자끼리나 Vector2 끼리)
  - `duration` number: 걸리는 시간. (초, 주지 않으면 그대로, 기본 1)
  - returns Tween: 이 트윈. (이어 부르기)
- `delay(amount)` (method, since 0.0.8): 시작 전 대기 시간 설정.
  - `amount` number: 시작 전 대기 시간. (초)
  - returns Tween: 이 트윈. (이어 부르기)
- `easing(easingFunction)` (method, since 0.0.8): 트윈 함수 설정.
  - `easingFunction` Function: 진행률(0 ~ 1)을 받아 바뀐 진행률을 돌려주는 함수. (Tween.easingFunction 의 것, 기본 linear)
  - returns Tween: 이 트윈. (이어 부르기)
- `setUpdate(callback)` (method, since 0.0.8): 갱신 콜백 설정.
  - `callback` Function: tick 마다 이번 값 객체를 받는 함수. (대기 시간 동안은 부르지 않는다)
  - returns Tween: 이 트윈. (이어 부르기)
- `setComplete(callback)` (method, since 0.0.8): 완료 콜백 설정.
  - `callback` Function: 진행률이 1 에 닿으면 한 번 부를 함수. (인자 없음, 대상 노드가 떨어져 끝날 때는 부르지 않는다)
  - returns Tween: 이 트윈. (이어 부르기)
- `setTarget(node)` (method, since 0.4.0): 대상 노드 설정. (값 이름이 TweenProperty 이름이면 이 노드에 쓴다)
  - `node` WorldNode | null: 대상 노드. (null 이면 노드에 쓰지 않고 콜백으로만 값을 준다)
  - returns Tween: 이 트윈. (이어 부르기)
- `getTarget()` (method, since 0.4.0): 대상 노드 반환. (setTarget 을 하지 않았으면 null)
  - returns WorldNode | null: 대상 노드. (없으면 null)
- `start()` (method, since 0.0.8): 시작.
- `stop()` (method, since 0.0.8): 정지.
- `isFinished()` (method, since 0.0.8): 완료 여부 반환.
  - returns boolean: 끝났으면 참. (stop 을 불렀거나 진행률이 1 에 닿았을 때)
- `static easingFunction` (static, since 0.0.8): 트윈 함수.

## Functions and constants

- `applyTweenProperty(node, tweenProperty, value)` (method, since 0.4.0): 노드에 트윈 속성 값 쓰기. (노드 속성 이름이 아니면 아무것도 하지 않는다)
  - `node` WorldNode: 값을 쓸 노드.
  - `tweenProperty` string: TweenProperty 의 값.
  - `value` number | Vector2: 쓸 값. (Vector2 속성에 숫자를 주면 두 축에 같이 쓴다, 숫자 속성에 Vector2 를 주면 쓰지 않는다)

## TweenProperty (enum)

트윈이 바로 쓰는 노드 속성 이름. (setTarget 으로 대상 노드를 정했을 때, 값 이름이 이 이름이면 그 노드에 쓴다)
- Vector2 속성에 숫자를 주면 두 축에 같이 쓴다. (예: { localScale: 0 } → setLocalScale(Vector2.create(0, 0)))
- 숫자 속성에 Vector2 를 주면 쓰지 않는다.

- `localPosition` = `localPosition`: 로컬 위치. (Vector2, setLocalPosition)
- `localScale` = `localScale`: 로컬 크기 배율. (Vector2, setLocalScale)
- `localRotation` = `localRotation`: 로컬 회전. (숫자, 도, setLocalRotation)
- `localOpacity` = `localOpacity`: 로컬 불투명도. (숫자, 0 ~ 1, setLocalOpacity)
- `contentSize` = `contentSize`: 내용 크기. (Vector2, 픽셀, setContentSize)
