Added roll dice button
This commit is contained in:
parent
d04624edfd
commit
dbef10d80a
10 changed files with 472 additions and 82 deletions
46
PuzzleGameProject/Assets/Scripts/DicePair.cs
Normal file
46
PuzzleGameProject/Assets/Scripts/DicePair.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class DicePair
|
||||
{
|
||||
private (Die, Die) _pair = (null, null);
|
||||
|
||||
public void SelectDie(Die die)
|
||||
{
|
||||
if (_pair.Item1 == null && _pair.Item2 == null)
|
||||
{
|
||||
_pair.Item1 = die;
|
||||
die.DieBeingUsed(true, false);
|
||||
}
|
||||
else if (_pair.Item2 == null)
|
||||
{
|
||||
_pair.Item2 = die;
|
||||
_pair.Item1.DieBeingUsed(true, true);
|
||||
die.DieBeingUsed(true, true);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsDieInPair(Die die)
|
||||
{
|
||||
if (_pair.Item1 == die || _pair.Item2 == die)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AreBothDiceSelected()
|
||||
{
|
||||
if (_pair.Item1 != null && _pair.Item2 != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int Sum()
|
||||
{
|
||||
return _pair.Item1.GetResult() + _pair.Item2.GetResult();
|
||||
}
|
||||
}
|
||||
2
PuzzleGameProject/Assets/Scripts/DicePair.cs.meta
Normal file
2
PuzzleGameProject/Assets/Scripts/DicePair.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: df5fed04808fcf647aa3639c21e2bb2e
|
||||
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,25 @@ using TMPro;
|
|||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public enum DiceColor
|
||||
{
|
||||
Black,
|
||||
White,
|
||||
}
|
||||
|
||||
public class Die : MonoBehaviour
|
||||
{
|
||||
public DiceColor color;
|
||||
private int _result = 0;
|
||||
[SerializeField] private Button dieButton = null; // assign in the editor
|
||||
[SerializeField] private Button dieButton; // assign in the editor
|
||||
private Color originalColor;
|
||||
public event EventHandler<Die> DieClicked;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
dieButton.onClick.AddListener(() => { DiePressed();});
|
||||
|
||||
originalColor = gameObject.GetComponent<Image>().color;
|
||||
}
|
||||
|
||||
public void SetResult(int result) {
|
||||
|
|
@ -24,8 +34,9 @@ public class Die : MonoBehaviour
|
|||
return _result;
|
||||
}
|
||||
|
||||
public void ClearDie() {
|
||||
gameObject.GetComponent<TextMeshProUGUI>().text = String.Empty;
|
||||
public void ResetDie()
|
||||
{
|
||||
gameObject.GetComponent<Image>().color = originalColor;
|
||||
}
|
||||
|
||||
public void DieBeingUsed(bool isFirstPair, bool isPairComplete)
|
||||
|
|
@ -54,6 +65,7 @@ public class Die : MonoBehaviour
|
|||
|
||||
private void DieSelectedAndPairComplete()
|
||||
{
|
||||
gameObject.GetComponent<Outline>().enabled = false;
|
||||
gameObject.GetComponent<Image>().color =
|
||||
ColorHelper.AddColorTint(gameObject.GetComponent<Image>().color, ColorHelper.OkayGreen, 0.5f);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@ using System;
|
|||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
|
||||
public enum GameState
|
||||
{
|
||||
RollDice,
|
||||
PickRoomOne,
|
||||
PickRoomTwo,
|
||||
PickDiceOne,
|
||||
|
|
@ -12,28 +14,35 @@ public enum GameState
|
|||
}
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public GameState state;
|
||||
public GameObject rooms;
|
||||
public GameObject dice;
|
||||
public DiceRoller diceRoller;
|
||||
[SerializeField] private GameState state;
|
||||
[SerializeField] private GameObject rooms;
|
||||
[SerializeField] private DiceRoller diceRoller;
|
||||
|
||||
private (Die, Die) _dicePairOne = (null, null);
|
||||
private (Die, Die) _dicePairTwo = (null, null);
|
||||
private DicePair _dicePairOne = new DicePair();
|
||||
private DicePair _dicePairTwo = new DicePair();
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
state = GameState.PickDiceOne;
|
||||
diceRoller.diceRolled += DiceRolled;
|
||||
foreach (Transform roomTransform in rooms.transform)
|
||||
{
|
||||
roomTransform.gameObject.GetComponent<Room>().ValidRoomClicked += ValidRoomClicked;
|
||||
}
|
||||
|
||||
foreach (Transform diceTransform in dice.transform)
|
||||
foreach (Transform diceTransform in diceRoller.dice.transform)
|
||||
{
|
||||
diceTransform.gameObject.GetComponent<Die>().DieClicked += DieClicked;
|
||||
}
|
||||
diceRoller.RollDice();
|
||||
StartNewTurn();
|
||||
}
|
||||
|
||||
private void StartNewTurn()
|
||||
{
|
||||
state = GameState.RollDice;
|
||||
diceRoller.Enable();
|
||||
diceRoller.ResetDice();
|
||||
_dicePairOne = new DicePair();
|
||||
_dicePairTwo = new DicePair();
|
||||
}
|
||||
|
||||
void ValidRoomClicked(object sender, Room room) {
|
||||
|
|
@ -41,6 +50,14 @@ public class GameManager : MonoBehaviour
|
|||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case GameState.PickRoomOne when room.number != _dicePairOne.Sum():
|
||||
case GameState.PickRoomTwo when room.number != _dicePairTwo.Sum():
|
||||
return;
|
||||
}
|
||||
|
||||
room.SetRoomExplored();
|
||||
if (state == GameState.PickRoomOne)
|
||||
{
|
||||
|
|
@ -48,7 +65,7 @@ public class GameManager : MonoBehaviour
|
|||
}
|
||||
else
|
||||
{
|
||||
// Not implemented
|
||||
StartNewTurn();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -60,43 +77,59 @@ public class GameManager : MonoBehaviour
|
|||
return;
|
||||
}
|
||||
|
||||
if (state == GameState.PickDiceOne)
|
||||
switch (state)
|
||||
{
|
||||
if (_dicePairOne.Item1 == null && _dicePairOne.Item2 == null)
|
||||
case GameState.PickDiceOne:
|
||||
{
|
||||
_dicePairOne.Item1 = die;
|
||||
die.DieBeingUsed(true, false);
|
||||
}
|
||||
else if (_dicePairOne.Item2 == null)
|
||||
{
|
||||
_dicePairOne.Item2 = die;
|
||||
_dicePairOne.Item1.DieBeingUsed(true, true);
|
||||
die.DieBeingUsed(true, true);
|
||||
}
|
||||
if (!_dicePairOne.IsDieInPair(die))
|
||||
{
|
||||
_dicePairOne.SelectDie(die);
|
||||
|
||||
if (_dicePairOne.Item1 != null && _dicePairOne.Item2 != null)
|
||||
{
|
||||
state = GameState.PickRoomOne;
|
||||
if (_dicePairOne.AreBothDiceSelected())
|
||||
{
|
||||
state = GameState.PickRoomOne;
|
||||
HighLightValidRoomsWithNumber(_dicePairOne.Sum());
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(state == GameState.PickDiceTwo)
|
||||
{
|
||||
if (_dicePairTwo.Item1 == null && _dicePairTwo.Item2 == null)
|
||||
case GameState.PickDiceTwo:
|
||||
{
|
||||
_dicePairTwo.Item1 = die;
|
||||
die.DieBeingUsed(false, false);
|
||||
}
|
||||
else if (_dicePairTwo.Item2 == null)
|
||||
{
|
||||
_dicePairTwo.Item2 = die;
|
||||
_dicePairTwo.Item1.DieBeingUsed(false, true);
|
||||
die.DieBeingUsed(false, true);
|
||||
}
|
||||
if (!_dicePairTwo.IsDieInPair(die))
|
||||
{
|
||||
_dicePairTwo.SelectDie(die);
|
||||
|
||||
if (_dicePairTwo.Item1 != null && _dicePairTwo.Item2 != null)
|
||||
{
|
||||
state = GameState.PickRoomTwo;
|
||||
if (_dicePairTwo.AreBothDiceSelected())
|
||||
{
|
||||
state = GameState.PickRoomTwo;
|
||||
HighLightValidRoomsWithNumber(_dicePairTwo.Sum());
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HighLightValidRoomsWithNumber(int number)
|
||||
{
|
||||
foreach (Transform roomTransform in rooms.transform)
|
||||
{
|
||||
Room room = roomTransform.gameObject.GetComponent<Room>();
|
||||
if (room.number == number && room.IsValidRoomToExplore())
|
||||
{
|
||||
room.HighlightRoomAsOption();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DiceRolled(object sender, EventArgs e)
|
||||
{
|
||||
if (state == GameState.RollDice)
|
||||
{
|
||||
diceRoller.Disable();
|
||||
state = GameState.PickDiceOne;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ public class Room : MonoBehaviour
|
|||
public event EventHandler<Room> ValidRoomClicked;
|
||||
|
||||
bool _isExplored = false;
|
||||
|
||||
private Color _roomNumberOriginalColor;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start() {
|
||||
|
|
@ -28,10 +30,22 @@ public class Room : MonoBehaviour
|
|||
|
||||
public void SetRoomExplored() {
|
||||
_isExplored = true;
|
||||
UnhighlightRoomAsOption();
|
||||
gameObject.GetComponent<SpriteRenderer>().color =
|
||||
ColorHelper.AddColorTint(gameObject.GetComponent<SpriteRenderer>().color, Color.grey, 0.5f);
|
||||
}
|
||||
|
||||
public void HighlightRoomAsOption()
|
||||
{
|
||||
_roomNumberOriginalColor = gameObject.GetComponentInChildren<TextMeshProUGUI>().color;
|
||||
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = Color.blue;
|
||||
}
|
||||
|
||||
public void UnhighlightRoomAsOption()
|
||||
{
|
||||
gameObject.GetComponentInChildren<TextMeshProUGUI>().color = _roomNumberOriginalColor;
|
||||
}
|
||||
|
||||
void SetPropertiesOfEntrance() {
|
||||
gameObject.GetComponent<SpriteRenderer>().color = Color.green;
|
||||
isEntrance = true;
|
||||
|
|
@ -50,7 +64,7 @@ public class Room : MonoBehaviour
|
|||
}
|
||||
|
||||
// Check if the room is valid to be explored. If so trigger the event.
|
||||
bool IsValidRoomToExplore() {
|
||||
public bool IsValidRoomToExplore() {
|
||||
if (_isExplored) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue