80 lines
2.2 KiB
C#
80 lines
2.2 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.SceneManagement;
|
||
|
||
public class CableMiniGame : MonoBehaviour
|
||
{
|
||
[System.Serializable]
|
||
public class Cable
|
||
{
|
||
public string colorName; // Název barvy kabelu
|
||
public Button sourceButton; // Zdrojový konektor
|
||
public Button targetButton; // Cílový konektor
|
||
public Image cableImage; // Pro změnu barvy kabelu
|
||
}
|
||
|
||
public Cable[] cables;
|
||
public string previousSceneName; // Scéna kam se vrátit při chybě
|
||
public Color wrongAttemptColor = Color.white; // Bliknutí při špatném pokusu
|
||
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)
|
||
{
|
||
// Správné propojení
|
||
Debug.Log("Správně!");
|
||
cable.cableImage.color = GetColorFromName(selectedColor);
|
||
}
|
||
else
|
||
{
|
||
// Špatné propojení – reset scény
|
||
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);
|
||
}
|
||
} |