# animation/action (vanilla.js 0.4.0)

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

Action.

## Action extends Object

Action.
- A scripting-style action class that runs steps sequentially.
- Usage example:
    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);

    // Every frame:
    action.step(timeDelta);

Example: Wait, call and repeat

```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): Constructor.
- `wait(duration, callbacks = {})` (method, since 0.0.9): Waits for a set time.
  - callbacks.started(target)              : called when the step starts.
  - callbacks.updated(progress, target)    : called every frame. progress = 0~1.
  - callbacks.completed(target)            : called when the step completes.
  - `duration` number: Time to wait. (seconds, ends on the next step if 0 or less)
  - `callbacks` { started?: Function, updated?: Function, completed?: Function }: { started, updated, completed } callbacks. (all optional)
  - returns Action: This action, for chaining.

  Example: Receive progress callbacks

  ```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): Waits until the condition becomes true. (the condition function receives the target)
  - callbacks.started(target)              : called when the step starts.
  - callbacks.updated(target)              : called every frame until the condition is met.
  - callbacks.completed(target)            : called when the condition is met.
  - `conditionFn` Function: Function that receives the target and moves to the next step when it returns true. (called on every step)
  - `callbacks` { started?: Function, updated?: Function, completed?: Function }: { started, updated, completed } callbacks. (all optional)
  - returns Action: This action, for chaining.
- `call(fn)` (method, since 0.0.9): Calls a callback immediately.
  - `fn` Function: Function that receives the target. (called as soon as the step starts, then moves to the next step)
  - returns Action: This action, for chaining.
- `repeat(times, innerAction)` (method, since 0.0.9): Repeats the inner action N times. (0 or less skips it without running)
  - `times` number: Repeat count. (skipped without running if 0 or less)
  - `innerAction` Action: Action to repeat. (started again with the same target each time it finishes)
  - returns Action: This action, for chaining.
- `forever(innerAction)` (method, since 0.0.9): Repeats the inner action forever.
  - `innerAction` Action: Action to start again each time it finishes.
  - returns Action: This action, for chaining.
- `repeatWhile(conditionFn, innerAction)` (method, since 0.4.0): Repeats the inner action while the condition is true. (the condition function receives the target)
  - `conditionFn` Function: Function that receives the target and runs one more time if true. (called when the step starts and each time the inner action finishes)
  - `innerAction` Action: Action to repeat.
  - returns Action: This action, for chaining.

  Example: Repeat while a condition is true

  ```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): Starts running. (from the first step)
  - Holds the target, so step passes it to the callbacks and getTarget returns it. (start(node_tree, Some(node_id)) in the Rust edition)
  - `target` System.Object: Target passed to callbacks and condition functions. (keeps the previous target if undefined)
  - returns Action: This action, for chaining.
- `startWith(target)` (method, since 0.4.0): Starts running without holding the target. (from the first step)
  - Calls up to the first step's started callback right here with target, and does not hold the target. (getTarget stays as it was)
  - After this, pass the target to stepWith every time. The inner actions of repeat, forever and loop run this way too.
  - `target` System.Object: The target to pass to the callbacks and condition functions this time.
  - returns Action: This action, for chaining.

  Example: Running by passing the target every time

  ```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): Stops running.
- `step(timeDelta)` (method, since 0.0.9): Updates. (call every frame)
  - Passes the target held by start to the callbacks. (step(node_tree, time_delta) in the Rust edition)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `stepWith(target, timeDelta)` (method, since 0.4.0): Updates with a given target. (call every frame)
  - Does nothing before start or after it is done. Passes target to the callbacks instead of the held target.
  - `target` System.Object: The target to pass to the callbacks and condition functions this time.
  - `timeDelta` number: Time since the last frame. (Seconds)

  Example: Running by passing the target from a scene update

  ```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 whether it is complete.
  - returns boolean: True if all steps have finished or stop was called.
- `isRunning()` (method, since 0.0.9): Returns whether it is running.
  - returns boolean: True if started and not finished yet.
- `getTarget()` (method, since 0.0.9): Returns the target.
  - returns System.Object: Target passed to start. null if none.
