155 lines
3.8 KiB
C#
155 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlTypes;
|
|
using System.Threading.Tasks;
|
|
using TMPro;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public enum MinigameState
|
|
{
|
|
Idle,
|
|
ShowingSequance,
|
|
PlayerTurn,
|
|
GameOver,
|
|
MinigameWon
|
|
}
|
|
|
|
public class ButtonsMinigame : MonoBehaviour, ITask
|
|
{
|
|
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 MinigameState state = MinigameState.Idle;
|
|
private List<int> sequence = new List<int>();
|
|
|
|
public string TaskID { get; set; }
|
|
|
|
public TaskType TaskType { get; set; }
|
|
|
|
public string TaskName { get; set; }
|
|
|
|
public (double, double) TaskLocation { get; set; }
|
|
|
|
public bool IsCompleted { get; private set; }
|
|
private System.Action<ITask> _onComplete;
|
|
private Action<ITask> _onExit;
|
|
void GenerateSequence()
|
|
{
|
|
sequence.Clear();
|
|
for (int i = 0; i < round; i++)
|
|
{
|
|
int randomButton = UnityEngine.Random.Range(0,5);
|
|
sequence.Add(randomButton);
|
|
}
|
|
}
|
|
|
|
IEnumerator PlaySequence()
|
|
{
|
|
|
|
SetState(MinigameState.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(MinigameState.PlayerTurn);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
SetState(MinigameState.Idle);
|
|
}
|
|
|
|
public void StartGame()
|
|
{
|
|
round = 1;
|
|
playerStep = 0;
|
|
GenerateSequence();
|
|
StartCoroutine(PlaySequence());
|
|
}
|
|
|
|
void SetState(MinigameState newState)
|
|
{
|
|
state = newState;
|
|
|
|
switch (newState)
|
|
{
|
|
case MinigameState.Idle:
|
|
statusText.text = "Get Ready!";
|
|
break;
|
|
case MinigameState.ShowingSequance:
|
|
statusText.text = "Watch the sequence";
|
|
break;
|
|
case MinigameState.PlayerTurn:
|
|
statusText.text = "Your turn!";
|
|
break;
|
|
case MinigameState.GameOver:
|
|
statusText.text = "Gameover";
|
|
break;
|
|
case MinigameState.MinigameWon:
|
|
statusText.text = "Task Completed";
|
|
break;
|
|
}
|
|
|
|
startButton.SetActive(state == MinigameState.Idle || state == MinigameState.GameOver);
|
|
}
|
|
public void OnButtonClicked(int buttonIndex)
|
|
{
|
|
if (state != MinigameState.PlayerTurn) return;
|
|
if (buttonIndex == sequence[playerStep])
|
|
{
|
|
playerStep++;
|
|
if (playerStep >= sequence.Count)
|
|
{
|
|
if (round >= winRound)
|
|
{
|
|
SetState(MinigameState.MinigameWon);
|
|
Complete();
|
|
}
|
|
else
|
|
{
|
|
round++;
|
|
GenerateSequence();
|
|
StartCoroutine(PlaySequence());
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SetState(MinigameState.GameOver);
|
|
Debug.Log("GameOver");
|
|
}
|
|
}
|
|
|
|
public void Initialize(System.Action<ITask> onCompleted)
|
|
{
|
|
_onComplete = onCompleted;
|
|
}
|
|
|
|
public void ExitTask(System.Action<ITask> onExit)
|
|
{
|
|
onExit.Invoke(this);
|
|
}
|
|
|
|
public void Complete()
|
|
{
|
|
_onComplete?.Invoke(this);
|
|
}
|
|
}
|