Skip to content

Commit

Permalink
only check for wrapping when camera moves, better naming, comment mai…
Browse files Browse the repository at this point in the history
…n wrap function
  • Loading branch information
pcen committed Sep 17, 2023
1 parent a4eaccb commit 0d14b81
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions C7/Map/MapViewCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +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 int wrapEdgeTileMargin = 2; // margin in number of tiles to trigger map wrapping
private HorizontalWrapState hwrap = HorizontalWrapState.None;

public void attachToMapView(MapView map) {
this.map = map;
worldWrapLeeway = wrapLeeway * map.tileSize.X;
wrapEdgeMargin = wrapEdgeTileMargin * map.tileSize.X;
map.updateAnimations();
checkWorldWrap();
}
Expand All @@ -47,27 +47,32 @@ 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 - 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 float wrapEdgeMargin = 0;
float wrapRightEdge {get => map.worldEdgeRight - wrapEdgeMargin; }
float wrapLeftEdge {get => map.worldEdgeLeft + wrapEdgeMargin; }
private bool enteringRightWrap(Rect2 v) => hwrap != HorizontalWrapState.Right && v.End.X >= wrapRightEdge;
private bool enteringLeftWrap(Rect2 v) => hwrap != HorizontalWrapState.Left && v.Position.X <= wrapLeftEdge;
private bool atEdgeOfRightWrap(Rect2 v) => hwrap == HorizontalWrapState.Right && v.End.X >= wrapRightEdge + map.pixelWidth;
private bool atEdgeOfLeftWrap(Rect2 v) => hwrap == HorizontalWrapState.Left && v.Position.X <= wrapLeftEdge - map.pixelWidth;
private HorizontalWrapState currentHwrap(Rect2 v) {
return v.Position.X <= map.worldEdgeLeft + worldWrapLeeway ? HorizontalWrapState.Left : (v.End.X >= map.worldEdgeRight - worldWrapLeeway ? HorizontalWrapState.Right : HorizontalWrapState.None);
return v.Position.X <= wrapLeftEdge ? HorizontalWrapState.Left : (v.End.X >= wrapRightEdge ? HorizontalWrapState.Right : HorizontalWrapState.None);
}

// checkWorldWrap determines if the camera is about to spill over the world map and will:
// - move the second "wrap" tilemap to the appropriate edge
// to give the illusion of true wrapping tilemap
// - teleport the camera one world-width to the left or right when
// only the "wrap" tilemap is in view

private void checkWorldWrap() {
if (map is null || !map.wrapHorizontally) {
// TODO: for maps that do not wrap horizontally restrict movement
return;
}
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 @@ -78,10 +83,6 @@ 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 0d14b81

Please sign in to comment.