143 lines
3.9 KiB
C#
Raw Normal View History

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
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 };
private Quaternion startRotation;
private Action<int> onRollingComplete = null;
2026-02-11 19:53:16 +05:30
private IRollBase rollBase = null;
private bool hasNoActionOnRoll = false;
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;
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
}
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
}
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
}
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
}
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
}