Moved token mount intersection to use proper intersection shape

This commit is contained in:
Mitchell McCaffrey 2020-07-18 15:21:33 +10:00
parent 723e53948e
commit f1986c0c14

View File

@ -45,15 +45,15 @@ function MapToken({
}, [tokenSourceImage]); }, [tokenSourceImage]);
function handleDragStart(event) { function handleDragStart(event) {
const tokenImage = event.target; const tokenGroup = event.target;
const tokenImageRect = tokenImage.getClientRect(); const tokenImage = imageRef.current;
if (token && token.isVehicle) { if (token && token.isVehicle) {
// Find all other tokens on the map // Find all other tokens on the map
const layer = tokenImage.getLayer(); const layer = tokenGroup.getLayer();
const tokens = layer.find(".token"); const tokens = layer.find(".token");
for (let other of tokens) { for (let other of tokens) {
if (other === tokenImage) { if (other === tokenGroup) {
continue; continue;
} }
const otherRect = other.getClientRect(); const otherRect = other.getClientRect();
@ -61,16 +61,10 @@ function MapToken({
x: otherRect.x + otherRect.width / 2, x: otherRect.x + otherRect.width / 2,
y: otherRect.y + otherRect.height / 2, y: otherRect.y + otherRect.height / 2,
}; };
// Check the other tokens center overlaps this tokens bounding box if (tokenImage.intersects(otherCenter)) {
if (
otherCenter.x > tokenImageRect.x &&
otherCenter.x < tokenImageRect.x + tokenImageRect.width &&
otherCenter.y > tokenImageRect.y &&
otherCenter.y < tokenImageRect.y + tokenImageRect.height
) {
// Save and restore token position after moving layer // Save and restore token position after moving layer
const position = other.absolutePosition(); const position = other.absolutePosition();
other.moveTo(tokenImage); other.moveTo(tokenGroup);
other.absolutePosition(position); other.absolutePosition(position);
} }
} }
@ -80,12 +74,12 @@ function MapToken({
} }
function handleDragEnd(event) { function handleDragEnd(event) {
const tokenImage = event.target; const tokenGroup = event.target;
const mountChanges = {}; const mountChanges = {};
if (token && token.isVehicle) { if (token && token.isVehicle) {
const layer = tokenImage.getLayer(); const layer = tokenGroup.getLayer();
const mountedTokens = tokenImage.find(".token"); const mountedTokens = tokenGroup.find(".token");
for (let mountedToken of mountedTokens) { for (let mountedToken of mountedTokens) {
// Save and restore token position after moving layer // Save and restore token position after moving layer
const position = mountedToken.absolutePosition(); const position = mountedToken.absolutePosition();
@ -105,8 +99,8 @@ function MapToken({
...mountChanges, ...mountChanges,
[tokenState.id]: { [tokenState.id]: {
...tokenState, ...tokenState,
x: tokenImage.x() / mapWidth, x: tokenGroup.x() / mapWidth,
y: tokenImage.y() / mapHeight, y: tokenGroup.y() / mapHeight,
lastEditedBy: userId, lastEditedBy: userId,
}, },
}); });