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)
143 Origin = _mover.GetPosition(),
145 DurationSec = MathF.Max(0f, durationSec),
147 SnapEpsilon = MathF.Max(0f, snapEpsilon),
176 float snapEpsilon = 0.5f)
178 var easingFunc = EasingFunctions.From(easingKind);
212 Func<float, float>? easing =
null,
213 float snapEpsilon = 0.5f)
215 var goal = _mover.GetPosition() + delta;
248 float snapEpsilon = 0.5f)
250 var goal = _mover.GetPosition() + delta;
278 float snapEpsilon = 0.5f)
280 var goal = _mover.GetPosition() + delta;
284 MathF.Max(0f, speedPerSec),
312 float snapEpsilon = 0.5f)
319 SpeedPerSec = MathF.Max(0f, speedPerSec),
320 SnapEpsilon = MathF.Max(0f, snapEpsilon)
333 _scriptCompleted =
null;
337 var script = _state.Script;
341 _state.Script =
default;
342 _scriptCompleted =
null;
352 _scriptCompleted =
null;
354 _state.Script = script;
357 _state.Acceleration = Vector2.Zero;
358 _state.Velocity = Vector2.Zero;
365 private bool AdvanceScripted(
float dt)
367 switch (_state.Script.
Type)
373 return AdvanceTween(dt);
376 return AdvanceToward(dt);
383 private bool AdvanceTween(
float dt)
389 _state.Acceleration = Vector2.Zero;
390 _state.Velocity = Vector2.Zero;
398 _state.Script.ElapsedSec += MathF.Max(0f, dt);
400 float t = Math.Clamp(
401 _state.Script.ElapsedSec / _state.Script.DurationSec,
405 if (_state.Script.Easing is not
null)
408 _state.Script.Easing(t),
413 var pos = Vector2.Lerp(
414 _state.Script.Origin,
415 _state.Script.Target,
418 _mover.SetPosition(pos);
420 _state.Acceleration = Vector2.Zero;
421 _state.Velocity = Vector2.Zero;
425 var snapDistanceSquared =
426 _state.Script.SnapEpsilon *
427 _state.Script.SnapEpsilon;
430 Vector2.DistanceSquared(
432 _state.Script.Target) <= snapDistanceSquared;
434 if (reachedTarget || t >= 1f)
436 _mover.SetPosition(_state.Script.Target);
438 _state.Acceleration = Vector2.Zero;
439 _state.Velocity = Vector2.Zero;
448 private bool AdvanceToward(
float dt)
450 var current = _mover.GetPosition();
451 var to = _state.Script.Target - current;
452 var distance = to.Length();
454 if (distance <= _state.Script.SnapEpsilon ||
455 _state.Script.SpeedPerSec <= 0f ||
458 _mover.SetPosition(_state.Script.Target);
460 _state.Velocity = Vector2.Zero;
461 _state.Acceleration = Vector2.Zero;
470 _state.Script.SpeedPerSec * dt;
472 if (stepLength >= distance)
474 _mover.SetPosition(_state.Script.Target);
476 _state.Velocity = Vector2.Zero;
477 _state.Acceleration = Vector2.Zero;
485 var direction = to / distance;
487 _state.Acceleration = Vector2.Zero;
491 _state.Script.SpeedPerSec;
498 private void CompleteScript()
500 var script = _state.Script;
501 var completed = _scriptCompleted;
506 _state.Script =
default;
507 _scriptCompleted =
null;
510 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...