@@ -18,10 +18,18 @@ public partial class MapViewCamera : Camera2D {
1818 private int wrapEdgeTileMargin = 2 ; // margin in number of tiles to trigger map wrapping
1919 private HorizontalWrapState hwrap = HorizontalWrapState . None ;
2020
21- public void attachToMapView ( MapView map ) {
21+ public async void attachToMapView ( MapView map ) {
2222 this . map = map ;
2323 wrapEdgeMargin = wrapEdgeTileMargin * map . tileSize . X ;
2424 map . updateAnimations ( ) ;
25+
26+ // Awaiting a 0 second timer is a workaround to force GlobalCanvasTransform to be updated.
27+ // This is necessary when the camera's starting position is close to the edge of the map.
28+ // Without it, the GlobalCanvasTransform will not be updated until the camera is moved,
29+ // resulting in broken map wrapping. I tried awaiting "process_frame" but it does not seem
30+ // to work, although I believe that is what we want to do here. GPT-4 suggested waiting for
31+ // a 0 second timer, which seems to work.
32+ await ToSignal ( GetTree ( ) . CreateTimer ( 0 ) , "timeout" ) ;
2533 checkWorldWrap ( ) ;
2634 }
2735
@@ -63,7 +71,6 @@ private HorizontalWrapState currentHwrap(Rect2 v) {
6371 // to give the illusion of true wrapping tilemap
6472 // - teleport the camera one world-width to the left or right when
6573 // only the "wrap" tilemap is in view
66-
6774 private void checkWorldWrap ( ) {
6875 if ( map is null || ! map . wrapHorizontally ) {
6976 // TODO: for maps that do not wrap horizontally restrict movement
@@ -94,10 +101,9 @@ public override void _UnhandledInput(InputEvent @event) {
94101 }
95102 }
96103
97- public Rect2 getVisibleWorld ( ) {
98- Transform2D vpToGlobal = ( GetViewport ( ) . GlobalCanvasTransform * GetCanvasTransform ( ) ) . AffineInverse ( ) ;
99- return vpToGlobal * GetViewportRect ( ) ;
100- }
104+ private Transform2D viewportToGlobalTransform => ( GetViewport ( ) . GlobalCanvasTransform * GetCanvasTransform ( ) ) . AffineInverse ( ) ;
105+
106+ public Rect2 getVisibleWorld ( ) => viewportToGlobalTransform * GetViewportRect ( ) ;
101107
102108 public void centerOnTile ( Tile tile , MapView map ) {
103109 Vector2 target = map . tileToLocal ( tile ) ;
0 commit comments