Add basic intro countdown and game states
This commit is contained in:
parent
89e482d387
commit
15abaa80fa
10 changed files with 740 additions and 6 deletions
84
Assets/Scripts/GameStateManager.cs
Normal file
84
Assets/Scripts/GameStateManager.cs
Normal file
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameStateManager : MonoBehaviour
|
||||
{
|
||||
|
||||
public static GameStateManager Instace{ get; private set; }
|
||||
|
||||
public event EventHandler OnStateChanged;
|
||||
|
||||
private enum State
|
||||
{
|
||||
WaitingToStart,
|
||||
StartingCountdown,
|
||||
GamePlaying,
|
||||
GameOver,
|
||||
}
|
||||
|
||||
private State state;
|
||||
|
||||
// --- Timers ---
|
||||
private float waitingToStartTimer = 1f;
|
||||
private float countdownToStartTimer = 3f;
|
||||
private float gamePlayTimer = 10f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
state = State.WaitingToStart;
|
||||
Instace = this;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case State.WaitingToStart:
|
||||
waitingToStartTimer -= Time.deltaTime;
|
||||
if (waitingToStartTimer < 0f)
|
||||
{
|
||||
state = State.StartingCountdown;
|
||||
OnStateChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
break;
|
||||
case State.StartingCountdown:
|
||||
countdownToStartTimer -= Time.deltaTime;
|
||||
if (countdownToStartTimer < 0f)
|
||||
{
|
||||
state = State.GamePlaying;
|
||||
OnStateChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
break;
|
||||
case State.GamePlaying:
|
||||
gamePlayTimer -= Time.deltaTime;
|
||||
if (gamePlayTimer < 0f)
|
||||
{
|
||||
state = State.GameOver;
|
||||
OnStateChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
break;
|
||||
case State.GameOver:
|
||||
break;
|
||||
}
|
||||
|
||||
Debug.Log(state);
|
||||
}
|
||||
|
||||
public bool IsGamePlaying()
|
||||
{
|
||||
return state == State.GamePlaying;
|
||||
}
|
||||
|
||||
public bool IsCountdownToStartActive()
|
||||
{
|
||||
return state == State.StartingCountdown;
|
||||
}
|
||||
|
||||
public float GetCountdownToStartTimer()
|
||||
{
|
||||
return countdownToStartTimer;
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/GameStateManager.cs.meta
Normal file
11
Assets/Scripts/GameStateManager.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bae018fdb9cea3b499d7bcd68103404c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -34,14 +34,19 @@ public class Player : MonoBehaviour, IKitchenObjectParent
|
|||
|
||||
private void GameInput_OnInteractAlteranteAction(object sender, EventArgs e)
|
||||
{
|
||||
if (GameStateManager.Instace.IsGamePlaying()) return;
|
||||
|
||||
if (selectedCounter != null)
|
||||
{
|
||||
selectedCounter.InteractAlternate(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void GameInput_OnInteractAction(object sender, System.EventArgs e)
|
||||
{
|
||||
if (GameStateManager.Instace.IsGamePlaying()) return;
|
||||
|
||||
if (selectedCounter != null)
|
||||
{
|
||||
selectedCounter.Interact(this);
|
||||
|
|
45
Assets/Scripts/UI/GameStartCountdownUI.cs
Normal file
45
Assets/Scripts/UI/GameStartCountdownUI.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameStartCountdownUI : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private TextMeshProUGUI countdownText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
GameStateManager.Instace.OnStateChanged += GameStateManager_OnStateChanged;
|
||||
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void GameStateManager_OnStateChanged(object sender, System.EventArgs e)
|
||||
{
|
||||
|
||||
if (GameStateManager.Instace.IsCountdownToStartActive())
|
||||
{
|
||||
Show();
|
||||
} else
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
countdownText.text = Mathf.Ceil(GameStateManager.Instace.GetCountdownToStartTimer()).ToString();
|
||||
}
|
||||
|
||||
private void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/GameStartCountdownUI.cs.meta
Normal file
11
Assets/Scripts/UI/GameStartCountdownUI.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2e210821ea09ba64fa474a98e4760bcb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in a new issue