15public sealed class Camera
17 private readonly
Scene _scene;
18 private PointF _positionPx =
new(0, 0);
19 private Func<PointF>? _followWorldPx;
20 private bool _hardFollow;
22 private int _followAxis;
23 private PointF _followAnchor =
new(0.5f, 0.5f);
24 private SceneLayer? TopologyLayer => _followLayer ?? _scene.SceneLayers.FirstOrDefault(layer => layer.Visible);
28 private PointF? _panTargetUpperLeftPx;
29 private float _panLerpPerSecond;
41 if (_positionPx != value)
45 if (_scene is not
null)
46 _scene.FullRefreshNeeded =
true;
65 public Rectangle
DeadZonePx {
get;
set; } = Rectangle.Empty;
74 internal Camera(
Scene scene) => _scene = scene ??
throw new ArgumentNullException(nameof(scene));
80 internal Func<SizeF> GetVisibleWorldSizePx {
get;
set; } = () =>
new SizeF(1280, 720);
82 #region Camera movement methods
92 public void SnapTo(PointF worldUpperLeftPx)
94 PositionPx = ClampToWorldBounds(worldUpperLeftPx);
107 var vis = GetVisibleWorldSizePx();
111 worldCenterPx.X - vis.Width * 0.5f,
112 worldCenterPx.Y - vis.Height * 0.5f));
127 float tileCenterX = anchor.X + layer.TileWidth * 0.5f;
128 float tileCenterY = anchor.Y + layer.TileHeight * 0.5f;
130 var vis = GetVisibleWorldSizePx();
133 var camTopLeft =
new PointF(
134 tileCenterX - vis.Width * 0.5f,
135 tileCenterY - vis.Height * 0.5f);
157 Follow(() => worldCenterPx, hardFollow:
false);
176 float tileCenterX = anchor.X + layer.TileWidth * 0.5f;
177 float tileCenterY = anchor.Y + layer.TileHeight * 0.5f;
179 PanCenterTo(
new PointF(tileCenterX, tileCenterY), speed);
194 public void PanTo(PointF worldTopLeftPx,
float speed)
199 _followWorldPx =
null;
204 _panTargetUpperLeftPx =
null;
209 _panLerpPerSecond = speed;
210 _panTargetUpperLeftPx = ClampToWorldBounds(worldTopLeftPx);
231 var vis = GetVisibleWorldSizePx();
233 var desiredUpperLeft =
new PointF(
234 worldCenterPx.X - vis.Width * 0.5f,
235 worldCenterPx.Y - vis.Height * 0.5f);
238 PanTo(desiredUpperLeft, speed);
256 if (durationSeconds <= 0f)
259 _panTargetUpperLeftPx =
null;
271 float k = -(float)Math.Log(0.01f) / durationSeconds;
276 _followWorldPx =
null;
279 _panLerpPerSecond = k;
280 _panTargetUpperLeftPx = ClampToWorldBounds(worldTopLeftPx);
300 var vis = GetVisibleWorldSizePx();
302 var desiredUpperLeft =
new PointF(
303 worldCenterPx.X - vis.Width * 0.5f,
304 worldCenterPx.Y - vis.Height * 0.5f);
328 float durationSeconds)
331 throw new ArgumentNullException(nameof(layer));
336 float tileCenterX = anchor.X + layer.TileWidth * 0.5f;
337 float tileCenterY = anchor.Y + layer.TileHeight * 0.5f;
361 internal void CancelPan()
363 _panTargetUpperLeftPx =
null;
364 _panLerpPerSecond = 0f;
382 public void Follow(Func<PointF> getWorldPixel,
bool hardFollow =
false)
384 FollowAt(getWorldPixel,
new PointF(0.5f, 0.5f), hardFollow);
410 public void FollowAt(Func<PointF> getWorldPixel, PointF viewportAnchor,
bool hardFollow =
false)
412 ArgumentNullException.ThrowIfNull(getWorldPixel);
413 ValidateViewportAnchor(viewportAnchor, nameof(viewportAnchor));
417 _followWorldPx = getWorldPixel;
418 _followAnchor = viewportAnchor;
419 _hardFollow = hardFollow;
430 public void FollowTo(PointF worldPointOfInterestPx)
433 Follow(() => worldPointOfInterestPx);
448 new PointF(0.5f, 0.5f),
470 PointF viewportAnchor,
474 ArgumentNullException.ThrowIfNull(target);
475 ValidateViewportAnchor(viewportAnchor, nameof(viewportAnchor));
487 ? GetCenteredTile(layer, pos)
488 :
new PointF(pos.X, pos.Y);
529 float horizontalAnchor,
533 ArgumentNullException.ThrowIfNull(target);
534 ValidateNormalizedAnchor(horizontalAnchor, nameof(horizontalAnchor));
549 var anchor = layer.GridToWorldPx(
new PointF(pos.X, pos.Y));
550 worldCenterX = anchor.X + layer.TileWidth * 0.5f;
554 worldCenterX = pos.X;
557 var vis = GetVisibleWorldSizePx();
563 new PointF(horizontalAnchor, 0.5f),
603 float verticalAnchor,
607 ArgumentNullException.ThrowIfNull(target);
608 ValidateNormalizedAnchor(verticalAnchor, nameof(verticalAnchor));
623 var anchor = layer.GridToWorldPx(
new PointF(pos.X, pos.Y));
624 worldCenterY = anchor.Y + layer.TileHeight * 0.5f;
628 worldCenterY = pos.Y;
631 var vis = GetVisibleWorldSizePx();
637 new PointF(0.5f, verticalAnchor),
652 _followWorldPx =
null;
653 _followAnchor =
new PointF(0.5f, 0.5f);
654 _panTargetUpperLeftPx =
null;
658 #endregion Camera movement methods
660 internal void Update(
float dtSeconds)
663 if (_panTargetUpperLeftPx is { } panTarget)
665 var clamped = ClampToWorldBounds(panTarget);
667 if (_hardFollow || _panLerpPerSecond <= 0f)
670 _panTargetUpperLeftPx =
null;
674 float t = 1f - (float)Math.Exp(-_panLerpPerSecond * Math.Max(0f, dtSeconds));
676 var newPos =
new PointF(
683 if (Math.Abs(newPos.X - clamped.X) < 0.5f &&
684 Math.Abs(newPos.Y - clamped.Y) < 0.5f)
687 _panTargetUpperLeftPx =
null;
692 if (_panTargetUpperLeftPx is not
null)
697 if (_followWorldPx is
null)
700 var target = _followWorldPx();
702 if (TopologyLayer is { } layer && (layer.WrapHorizontally || layer.WrapVertically))
704 var visible = GetVisibleWorldSizePx();
706 var followReference =
new PointF(
707 PositionPx.X + visible.Width * _followAnchor.X,
708 PositionPx.Y + visible.Height * _followAnchor.Y);
710 var period = layer.GetPeriod();
712 if (_followAxis == 1)
714 target = NearestWrappedByAxis(
720 target.Y = followReference.Y;
722 else if (_followAxis == 2)
724 target = NearestWrappedByAxis(
730 target.X = followReference.X;
734 target = period.Nearest(target, followReference);
738 var desiredUL = DesiredUpperLeftToContainTarget(target);
747 var clamped = ClampToWorldBounds(desiredUL);
755 #region private methods
757 private PointF DesiredUpperLeftToContainTarget(PointF targetWorldPx)
759 var vis = GetVisibleWorldSizePx();
763 targetWorldPx.X - vis.Width * _followAnchor.X,
764 targetWorldPx.Y - vis.Height * _followAnchor.Y);
766 var viewWorld =
new RectangleF(
772 var dzWorld =
new RectangleF(
778 if (dzWorld.Contains(targetWorldPx))
784 if (targetWorldPx.X < dzWorld.Left)
785 newX -= dzWorld.Left - targetWorldPx.X;
787 if (targetWorldPx.X > dzWorld.Right)
788 newX += targetWorldPx.X - dzWorld.Right;
790 if (targetWorldPx.Y < dzWorld.Top)
791 newY -= dzWorld.Top - targetWorldPx.Y;
793 if (targetWorldPx.Y > dzWorld.Bottom)
794 newY += targetWorldPx.Y - dzWorld.Bottom;
796 return new PointF(newX, newY);
799 private static PointF NearestWrappedByAxis(
805 var delta =
new PointF(reference.X - point.X, reference.Y - point.Y);
806 var coefficients = period.Coefficients(delta);
807 int baseColumn = (int)Math.Round(coefficients.X);
808 int baseRow = (int)Math.Round(coefficients.Y);
810 int minColumn = period.WrapColumns ? baseColumn - 4 : 0;
811 int maxColumn = period.WrapColumns ? baseColumn + 4 : 0;
812 int minRow = period.WrapRows ? baseRow - 4 : 0;
813 int maxRow = period.WrapRows ? baseRow + 4 : 0;
815 var best = period.Nearest(point, reference);
816 float bestAxisDistance = followX ? Math.Abs(best.X - reference.X) : Math.Abs(best.Y - reference.Y);
818 float bestDistanceSquared = (best.X - reference.X) * (best.X - reference.X) +
819 (best.Y - reference.Y) * (best.Y - reference.Y);
821 for (
int c = minColumn; c <= maxColumn; c++)
822 for (
int r = minRow; r <= maxRow; r++)
824 var offset = period.Offset(c, r);
825 var candidate =
new PointF(point.X + offset.X, point.Y + offset.Y);
826 float axisDistance = followX ? Math.Abs(candidate.X - reference.X) : Math.Abs(candidate.Y - reference.Y);
828 float distanceSquared = (candidate.X - reference.X) * (candidate.X - reference.X) +
829 (candidate.Y - reference.Y) * (candidate.Y - reference.Y);
831 if (axisDistance < bestAxisDistance ||
832 (axisDistance == bestAxisDistance && distanceSquared < bestDistanceSquared))
835 bestAxisDistance = axisDistance;
836 bestDistanceSquared = distanceSquared;
843 private PointF ClampToWorldBounds(PointF ul)
848 var vis = GetVisibleWorldSizePx();
850 if (TopologyLayer is { } layer && (layer.WrapHorizontally || layer.WrapVertically))
852 var period = layer.GetPeriod();
853 var position = period.Coefficients(ul);
855 PointF[] Corners(RectangleF rect) =>
new[]
857 period.Coefficients(
new(rect.Left, rect.Top)), period.Coefficients(
new(rect.Right, rect.Top)),
858 period.Coefficients(
new(rect.Left, rect.Bottom)), period.Coefficients(
new(rect.Right, rect.Bottom))
863 var viewport = Corners(
870 float lowX = bounds.Min(p => p.X) - viewport.Min(p => p.X);
871 float lowY = bounds.Min(p => p.Y) - viewport.Min(p => p.Y);
872 float highX = Math.Max(lowX, bounds.Max(p => p.X) - viewport.Max(p => p.X));
873 float highY = Math.Max(lowY, bounds.Max(p => p.Y) - viewport.Max(p => p.Y));
875 return period.Offset(
876 layer.WrapHorizontally ? position.X : Math.Clamp(position.X, lowX, highX),
877 layer.WrapVertically ? position.Y : Math.Clamp(position.Y, lowY, highY));
882 float maxX = WorldBoundsPx.Right - vis.Width;
883 float maxY = WorldBoundsPx.Bottom - vis.Height;
892 Math.Clamp(ul.X, minX, maxX),
893 Math.Clamp(ul.Y, minY, maxY));
896 private static void ValidateViewportAnchor(PointF viewportAnchor,
string paramName)
898 ValidateNormalizedAnchor(viewportAnchor.X, paramName);
899 ValidateNormalizedAnchor(viewportAnchor.Y, paramName);
902 private static void ValidateNormalizedAnchor(
float value,
string paramName)
904 if (!
float.IsFinite(value) || value < 0f || value > 1f)
905 throw new ArgumentOutOfRangeException(
908 "Camera follow anchors must be finite values from 0 through 1.");
918 private static PointF GetCenteredTile(SceneLayer layer, Vector2 gridPos)
920 var anchor = layer.
GridToWorldPx(
new PointF(gridPos.X, gridPos.Y));
927 #endregion private methods