update method

  1. @override
void update(
  1. double deltaSeconds
)
override

Called once per frame while the component is mounted, enabled, and loaded. deltaSeconds is the elapsed time since the previous tick. A traversal visits each component at most once. Removing this component or an earlier sibling is safe. A component inserted before the current traversal position starts on the next frame. Reordering component or child lists during traversal is unsupported.

Implementation

@override
void update(double deltaSeconds) {
  final dt = clampDeltaSeconds(deltaSeconds);
  final lookR = smoothingResponse(dt);
  _yaw += (_yawGoal - _yaw) * lookR;
  _pitch += (_pitchGoal - _pitch) * lookR;

  final moveForward = moveVertical
      ? forward
      : (Vector3(forward.x, 0.0, forward.z)..normalize());
  var alongForward = _moveInput.y;
  var alongRight = _moveInput.x;
  var alongUp = moveVertical ? _elevateInput : 0.0;
  if (_heldKeys.contains(LogicalKeyboardKey.keyW)) alongForward += 1.0;
  if (_heldKeys.contains(LogicalKeyboardKey.keyS)) alongForward -= 1.0;
  if (_heldKeys.contains(LogicalKeyboardKey.keyD)) alongRight += 1.0;
  if (_heldKeys.contains(LogicalKeyboardKey.keyA)) alongRight -= 1.0;
  if (moveVertical) {
    if (_heldKeys.contains(LogicalKeyboardKey.keyE)) alongUp += 1.0;
    if (_heldKeys.contains(LogicalKeyboardKey.keyQ)) alongUp -= 1.0;
  }
  // [_right] points toward the screen's left, so rightward motion subtracts
  // it.
  final targetVelocity = Vector3.zero()
    ..addScaled(moveForward, alongForward)
    ..addScaled(_right, -alongRight)
    ..addScaled(Vector3(0.0, 1.0, 0.0), alongUp);
  final boosted =
      _boostInput ||
      _heldKeys.contains(LogicalKeyboardKey.shiftLeft) ||
      _heldKeys.contains(LogicalKeyboardKey.shiftRight);
  // Clamp rather than normalize, so an analog push keeps its magnitude while
  // key combinations still top out at full speed.
  final length2 = targetVelocity.length2;
  if (length2 > 1.0) targetVelocity.scale(1.0 / math.sqrt(length2));
  targetVelocity.scale(speed * (boosted ? boostMultiplier : 1.0));
  final moveR = settleResponse(movementSmoothing, dt);
  _velocity.addScaled(targetVelocity - _velocity, moveR);
  if (_velocity.length2 > 1e-12) _position.addScaled(_velocity, dt);

  node.lookAtFrom(_position, _position + forward);
}