Implemented turn passing and taking damage from it.
This commit is contained in:
parent
31917ba415
commit
ede5ef1533
6 changed files with 572 additions and 15 deletions
|
|
@ -14,17 +14,21 @@ public enum GameState
|
|||
}
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameState state;
|
||||
[SerializeField] public GameState state;
|
||||
[SerializeField] private GameObject rooms;
|
||||
[SerializeField] private DiceRoller diceRoller;
|
||||
[SerializeField] private PassManager passManager;
|
||||
[SerializeField] private Player player;
|
||||
|
||||
private DicePair _dicePairOne = new DicePair();
|
||||
private DicePair _dicePairTwo = new DicePair();
|
||||
private DicePair _dicePairOne = new();
|
||||
private DicePair _dicePairTwo = new();
|
||||
public event EventHandler<GameState> StateChanged;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
diceRoller.diceRolled += DiceRolled;
|
||||
passManager.PassRequested += PassRequested;
|
||||
foreach (Transform roomTransform in rooms.transform)
|
||||
{
|
||||
roomTransform.gameObject.GetComponent<Room>().ValidRoomClicked += ValidRoomClicked;
|
||||
|
|
@ -35,10 +39,15 @@ public class GameManager : MonoBehaviour
|
|||
}
|
||||
StartNewTurn();
|
||||
}
|
||||
|
||||
private void ChangeState(GameState stateToChangeTo) {
|
||||
state = stateToChangeTo;
|
||||
StateChanged?.Invoke(this, state);
|
||||
}
|
||||
|
||||
private void StartNewTurn()
|
||||
{
|
||||
state = GameState.RollDice;
|
||||
ChangeState(GameState.RollDice);
|
||||
diceRoller.Enable();
|
||||
diceRoller.ResetDice();
|
||||
_dicePairOne = new DicePair();
|
||||
|
|
@ -61,13 +70,12 @@ public class GameManager : MonoBehaviour
|
|||
room.SetRoomExplored();
|
||||
if (state == GameState.PickRoomOne)
|
||||
{
|
||||
state = GameState.PickDiceTwo;
|
||||
ChangeState(GameState.PickDiceTwo);
|
||||
}
|
||||
else
|
||||
{
|
||||
StartNewTurn();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DieClicked(object sender, Die die)
|
||||
|
|
@ -82,7 +90,7 @@ public class GameManager : MonoBehaviour
|
|||
|
||||
if (_dicePairOne.AreBothDiceSelected())
|
||||
{
|
||||
state = GameState.PickRoomOne;
|
||||
ChangeState(GameState.PickRoomOne);
|
||||
HighLightValidRoomsWithNumber(_dicePairOne);
|
||||
}
|
||||
}
|
||||
|
|
@ -101,7 +109,7 @@ public class GameManager : MonoBehaviour
|
|||
|
||||
if (_dicePairTwo.AreBothDiceSelected())
|
||||
{
|
||||
state = GameState.PickRoomTwo;
|
||||
ChangeState(GameState.PickRoomTwo);
|
||||
HighLightValidRoomsWithNumber(_dicePairTwo);
|
||||
}
|
||||
}
|
||||
|
|
@ -117,7 +125,7 @@ public class GameManager : MonoBehaviour
|
|||
{
|
||||
_dicePairOne.UnselectDie(die);
|
||||
UnhighLightRoomsAsOptions();
|
||||
state = GameState.PickDiceOne;
|
||||
ChangeState(GameState.PickDiceOne);
|
||||
}
|
||||
break;
|
||||
case GameState.PickRoomTwo:
|
||||
|
|
@ -125,12 +133,17 @@ public class GameManager : MonoBehaviour
|
|||
{
|
||||
_dicePairTwo.UnselectDie(die);
|
||||
UnhighLightRoomsAsOptions();
|
||||
state = GameState.PickDiceTwo;
|
||||
ChangeState(GameState.PickDiceTwo);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void PassTurn() {
|
||||
player.TakeDamage(1);
|
||||
StartNewTurn();
|
||||
}
|
||||
|
||||
void HighLightValidRoomsWithNumber(DicePair pair)
|
||||
{
|
||||
foreach (Transform roomTransform in rooms.transform)
|
||||
|
|
@ -156,7 +169,14 @@ public class GameManager : MonoBehaviour
|
|||
if (state == GameState.RollDice)
|
||||
{
|
||||
diceRoller.Disable();
|
||||
state = GameState.PickDiceOne;
|
||||
ChangeState(GameState.PickDiceOne);
|
||||
}
|
||||
}
|
||||
|
||||
private void PassRequested(object sender, EventArgs e) {
|
||||
if (state is GameState.PickDiceOne or GameState.PickDiceTwo)
|
||||
{
|
||||
PassTurn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
36
PuzzleGameProject/Assets/Scripts/PassManager.cs
Normal file
36
PuzzleGameProject/Assets/Scripts/PassManager.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEngine.Video;
|
||||
using Button = UnityEngine.UI.Button;
|
||||
|
||||
public class PassManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Button passButton;
|
||||
[SerializeField] private GameManager gameManager;
|
||||
|
||||
public event EventHandler PassRequested;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
passButton.onClick.AddListener(OnPassClicked);
|
||||
gameManager.StateChanged += OnGameStateChange;
|
||||
}
|
||||
|
||||
private void OnGameStateChange(object sender, GameState state) {
|
||||
if (state == GameState.RollDice)
|
||||
{
|
||||
passButton.interactable = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
passButton.interactable = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPassClicked()
|
||||
{
|
||||
PassRequested?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
2
PuzzleGameProject/Assets/Scripts/PassManager.cs.meta
Normal file
2
PuzzleGameProject/Assets/Scripts/PassManager.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 95487118287b4514fb9b9fb7018ae717
|
||||
39
PuzzleGameProject/Assets/Scripts/Player.cs
Normal file
39
PuzzleGameProject/Assets/Scripts/Player.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using Microsoft.Unity.VisualStudio.Editor;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private int maxHealth;
|
||||
[SerializeField] private GameObject healthGameObject;
|
||||
private int _healthIndex = 0;
|
||||
private readonly int[] _healthBar = {0, 0, -1, -2, -4, -6, -9, -12, -16, -20};
|
||||
|
||||
private void Start() {
|
||||
UpdateGUI();
|
||||
}
|
||||
|
||||
public void TakeDamage(int damage)
|
||||
{
|
||||
if (_healthIndex + damage < _healthBar.Length)
|
||||
{
|
||||
_healthIndex += damage;
|
||||
UpdateGUI();
|
||||
}
|
||||
}
|
||||
|
||||
public void Heal(int healAmount)
|
||||
{
|
||||
if (_healthIndex - healAmount >= 0)
|
||||
{
|
||||
_healthIndex -= healAmount;
|
||||
UpdateGUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGUI()
|
||||
{
|
||||
healthGameObject.GetComponent<TextMeshProUGUI>().text = _healthBar[_healthIndex].ToString();
|
||||
}
|
||||
}
|
||||
2
PuzzleGameProject/Assets/Scripts/Player.cs.meta
Normal file
2
PuzzleGameProject/Assets/Scripts/Player.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 516f63f4d32b3614b92fb0535c742078
|
||||
Loading…
Add table
Add a link
Reference in a new issue