Skip to content

Commit 0d14b81

Browse files
committed
only check for wrapping when camera moves, better naming, comment main wrap function
1 parent a4eaccb commit 0d14b81

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

C7/Map/MapViewCamera.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public partial class MapViewCamera : Camera2D {
1515
private readonly float minZoom = 0.2f;
1616
public float zoomFactor { get; private set; } = 1.0f;
1717
private MapView map;
18-
private int wrapLeeway = 2; // leeway in number of tiles to consider camera at map edge
18+
private int wrapEdgeTileMargin = 2; // margin in number of tiles to trigger map wrapping
1919
private HorizontalWrapState hwrap = HorizontalWrapState.None;
2020

2121
public void attachToMapView(MapView map) {
2222
this.map = map;
23-
worldWrapLeeway = wrapLeeway * map.tileSize.X;
23+
wrapEdgeMargin = wrapEdgeTileMargin * map.tileSize.X;
2424
map.updateAnimations();
2525
checkWorldWrap();
2626
}
@@ -47,27 +47,32 @@ public void setPosition(Vector2 position) {
4747
Position = position;
4848
checkWorldWrap();
4949
}
50-
private int worldWrapLeeway = 0;
51-
52-
private bool enteringRightWrap(Rect2 v) => hwrap != HorizontalWrapState.Right && v.End.X >= map.worldEdgeRight - worldWrapLeeway;
53-
private bool enteringLeftWrap(Rect2 v) => hwrap != HorizontalWrapState.Left && v.Position.X <= map.worldEdgeLeft + worldWrapLeeway;
54-
private bool atEdgeOfRightWrap(Rect2 v) => hwrap == HorizontalWrapState.Right && v.End.X >= map.worldEdgeRight + map.pixelWidth;
55-
private bool atEdgeOfLeftWrap(Rect2 v) => hwrap == HorizontalWrapState.Left && v.Position.X <= map.worldEdgeLeft - map.pixelWidth;
50+
private float wrapEdgeMargin = 0;
51+
float wrapRightEdge {get => map.worldEdgeRight - wrapEdgeMargin; }
52+
float wrapLeftEdge {get => map.worldEdgeLeft + wrapEdgeMargin; }
53+
private bool enteringRightWrap(Rect2 v) => hwrap != HorizontalWrapState.Right && v.End.X >= wrapRightEdge;
54+
private bool enteringLeftWrap(Rect2 v) => hwrap != HorizontalWrapState.Left && v.Position.X <= wrapLeftEdge;
55+
private bool atEdgeOfRightWrap(Rect2 v) => hwrap == HorizontalWrapState.Right && v.End.X >= wrapRightEdge + map.pixelWidth;
56+
private bool atEdgeOfLeftWrap(Rect2 v) => hwrap == HorizontalWrapState.Left && v.Position.X <= wrapLeftEdge - map.pixelWidth;
5657
private HorizontalWrapState currentHwrap(Rect2 v) {
57-
return v.Position.X <= map.worldEdgeLeft + worldWrapLeeway ? HorizontalWrapState.Left : (v.End.X >= map.worldEdgeRight - worldWrapLeeway ? HorizontalWrapState.Right : HorizontalWrapState.None);
58+
return v.Position.X <= wrapLeftEdge ? HorizontalWrapState.Left : (v.End.X >= wrapRightEdge ? HorizontalWrapState.Right : HorizontalWrapState.None);
5859
}
5960

61+
// checkWorldWrap determines if the camera is about to spill over the world map and will:
62+
// - move the second "wrap" tilemap to the appropriate edge
63+
// to give the illusion of true wrapping tilemap
64+
// - teleport the camera one world-width to the left or right when
65+
// only the "wrap" tilemap is in view
66+
6067
private void checkWorldWrap() {
6168
if (map is null || !map.wrapHorizontally) {
6269
// TODO: for maps that do not wrap horizontally restrict movement
6370
return;
6471
}
6572
Rect2 visibleWorld = getVisibleWorld();
6673
if (enteringRightWrap(visibleWorld)) {
67-
GD.Print("moving wrap to right");
6874
map.setHorizontalWrap(HorizontalWrapState.Right);
6975
} else if (enteringLeftWrap(visibleWorld)) {
70-
GD.Print("moving wrap to left");
7176
map.setHorizontalWrap(HorizontalWrapState.Left);
7277
}
7378
if (atEdgeOfRightWrap(visibleWorld)) {
@@ -78,10 +83,6 @@ private void checkWorldWrap() {
7883
hwrap = currentHwrap(visibleWorld);
7984
}
8085

81-
public override void _Process(double delta) {
82-
checkWorldWrap();
83-
}
84-
8586
public override void _UnhandledInput(InputEvent @event) {
8687
switch (@event) {
8788
case InputEventMouseMotion mouseDrag when mouseDrag.ButtonMask == MouseButtonMask.Left:

0 commit comments

Comments
 (0)