78 lines
2.0 KiB
C#
78 lines
2.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class CableMiniGame : MonoBehaviour
|
|
{
|
|
[System.Serializable]
|
|
public class Cable
|
|
{
|
|
public string colorName;
|
|
public Button sourceButton;
|
|
public Button targetButton;
|
|
public Image cableImage;
|
|
}
|
|
|
|
public Cable[] cables;
|
|
public string previousSceneName;
|
|
public Color wrongAttemptColor = Color.white;
|
|
public float blinkDuration = 0.2f;
|
|
|
|
private string selectedColor = null;
|
|
|
|
void Start()
|
|
{
|
|
foreach (var cable in cables)
|
|
{
|
|
cable.sourceButton.onClick.AddListener(() => OnSourceClicked(cable));
|
|
cable.targetButton.onClick.AddListener(() => OnTargetClicked(cable));
|
|
}
|
|
}
|
|
|
|
void OnSourceClicked(Cable cable)
|
|
{
|
|
selectedColor = cable.colorName;
|
|
Debug.Log("Vybrána barva: " + selectedColor);
|
|
}
|
|
|
|
void OnTargetClicked(Cable cable)
|
|
{
|
|
if (selectedColor == null)
|
|
return;
|
|
|
|
if (selectedColor == cable.colorName)
|
|
{
|
|
Debug.Log("Správně!");
|
|
cable.cableImage.color = GetColorFromName(selectedColor);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Špatně, restart!");
|
|
StartCoroutine(BlinkAndReset(cable.cableImage));
|
|
}
|
|
|
|
selectedColor = null;
|
|
}
|
|
|
|
Color GetColorFromName(string name)
|
|
{
|
|
switch (name.ToLower())
|
|
{
|
|
case "red": return Color.red;
|
|
case "blue": return Color.blue;
|
|
case "green": return Color.green;
|
|
case "yellow": return Color.yellow;
|
|
case "purple": return new Color(0.5f, 0, 0.5f);
|
|
default: return Color.white;
|
|
}
|
|
}
|
|
|
|
System.Collections.IEnumerator BlinkAndReset(Image img)
|
|
{
|
|
Color original = img.color;
|
|
img.color = wrongAttemptColor;
|
|
yield return new WaitForSeconds(blinkDuration);
|
|
img.color = original;
|
|
SceneManager.LoadScene(previousSceneName);
|
|
}
|
|
} |