120 lines
3.1 KiB
C#
Raw Normal View History

using System;
2026-01-28 20:46:45 +05:30
using System.Collections;
using UnityEngine;
2026-02-12 14:38:40 +05:30
using UnityEngine.UI;
2026-01-28 20:46:45 +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-02-12 14:38:40 +05:30
[SerializeField] private Button diceButton;
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;
private Quaternion startRotation;
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;
startRotation = transform.localRotation;
2026-01-28 20:46:45 +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
{
Debug.Log($"Start rolling: {rolling}");
2026-01-28 20:46:45 +05:30
if (!rolling)
{
2026-01-30 12:54:43 +05:30
Debug.Log($"isBot: {isBot}");
onRollingComplete = onComplete;
2026-01-28 20:46:45 +05:30
StartCoroutine(RollRoutine());
}
2026-01-28 20:46:45 +05:30
}
IEnumerator RollRoutine()
{
rolling = true;
// MICRO DELAY → breaks physics sync between dice
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
float spinMultiplier = UnityEngine.Random.Range(0.8f, 1.25f);
2026-01-28 20:46:45 +05:30
// RANDOM TORQUE
rb.AddTorque(
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(
UnityEngine.Random.Range(-1f, 1f),
2026-01-28 20:46:45 +05:30
0f,
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();
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;
}
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-12 14:38:40 +05:30
public void SetDiceButtonInteraction(bool status)
{
diceButton.interactable = status;
}
public void ResetOnSessionEnd()
2026-01-30 12:54:43 +05:30
{
ResetDice();
transform.localRotation = startRotation;
2026-01-30 12:54:43 +05:30
onRollingComplete = null;
2026-01-28 20:46:45 +05:30
}
}