using System; using System.Collections; using UnityEngine; public class DiceView : MonoBehaviour, IBase { private Rigidbody rb; private bool rolling; [SerializeField] private DiceSide[] diceSides; [SerializeField] private float sideValueTime = 1.2f; [SerializeField] private Vector3 startPos = new Vector3(0, 20, 0); [Header("Physics Randomness")] [SerializeField] private float baseSpinForce = 900f; [SerializeField] private float sideForce = 0.18f; [SerializeField] private float liftForce = 0.1f; private Quaternion startRotation; private Action onRollingComplete = null; void Awake() { rb = GetComponent(); rb.useGravity = false; transform.localPosition = startPos; startRotation = transform.localRotation; } public void Roll(Action onComplete, bool isBot) { Debug.Log($"Start rolling: {rolling}"); if (!rolling) { Debug.Log($"isBot: {isBot}"); onRollingComplete = onComplete; StartCoroutine(RollRoutine()); } } IEnumerator RollRoutine() { rolling = true; // MICRO DELAY → breaks physics sync between dice yield return new WaitForSeconds(UnityEngine.Random.Range(0.01f, 0.06f)); rb.useGravity = true; // PER-DICE FORCE MULTIPLIER float spinMultiplier = UnityEngine.Random.Range(0.8f, 1.25f); // RANDOM TORQUE rb.AddTorque( UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier, UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier, UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier, ForceMode.Impulse ); // RANDOM SIDE FORCE Vector3 sideDir = new Vector3( UnityEngine.Random.Range(-1f, 1f), 0f, UnityEngine.Random.Range(-1f, 1f) ).normalized; rb.AddForce(sideDir * sideForce, ForceMode.Impulse); // SMALL UPWARD FORCE rb.AddForce(Vector3.up * liftForce, ForceMode.Impulse); yield return new WaitForSeconds(sideValueTime); int value = GetDiceValue(); //TODO: Use the dice value as needed Debug.Log($"Dice rolled: {value}"); ResetDice(); onRollingComplete?.Invoke(value); } int GetDiceValue() { foreach (DiceSide side in diceSides) { if (side.OnGround()) return side.sideValue; } return 1; } public void ResetDice() { rb.useGravity = false; rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; transform.localPosition = startPos; rolling = false; } public void ResetOnSessionEnd() { ResetDice(); transform.localRotation = startRotation; onRollingComplete = null; } }