Refactored clicking on dice and rooms so that logic is moved out of the game manager
This commit is contained in:
parent
ede5ef1533
commit
e7ea8302ad
8 changed files with 150 additions and 67 deletions
|
|
@ -15,11 +15,14 @@ public class Die : MonoBehaviour
|
|||
private int _result = 0;
|
||||
[SerializeField] private Button dieButton; // assign in the editor
|
||||
private Color originalColor;
|
||||
public event EventHandler<Die> DieClicked;
|
||||
private bool _isClickable = false;
|
||||
private bool _selected = false;
|
||||
public event Action<Die> DieSelected;
|
||||
public event Action<Die> DieUnselected;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
dieButton.onClick.AddListener(() => { DiePressed();});
|
||||
private void Start() {
|
||||
GameManager.Instance.StateChanged += HandleStateChange;
|
||||
dieButton.onClick.AddListener(DiePressed);
|
||||
|
||||
originalColor = gameObject.GetComponent<Image>().color;
|
||||
}
|
||||
|
|
@ -34,8 +37,8 @@ public class Die : MonoBehaviour
|
|||
return _result;
|
||||
}
|
||||
|
||||
public void ResetDie()
|
||||
{
|
||||
public void ResetDie() {
|
||||
_selected = false;
|
||||
gameObject.GetComponent<Image>().color = originalColor;
|
||||
gameObject.GetComponent<Outline>().enabled = false;
|
||||
}
|
||||
|
|
@ -56,18 +59,46 @@ public class Die : MonoBehaviour
|
|||
|
||||
private void DiePressed()
|
||||
{
|
||||
DieClicked?.Invoke(this, this);
|
||||
if (!_isClickable) return;
|
||||
|
||||
if (_selected)
|
||||
{
|
||||
DieUnselected?.Invoke(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
DieSelected?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void DieSelectedButPairNotComplete()
|
||||
{
|
||||
_selected = true;
|
||||
gameObject.GetComponent<Outline>().enabled = true;
|
||||
}
|
||||
|
||||
private void DieSelectedAndPairComplete()
|
||||
{
|
||||
private void DieSelectedAndPairComplete() {
|
||||
_selected = true;
|
||||
gameObject.GetComponent<Outline>().enabled = false;
|
||||
gameObject.GetComponent<Image>().color =
|
||||
ColorHelper.AddColorTint(gameObject.GetComponent<Image>().color, ColorHelper.OkayGreen, 0.5f);
|
||||
}
|
||||
|
||||
private void HandleStateChange(GameState state) {
|
||||
switch (state)
|
||||
{
|
||||
case GameState.PickDiceOne:
|
||||
case GameState.PickDiceTwo:
|
||||
case GameState.PickRoomOne:
|
||||
case GameState.PickRoomTwo:
|
||||
_isClickable = true;
|
||||
break;
|
||||
case GameState.RollDice:
|
||||
_isClickable = false;
|
||||
break;
|
||||
default:
|
||||
_isClickable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue