Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
MovementController.Scripted.cs
1using System.Numerics;
4
6
10public sealed partial class MovementController
11{
12 private Action<ScriptedMovement>? _scriptCompleted;
13
25 public MovementController OnBeginning(Action callback)
26 {
27 ArgumentNullException.ThrowIfNull(callback);
28
29 return OnBeginning(_ => callback());
30 }
31
45 public MovementController OnBeginning(
46 Action<ScriptedMovement> callback)
47 {
48 ArgumentNullException.ThrowIfNull(callback);
49
50 if (!IsScripted)
51 {
52 throw new InvalidOperationException(
53 "OnBeginning must be called while a scripted movement is active.");
54 }
55
56 callback(_state.Script);
57
58 return this;
59 }
60
73 public MovementController OnComplete(Action callback)
74 {
75 ArgumentNullException.ThrowIfNull(callback);
76
77 return OnComplete(_ => callback());
78 }
79
94 public MovementController OnComplete(
95 Action<ScriptedMovement> callback)
96 {
97 ArgumentNullException.ThrowIfNull(callback);
98
99 if (!IsScripted)
100 {
101 throw new InvalidOperationException(
102 "OnComplete must be called while a scripted movement is active.");
103 }
104
105 _scriptCompleted += callback;
106
107 return this;
108 }
109
133 public MovementController MoveTo(
134 Vector2 target,
135 float durationSec,
136 Func<float, float>? easing = null,
137 float snapEpsilon = 0.5f)
138 {
139 return StartScript(
141 {
142 Type = MovementScriptType.TweenTo,
143 Origin = _mover.GetPosition(),
144 Target = target,
145 DurationSec = MathF.Max(0f, durationSec),
146 ElapsedSec = 0f,
147 SnapEpsilon = MathF.Max(0f, snapEpsilon),
148 Easing = easing
149 });
150 }
151
172 public MovementController MoveTo(
173 Vector2 target,
174 float seconds,
175 EasingKind easingKind,
176 float snapEpsilon = 0.5f)
177 {
178 var easingFunc = EasingFunctions.From(easingKind);
179
180 return MoveTo(
181 target,
182 seconds,
183 easingFunc,
184 snapEpsilon);
185 }
186
209 public MovementController MoveBy(
210 Vector2 delta,
211 float durationSec,
212 Func<float, float>? easing = null,
213 float snapEpsilon = 0.5f)
214 {
215 var goal = _mover.GetPosition() + delta;
216
217 return MoveTo(
218 goal,
219 durationSec,
220 easing,
221 snapEpsilon);
222 }
223
244 public MovementController MoveBy(
245 Vector2 delta,
246 float durationSec,
247 EasingKind easingKind,
248 float snapEpsilon = 0.5f)
249 {
250 var goal = _mover.GetPosition() + delta;
251
252 return MoveTo(
253 goal,
254 durationSec,
255 easingKind,
256 snapEpsilon);
257 }
258
275 public MovementController MoveBy(
276 Vector2 delta,
277 float speedPerSec,
278 float snapEpsilon = 0.5f)
279 {
280 var goal = _mover.GetPosition() + delta;
281
282 return MoveToward(
283 goal,
284 MathF.Max(0f, speedPerSec),
285 snapEpsilon);
286 }
287
309 public MovementController MoveToward(
310 Vector2 target,
311 float speedPerSec,
312 float snapEpsilon = 0.5f)
313 {
314 return StartScript(
316 {
317 Type = MovementScriptType.Toward,
318 Target = target,
319 SpeedPerSec = MathF.Max(0f, speedPerSec),
320 SnapEpsilon = MathF.Max(0f, snapEpsilon)
321 });
322 }
323
329 public void CancelScript()
330 {
331 if (!IsScripted)
332 {
333 _scriptCompleted = null;
334 return;
335 }
336
337 var script = _state.Script;
338
339 // Clear the old script before invoking external code so that an event
340 // handler can safely begin a new movement.
341 _state.Script = default;
342 _scriptCompleted = null;
343
344 ScriptedMovementStopped?.Invoke(script);
345 }
346
347 private MovementController StartScript(
348 ScriptedMovement script)
349 {
350 // A newly assigned script supersedes any completion callback associated
351 // with the previously active script.
352 _scriptCompleted = null;
353
354 _state.Script = script;
355
356 // Scripted movement overrides integrated movement.
357 _state.Acceleration = Vector2.Zero;
358 _state.Velocity = Vector2.Zero;
359
360 ScriptedMovementStarted?.Invoke(script);
361
362 return this;
363 }
364
365 private bool AdvanceScripted(float dt)
366 {
367 switch (_state.Script.Type)
368 {
369 case MovementScriptType.None:
370 return false;
371
372 case MovementScriptType.TweenTo:
373 return AdvanceTween(dt);
374
375 case MovementScriptType.Toward:
376 return AdvanceToward(dt);
377
378 default:
379 return false;
380 }
381 }
382
383 private bool AdvanceTween(float dt)
384 {
385 if (_state.Script.DurationSec <= 0f)
386 {
387 _mover.SetPosition(_state.Script.Target);
388
389 _state.Acceleration = Vector2.Zero;
390 _state.Velocity = Vector2.Zero;
391
392 Step(0f);
393 CompleteScript();
394
395 return true;
396 }
397
398 _state.Script.ElapsedSec += MathF.Max(0f, dt);
399
400 float t = Math.Clamp(
401 _state.Script.ElapsedSec / _state.Script.DurationSec,
402 0f,
403 1f);
404
405 if (_state.Script.Easing is not null)
406 {
407 t = Math.Clamp(
408 _state.Script.Easing(t),
409 0f,
410 1f);
411 }
412
413 var pos = Vector2.Lerp(
414 _state.Script.Origin,
415 _state.Script.Target,
416 t);
417
418 _mover.SetPosition(pos);
419
420 _state.Acceleration = Vector2.Zero;
421 _state.Velocity = Vector2.Zero;
422
423 Step(0f);
424
425 var snapDistanceSquared =
426 _state.Script.SnapEpsilon *
427 _state.Script.SnapEpsilon;
428
429 bool reachedTarget =
430 Vector2.DistanceSquared(
431 pos,
432 _state.Script.Target) <= snapDistanceSquared;
433
434 if (reachedTarget || t >= 1f)
435 {
436 _mover.SetPosition(_state.Script.Target);
437
438 _state.Acceleration = Vector2.Zero;
439 _state.Velocity = Vector2.Zero;
440
441 Step(0f);
442 CompleteScript();
443 }
444
445 return true;
446 }
447
448 private bool AdvanceToward(float dt)
449 {
450 var current = _mover.GetPosition();
451 var to = _state.Script.Target - current;
452 var distance = to.Length();
453
454 if (distance <= _state.Script.SnapEpsilon ||
455 _state.Script.SpeedPerSec <= 0f ||
456 dt <= 0f)
457 {
458 _mover.SetPosition(_state.Script.Target);
459
460 _state.Velocity = Vector2.Zero;
461 _state.Acceleration = Vector2.Zero;
462
463 Step(0f);
464 CompleteScript();
465
466 return true;
467 }
468
469 var stepLength =
470 _state.Script.SpeedPerSec * dt;
471
472 if (stepLength >= distance)
473 {
474 _mover.SetPosition(_state.Script.Target);
475
476 _state.Velocity = Vector2.Zero;
477 _state.Acceleration = Vector2.Zero;
478
479 Step(0f);
480 CompleteScript();
481
482 return true;
483 }
484
485 var direction = to / distance;
486
487 _state.Acceleration = Vector2.Zero;
488
489 _state.Velocity =
490 direction *
491 _state.Script.SpeedPerSec;
492
493 Step(dt);
494
495 return true;
496 }
497
498 private void CompleteScript()
499 {
500 var script = _state.Script;
501 var completed = _scriptCompleted;
502
503 // Clear the completed movement before invoking external code.
504 // Event handlers and callbacks may safely begin another movement
505 // without that new movement subsequently being erased.
506 _state.Script = default;
507 _scriptCompleted = null;
508
509 ScriptedMovementStopped?.Invoke(script);
510 completed?.Invoke(script);
511 }
512}
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....
Definition EasingKind.cs:9
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...