Skip to content

Commit

Permalink
add tile leeway for wrapping to hide temporary map tear
Browse files Browse the repository at this point in the history
  • Loading branch information
pcen committed Sep 17, 2023
1 parent b86dd33 commit a4eaccb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 3 additions & 8 deletions C7/Map/MapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapView>();
public bool wrapHorizontally {get; private set;}
public int worldEdgeRight {get; private set;}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions C7/Map/MapViewCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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() {
Expand All @@ -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)) {
Expand All @@ -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:
Expand Down

0 comments on commit a4eaccb

Please sign in to comment.