166 lines
3.9 KiB
C#
Raw Permalink 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;
2026-02-12 14:38:40 +05:30
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;
[SerializeField] private Button diceButton;
2026-02-12 21:34:08 +05:30
[SerializeField] private RectTransform diceRectTransform;
[SerializeField] private int diceVal;
2026-01-28 20:46:45 +05:30
private Animator animator;
2026-02-12 21:34:08 +05:30
private List<int> probabilityValues = new List<int>() { 2, 3, 4 };
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>();
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
{
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-02-12 21:34:08 +05:30
if (diceVal != 0) rolledVal = diceVal;
}
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);
2026-02-11 19:53:16 +05:30
if (rolledVal == Ludo_3D_Constants.Max_Dice_Rolls)
ResetRollData();
ResetData();
2026-02-11 19:53:16 +05:30
onRollingComplete?.Invoke(rolledVal);
2026-01-28 20:46:45 +05:30
}
public void OnReturnedToIdle()
{
2026-02-12 21:34:08 +05:30
Debug.Log($"DiceView :: OnReturnedToIdle: {rolledVal}");
animator.enabled = false;
if (rolledVal == 0)
{
2026-02-12 21:34:08 +05:30
ShowDefaultSprite();
}
else
{
diceImage.sprite = diceSideDatas[rolledVal - 1].sprite;
}
}
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
}
2026-02-12 14:38:40 +05:30
public void SetDiceButtonInteraction(bool status)
{
diceButton.interactable = status;
}
public void ResetOnSessionEnd()
2026-01-30 12:54:43 +05:30
{
ResetData();
2026-02-12 21:34:08 +05:30
rolledVal = 0;
OnReturnedToIdle();
diceRectTransform.rotation = Quaternion.identity;
diceRectTransform.localScale = Vector3.one;
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-02-12 21:34:08 +05:30
public void ShowDefaultSprite()
{
Debug.Log($"DiceView :: ShowDefaultSprite");
diceImage.sprite = defaultSprite;
}
2026-01-28 20:46:45 +05:30
}