12 private Action<ScriptedMovement>? _scriptCompleted;
27 ArgumentNullException.ThrowIfNull(callback);
46 Action<ScriptedMovement> callback)
48 ArgumentNullException.ThrowIfNull(callback);
52 throw new InvalidOperationException(
53 "OnBeginning must be called while a scripted movement is active.");
56 callback(_state.Script);
75 ArgumentNullException.ThrowIfNull(callback);
95 Action<ScriptedMovement> callback)
97 ArgumentNullException.ThrowIfNull(callback);
101 throw new InvalidOperationException(
102 "OnComplete must be called while a scripted movement is active.");
105 _scriptCompleted += callback;
136 Func<float, float>? easing =
null,
137 float snapEpsilon = 0.5f)
142 Origin = _mover.GetPosition(),
144 DurationSec = MathF.Max(0f, durationSec),
146 SnapEpsilon = MathF.Max(0f, snapEpsilon),
175 float snapEpsilon = 0.5f)
177 var easingFunc = EasingFunctions.From(easingKind);
211 Func<float, float>? easing =
null,
212 float snapEpsilon = 0.5f)
214 var goal = _mover.GetPosition() + delta;
247 float snapEpsilon = 0.5f)
249 var goal = _mover.GetPosition() + delta;
277 float snapEpsilon = 0.5f)
279 var goal = _mover.GetPosition() + delta;
283 MathF.Max(0f, speedPerSec),
311 float snapEpsilon = 0.5f)
317 SpeedPerSec = MathF.Max(0f, speedPerSec),
318 SnapEpsilon = MathF.Max(0f, snapEpsilon)
331 _scriptCompleted =
null;
335 var script = _state.Script;
339 _state.Script =
default;
340 _scriptCompleted =
null;
350 _scriptCompleted =
null;
352 _state.Script = script;
355 _state.Acceleration = Vector2.Zero;
356 _state.Velocity = Vector2.Zero;
363 private bool AdvanceScripted(
float dt)
365 switch (_state.Script.
Type)
371 return AdvanceTween(dt);
374 return AdvanceToward(dt);
381 private bool AdvanceTween(
float dt)
387 _state.Acceleration = Vector2.Zero;
388 _state.Velocity = Vector2.Zero;
396 _state.Script.ElapsedSec += MathF.Max(0f, dt);
398 float t = Math.Clamp(
399 _state.Script.ElapsedSec / _state.Script.DurationSec,
403 if (_state.Script.Easing is not
null)
406 _state.Script.Easing(t),
411 var pos = Vector2.Lerp(
412 _state.Script.Origin,
413 _state.Script.Target,
416 _mover.SetPosition(pos);
418 _state.Acceleration = Vector2.Zero;
419 _state.Velocity = Vector2.Zero;
423 var snapDistanceSquared =
424 _state.Script.SnapEpsilon *
425 _state.Script.SnapEpsilon;
428 Vector2.DistanceSquared(
430 _state.Script.Target) <= snapDistanceSquared;
432 if (reachedTarget || t >= 1f)
434 _mover.SetPosition(_state.Script.Target);
436 _state.Acceleration = Vector2.Zero;
437 _state.Velocity = Vector2.Zero;
446 private bool AdvanceToward(
float dt)
448 var current = _mover.GetPosition();
449 var to = _state.Script.Target - current;
450 var distance = to.Length();
452 if (distance <= _state.Script.SnapEpsilon ||
453 _state.Script.SpeedPerSec <= 0f ||
456 _mover.SetPosition(_state.Script.Target);
458 _state.Velocity = Vector2.Zero;
459 _state.Acceleration = Vector2.Zero;
468 _state.Script.SpeedPerSec * dt;
470 if (stepLength >= distance)
472 _mover.SetPosition(_state.Script.Target);
474 _state.Velocity = Vector2.Zero;
475 _state.Acceleration = Vector2.Zero;
483 var direction = to / distance;
485 _state.Acceleration = Vector2.Zero;
488 _state.Script.SpeedPerSec;
495 private void CompleteScript()
497 var script = _state.Script;
498 var completed = _scriptCompleted;
503 _state.Script =
default;
504 _scriptCompleted =
null;
507 completed?.Invoke(script);
Central controller responsible for managing all movement modes for a single IMovable object....
MovementController OnComplete(Action< ScriptedMovement > callback)
Registers a callback to invoke when the currently active scripted movement completes normally.
MovementController MoveBy(Vector2 delta, float durationSec, EasingKind easingKind, float snapEpsilon=0.5f)
Begins scripted relative motion by a delta over a fixed duration using an easing preset.
MovementController OnComplete(Action callback)
Registers a callback to invoke when the currently active scripted movement completes normally.
bool IsScripted
Indicates whether a scripted movement (tween or MoveToward) is currently active.
MovementController MoveToward(Vector2 target, float speedPerSec, float snapEpsilon=0.5f)
Begins scripted motion toward the specified target position at a constant speed.
Action< ScriptedMovement >? ScriptedMovementStarted
Raised when a scripted movement (tween or MoveToward) begins. Subscribers are notified exactly once p...
MovementController OnBeginning(Action callback)
Invokes a callback for the currently active scripted movement.
MovementController MoveBy(Vector2 delta, float durationSec, Func< float, float >? easing=null, float snapEpsilon=0.5f)
Begins scripted relative motion by a delta over a fixed duration with optional easing....
void CancelScript()
Cancels any active scripted movement and clears the current script state. Raises ScriptedMovementStop...
MovementController OnBeginning(Action< ScriptedMovement > callback)
Invokes a callback for the currently active scripted movement.
Action< ScriptedMovement >? ScriptedMovementStopped
Raised when a scripted movement (tween or MoveToward) completes or is explicitly cancelled....
MovementController MoveTo(Vector2 target, float durationSec, Func< float, float >? easing=null, float snapEpsilon=0.5f)
Begins a scripted tween toward the specified target position over a fixed duration....
MovementController MoveBy(Vector2 delta, float speedPerSec, float snapEpsilon=0.5f)
Begins scripted relative motion by a delta at a constant speed.
MovementController MoveTo(Vector2 target, float seconds, EasingKind easingKind, float snapEpsilon=0.5f)
Begins a scripted tween toward the specified target position over a fixed duration,...
void SetPosition(Vector2 pos)
Set the position in the mover's PositionSpace.
EasingKind
Enumeration of all supported easing functions for animation and movement interpolation....
MovementScriptType
Defines the type of scripted movement behavior applied to an object. Movement scripts control how an ...
Represents the state and parameters of a scripted movement operation applied to an object....
float DurationSec
The total duration of the movement in seconds. Only used by MovementScriptType.TweenTo to determine t...
Vector2 Target
The target position or destination for the movement. For MovementScriptType.TweenTo,...
MovementScriptType Type
The type of movement script currently active. Determines which fields are relevant and how the moveme...