48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using static TagsAndLayers;
|
|
using Random = Unity.Mathematics.Random;
|
|
|
|
public class DiceRoller: MonoBehaviour
|
|
{
|
|
private const int NUMBER_OF_WHITE_DICE = 4;
|
|
private const int NUMBER_OF_BLACK_DICE = 1;
|
|
|
|
private List<int> _rolledWhiteDice = new List<int>();
|
|
private List<int> _rolledBlackDice = new List<int>();
|
|
private System.Random _randomGen = new System.Random();
|
|
|
|
public GameObject dice;
|
|
|
|
public void RollDice() {
|
|
for (int i = 0; i < NUMBER_OF_WHITE_DICE; i++)
|
|
{
|
|
_rolledWhiteDice.Add(_randomGen.Next(1,6));
|
|
}
|
|
for (int i = 0; i < NUMBER_OF_BLACK_DICE; i++)
|
|
{
|
|
_rolledBlackDice.Add(_randomGen.Next(1,6));
|
|
}
|
|
UpdateGUI();
|
|
}
|
|
|
|
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>())
|
|
.ToArray();
|
|
Die[] blackDice = Helper.GetTaggedChildren(dice, BLACK_DIE_TAG).Select(die => die.GetComponent<Die>())
|
|
.ToArray();
|
|
|
|
// Set the result of the dice object with the randomly generated number
|
|
for (int i = 0; i < NUMBER_OF_WHITE_DICE; i++)
|
|
{
|
|
whiteDice[i].SetResult(_rolledWhiteDice[i]);
|
|
}
|
|
for (int i = 0; i < NUMBER_OF_BLACK_DICE; i++)
|
|
{
|
|
blackDice[i].SetResult(_rolledBlackDice[i]);
|
|
}
|
|
}
|
|
}
|