From 55b9be4e2d0c0371ed4e00414b2f3a1ef4272cea Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Mon, 25 Jan 2021 09:15:32 +1100 Subject: [PATCH] Added center grid snapping for tokens --- src/helpers/map.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/helpers/map.js b/src/helpers/map.js index ae8e44f..e7ac00f 100644 --- a/src/helpers/map.js +++ b/src/helpers/map.js @@ -188,6 +188,7 @@ export function snapNodeToMap( // Offsets to tranform the centered position into the four corners const cornerOffsets = [ + { x: 0, y: 0 }, halfSize, { x: -halfSize.x, y: -halfSize.y }, { x: halfSize.x, y: -halfSize.y }, @@ -219,7 +220,24 @@ export function snapNodeToMap( } } + // Snap to center of grid + // Subtract offset and half grid size to transform it into offset half space then transform it back + const halfGridSize = Vector2.multiply(gridSize, 0.5); + const centerSnap = Vector2.add( + Vector2.add( + Vector2.roundTo( + Vector2.subtract(Vector2.subtract(position, offset), halfGridSize), + gridSize + ), + halfGridSize + ), + offset + ); + const centerDistance = Vector2.length(Vector2.subtract(centerSnap, position)); + if (minCornerGridDistance < minCornerMinComponent * snappingThreshold) { node.position(minGridSnap); + } else if (centerDistance < Vector2.min(gridSize) * snappingThreshold) { + node.position(centerSnap); } }