From a4eaccbb963c7bbc9be80f80c7b2848bbae893c3 Mon Sep 17 00:00:00 2001 From: Paul Cento Date: Thu, 3 Aug 2023 13:27:19 +0200 Subject: [PATCH] add tile leeway for wrapping to hide temporary map tear --- C7/Map/MapView.cs | 11 +++-------- C7/Map/MapViewCamera.cs | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/C7/Map/MapView.cs b/C7/Map/MapView.cs index 4dc04786..5ef5869a 100644 --- a/C7/Map/MapView.cs +++ b/C7/Map/MapView.cs @@ -30,7 +30,7 @@ public partial class MapView : Node2D { private TileMap tilemap; private TileMap wrappingTilemap; private TileSet tileset; - private Vector2I tileSize = new Vector2I(128, 64); + public Vector2I tileSize {get; private set;} = new Vector2I(128, 64); private ILogger log = LogManager.ForContext(); public bool wrapHorizontally {get; private set;} public int worldEdgeRight {get; private set;} @@ -178,13 +178,8 @@ public void updateAnimations() { } public void setHorizontalWrap(HorizontalWrapState state) { - if (state == HorizontalWrapState.None) { - wrappingTerrainTilemap.Hide(); - wrappingTilemap.Hide(); - } else { + if (state != HorizontalWrapState.None) { Vector2I offset = horizontalWrapOffset(state); - wrappingTerrainTilemap.Show(); - wrappingTilemap.Show(); wrappingTerrainTilemap.Position = terrainTilemap.Position + offset; wrappingTilemap.Position = tilemap.Position + offset; } @@ -220,7 +215,7 @@ private void initializeTileMap() { } } - setHorizontalWrap(HorizontalWrapState.None); + setHorizontalWrap(HorizontalWrapState.Right); // just put it somewhere tilemap.ZIndex = 10; // need to figure out a good way to order z indices wrappingTilemap.ZIndex = 10; diff --git a/C7/Map/MapViewCamera.cs b/C7/Map/MapViewCamera.cs index d487d99f..7e77027b 100644 --- a/C7/Map/MapViewCamera.cs +++ b/C7/Map/MapViewCamera.cs @@ -15,10 +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 HorizontalWrapState hwrap = HorizontalWrapState.None; public void attachToMapView(MapView map) { this.map = map; + worldWrapLeeway = wrapLeeway * map.tileSize.X; map.updateAnimations(); checkWorldWrap(); } @@ -45,13 +47,14 @@ 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; - private bool enteringLeftWrap(Rect2 v) => hwrap != HorizontalWrapState.Left && v.Position.X <= map.worldEdgeLeft; + 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 HorizontalWrapState currentHwrap(Rect2 v) { - return v.Position.X <= map.worldEdgeLeft ? HorizontalWrapState.Left : (v.End.X >= map.worldEdgeRight ? HorizontalWrapState.Right : HorizontalWrapState.None); + return v.Position.X <= map.worldEdgeLeft + worldWrapLeeway ? HorizontalWrapState.Left : (v.End.X >= map.worldEdgeRight - worldWrapLeeway ? HorizontalWrapState.Right : HorizontalWrapState.None); } private void checkWorldWrap() { @@ -61,8 +64,10 @@ 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)) { @@ -73,6 +78,10 @@ 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: