Added roll dice button
This commit is contained in:
parent
d04624edfd
commit
dbef10d80a
10 changed files with 472 additions and 82 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using static TagsAndLayers;
|
||||
|
|
@ -13,10 +14,29 @@ public class DiceRoller: MonoBehaviour
|
|||
private List<int> _rolledWhiteDice = new List<int>();
|
||||
private List<int> _rolledBlackDice = new List<int>();
|
||||
private System.Random _randomGen = new System.Random();
|
||||
[SerializeField] private Button rollDiceButton;
|
||||
|
||||
public GameObject dice;
|
||||
|
||||
public void RollDice() {
|
||||
public event EventHandler diceRolled;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rollDiceButton.onClick.AddListener(RollDice);
|
||||
}
|
||||
|
||||
public void Enable()
|
||||
{
|
||||
rollDiceButton.interactable = true;
|
||||
}
|
||||
|
||||
public void Disable()
|
||||
{
|
||||
rollDiceButton.interactable = false;
|
||||
}
|
||||
|
||||
private void RollDice() {
|
||||
_rolledWhiteDice.Clear();
|
||||
_rolledBlackDice.Clear();
|
||||
for (int i = 0; i < NUMBER_OF_WHITE_DICE; i++)
|
||||
{
|
||||
_rolledWhiteDice.Add(_randomGen.Next(1,6));
|
||||
|
|
@ -25,9 +45,18 @@ public class DiceRoller: MonoBehaviour
|
|||
{
|
||||
_rolledBlackDice.Add(_randomGen.Next(1,6));
|
||||
}
|
||||
diceRolled?.Invoke(this, EventArgs.Empty);
|
||||
UpdateGUI();
|
||||
}
|
||||
|
||||
public void ResetDice()
|
||||
{
|
||||
foreach (Transform dieTransform in dice.transform)
|
||||
{
|
||||
dieTransform.GetComponent<Die>().ResetDie();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGUI() {
|
||||
// Get dice object from the game objects with the right tag.
|
||||
Die[] whiteDice = Helper.GetTaggedChildren(dice, WHITE_DIE_TAG).Select(die => die.GetComponent<Die>())
|
||||
|
|
@ -45,4 +74,5 @@ public class DiceRoller: MonoBehaviour
|
|||
blackDice[i].SetResult(_rolledBlackDice[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue