2026-01-30 11:30:40 +05:30
|
|
|
using System;
|
2026-01-28 20:46:45 +05:30
|
|
|
using System.Collections;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2026-01-30 11:30:40 +05:30
|
|
|
public class DiceView : MonoBehaviour, IBase
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
|
|
|
|
private Rigidbody rb;
|
|
|
|
|
private bool rolling;
|
|
|
|
|
|
|
|
|
|
[SerializeField] private DiceSide[] diceSides;
|
|
|
|
|
[SerializeField] private float sideValueTime = 1.2f;
|
2026-02-04 12:32:18 +05:30
|
|
|
[SerializeField] private Vector3 startPos = new Vector3(0, 20, 0);
|
2026-01-28 20:46:45 +05:30
|
|
|
|
|
|
|
|
[Header("Physics Randomness")]
|
|
|
|
|
[SerializeField] private float baseSpinForce = 900f;
|
|
|
|
|
[SerializeField] private float sideForce = 0.18f;
|
|
|
|
|
[SerializeField] private float liftForce = 0.1f;
|
|
|
|
|
|
2026-02-06 19:04:39 +05:30
|
|
|
private Quaternion startRotation;
|
2026-01-30 11:30:40 +05:30
|
|
|
private Action<int> onRollingComplete = null;
|
|
|
|
|
|
2026-01-28 20:46:45 +05:30
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
|
rb.useGravity = false;
|
2026-02-04 12:32:18 +05:30
|
|
|
transform.localPosition = startPos;
|
2026-02-06 19:04:39 +05:30
|
|
|
startRotation = transform.localRotation;
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|
2026-01-30 11:30:40 +05:30
|
|
|
|
2026-01-30 12:54:43 +05:30
|
|
|
public void Roll(Action<int> onComplete, bool isBot)
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
2026-01-31 20:33:50 +05:30
|
|
|
Debug.Log($"Start rolling: {rolling}");
|
|
|
|
|
|
2026-01-28 20:46:45 +05:30
|
|
|
if (!rolling)
|
2026-01-30 11:30:40 +05:30
|
|
|
{
|
2026-01-30 12:54:43 +05:30
|
|
|
Debug.Log($"isBot: {isBot}");
|
2026-01-30 11:30:40 +05:30
|
|
|
onRollingComplete = onComplete;
|
2026-01-28 20:46:45 +05:30
|
|
|
StartCoroutine(RollRoutine());
|
2026-01-30 11:30:40 +05:30
|
|
|
}
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator RollRoutine()
|
|
|
|
|
{
|
|
|
|
|
rolling = true;
|
|
|
|
|
|
|
|
|
|
// MICRO DELAY → breaks physics sync between dice
|
2026-01-30 11:30:40 +05:30
|
|
|
yield return new WaitForSeconds(UnityEngine.Random.Range(0.01f, 0.06f));
|
2026-01-28 20:46:45 +05:30
|
|
|
|
|
|
|
|
rb.useGravity = true;
|
|
|
|
|
|
|
|
|
|
// PER-DICE FORCE MULTIPLIER
|
2026-01-30 11:30:40 +05:30
|
|
|
float spinMultiplier = UnityEngine.Random.Range(0.8f, 1.25f);
|
2026-01-28 20:46:45 +05:30
|
|
|
|
|
|
|
|
// RANDOM TORQUE
|
|
|
|
|
rb.AddTorque(
|
2026-01-30 11:30:40 +05:30
|
|
|
UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
|
|
|
|
|
UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
|
|
|
|
|
UnityEngine.Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
|
2026-01-28 20:46:45 +05:30
|
|
|
ForceMode.Impulse
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// RANDOM SIDE FORCE
|
|
|
|
|
Vector3 sideDir = new Vector3(
|
2026-01-30 11:30:40 +05:30
|
|
|
UnityEngine.Random.Range(-1f, 1f),
|
2026-01-28 20:46:45 +05:30
|
|
|
0f,
|
2026-01-30 11:30:40 +05:30
|
|
|
UnityEngine.Random.Range(-1f, 1f)
|
2026-01-28 20:46:45 +05:30
|
|
|
).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();
|
2026-01-31 20:33:50 +05:30
|
|
|
onRollingComplete?.Invoke(value);
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetDiceValue()
|
|
|
|
|
{
|
|
|
|
|
foreach (DiceSide side in diceSides)
|
|
|
|
|
{
|
|
|
|
|
if (side.OnGround())
|
|
|
|
|
return side.sideValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 19:04:39 +05:30
|
|
|
public void ResetDice()
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
|
|
|
|
rb.useGravity = false;
|
|
|
|
|
rb.velocity = Vector3.zero;
|
|
|
|
|
rb.angularVelocity = Vector3.zero;
|
2026-02-04 12:32:18 +05:30
|
|
|
transform.localPosition = startPos;
|
2026-01-28 20:46:45 +05:30
|
|
|
rolling = false;
|
2026-01-30 12:54:43 +05:30
|
|
|
}
|
|
|
|
|
|
2026-02-06 19:04:39 +05:30
|
|
|
public void ResetOnSessionEnd()
|
2026-01-30 12:54:43 +05:30
|
|
|
{
|
2026-02-06 19:04:39 +05:30
|
|
|
ResetDice();
|
|
|
|
|
transform.localRotation = startRotation;
|
2026-01-30 12:54:43 +05:30
|
|
|
onRollingComplete = null;
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|
|
|
|
|
}
|