From 0d14b81d19767b0c91fcb992aa9c4a7393c4792a Mon Sep 17 00:00:00 2001 From: Paul Cento Date: Thu, 3 Aug 2023 14:06:49 +0200 Subject: [PATCH] only check for wrapping when camera moves, better naming, comment main wrap function --- C7/Map/MapViewCamera.cs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/C7/Map/MapViewCamera.cs b/C7/Map/MapViewCamera.cs index 7e77027b..a42348af 100644 --- a/C7/Map/MapViewCamera.cs +++ b/C7/Map/MapViewCamera.cs @@ -15,12 +15,12 @@ public partial class MapViewCamera : Camera2D { private readonly float minZoom = 0.2f; public float zoomFactor { get; private set; } = 1.0f; private MapView map; - private int wrapLeeway = 2; // leeway in number of tiles to consider camera at map edge + private int wrapEdgeTileMargin = 2; // margin in number of tiles to trigger map wrapping private HorizontalWrapState hwrap = HorizontalWrapState.None; public void attachToMapView(MapView map) { this.map = map; - worldWrapLeeway = wrapLeeway * map.tileSize.X; + wrapEdgeMargin = wrapEdgeTileMargin * map.tileSize.X; map.updateAnimations(); checkWorldWrap(); } @@ -47,16 +47,23 @@ public void setPosition(Vector2 position) { Position = position; checkWorldWrap(); } - private int worldWrapLeeway = 0; - - private bool enteringRightWrap(Rect2 v) => hwrap != HorizontalWrapState.Right && v.End.X >= map.worldEdgeRight - worldWrapLeeway; - private bool enteringLeftWrap(Rect2 v) => hwrap != HorizontalWrapState.Left && v.Position.X <= map.worldEdgeLeft + worldWrapLeeway; - private bool atEdgeOfRightWrap(Rect2 v) => hwrap == HorizontalWrapState.Right && v.End.X >= map.worldEdgeRight + map.pixelWidth; - private bool atEdgeOfLeftWrap(Rect2 v) => hwrap == HorizontalWrapState.Left && v.Position.X <= map.worldEdgeLeft - map.pixelWidth; + private float wrapEdgeMargin = 0; + float wrapRightEdge {get => map.worldEdgeRight - wrapEdgeMargin; } + float wrapLeftEdge {get => map.worldEdgeLeft + wrapEdgeMargin; } + private bool enteringRightWrap(Rect2 v) => hwrap != HorizontalWrapState.Right && v.End.X >= wrapRightEdge; + private bool enteringLeftWrap(Rect2 v) => hwrap != HorizontalWrapState.Left && v.Position.X <= wrapLeftEdge; + private bool atEdgeOfRightWrap(Rect2 v) => hwrap == HorizontalWrapState.Right && v.End.X >= wrapRightEdge + map.pixelWidth; + private bool atEdgeOfLeftWrap(Rect2 v) => hwrap == HorizontalWrapState.Left && v.Position.X <= wrapLeftEdge - map.pixelWidth; private HorizontalWrapState currentHwrap(Rect2 v) { - return v.Position.X <= map.worldEdgeLeft + worldWrapLeeway ? HorizontalWrapState.Left : (v.End.X >= map.worldEdgeRight - worldWrapLeeway ? HorizontalWrapState.Right : HorizontalWrapState.None); + return v.Position.X <= wrapLeftEdge ? HorizontalWrapState.Left : (v.End.X >= wrapRightEdge ? HorizontalWrapState.Right : HorizontalWrapState.None); } + // checkWorldWrap determines if the camera is about to spill over the world map and will: + // - move the second "wrap" tilemap to the appropriate edge + // to give the illusion of true wrapping tilemap + // - teleport the camera one world-width to the left or right when + // only the "wrap" tilemap is in view + private void checkWorldWrap() { if (map is null || !map.wrapHorizontally) { // TODO: for maps that do not wrap horizontally restrict movement @@ -64,10 +71,8 @@ private void checkWorldWrap() { } Rect2 visibleWorld = getVisibleWorld(); if (enteringRightWrap(visibleWorld)) { - GD.Print("moving wrap to right"); map.setHorizontalWrap(HorizontalWrapState.Right); } else if (enteringLeftWrap(visibleWorld)) { - GD.Print("moving wrap to left"); map.setHorizontalWrap(HorizontalWrapState.Left); } if (atEdgeOfRightWrap(visibleWorld)) { @@ -78,10 +83,6 @@ private void checkWorldWrap() { hwrap = currentHwrap(visibleWorld); } - public override void _Process(double delta) { - checkWorldWrap(); - } - public override void _UnhandledInput(InputEvent @event) { switch (@event) { case InputEventMouseMotion mouseDrag when mouseDrag.ButtonMask == MouseButtonMask.Left: