127 lines
3.1 KiB
C#
127 lines
3.1 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using System.Data.SqlTypes;
|
|
using UnityEditor;
|
|
|
|
public enum GameState
|
|
{
|
|
Idle,
|
|
ShowingSequance,
|
|
PlayerTurn,
|
|
GameOver,
|
|
MinigameWon
|
|
}
|
|
|
|
public class ButtonsMinigame : MonoBehaviour
|
|
{
|
|
public Light[] lights;
|
|
public int round = 1;
|
|
public float glowDuration = 0.4f;
|
|
public float gapBetween = 0.15f;
|
|
public float delayBeforeSequence = 0.8f;
|
|
public int winRound = 5;
|
|
public TMP_Text statusText;
|
|
public GameObject startButton;
|
|
private int playerStep = 0;
|
|
private GameState state = GameState.Idle;
|
|
private List<int> sequence = new List<int>();
|
|
|
|
void GenerateSequence()
|
|
{
|
|
sequence.Clear();
|
|
for (int i = 0; i < round; i++)
|
|
{
|
|
int randomButton = Random.Range(0,5);
|
|
sequence.Add(randomButton);
|
|
}
|
|
}
|
|
|
|
IEnumerator PlaySequence()
|
|
{
|
|
|
|
SetState(GameState.ShowingSequance);
|
|
yield return new WaitForSeconds(delayBeforeSequence);
|
|
|
|
for (int i = 0; i < sequence.Count; i++)
|
|
{
|
|
int buttonIndex = sequence[i];
|
|
lights[buttonIndex].gameObject.SetActive(true);
|
|
yield return new WaitForSeconds (glowDuration);
|
|
lights[buttonIndex].gameObject.SetActive(false);
|
|
yield return new WaitForSeconds (gapBetween);
|
|
}
|
|
|
|
playerStep = 0;
|
|
SetState(GameState.PlayerTurn);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
SetState(GameState.Idle);
|
|
}
|
|
|
|
public void StartGame()
|
|
{
|
|
round = 1;
|
|
playerStep = 0;
|
|
GenerateSequence();
|
|
StartCoroutine(PlaySequence());
|
|
}
|
|
|
|
void SetState(GameState newState)
|
|
{
|
|
state = newState;
|
|
|
|
switch (newState)
|
|
{
|
|
case GameState.Idle:
|
|
statusText.text = "Get Ready!";
|
|
break;
|
|
case GameState.ShowingSequance:
|
|
statusText.text = "Watch the sequence";
|
|
break;
|
|
case GameState.PlayerTurn:
|
|
statusText.text = "Your turn!";
|
|
break;
|
|
case GameState.GameOver:
|
|
statusText.text = "Gameover";
|
|
break;
|
|
case GameState.MinigameWon:
|
|
statusText.text = "Task Completed";
|
|
break;
|
|
}
|
|
|
|
startButton.SetActive(state == GameState.Idle || state == GameState.GameOver);
|
|
}
|
|
public void OnButtonClicked(int buttonIndex)
|
|
{
|
|
if (state != GameState.PlayerTurn) return;
|
|
if (buttonIndex == sequence[playerStep])
|
|
{
|
|
playerStep++;
|
|
if (playerStep >= sequence.Count)
|
|
{
|
|
if (round >= winRound)
|
|
{
|
|
SetState(GameState.MinigameWon);
|
|
}
|
|
else
|
|
{
|
|
round++;
|
|
GenerateSequence();
|
|
StartCoroutine(PlaySequence());
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SetState(GameState.GameOver);
|
|
Debug.Log("GameOver");
|
|
}
|
|
}
|
|
|
|
}
|