Skip to content

Commit

Permalink
[piece-movement] add on drag animation end callback to apply a smooth…
Browse files Browse the repository at this point in the history
…er experience for the last moved piece
  • Loading branch information
Tryferos committed Dec 14, 2024
1 parent 85f4e7c commit c7731ba
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions resources/js/Game/Board/Piece/Piece.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const Piece = ({code: pieceCode = 0, origin_position: position, rotation,
const [shadowPosition, setShadowPosition] = useState<THREE.Vector3>();
const onStart = useRef<() => void>();
const onComplete = useRef<(moveType: MoveType) => void>();
const onDragAnimationEnd = useRef<() => void>();
const [boardState, setBoardState] = useState<BoardState>()

const canMovePiece = useMemo(() => {
Expand Down Expand Up @@ -89,16 +90,13 @@ export const Piece = ({code: pieceCode = 0, origin_position: position, rotation,
}, [boardState?.gameState.state])

const onLockIn = () => {
if((boardState?.move?.code !== pieceCode || !isPiecePositionValid()) && hasMoved){
onMoveReject();
}else if(isPositionValid && boardState?.move?.code === pieceCode && ref.current){
boardState?.endTurn();
boardState?.addBoardPiece(ref.current)
}else if(hasMoved){
onMoveReject();
if(ref.current){
gsap.to(ref.current.position, {
y: blockSize*0.5,
duration: animationDuration,
onComplete: onDragAnimationEnd.current
});
}
setIsDragging(false);
removeDragAnimation();
}

useEffect(() => {
Expand Down Expand Up @@ -134,6 +132,22 @@ export const Piece = ({code: pieceCode = 0, origin_position: position, rotation,

}, [ref.current, preQuaternion, isPositionValid, prePosition])

useEffect(() => {

onDragAnimationEnd.current = () => {
if((boardState?.move?.code !== pieceCode || !isPiecePositionValid()) && hasMoved){
onMoveReject();
}else if(isPositionValid && boardState?.move?.code === pieceCode && ref.current){
boardState?.endTurn();
boardState?.addBoardPiece(ref.current)
}else if(hasMoved){
onMoveReject();
}
setIsDragging(false);
}

}, [boardState?.move, pieceCode, hasMoved, ref.current, isPositionValid])

const getOriginalQuaternion = () => {
const flipAxis = getFlipAxis();
return new THREE.Quaternion().setFromEuler(
Expand Down Expand Up @@ -489,12 +503,6 @@ export const Piece = ({code: pieceCode = 0, origin_position: position, rotation,
}
}

const removeDragAnimation = () => {
if(ref.current){
gsap.to(ref.current.position, {y: blockSize*0.5, duration: animationDuration, ease: 'power1.inOut'});
}
}

const onDrag = () => {
//* Snap the shadow to the grid

Expand Down

0 comments on commit c7731ba

Please sign in to comment.