Ashby Issac 9eef3eb04c Merge branch 'Gameplay/ashby-gameplay' into Gameplay/main-gameplay
# Conflicts:
#	Assets/External-Assets/packages/Project/Scripts/Gameplay/Snake and Ladder/DiceView.cs
#	Assets/Scripts/Gameplay/GameplayManager.cs
#	Assets/Scripts/Input/DiceRollHandler.cs
2026-01-30 12:57:17 +05:30

109 lines
2.7 KiB
C#

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;
[Header("Physics Randomness")]
[SerializeField] private float baseSpinForce = 900f;
[SerializeField] private float sideForce = 0.18f;
[SerializeField] private float liftForce = 0.1f;
private Action<int> onRollingComplete = null;
void Awake()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
transform.localPosition = new Vector3(0, 20, 0);
}
public void Roll(Action<int> onComplete, bool isBot)
{
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}");
onRollingComplete?.Invoke(value);
ResetDice();
}
int GetDiceValue()
{
foreach (DiceSide side in diceSides)
{
if (side.OnGround())
return side.sideValue;
}
return 1;
}
void ResetDice()
{
rb.useGravity = false;
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
transform.localPosition = new Vector3(0, 20, 0);
rolling = false;
Invoke(nameof(ResetEvent), 0.5f);
}
private void ResetEvent()
{
onRollingComplete = null;
}
}