addState method

CStateMachine<T> addState(
  1. String name, {
  2. void onEnter()?,
  3. void onExit()?,
  4. void onUpdate(
    1. double dt
    )?,
})

Define a state. Must be called before start. Calling after start throws, because addState defines the machine's shape once; it's not meant to be mutated while running.

Implementation

CStateMachine<T> addState(
  String name, {
  void Function()? onEnter,
  void Function()? onExit,
  void Function(double dt)? onUpdate,
}) {
  if (_started) {
    throw StateError(
      'addState("$name") called after start() — define all states before starting.',
    );
  }
  if (_states.containsKey(name)) {
    throw ArgumentError('State "$name" already defined.');
  }
  _states[name] = .new(
    name,
    onEnter: onEnter,
    onExit: onExit,
    onUpdate: onUpdate,
  );
  return this;
}