# Conflicts: # Assets/External-Assets/packages/Project/Scripts/Gameplay/Snake and Ladder/DiceView.cs
156 lines
3.9 KiB
C#
156 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public enum DiceSideVal
|
|
{
|
|
One = 0,
|
|
Two = 1,
|
|
Three = 2,
|
|
Four = 3,
|
|
Five = 4,
|
|
Six = 5,
|
|
}
|
|
|
|
[Serializable]
|
|
public class DiceSideData
|
|
{
|
|
public DiceSideVal sideVal;
|
|
public Sprite sprite;
|
|
}
|
|
|
|
public class DiceView : MonoBehaviour, IBase
|
|
{
|
|
[SerializeField] private DiceSideData[] diceSideDatas;
|
|
[SerializeField] private Image diceImage;
|
|
[SerializeField] private Sprite defaultSprite;
|
|
[SerializeField] private Button diceButton;
|
|
|
|
private Animator animator;
|
|
private List<int> probabilityValues = new List<int>() { 3, 4, 5 };
|
|
private Action<int> onRollingComplete = null;
|
|
|
|
private IRollBase rollBase = null;
|
|
|
|
private bool rolling;
|
|
private bool hasNoActionOnRoll = false;
|
|
private int rolledVal = 0;
|
|
|
|
private void Awake()
|
|
{
|
|
animator = GetComponent<Animator>();
|
|
Debug.Log($"Dice roll test: StartRollingAction: animator: {animator}");
|
|
}
|
|
|
|
public void Roll(Action<int> onComplete, bool isBot)
|
|
{
|
|
Debug.Log($"Start rolling: {rolling}");
|
|
|
|
if (!rolling)
|
|
{
|
|
Debug.Log($"isBot: {isBot}");
|
|
animator.enabled = true;
|
|
onRollingComplete = onComplete;
|
|
StartRollingAction();
|
|
}
|
|
}
|
|
|
|
private void StartRollingAction()
|
|
{
|
|
Debug.Log($"Dice roll test: StartRollingAction: animator: {animator}, {gameObject.name}");
|
|
rolling = true;
|
|
rolledVal = 0;
|
|
// start animation
|
|
animator.SetTrigger(Ludo_3D_Constants.RollDiceTriggerString);
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Summary:
|
|
* Call when the dice roll animation is complete
|
|
*/
|
|
public void OnDiceRollingCompleted()
|
|
{
|
|
animator.ResetTrigger(Ludo_3D_Constants.RollDiceTriggerString);
|
|
Debug.Log($"Dice roll test: OnDiceRollingCompleted");
|
|
|
|
if (rolledVal == Ludo_3D_Constants.Max_Dice_Rolls)
|
|
ResetRollData();
|
|
|
|
ResetData();
|
|
Debug.Log($"Dice roll test: Dice rolled: {rolledVal}");
|
|
onRollingComplete?.Invoke(rolledVal);
|
|
}
|
|
|
|
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}");
|
|
}
|
|
|
|
private int GetDiceValue()
|
|
{
|
|
return Random.Range(1, Ludo_3D_Constants.Max_Dice_Rolls + 1);
|
|
}
|
|
|
|
public void ResetRollData()
|
|
{
|
|
if (rollBase != null) rollBase.ResetRollData();
|
|
}
|
|
|
|
public void ResetData()
|
|
{
|
|
rollBase = null;
|
|
rolling = false;
|
|
}
|
|
|
|
public void SetDiceButtonInteraction(bool status)
|
|
{
|
|
diceButton.interactable = status;
|
|
|
|
}
|
|
|
|
public void ResetOnSessionEnd()
|
|
{
|
|
ResetData();
|
|
onRollingComplete = null;
|
|
}
|
|
|
|
public void InitOnNoActionsAfterRoll(IRollBase rollBase, bool state)
|
|
{
|
|
hasNoActionOnRoll = state;
|
|
if (!state)
|
|
return;
|
|
|
|
this.rollBase = rollBase;
|
|
}
|
|
}
|