14 private Func<Vector2>? _followPixel;
15 private Vector2 _followOffsetPx;
16 private bool _followHard;
17 private float _followSpeedPxPerSec;
18 private float _followSnapPx;
21 private IMovableOnSceneLayer? _followGridTarget;
22 private Vector2 _followGridOffset;
23 private float _followSpeedTilesPerSec;
24 private float _followSnapTiles;
27 private Func<float, float>? _followEasing;
28 private float _followDurationSec;
46 Func<Vector2> getPixelPos,
49 Vector2 offsetPx =
default)
52 throw new ArgumentOutOfRangeException(nameof(speed));
54 _followPixel = getPixelPos ??
throw new ArgumentNullException(nameof(getPixelPos));
55 _followGridTarget =
null;
60 _followOffsetPx = offsetPx;
61 _followSpeedPxPerSec = speed;
62 _followSnapPx = MathF.Max(0f, snap);
63 _followSpeedTilesPerSec = 0f;
64 _followSnapTiles = 0f;
69 _followOffsetPx = Vector2.Zero;
70 _followSpeedTilesPerSec = speed;
71 _followSnapTiles = MathF.Max(0f, snap);
72 _followSpeedPxPerSec = 0f;
78 _followDurationSec = 0f;
98 Func<Vector2> getPixelPos,
100 Func<float, float>? easing =
null,
102 Vector2 offsetPx =
default)
104 if (durationSec <= 0f)
105 throw new ArgumentOutOfRangeException(nameof(durationSec));
107 _followPixel = getPixelPos ??
throw new ArgumentNullException(nameof(getPixelPos));
108 _followGridTarget =
null;
111 _followOffsetPx = offsetPx;
112 _followEasing = easing ?? EasingFunctions.Linear;
113 _followDurationSec = durationSec;
118 _followSnapPx = MathF.Max(0f, snap);
119 _followSnapTiles = 0f;
123 _followSnapTiles = MathF.Max(0f, snap);
128 _followSpeedPxPerSec = 0f;
129 _followSpeedTilesPerSec = 0f;
149 Func<Vector2> getPixelPos,
153 Vector2 offsetPx =
default)
155 var easing = EasingFunctions.From(easingKind);
174 _followPixel = getPixelPos ??
throw new ArgumentNullException(nameof(getPixelPos));
175 _followGridTarget =
null;
178 _followOffsetPx = (_mover.PositionSpace ==
MovementSpace.Pixel) ? offsetPx : Vector2.Zero;
181 _followSpeedPxPerSec = 0f;
183 _followSpeedTilesPerSec = 0f;
184 _followSnapTiles = 0f;
185 _followEasing =
null;
186 _followDurationSec = 0f;
202 float speedTilesPerSec,
203 float snapTiles = 0.25f,
204 Vector2 gridOffset =
default,
205 Vector2 pixelOffset =
default)
207 if (speedTilesPerSec <= 0)
208 throw new ArgumentOutOfRangeException(nameof(speedTilesPerSec));
210 _followGridTarget = tileTarget ??
throw new ArgumentNullException(nameof(tileTarget));
211 _followGridOffset = gridOffset;
215 _followSpeedTilesPerSec = speedTilesPerSec;
216 _followSnapTiles = MathF.Max(0f, snapTiles);
217 _followSpeedPxPerSec = 0f;
220 _followOffsetPx = (_mover.PositionSpace ==
MovementSpace.Pixel) ? pixelOffset : Vector2.Zero;
223 _followEasing =
null;
224 _followDurationSec = 0f;
243 Func<float, float>? easing =
null,
245 Vector2 gridOffset =
default,
246 Vector2 pixelOffset =
default)
248 if (durationSec <= 0f)
249 throw new ArgumentOutOfRangeException(nameof(durationSec));
251 _followGridTarget = tileTarget ??
throw new ArgumentNullException(nameof(tileTarget));
252 _followGridOffset = gridOffset;
256 _followOffsetPx = (_mover.PositionSpace ==
MovementSpace.Pixel) ? pixelOffset : Vector2.Zero;
258 _followEasing = easing ?? EasingFunctions.Linear;
259 _followDurationSec = durationSec;
265 _followSnapPx = MathF.Max(0f, snap);
266 _followSnapTiles = MathF.Max(0f, snap);
269 _followSpeedPxPerSec = 0f;
270 _followSpeedTilesPerSec = 0f;
290 Vector2 gridOffset =
default,
291 Vector2 pixelOffset =
default)
293 var easing = EasingFunctions.From(easingKind);
315 Vector2 gridOffset =
default,
316 Vector2 pixelOffset =
default)
318 _followGridTarget = tileTarget ??
throw new ArgumentNullException(nameof(tileTarget));
319 _followGridOffset = gridOffset;
323 _followOffsetPx = (_mover.PositionSpace ==
MovementSpace.Pixel) ? pixelOffset : Vector2.Zero;
326 _followSpeedTilesPerSec = 0f;
327 _followSnapTiles = 0f;
328 _followSpeedPxPerSec = 0f;
330 _followEasing =
null;
331 _followDurationSec = 0f;
341 _followGridTarget =
null;
342 _followEasing =
null;
343 _followDurationSec = 0f;
346 _followSpeedPxPerSec = 0f;
348 _followSpeedTilesPerSec = 0f;
349 _followSnapTiles = 0f;
350 _followOffsetPx = Vector2.Zero;
351 _followGridOffset = Vector2.Zero;
356 private bool AdvanceFollow()
359 if (_followPixel is not
null)
361 var goalPx = _followPixel() + _followOffsetPx;
366 return Advance_TargetPixel_MoverGrid(goalPx);
371 return Advance_TargetPixel_MoverPixel(goalPx);
376 if (_followGridTarget is not
null)
378 var layer = _followGridTarget.SceneLayer;
381 var goalCoordinates = _followGridTarget.GetPosition() + _followGridOffset;
386 return Advance_TargetGrid_MoverGrid(goalCoordinates);
391 return Advance_TargetGrid_MoverPixel(goalCoordinates, layer);
398 private bool Advance_TargetPixel_MoverGrid(Vector2 goalPx)
401 if (_sceneLayer is
null || _coords is
null)
402 throw new InvalidOperationException(
"Pixel->Grid follow requires scene layer/coords.");
404 var gridPt = _coords.GetSceneLayerCoordinatesAtPixel(_sceneLayer,
new PointF(goalPx.X, goalPx.Y));
405 var goalGrid =
new Vector2(gridPt.X, gridPt.Y);
409 _mover.SetPosition(goalGrid);
413 if (_followEasing is not
null && _followDurationSec > 0f)
424 MoveToward(goalGrid, _followSpeedTilesPerSec, _followSnapTiles);
428 private bool Advance_TargetPixel_MoverPixel(Vector2 goalPx)
432 _mover.SetPosition(goalPx);
437 if (_followEasing is not
null && _followDurationSec > 0f)
448 MoveToward(goalPx, _followSpeedPxPerSec, _followSnapPx);
454 private bool Advance_TargetGrid_MoverGrid(Vector2 goalCoordinates)
458 _mover.SetPosition(goalCoordinates);
462 if (_followEasing is not
null && _followDurationSec > 0f)
474 MoveToward(goalCoordinates, _followSpeedTilesPerSec, _followSnapTiles);
478 private bool Advance_TargetGrid_MoverPixel(Vector2 goalCoordinates, SceneLayer sceneLayer)
480 var targetPx = sceneLayer.CoordinateSystem.GetAnchorPixelAtSceneLayerCoordinates(
482 new PointF(goalCoordinates.X, goalCoordinates.Y));
484 var goalPx =
new Vector2(targetPx.X, targetPx.Y) + _followOffsetPx;
488 _mover.SetPosition(goalPx);
492 if (_followEasing is not
null && _followDurationSec > 0f)
504 var pxRight = sceneLayer.CoordinateSystem.GetAnchorPixelAtSceneLayerCoordinates(
506 new PointF(goalCoordinates.X + 1f, goalCoordinates.Y));
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;
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.
EasingKind
Enumeration of all supported easing functions for animation and movement interpolation....
MovementSpace
Specifies the coordinate system used for movement calculations and updates.