Skip to content

Commit 48aecb4

Browse files
committed
fix issue where getVisibleWorld initially uses invalid GlobalCanvasTransform value
1 parent 289eae6 commit 48aecb4

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

C7/Map/MapView.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ private void initializeTileMap() {
217217

218218
setHorizontalWrap(HorizontalWrapState.Right); // just put it somewhere
219219

220-
tilemap.ZIndex = 10; // need to figure out a good way to order z indices
221-
wrappingTilemap.ZIndex = 10;
220+
tilemap.ZIndex = MapZIndex.Tiles; // need to figure out a good way to order z indices
221+
wrappingTilemap.ZIndex = MapZIndex.Tiles;
222222
AddChild(tilemap);
223223
AddChild(wrappingTilemap);
224224
AddChild(terrainTilemap);
@@ -297,7 +297,7 @@ public MapView(Game game, GameData data) {
297297
// TODO in the future convert civ3 coordinates to stacked
298298
// coordinates when reading from the civ3 save so Tile has
299299
// stacked coordinates
300-
foreach (C7GameData.Tile t in gameMap.tiles) {
300+
foreach (Tile t in gameMap.tiles) {
301301
Vector2I coords = stackedCoords(t);
302302
terrain[coords.X, coords.Y] = t.baseTerrainTypeKey;
303303
}

C7/Map/MapViewCamera.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)