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.Follow.cs
1using System.Drawing;
2using System.Numerics;
5
7
11public sealed partial class MovementController
12{
13 // --- Pixel-follow (DirectDrawing → DirectDrawing)
14 private Func<Vector2>? _followPixel; // returns current target pixel position each frame
15 private Vector2 _followOffsetPx;
16 private bool _followHard;
17 private float _followSpeedPxPerSec;
18 private float _followSnapPx;
19
20 // Grid-follow (Scene-layer → this DirectDrawing in pixels)
21 private IMovableOnSceneLayer? _followGridTarget;
22 private Vector2 _followGridOffset;
23 private float _followSpeedTilesPerSec;
24 private float _followSnapTiles;
25
26 // Easing follow
27 private Func<float, float>? _followEasing; // optional easing curve
28 private float _followDurationSec; // duration for easing-based follow
29
45 public void FollowPixelSoft(
46 Func<Vector2> getPixelPos,
47 float speed,
48 float snap = 0.5f,
49 Vector2 offsetPx = default)
50 {
51 if (speed <= 0)
52 throw new ArgumentOutOfRangeException(nameof(speed));
53
54 _followPixel = getPixelPos ?? throw new ArgumentNullException(nameof(getPixelPos));
55 _followGridTarget = null;
56 _followHard = false;
57
58 if (_mover.PositionSpace == MovementSpace.Pixel)
59 {
60 _followOffsetPx = offsetPx;
61 _followSpeedPxPerSec = speed;
62 _followSnapPx = MathF.Max(0f, snap);
63 _followSpeedTilesPerSec = 0f;
64 _followSnapTiles = 0f;
65 }
66 else
67 {
68 // grid follower chasing a pixel target: interpret speed/snap as tiles-based
69 _followOffsetPx = Vector2.Zero; // pixel offset not used for grid follower
70 _followSpeedTilesPerSec = speed;
71 _followSnapTiles = MathF.Max(0f, snap);
72 _followSpeedPxPerSec = 0f;
73 _followSnapPx = 0f;
74 }
75
76 // clear tween state
77 _followEasing = null;
78 _followDurationSec = 0f;
79 }
80
97 public void FollowPixelSoft(
98 Func<Vector2> getPixelPos,
99 float durationSec,
100 Func<float, float>? easing = null,
101 float snap = 0.5f,
102 Vector2 offsetPx = default)
103 {
104 if (durationSec <= 0f)
105 throw new ArgumentOutOfRangeException(nameof(durationSec));
106
107 _followPixel = getPixelPos ?? throw new ArgumentNullException(nameof(getPixelPos));
108 _followGridTarget = null;
109 _followHard = false;
110
111 _followOffsetPx = offsetPx; // only applied for pixel followers
112 _followEasing = easing ?? EasingFunctions.Linear;
113 _followDurationSec = durationSec;
114
115 // Interpret snap in the follower’s space
116 if (_mover.PositionSpace == MovementSpace.Pixel)
117 {
118 _followSnapPx = MathF.Max(0f, snap);
119 _followSnapTiles = 0f;
120 }
121 else
122 {
123 _followSnapTiles = MathF.Max(0f, snap);
124 _followSnapPx = 0f;
125 }
126
127 // neutralize speed fields
128 _followSpeedPxPerSec = 0f;
129 _followSpeedTilesPerSec = 0f;
130 }
131
148 public void FollowPixelSoft(
149 Func<Vector2> getPixelPos,
150 float durationSec,
151 EasingKind easingKind,
152 float snap = 0.5f,
153 Vector2 offsetPx = default)
154 {
155 var easing = EasingFunctions.From(easingKind);
156
158 getPixelPos,
159 durationSec,
160 easing,
161 snap,
162 offsetPx);
163 }
164
172 public void FollowPixelHard(Func<Vector2> getPixelPos, Vector2 offsetPx = default)
173 {
174 _followPixel = getPixelPos ?? throw new ArgumentNullException(nameof(getPixelPos));
175 _followGridTarget = null;
176 _followHard = true;
177
178 _followOffsetPx = (_mover.PositionSpace == MovementSpace.Pixel) ? offsetPx : Vector2.Zero;
179
180 // neutralize speed/tween fields
181 _followSpeedPxPerSec = 0f;
182 _followSnapPx = 0f;
183 _followSpeedTilesPerSec = 0f;
184 _followSnapTiles = 0f;
185 _followEasing = null;
186 _followDurationSec = 0f;
187 }
188
200 public void FollowTileSoft(
201 IMovableOnSceneLayer tileTarget,
202 float speedTilesPerSec,
203 float snapTiles = 0.25f,
204 Vector2 gridOffset = default,
205 Vector2 pixelOffset = default)
206 {
207 if (speedTilesPerSec <= 0)
208 throw new ArgumentOutOfRangeException(nameof(speedTilesPerSec));
209
210 _followGridTarget = tileTarget ?? throw new ArgumentNullException(nameof(tileTarget));
211 _followGridOffset = gridOffset;
212 _followPixel = null;
213 _followHard = false;
214
215 _followSpeedTilesPerSec = speedTilesPerSec;
216 _followSnapTiles = MathF.Max(0f, snapTiles);
217 _followSpeedPxPerSec = 0f;
218 _followSnapPx = 0f;
219
220 _followOffsetPx = (_mover.PositionSpace == MovementSpace.Pixel) ? pixelOffset : Vector2.Zero;
221
222 // clear tween state
223 _followEasing = null;
224 _followDurationSec = 0f;
225 }
226
240 public void FollowTileSoft(
241 IMovableOnSceneLayer tileTarget,
242 float durationSec,
243 Func<float, float>? easing = null,
244 float snap = 0.5f,
245 Vector2 gridOffset = default,
246 Vector2 pixelOffset = default)
247 {
248 if (durationSec <= 0f)
249 throw new ArgumentOutOfRangeException(nameof(durationSec));
250
251 _followGridTarget = tileTarget ?? throw new ArgumentNullException(nameof(tileTarget));
252 _followGridOffset = gridOffset;
253 _followPixel = null;
254 _followHard = false;
255
256 _followOffsetPx = (_mover.PositionSpace == MovementSpace.Pixel) ? pixelOffset : Vector2.Zero;
257
258 _followEasing = easing ?? EasingFunctions.Linear;
259 _followDurationSec = durationSec;
260
261 // snap in follower’s space at runtime:
262 // - pixel follower uses _followSnapPx
263 // - grid follower uses _followSnapTiles
264 // so just store both clamped and let the branch pick the right one:
265 _followSnapPx = MathF.Max(0f, snap);
266 _followSnapTiles = MathF.Max(0f, snap);
267
268 // neutralize speed fields
269 _followSpeedPxPerSec = 0f;
270 _followSpeedTilesPerSec = 0f;
271 }
272
285 public void FollowTileSoft(
286 IMovableOnSceneLayer tileTarget,
287 float durationSec,
288 EasingKind easingKind,
289 float snap = 0.5f,
290 Vector2 gridOffset = default,
291 Vector2 pixelOffset = default)
292 {
293 var easing = EasingFunctions.From(easingKind);
294
296 tileTarget,
297 durationSec,
298 easing,
299 snap,
300 gridOffset,
301 pixelOffset);
302 }
303
313 public void FollowTileHard(
314 IMovableOnSceneLayer tileTarget,
315 Vector2 gridOffset = default,
316 Vector2 pixelOffset = default)
317 {
318 _followGridTarget = tileTarget ?? throw new ArgumentNullException(nameof(tileTarget));
319 _followGridOffset = gridOffset;
320 _followPixel = null;
321 _followHard = true;
322
323 _followOffsetPx = (_mover.PositionSpace == MovementSpace.Pixel) ? pixelOffset : Vector2.Zero;
324
325 // neutralize speed/tween fields
326 _followSpeedTilesPerSec = 0f;
327 _followSnapTiles = 0f;
328 _followSpeedPxPerSec = 0f;
329 _followSnapPx = 0f;
330 _followEasing = null;
331 _followDurationSec = 0f;
332 }
333
338 public void Unfollow()
339 {
340 _followPixel = null;
341 _followGridTarget = null;
342 _followEasing = null;
343 _followDurationSec = 0f;
344 _followHard = false;
345
346 _followSpeedPxPerSec = 0f;
347 _followSnapPx = 0f;
348 _followSpeedTilesPerSec = 0f;
349 _followSnapTiles = 0f;
350 _followOffsetPx = Vector2.Zero;
351 _followGridOffset = Vector2.Zero;
352
353 CancelScript();
354 }
355
356 private bool AdvanceFollow()
357 {
358 // --- Pixel-follow ---
359 if (_followPixel is not null)
360 {
361 var goalPx = _followPixel() + _followOffsetPx;
362
363 if (_mover.PositionSpace == MovementSpace.Grid)
364 {
365 // grid-space mover following pixel goal...
366 return Advance_TargetPixel_MoverGrid(goalPx);
367 }
368 else
369 {
370 // pixel mover following pixel goal...
371 return Advance_TargetPixel_MoverPixel(goalPx);
372 }
373 }
374
375 // --- Grid-follow (target provides GRID coords each frame) ---
376 if (_followGridTarget is not null)
377 {
378 var layer = _followGridTarget.SceneLayer;
379
380 // target grid coordinate (+ any grid offset)
381 var goalCoordinates = _followGridTarget.GetPosition() + _followGridOffset;
382
383 if (_mover.PositionSpace == MovementSpace.Grid)
384 {
385 // GRID follower -> set/use GRID directly (no conversion)
386 return Advance_TargetGrid_MoverGrid(goalCoordinates);
387 }
388 else
389 {
390 // PIXEL follower -> convert GRID -> PIXEL, then apply optional pixel offset
391 return Advance_TargetGrid_MoverPixel(goalCoordinates, layer);
392 }
393 }
394
395 return false; // not following this frame
396 }
397
398 private bool Advance_TargetPixel_MoverGrid(Vector2 goalPx)
399 {
400 // follower is grid-space: convert pixel -> grid
401 if (_sceneLayer is null || _coords is null)
402 throw new InvalidOperationException("Pixel->Grid follow requires scene layer/coords.");
403
404 var gridPt = _coords.GetSceneLayerCoordinatesAtPixel(_sceneLayer, new PointF(goalPx.X, goalPx.Y));
405 var goalGrid = new Vector2(gridPt.X, gridPt.Y);
406
407 if (_followHard)
408 {
409 _mover.SetPosition(goalGrid);
410 return true;
411 }
412
413 if (_followEasing is not null && _followDurationSec > 0f)
414 {
415 MoveTo(
416 goalGrid,
417 _followDurationSec,
418 _followEasing,
419 _followSnapTiles);
420
421 return false;
422 }
423
424 MoveToward(goalGrid, _followSpeedTilesPerSec, _followSnapTiles);
425 return false;
426 }
427
428 private bool Advance_TargetPixel_MoverPixel(Vector2 goalPx)
429 {
430 if (_followHard)
431 {
432 _mover.SetPosition(goalPx);
433 return true;
434 }
435
436 // tweened follow
437 if (_followEasing is not null && _followDurationSec > 0f)
438 {
439 MoveTo(
440 goalPx,
441 _followDurationSec,
442 _followEasing,
443 _followSnapPx);
444 }
445 else
446 {
447 // constant-speed pursue
448 MoveToward(goalPx, _followSpeedPxPerSec, _followSnapPx);
449 }
450
451 return false; // we scheduled a script; let scripted stage consume this frame
452 }
453
454 private bool Advance_TargetGrid_MoverGrid(Vector2 goalCoordinates)
455 {
456 if (_followHard)
457 {
458 _mover.SetPosition(goalCoordinates);
459 return true;
460 }
461
462 if (_followEasing is not null && _followDurationSec > 0f)
463 {
464 MoveTo(
465 goalCoordinates,
466 _followDurationSec,
467 _followEasing,
468 _followSnapTiles);
469
470 return false;
471 }
472
473 // soft follow in tiles/sec and snap in tiles
474 MoveToward(goalCoordinates, _followSpeedTilesPerSec, _followSnapTiles);
475 return false;
476 }
477
478 private bool Advance_TargetGrid_MoverPixel(Vector2 goalCoordinates, SceneLayer sceneLayer)
479 {
480 var targetPx = sceneLayer.CoordinateSystem.GetAnchorPixelAtSceneLayerCoordinates(
481 sceneLayer,
482 new PointF(goalCoordinates.X, goalCoordinates.Y));
483
484 var goalPx = new Vector2(targetPx.X, targetPx.Y) + _followOffsetPx;
485
486 if (_followHard)
487 {
488 _mover.SetPosition(goalPx);
489 return true;
490 }
491
492 if (_followEasing is not null && _followDurationSec > 0f)
493 {
494 MoveTo(
495 goalPx,
496 _followDurationSec,
497 _followEasing,
498 _followSnapPx);
499
500 return false;
501 }
502
503 // tiles/sec → px/sec (sample +X to estimate px-per-tile length)
504 var pxRight = sceneLayer.CoordinateSystem.GetAnchorPixelAtSceneLayerCoordinates(
505 sceneLayer,
506 new PointF(goalCoordinates.X + 1f, goalCoordinates.Y));
507
508 float pxPerTile = MathF.Max(1f, new Vector2(pxRight.X - targetPx.X, pxRight.Y - targetPx.Y).Length());
509 float speedPxPerSec = _followSpeedTilesPerSec * pxPerTile;
510 float snap = _followSnapTiles * pxPerTile;
511
512 MoveToward(goalPx, speedPxPerSec, snap);
513 return false;
514 }
515}
Central controller responsible for managing all movement modes for a single IMovable object....
void FollowTileSoft(IMovableOnSceneLayer tileTarget, float durationSec, Func< float, float >? easing=null, float snap=0.5f, Vector2 gridOffset=default, Vector2 pixelOffset=default)
Start following a grid (tile) target using a duration-based tween (easing). When easing is active,...
void FollowPixelSoft(Func< Vector2 > getPixelPos, float speed, float snap=0.5f, Vector2 offsetPx=default)
Start following a pixel-space target at a constant speed. The speed and snap units are interpreted ...
MovementController MoveToward(Vector2 target, float speedPerSec, float snapEpsilon=0.5f)
Begins scripted motion toward the specified target position at a constant speed.
void FollowTileSoft(IMovableOnSceneLayer tileTarget, float durationSec, EasingKind easingKind, float snap=0.5f, Vector2 gridOffset=default, Vector2 pixelOffset=default)
Convenience overload of FollowTileSoft(IMovableOnSceneLayer, float, Func<float, float>?...
void FollowPixelSoft(Func< Vector2 > getPixelPos, float durationSec, EasingKind easingKind, float snap=0.5f, Vector2 offsetPx=default)
Start following a pixel-space target using a duration-based tween (easing). When easing is active,...
void CancelScript()
Cancels any active scripted movement and clears the current script state. Raises ScriptedMovementStop...
void FollowPixelHard(Func< Vector2 > getPixelPos, Vector2 offsetPx=default)
Hard-follow a pixel-space target. The follower snaps directly to the target every update (no tweening...
void FollowPixelSoft(Func< Vector2 > getPixelPos, float durationSec, Func< float, float >? easing=null, float snap=0.5f, Vector2 offsetPx=default)
Start following a pixel-space target using a duration-based tween (easing). When easing is active,...
void FollowTileSoft(IMovableOnSceneLayer tileTarget, float speedTilesPerSec, float snapTiles=0.25f, Vector2 gridOffset=default, Vector2 pixelOffset=default)
Start following a grid (tile) target at a constant speed (tiles/sec). If the follower is pixel-space,...
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....
void FollowTileHard(IMovableOnSceneLayer tileTarget, Vector2 gridOffset=default, Vector2 pixelOffset=default)
Hard-follow a grid (tile) target. The follower snaps directly to the target grid coordinate each upda...
void Unfollow()
Stop following any target and clear all follow/tween state (targets, speeds, offsets,...
Implemented by grid-space IMovable objects that belong to a specific Scenes.SceneLayer.
MovementSpace PositionSpace
Which unit system this mover uses for its position.
Definition IMovable.cs:14
EasingKind
Enumeration of all supported easing functions for animation and movement interpolation....
Definition EasingKind.cs:9
MovementSpace
Specifies the coordinate system used for movement calculations and updates.