Change vehicle mount movement method to fix bug with child position not updating
This commit is contained in:
parent
5bd5586efc
commit
838ee68614
@ -20,6 +20,7 @@ import TokenLabel from "../token/TokenLabel";
|
|||||||
import TokenOutline from "../token/TokenOutline";
|
import TokenOutline from "../token/TokenOutline";
|
||||||
|
|
||||||
import { Intersection, getScaledOutline } from "../../helpers/token";
|
import { Intersection, getScaledOutline } from "../../helpers/token";
|
||||||
|
import Vector2 from "../../helpers/Vector2";
|
||||||
|
|
||||||
import { tokenSources } from "../../tokens";
|
import { tokenSources } from "../../tokens";
|
||||||
|
|
||||||
@ -48,10 +49,14 @@ function MapToken({
|
|||||||
|
|
||||||
const snapPositionToGrid = useGridSnapping();
|
const snapPositionToGrid = useGridSnapping();
|
||||||
|
|
||||||
|
const intersectingTokensRef = useRef([]);
|
||||||
|
const previousDragPositionRef = useRef({ x: 0, y: 0 });
|
||||||
|
|
||||||
function handleDragStart(event) {
|
function handleDragStart(event) {
|
||||||
const tokenGroup = event.target;
|
const tokenGroup = event.target;
|
||||||
|
|
||||||
if (tokenState.category === "vehicle") {
|
if (tokenState.category === "vehicle") {
|
||||||
|
previousDragPositionRef.current = tokenGroup.position();
|
||||||
const tokenIntersection = new Intersection(
|
const tokenIntersection = new Intersection(
|
||||||
getScaledOutline(tokenState, tokenWidth, tokenHeight),
|
getScaledOutline(tokenState, tokenWidth, tokenHeight),
|
||||||
{ x: tokenX - tokenWidth / 2, y: tokenY - tokenHeight / 2 },
|
{ x: tokenX - tokenWidth / 2, y: tokenY - tokenHeight / 2 },
|
||||||
@ -67,10 +72,7 @@ function MapToken({
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (tokenIntersection.intersects(other.position())) {
|
if (tokenIntersection.intersects(other.position())) {
|
||||||
// Save and restore token position after moving layer
|
intersectingTokensRef.current.push(other);
|
||||||
const position = other.absolutePosition();
|
|
||||||
other.moveTo(tokenGroup);
|
|
||||||
other.absolutePosition(position);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,6 +86,16 @@ function MapToken({
|
|||||||
if (map.snapToGrid) {
|
if (map.snapToGrid) {
|
||||||
tokenGroup.position(snapPositionToGrid(tokenGroup.position()));
|
tokenGroup.position(snapPositionToGrid(tokenGroup.position()));
|
||||||
}
|
}
|
||||||
|
if (tokenState.category === "vehicle") {
|
||||||
|
const deltaPosition = Vector2.subtract(
|
||||||
|
tokenGroup.position(),
|
||||||
|
previousDragPositionRef.current
|
||||||
|
);
|
||||||
|
for (let other of intersectingTokensRef.current) {
|
||||||
|
other.position(Vector2.add(other.position(), deltaPosition));
|
||||||
|
}
|
||||||
|
previousDragPositionRef.current = tokenGroup.position();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleDragEnd(event) {
|
function handleDragEnd(event) {
|
||||||
@ -91,20 +103,15 @@ function MapToken({
|
|||||||
|
|
||||||
const mountChanges = {};
|
const mountChanges = {};
|
||||||
if (tokenState.category === "vehicle") {
|
if (tokenState.category === "vehicle") {
|
||||||
const parent = tokenGroup.getParent();
|
for (let other of intersectingTokensRef.current) {
|
||||||
const mountedTokens = tokenGroup.find(".character");
|
mountChanges[other.id()] = {
|
||||||
for (let mountedToken of mountedTokens) {
|
x: other.x() / mapWidth,
|
||||||
// Save and restore token position after moving layer
|
y: other.y() / mapHeight,
|
||||||
const position = mountedToken.absolutePosition();
|
|
||||||
mountedToken.moveTo(parent);
|
|
||||||
mountedToken.absolutePosition(position);
|
|
||||||
mountChanges[mountedToken.id()] = {
|
|
||||||
x: mountedToken.x() / mapWidth,
|
|
||||||
y: mountedToken.y() / mapHeight,
|
|
||||||
lastModifiedBy: userId,
|
lastModifiedBy: userId,
|
||||||
lastModified: Date.now(),
|
lastModified: Date.now(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
intersectingTokensRef.current = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
setPreventMapInteraction(false);
|
setPreventMapInteraction(false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user