2026-01-30 11:30:40 +05:30
|
|
|
using System;
|
2026-01-28 20:46:45 +05:30
|
|
|
using System.Collections;
|
2026-02-11 19:53:16 +05:30
|
|
|
using System.Collections.Generic;
|
2026-01-28 20:46:45 +05:30
|
|
|
using UnityEngine;
|
2026-02-11 19:53:16 +05:30
|
|
|
using Random = UnityEngine.Random;
|
2026-01-28 20:46:45 +05:30
|
|
|
|
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-11 19:53:16 +05:30
|
|
|
private List<int> probabilityValues = new List<int>() { 3, 4, 5 };
|
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-02-11 19:53:16 +05:30
|
|
|
|
|
|
|
|
private IRollBase rollBase = null;
|
|
|
|
|
private bool hasNoActionOnRoll = false;
|
2026-01-30 11:30:40 +05:30
|
|
|
|
2026-02-11 19:53:16 +05:30
|
|
|
private void Awake()
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-02-11 19:53:16 +05:30
|
|
|
private IEnumerator RollRoutine()
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
|
|
|
|
rolling = true;
|
|
|
|
|
|
|
|
|
|
// MICRO DELAY → breaks physics sync between dice
|
2026-02-11 19:53:16 +05:30
|
|
|
yield return new WaitForSeconds(Random.Range(0.01f, 0.06f));
|
2026-01-28 20:46:45 +05:30
|
|
|
|
|
|
|
|
rb.useGravity = true;
|
|
|
|
|
|
|
|
|
|
// PER-DICE FORCE MULTIPLIER
|
2026-02-11 19:53:16 +05:30
|
|
|
float spinMultiplier = Random.Range(0.8f, 1.25f);
|
2026-01-28 20:46:45 +05:30
|
|
|
|
|
|
|
|
// RANDOM TORQUE
|
|
|
|
|
rb.AddTorque(
|
2026-02-11 19:53:16 +05:30
|
|
|
Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
|
|
|
|
|
Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
|
|
|
|
|
Random.Range(-baseSpinForce, baseSpinForce) * spinMultiplier,
|
2026-01-28 20:46:45 +05:30
|
|
|
ForceMode.Impulse
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// RANDOM SIDE FORCE
|
|
|
|
|
Vector3 sideDir = new Vector3(
|
2026-02-11 19:53:16 +05:30
|
|
|
Random.Range(-1f, 1f),
|
2026-01-28 20:46:45 +05:30
|
|
|
0f,
|
2026-02-11 19:53:16 +05:30
|
|
|
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);
|
|
|
|
|
|
2026-02-11 19:53:16 +05:30
|
|
|
int rolledVal = 0;
|
|
|
|
|
if (hasNoActionOnRoll)
|
|
|
|
|
{
|
|
|
|
|
if (rollBase.SixRollCount == 0)
|
|
|
|
|
{
|
|
|
|
|
rollBase.UpdateMaxRollCount(probabilityValues[Random.Range(0, probabilityValues.Count)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rollBase.UpdateSixRollCount();
|
|
|
|
|
rolledVal = rollBase.SixRollCount == rollBase.MaxRollCount ?
|
|
|
|
|
Ludo_3D_Constants.Max_Dice_Rolls : GetDiceValue();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rolledVal = GetDiceValue();
|
|
|
|
|
}
|
2026-01-28 20:46:45 +05:30
|
|
|
|
2026-02-11 19:53:16 +05:30
|
|
|
if (rolledVal == Ludo_3D_Constants.Max_Dice_Rolls)
|
|
|
|
|
ResetRollData();
|
|
|
|
|
|
2026-01-28 20:46:45 +05:30
|
|
|
ResetDice();
|
2026-02-11 19:53:16 +05:30
|
|
|
Debug.Log($"Dice rolled: {rolledVal}");
|
|
|
|
|
onRollingComplete?.Invoke(rolledVal);
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|
2026-02-11 19:53:16 +05:30
|
|
|
|
|
|
|
|
private int GetDiceValue()
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
2026-02-11 19:53:16 +05:30
|
|
|
return Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1);
|
|
|
|
|
}
|
2026-01-28 20:46:45 +05:30
|
|
|
|
2026-02-11 19:53:16 +05:30
|
|
|
public void ResetRollData()
|
|
|
|
|
{
|
|
|
|
|
if (rollBase != null) rollBase.ResetRollData();
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|
|
|
|
|
|
2026-02-06 19:04:39 +05:30
|
|
|
public void ResetDice()
|
2026-01-28 20:46:45 +05:30
|
|
|
{
|
2026-02-11 19:53:16 +05:30
|
|
|
rollBase = null;
|
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
|
|
|
}
|
2026-02-11 19:53:16 +05:30
|
|
|
|
|
|
|
|
public void InitOnNoActionsAfterRoll(IRollBase rollBase, bool state)
|
|
|
|
|
{
|
|
|
|
|
hasNoActionOnRoll = state;
|
|
|
|
|
if (!state)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.rollBase = rollBase;
|
|
|
|
|
}
|
2026-01-28 20:46:45 +05:30
|
|
|
}
|