Ashby Issac 6b5dd5d0f0 Bug fixes + Refactored/Restructurd changes + Dice roll logical changes.
Remove event deallocation function call in DiceView due to flow breakage because of timer in delay.
Added a debug test bool inside GameplayManager for testing dice logic without dice animation.
Fixed issue with additional steps being kept track for player (PlayerPawn.cs)
Bound restart logic for the Game over popup alone.
Added a maxDiceSixRollCounter for keeping track of how many times 6 can be rolled.
Fixed issue with wrong player being selected while restart is clicked.
Fixed indexing logical errors while finding possible tile data.
Fixed issue with player attacking another player and moving constantly.
Fixed issue with player not switching when there is no option for dice roll.
Handled dice roll logic when pawns are in finishing path.
Fixed indexing issue with availPlayers collection where wrong indexes were being selected for removing from the collection.
Fixed issue with tile data not resetting on restart.
Restructured logic for showing winners inside Game Over popup.
Restructured GameOverPopup and GameOverScreen with respect to showing/hiding screens/popups.
2026-01-31 20:33:50 +05:30

110 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)
{
Debug.Log($"Start rolling: {rolling}");
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}");
ResetDice();
onRollingComplete?.Invoke(value);
}
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.1f);
}
private void ResetEvent()
{
onRollingComplete = null;
}
}