|
|
|
@@ -1,74 +1,103 @@
|
|
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
|
|
|
|
import React, { useState, useEffect, useRef } from "react"
|
|
|
|
|
import { dotPositions } from "./diceDotPositions"
|
|
|
|
|
|
|
|
|
|
const Dice = ({ onRoll }) => {
|
|
|
|
|
const [diceValue, setDiceValue] = useState(1);
|
|
|
|
|
const [isRolling, setIsRolling] = useState(false);
|
|
|
|
|
const animationRef = useRef(null);
|
|
|
|
|
const rollTimeoutRef = useRef(null);
|
|
|
|
|
const Dice = ({ onRoll, selectedValue }) => {
|
|
|
|
|
const [diceValue, setDiceValue] = useState(1)
|
|
|
|
|
const [isRolling, setIsRolling] = useState(false)
|
|
|
|
|
const animationRef = useRef(null)
|
|
|
|
|
const rollTimeoutRef = useRef(null)
|
|
|
|
|
|
|
|
|
|
const diceFaces = [
|
|
|
|
|
[<div key="center" className="dice-dot"></div>],
|
|
|
|
|
[<div key="top-left" className="dice-dot"></div>, <div key="bottom-right" className="dice-dot"></div>],
|
|
|
|
|
[<div key="top-left" className="dice-dot"></div>, <div key="center" className="dice-dot"></div>, <div key="bottom-right" className="dice-dot"></div>],
|
|
|
|
|
[<div key="top-left" className="dice-dot"></div>, <div key="top-right" className="dice-dot"></div>,
|
|
|
|
|
<div key="bottom-left" className="dice-dot"></div>, <div key="bottom-right" className="dice-dot"></div>],
|
|
|
|
|
[<div key="top-left" className="dice-dot"></div>, <div key="top-right" className="dice-dot"></div>,
|
|
|
|
|
<div key="center" className="dice-dot"></div>,
|
|
|
|
|
<div key="bottom-left" className="dice-dot"></div>, <div key="bottom-right" className="dice-dot"></div>],
|
|
|
|
|
[<div key="top-left" className="dice-dot"></div>, <div key="top-right" className="dice-dot"></div>,
|
|
|
|
|
<div key="middle-left" className="dice-dot"></div>, <div key="middle-right" className="dice-dot"></div>,
|
|
|
|
|
<div key="bottom-left" className="dice-dot"></div>, <div key="bottom-right" className="dice-dot"></div>]
|
|
|
|
|
];
|
|
|
|
|
[<div key="center" className="dice-dot" style={dotPositions.center}></div>],
|
|
|
|
|
[
|
|
|
|
|
<div key="top-left" className="dice-dot" style={dotPositions.topLeft}></div>,
|
|
|
|
|
<div key="bottom-right" className="dice-dot" style={dotPositions.bottomRight}></div>,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
<div key="top-left" className="dice-dot" style={dotPositions.topLeft}></div>,
|
|
|
|
|
<div key="center" className="dice-dot" style={dotPositions.center}></div>,
|
|
|
|
|
<div key="bottom-right" className="dice-dot" style={dotPositions.bottomRight}></div>,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
<div key="top-left" className="dice-dot" style={dotPositions.topLeft}></div>,
|
|
|
|
|
<div key="top-right" className="dice-dot" style={dotPositions.topRight}></div>,
|
|
|
|
|
<div key="bottom-left" className="dice-dot" style={dotPositions.bottomLeft}></div>,
|
|
|
|
|
<div key="bottom-right" className="dice-dot" style={dotPositions.bottomRight}></div>,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
<div key="top-left" className="dice-dot" style={dotPositions.topLeft}></div>,
|
|
|
|
|
<div key="top-right" className="dice-dot" style={dotPositions.topRight}></div>,
|
|
|
|
|
<div key="center" className="dice-dot" style={dotPositions.center}></div>,
|
|
|
|
|
<div key="bottom-left" className="dice-dot" style={dotPositions.bottomLeft}></div>,
|
|
|
|
|
<div key="bottom-right" className="dice-dot" style={dotPositions.bottomRight}></div>,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
<div key="top-left" className="dice-dot" style={dotPositions.topLeft}></div>,
|
|
|
|
|
<div key="top-right" className="dice-dot" style={dotPositions.topRight}></div>,
|
|
|
|
|
<div key="middle-left" className="dice-dot" style={dotPositions.middleLeft}></div>,
|
|
|
|
|
<div key="middle-right" className="dice-dot" style={dotPositions.middleRight}></div>,
|
|
|
|
|
<div key="bottom-left" className="dice-dot" style={dotPositions.bottomLeft}></div>,
|
|
|
|
|
<div key="bottom-right" className="dice-dot" style={dotPositions.bottomRight}></div>,
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
if (animationRef.current) cancelAnimationFrame(animationRef.current);
|
|
|
|
|
if (rollTimeoutRef.current) clearTimeout(rollTimeoutRef.current);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
if (animationRef.current) cancelAnimationFrame(animationRef.current)
|
|
|
|
|
if (rollTimeoutRef.current) clearTimeout(rollTimeoutRef.current)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const rollDice = () => {
|
|
|
|
|
if (isRolling) return;
|
|
|
|
|
// Helper that starts the rolling animation and finishes with targetValue if provided
|
|
|
|
|
const startRoll = (targetValue = null) => {
|
|
|
|
|
if (isRolling) return
|
|
|
|
|
|
|
|
|
|
setIsRolling(true);
|
|
|
|
|
setIsRolling(true)
|
|
|
|
|
|
|
|
|
|
let duration = 0;
|
|
|
|
|
const rollInterval = 80; // ms between dice face changes
|
|
|
|
|
const maxDuration = 1500; // total animation time
|
|
|
|
|
let duration = 0
|
|
|
|
|
const rollInterval = 80 // ms between dice face changes
|
|
|
|
|
const maxDuration = 1500 // total animation time
|
|
|
|
|
|
|
|
|
|
const rollAnimation = () => {
|
|
|
|
|
const randomValue = Math.floor(Math.random() * 6) + 1;
|
|
|
|
|
setDiceValue(randomValue);
|
|
|
|
|
const randomValue = Math.floor(Math.random() * 6) + 1
|
|
|
|
|
setDiceValue(randomValue)
|
|
|
|
|
|
|
|
|
|
duration += rollInterval;
|
|
|
|
|
duration += rollInterval
|
|
|
|
|
|
|
|
|
|
if (duration < maxDuration) {
|
|
|
|
|
// Speed effect: slow down towards the end
|
|
|
|
|
const nextInterval = rollInterval * (1 + (duration / maxDuration) * 2);
|
|
|
|
|
const nextInterval = rollInterval * (1 + (duration / maxDuration) * 2)
|
|
|
|
|
rollTimeoutRef.current = setTimeout(() => {
|
|
|
|
|
animationRef.current = requestAnimationFrame(rollAnimation);
|
|
|
|
|
}, nextInterval);
|
|
|
|
|
animationRef.current = requestAnimationFrame(rollAnimation)
|
|
|
|
|
}, nextInterval)
|
|
|
|
|
} else {
|
|
|
|
|
// Final roll
|
|
|
|
|
const finalValue = Math.floor(Math.random() * 6) + 1;
|
|
|
|
|
setDiceValue(finalValue);
|
|
|
|
|
setIsRolling(false);
|
|
|
|
|
// Final roll (use targetValue if provided)
|
|
|
|
|
const finalValue = targetValue != null ? Number(targetValue) : Math.floor(Math.random() * 6) + 1
|
|
|
|
|
setDiceValue(finalValue)
|
|
|
|
|
setIsRolling(false)
|
|
|
|
|
|
|
|
|
|
if (onRoll) onRoll(finalValue);
|
|
|
|
|
if (onRoll) onRoll(finalValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
animationRef.current = requestAnimationFrame(rollAnimation);
|
|
|
|
|
};
|
|
|
|
|
animationRef.current = requestAnimationFrame(rollAnimation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Click to roll randomly
|
|
|
|
|
const rollDice = () => {
|
|
|
|
|
startRoll(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If parent provides a selectedValue, animate to that value
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (selectedValue != null) {
|
|
|
|
|
startRoll(Number(selectedValue))
|
|
|
|
|
}
|
|
|
|
|
}, [selectedValue])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={`dice-container ${isRolling ? 'rolling' : ''}`}
|
|
|
|
|
onClick={rollDice}
|
|
|
|
|
>
|
|
|
|
|
<div className="dice">
|
|
|
|
|
{diceFaces[diceValue - 1]}
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`dice-container ${isRolling ? "rolling" : ""}`} onClick={rollDice}>
|
|
|
|
|
<div className="dice">{diceFaces[diceValue - 1]}</div>
|
|
|
|
|
<style jsx>{`
|
|
|
|
|
.dice-container {
|
|
|
|
|
width: 80px;
|
|
|
|
@@ -107,11 +136,21 @@ const Dice = ({ onRoll }) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@keyframes roll {
|
|
|
|
|
0% { transform: rotateX(0deg) rotateY(0deg); }
|
|
|
|
|
25% { transform: rotateX(90deg) rotateY(45deg); }
|
|
|
|
|
50% { transform: rotateX(180deg) rotateY(90deg); }
|
|
|
|
|
75% { transform: rotateX(270deg) rotateY(135deg); }
|
|
|
|
|
100% { transform: rotateX(360deg) rotateY(180deg); }
|
|
|
|
|
0% {
|
|
|
|
|
transform: rotateX(0deg) rotateY(0deg);
|
|
|
|
|
}
|
|
|
|
|
25% {
|
|
|
|
|
transform: rotateX(90deg) rotateY(45deg);
|
|
|
|
|
}
|
|
|
|
|
50% {
|
|
|
|
|
transform: rotateX(180deg) rotateY(90deg);
|
|
|
|
|
}
|
|
|
|
|
75% {
|
|
|
|
|
transform: rotateX(270deg) rotateY(135deg);
|
|
|
|
|
}
|
|
|
|
|
100% {
|
|
|
|
|
transform: rotateX(360deg) rotateY(180deg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dice-dot {
|
|
|
|
@@ -123,17 +162,10 @@ const Dice = ({ onRoll }) => {
|
|
|
|
|
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Positioning dots */
|
|
|
|
|
.dice-dot:nth-child(1) { top: 50%; left: 50%; transform: translate(-50%, -50%); } /* Center */
|
|
|
|
|
.dice-dot:nth-child(2) { top: 20%; left: 20%; } /* Top-left */
|
|
|
|
|
.dice-dot:nth-child(3) { bottom: 20%; right: 20%; } /* Bottom-right */
|
|
|
|
|
.dice-dot:nth-child(4) { top: 20%; right: 20%; } /* Top-right */
|
|
|
|
|
.dice-dot:nth-child(5) { bottom: 20%; left: 20%; } /* Bottom-left */
|
|
|
|
|
.dice-dot:nth-child(6) { top: 50%; left: 20%; transform: translateY(-50%); } /* Middle-left */
|
|
|
|
|
.dice-dot:nth-child(7) { top: 50%; right: 20%; transform: translateY(-50%); } /* Middle-right */
|
|
|
|
|
/* removed :nth-child positioning — positions are provided inline from diceDotPositions.js */
|
|
|
|
|
`}</style>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Dice;
|
|
|
|
|
export default Dice
|
|
|
|
|