Controller voor de Klok minigame
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ClockController : MonoBehaviour
{
[SerializeField]
private GameObject ClockMinute;
[SerializeField]
private GameObject ClockHour;
[SerializeField]
private GameObject codeManager;
private string for1;
private string for2;
private string for3;
private bool isPlaying = false;
private List<string> ifSocketBlocks = new List<string>();
[SerializeField]
private List<GameObject> uiElements;
[SerializeField]
private GameObject ToDoListItem;
[SerializeField]
private SpawnKey spawnKey;
private bool keySpawned = false;
[SerializeField]
private AudioSource audioSource;
private void Update()
{
if(isPlaying)
{
StartClock();
isPlaying = false;
}
}
public void playGame()
{
For();
isPlaying = true;
}
private void For()
{
foreach (Transform child in codeManager.transform)
{
//look for the gameobject with the name IF
if (child.name == "For")
{
foreach (Transform socket in child)
{
ifSocketBlocks.Add(socket.tag);
}
}
}
for1 = ifSocketBlocks.Count > 0 ? ifSocketBlocks[0] : null;
for2 = ifSocketBlocks.Count > 1 ? ifSocketBlocks[1] : null;
for3 = ifSocketBlocks.Count > 2 ? ifSocketBlocks[2] : null;
ifSocketBlocks.Clear();
}
private void StartClock()
{
if (ValidateClockStructure())
{
StartCoroutine(RunClock());
}
else
{
isPlaying = false;
}
}
IEnumerator RunClock()
{
// Hours settings
int hoursStart = 0;
int hoursEnd = 12;
// Minutes settings
int minutesStart = 0;
int minutesEnd = 60; // Default to 60 minutes
// Parse the end values
if (for2 != null && int.TryParse(for2, out int parsedHoursEnd))
{
hoursEnd = parsedHoursEnd;
}
// Validate both loop structures
if (IsHoursTag(for1) && IsNestedTag(for3))
{
uiElements.ForEach(uiElement => uiElement.SetActive(false));
ToDoListItem.SetActive(true);
if (!keySpawned)
{
spawnKey.spawnKey();
keySpawned = true;
audioSource.Play();
}
for (int hours = hoursStart; hours < hoursEnd; hours++)
{
for (int minutes = minutesStart; minutes < minutesEnd; minutes++)
{
ClockMinute.transform.Rotate(0, 0, 3);
ClockHour.transform.Rotate(0, 0, 0.25f);
yield return new WaitForSeconds(0.1f); // Fast time for testing
}
}
isPlaying = false;
}
else
{
Debug.LogError("Invalid clock structure.");
}
}
bool IsHoursTag(string tag)
{
return tag?.ToLower() == "hours";
}
bool IsNestedTag(string tag)
{
return tag?.ToLower() == "nested";
}
private bool ValidateClockStructure()
{
// Validate hours loop
if (!IsHoursTag(for1))
{
Debug.LogError("First nested value must be 'hours'");
return false;
}
if (!int.TryParse(for2, out _))
{
Debug.LogError("Second nested value must be a number");
return false;
}
if (!IsNestedTag(for3))
{
Debug.LogError("Third nested value must be 'nested'");
return false;
}
return true;
}
}