149 lines
3.8 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;
using UnityEngine.Serialization;
using UnityEngine.UI;
2026-02-11 19:53:16 +05:30
using Random = UnityEngine.Random;
2026-01-28 20:46:45 +05:30
public enum DiceSideVal
2026-01-28 20:46:45 +05:30
{
One = 0,
Two = 1,
Three = 2,
Four = 3,
Five = 4,
Six = 5,
}
2026-01-28 20:46:45 +05:30
[Serializable]
public class DiceSideData
{
public DiceSideVal sideVal;
public Sprite sprite;
}
2026-01-28 20:46:45 +05:30
public class DiceView : MonoBehaviour, IBase
{
[SerializeField] private DiceSideData[] diceSideDatas;
[SerializeField] private Image diceImage;
[SerializeField] private Sprite defaultSprite;
2026-01-28 20:46:45 +05:30
private Animator animator;
2026-02-11 19:53:16 +05:30
private List<int> probabilityValues = new List<int>() { 3, 4, 5 };
private Action<int> onRollingComplete = null;
2026-02-11 19:53:16 +05:30
private IRollBase rollBase = null;
private bool rolling;
2026-02-11 19:53:16 +05:30
private bool hasNoActionOnRoll = false;
private int rolledVal = 0;
2026-02-11 19:53:16 +05:30
private void Awake()
2026-01-28 20:46:45 +05:30
{
animator = GetComponent<Animator>();
Debug.Log($"Dice roll test: StartRollingAction: animator: {animator}");
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}");
animator.enabled = true;
onRollingComplete = onComplete;
StartRollingAction();
}
2026-01-28 20:46:45 +05:30
}
private void StartRollingAction()
2026-01-28 20:46:45 +05:30
{
Debug.Log($"Dice roll test: StartRollingAction: animator: {animator}, {gameObject.name}");
2026-01-28 20:46:45 +05:30
rolling = true;
rolledVal = 0;
// start animation
animator.SetTrigger(Ludo_3D_Constants.RollDiceTriggerString);
2026-02-11 19:53:16 +05:30
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();
2026-02-11 19:53:16 +05:30
}
else
{
rolledVal = GetDiceValue();
}
}
2026-01-28 20:46:45 +05:30
/*
* Summary:
* Call when the dice roll animation is complete
*/
public void OnDiceRollingCompleted()
{
animator.ResetTrigger(Ludo_3D_Constants.RollDiceTriggerString);
Debug.Log($"Dice roll test: OnDiceRollingCompleted");
2026-02-11 19:53:16 +05:30
if (rolledVal == Ludo_3D_Constants.Max_Dice_Rolls)
ResetRollData();
ResetData();
Debug.Log($"Dice roll test: Dice rolled: {rolledVal}");
2026-02-11 19:53:16 +05:30
onRollingComplete?.Invoke(rolledVal);
2026-01-28 20:46:45 +05:30
}
public void OnReturnedToIdle()
{
Debug.Log($"Dice roll test: OnReturnedToIdle: rolled: {rolledVal}");
animator.enabled = false;
if (rolledVal == 0)
{
diceImage.sprite = defaultSprite;
Debug.Log($"Dice roll test: OnReturnedToIdle: setting default sprite");
}
else
{
diceImage.sprite = diceSideDatas[rolledVal - 1].sprite;
}
Debug.Log($"Dice roll test: OnReturnedToIdle: diceImage.sprite: {diceImage.sprite.name}");
}
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 ResetData()
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
rolling = false;
2026-01-30 12:54:43 +05:30
}
public void ResetOnSessionEnd()
2026-01-30 12:54:43 +05:30
{
ResetData();
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
}