Hra kabely

This commit is contained in:
2026-03-28 19:28:59 +01:00
parent f7926a218e
commit 1de91b0d57
80 changed files with 22549 additions and 653 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 078ea16f7b9620d4f839c1d44f968b45
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,214 @@
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class FlappyBirdAllInOne : MonoBehaviour, ITask
{
[Header("Player")]
public Rigidbody2D rb;
public float jumpForce = 5f;
public bool isDead = false;
[Header("Pipes")]
public GameObject pipePrefab;
public Transform spawnPoint;
public float spawnRate = 2f;
public float heightOffset = 2f;
public float pipeSpeed = 2f;
private float timer = 0;
[Header("UI")]
public TextMeshProUGUI scoreText;
public GameObject gameOverPanel;
private int score = 0;
private Action<ITask> _onCompleted;
private Action<ITask> _onExit;
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; }
void Start()
{
Time.timeScale = 1f;
score = 0;
UpdateScore();
}
void Update()
{
if (isDead) return;
HandleInput();
HandleSpawning();
}
void HandleInput()
{
if (Input.GetMouseButtonDown(0))
{
Jump();
}
}
void Jump()
{
rb.linearVelocity = Vector2.zero;
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
void HandleSpawning()
{
timer += Time.deltaTime;
if (timer >= spawnRate)
{
SpawnPipe();
timer = 0;
}
}
void SpawnPipe()
{
float yOffset = UnityEngine.Random.Range(-heightOffset, heightOffset);
GameObject pipe = Instantiate(
pipePrefab,
spawnPoint.position + new Vector3(0, yOffset, 0),
Quaternion.identity
);
pipe.AddComponent<PipeMover>().Init(pipeSpeed, this);
}
public void AddScore()
{
score++;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = score.ToString();
}
public void GameOver()
{
isDead = true;
gameOverPanel.SetActive(true);
Time.timeScale = 0f;
}
public void Restart()
{
Time.timeScale = 1f;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void OnCollisionEnter2D(Collision2D collision)
{
GameOver();
}
public void Initialize(Action<ITask> onCompleted)
{
IsCompleted = false;
_onCompleted = onCompleted;
}
public void Complete()
{
if (IsCompleted) return;
IsCompleted = true;
_onCompleted?.Invoke(this);
ExitTask(_onExit);
}
public void ExitTask(Action<ITask> onExit)
{
onExit?.Invoke(this);
}
}
public class PipeMover : MonoBehaviour
{
private float speed;
private FlappyBirdAllInOne game;
public void Init(float moveSpeed, FlappyBirdAllInOne gm)
{
speed = moveSpeed;
game = gm;
}
void Update()
{
transform.position += Vector3.left * speed * Time.deltaTime;
if (transform.position.x < -10f)
{
Destroy(gameObject);
}
}
}
public class ScoreTrigger : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
FlappyBirdAllInOne gm = FindObjectOfType<FlappyBirdAllInOne>();
if (collision.CompareTag("Player") && gm != null)
{
gm.AddScore();
}
}
}
// =====================
// navod pro desing t<>m
// =====================
/*
1. Player:
- Sprite + Rigidbody2D (Gravity ~2-3)
- BoxCollider2D
- PlayerController script
- Tag = Player
2. Pipes:
- Prefab se 2 kolidery (top/bottom)
- Mezera mezi nimi
- PipeMove script
3. Score Zone:
- Trigger collider mezi trubkami
- ScoreZone script
4. Spawner:
- Empty GameObject
- PipeSpawner script
5. UI:
- TextMeshPro pro score
- GameOver panel + restart button
6. Mobile:
- Input.GetMouseButtonDown funguje i na tap
*/

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 19096191e142d154e956c7169cca9a1e

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9bee8d2d15f48de4c86e3b983e5d1ca6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,133 @@
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Collider2D))]
public class DraggableObject : MonoBehaviour
{
[Header("Přetahování")]
public float dragSmoothness = 15f;
[Header("Vizuální zpětná vazba")]
public SpriteRenderer spriteRenderer;
public Color normalColor = Color.white;
public Color dragColor = new Color(1f, 1f, 0.5f);
public float scaleOnDrag = 1.15f;
private Rigidbody2D rb;
private Camera mainCamera;
private bool isDragging = false;
private Vector3 targetPosition;
private Vector3 originalScale;
private bool hasBeenScored = false;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
mainCamera = Camera.main;
originalScale = transform.localScale;
if (spriteRenderer == null)
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Start()
{
rb.gravityScale = 0f;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
targetPosition = transform.position;
}
void Update()
{
HandleInput();
if (isDragging)
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * dragSmoothness);
}
void HandleInput()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
if (touch.phase == TouchPhase.Began) TryStartDrag(worldPos);
else if (touch.phase == TouchPhase.Moved ||
touch.phase == TouchPhase.Stationary) { if (isDragging) targetPosition = worldPos; }
else if (touch.phase == TouchPhase.Ended ||
touch.phase == TouchPhase.Canceled) { if (isDragging) EndDrag(); }
}
// na twest pro myŠ
else
{
Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
if (Input.GetMouseButtonDown(0)) TryStartDrag(worldPos);
else if (Input.GetMouseButton(0) && isDragging) targetPosition = worldPos;
else if (Input.GetMouseButtonUp(0) && isDragging) EndDrag();
}
}
void TryStartDrag(Vector3 worldPos)
{
if (GetComponent<Collider2D>().OverlapPoint(worldPos))
StartDrag(worldPos);
}
void StartDrag(Vector3 worldPos)
{
isDragging = true;
rb.linearVelocity = Vector2.zero;
targetPosition = worldPos;
transform.localScale = originalScale * scaleOnDrag;
if (spriteRenderer != null)
{
spriteRenderer.color = dragColor;
spriteRenderer.sortingOrder = 10;
}
}
void EndDrag()
{
isDragging = false;
transform.localScale = originalScale;
if (spriteRenderer != null)
{
spriteRenderer.color = normalColor;
spriteRenderer.sortingOrder = 0;
}
}
public void OnScored()
{
if (hasBeenScored) return;
hasBeenScored = true;
isDragging = false;
StartCoroutine(SinkIntoHole());
}
System.Collections.IEnumerator SinkIntoHole()
{
float duration = 0.35f;
float elapsed = 0f;
Vector3 startScale = transform.localScale;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float t = elapsed / duration;
transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t);
if (spriteRenderer != null)
spriteRenderer.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t);
yield return null;
}
gameObject.SetActive(false);
LevelManager.Instance?.RegisterItem();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fb5157d7cd78450439c40cd6f5afe6ac

View File

@@ -0,0 +1,105 @@
using UnityEngine;
using System.Collections;
public class Hole : MonoBehaviour
{
[Header("Nastavení")]
[Tooltip("Poloměr zachycení do kolika jednotek od středu se item 'vtáhne'")]
public float catchRadius = 0.6f;
[Tooltip("Síla vtahování itemu k díře")]
public float pullForce = 4f;
[Header("Pohyb díry (volitelné)")]
public bool hasMovement = false;
public float moveSpeed = 2f;
public Vector2 moveRange = new Vector2(1.5f, 0f);
[Header("Vizuál")]
public SpriteRenderer glowRenderer;
private Vector3 startPosition;
private bool isGlowing = false;
void Awake()
{
startPosition = transform.position;
CircleCollider2D col = GetComponent<CircleCollider2D>();
if (col != null)
{
col.isTrigger = true;
col.radius = catchRadius;
}
}
void Update()
{
if (hasMovement)
{
float x = startPosition.x + Mathf.Sin(Time.time * moveSpeed) * moveRange.x;
float y = startPosition.y + Mathf.Cos(Time.time * moveSpeed * 0.7f) * moveRange.y;
transform.position = new Vector3(x, y, transform.position.z);
}
}
void OnTriggerStay2D(Collider2D other)
{
DraggableObject draggable = other.GetComponent<DraggableObject>();
if (draggable == null) return;
float dist = Vector2.Distance(transform.position, other.transform.position);
Rigidbody2D rb = other.GetComponent<Rigidbody2D>();
if (rb != null)
{
Vector2 dir = ((Vector2)transform.position - rb.position).normalized;
rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode2D.Impulse);
}
if (dist < catchRadius * 0.4f)
{
draggable.OnScored();
SetGlow(false);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<DraggableObject>() != null) SetGlow(true);
}
void OnTriggerExit2D(Collider2D other)
{
if (other.GetComponent<DraggableObject>() != null) SetGlow(false);
}
void SetGlow(bool active)
{
isGlowing = active;
if (glowRenderer == null) return;
glowRenderer.enabled = active;
if (active) StartCoroutine(PulseGlow());
}
IEnumerator PulseGlow()
{
while (isGlowing && glowRenderer != null)
{
float t = Mathf.PingPong(Time.time * 3f, 1f);
Color c = glowRenderer.color;
glowRenderer.color = new Color(c.r, c.g, c.b, Mathf.Lerp(0.3f, 0.9f, t));
yield return null;
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, catchRadius);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, catchRadius * 0.4f);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ca7423fcca5f83249a2574cd84b7f806

View File

@@ -0,0 +1,41 @@
using UnityEngine;
using UnityEngine.Events;
public class LevelManager : MonoBehaviour
{
public static LevelManager Instance;
[Header("Nastavení levelu")]
[Tooltip("Kolik itemů musí hráč trefit pro splnění levelu")]
public int itemsToScore = 3;
[Header("Event vyvolá se po trefení všech itemů")]
public UnityEvent OnAllItemsScored;
private int scoredCount = 0;
void Awake()
{
if (Instance == null) Instance = this;
else Destroy(gameObject);
}
public void RegisterItem()
{
scoredCount++;
Debug.Log($"Trefeno: {scoredCount} / {itemsToScore}");
if (scoredCount >= itemsToScore)
{
OnAllItemsScored?.Invoke();
}
}
public void ResetCounter()
{
scoredCount = 0;
}
public int GetScoredCount() => scoredCount;
public int GetTotalCount() => itemsToScore;
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a819c02c3679b5a449b41052d2e6b3c9

View File

@@ -0,0 +1,113 @@
using UnityEngine;
using System.Collections.Generic;
public class ObjectSpawner : MonoBehaviour
{
public static ObjectSpawner Instance;
[Header("Prefaby")]
public GameObject[] objectPrefabs;
public GameObject holePrefab;
[Header("Počty")]
[Tooltip("Kolik předmětů spawnovat")]
public int objectCount = 3;
[Tooltip("Kolik děr spawnovat")]
public int holeCount = 1;
[Header("Pohyb děr")]
public bool holesMove = false;
public float holeMoveSpeed = 2f;
[Header("Spawn hranice (odpovídají kameře)")]
public float minX = -3.5f;
public float maxX = 3.5f;
public float minY = -5f;
public float maxY = 4f;
[Header("Rodiče pro přehlednost (volitelné)")]
public Transform objectParent;
public Transform holeParent;
private List<GameObject> spawnedObjects = new List<GameObject>();
private List<GameObject> spawnedHoles = new List<GameObject>();
void Awake()
{
if (Instance == null) Instance = this;
else Destroy(gameObject);
}
void Start()
{
Spawn();
}
public void Spawn()
{
Clear();
// LevelManager na aktuální počet
if (LevelManager.Instance != null)
{
LevelManager.Instance.itemsToScore = objectCount;
LevelManager.Instance.ResetCounter();
}
SpawnHoles();
SpawnObjects();
}
void SpawnHoles()
{
for (int i = 0; i < holeCount; i++)
{
Vector2 pos = RandomPos(1f);
GameObject hole = Instantiate(holePrefab, pos, Quaternion.identity, holeParent);
Hole h = hole.GetComponent<Hole>();
if (h != null && holesMove)
{
h.hasMovement = true;
h.moveSpeed = holeMoveSpeed;
h.moveRange = new Vector2(Random.Range(0.8f, 1.8f), 0f);
}
spawnedHoles.Add(hole);
}
}
void SpawnObjects()
{
for (int i = 0; i < objectCount; i++)
{
GameObject prefab = objectPrefabs[Random.Range(0, objectPrefabs.Length)];
Vector2 pos = RandomPos(0.5f);
GameObject obj = Instantiate(prefab, pos, Quaternion.identity, objectParent);
// Náhodná barva
SpriteRenderer sr = obj.GetComponent<SpriteRenderer>();
if (sr != null)
sr.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f);
spawnedObjects.Add(obj);
}
}
public void Clear()
{
foreach (var o in spawnedObjects) if (o != null) Destroy(o);
foreach (var h in spawnedHoles) if (h != null) Destroy(h);
spawnedObjects.Clear();
spawnedHoles.Clear();
}
Vector2 RandomPos(float margin) =>
new Vector2(
Random.Range(minX + margin, maxX - margin),
Random.Range(minY + margin, maxY - margin)
);
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 071f79f81861c2741a92d8b044457d94

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 25326dbbba644974d81eaf9bddc8f76b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,98 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class DraggableKey : MonoBehaviour,
IBeginDragHandler, IDragHandler, IEndDragHandler, ITask
{
[Header("Key Settings")]
public string keyID;
public string correctSlotID;
public string previousSceneName;
[Header("Visual")]
public Color wrongAttemptColor = Color.red;
public float blinkDuration = 0.2f;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
private Vector2 startPosition;
private bool isOverCorrectSlot = false;
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 Action<ITask> _onCompleted;
private Action<ITask> _onExit;
public void Initialize(Action<ITask> onCompleted)
{
IsCompleted = false;
_onCompleted = onCompleted;
}
public void Complete()
{
if (IsCompleted) return;
IsCompleted = true;
_onCompleted?.Invoke(this);
ExitTask(_onExit);
}
public void ExitTask(Action<ITask> onExit)
{
onExit?.Invoke(this);
}
// ===== UNITY =====
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
startPosition = rectTransform.anchoredPosition;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
rectTransform.anchoredPosition += eventData.delta;
}
public void OnEndDrag(PointerEventData eventData)
{
canvasGroup.blocksRaycasts = true;
if (isOverCorrectSlot)
{
Complete();
}
}
public void SetSlotMatch(bool value)
{
isOverCorrectSlot = value;
}
void ResetPosition()
{
rectTransform.anchoredPosition = startPosition;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 21dc5fa96a2ceec428d7b0332e55cbe5

View File

@@ -0,0 +1,28 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class KeySlot : MonoBehaviour, IDropHandler
{
public string correctKeyID;
public void OnDrop(PointerEventData eventData)
{
DraggableKey key = eventData.pointerDrag.GetComponent<DraggableKey>();
if (key != null)
{
if (key.keyID == correctKeyID)
{
key.transform.position = transform.position;
key.enabled = false;
KeyminigameManager.Instance.CheckWin();
}
else
{
KeyminigameManager.Instance.Fail();
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4f5eef97f16bb6b47ba88f96f9d051e9

View File

@@ -0,0 +1,30 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class KeyminigameManager : MonoBehaviour
{
public static KeyminigameManager Instance;
private int correctCount = 0;
public int totalKeys = 3;
private void Awake()
{
Instance = this;
}
public void CheckWin()
{
correctCount++;
if (correctCount >= totalKeys)
{
Debug.Log("WIN");
}
}
public void Fail()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0ef083a6749a8ce459b724dafa1eb08f

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 488d6eb84e65aa94b8b3c77dcb2e21a3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,278 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CableMiniGame : MonoBehaviour, ITask
{
[System.Serializable]
public class Cable
{
public string colorName;
public Button sourceButton;
public Button targetButton;
[HideInInspector] public bool connected;
// Line UI
[HideInInspector] public GameObject lineObject;
[HideInInspector] public RectTransform lineRect;
[HideInInspector] public Image lineImage;
}
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 Action<ITask> _onCompleted;
private Action<ITask> _onExit;
[Header("MiniGame Settings")]
public Cable[] cables;
public Canvas canvas; // Assign your Canvas here in the inspector
public string previousSceneName;
public Color wrongAttemptColor = Color.white;
public float blinkDuration = 0.2f;
public float cableWidth = 5f;
private string selectedColor = null;
// ------------------------------
// Initialization / ITask Methods
// ------------------------------
public void Initialize(Action<ITask> onCompleted)
{
Debug.Log("[Init] Initializing mini game...");
IsCompleted = false;
_onCompleted = onCompleted;
foreach (var cable in cables)
{
cable.connected = false;
}
PrintAllCableStates("After Initialization");
}
public void Complete()
{
if (IsCompleted) return;
IsCompleted = true;
Debug.Log("[Complete] Task completed successfully!");
_onCompleted?.Invoke(this);
ExitTask(_onExit);
}
public void ExitTask(Action<ITask> onExit)
{
Debug.Log("[ExitTask] Exiting task...");
onExit?.Invoke(this);
}
// ------------------------------
// Unity Lifecycle
// ------------------------------
void Start()
{
if (canvas == null)
canvas = FindObjectOfType<Canvas>();
if (canvas == null)
Debug.LogError("[CableMiniGame] No Canvas found in scene!");
foreach (var cable in cables)
{
Cable localCable = cable;
cable.sourceButton.onClick.AddListener(() => OnSourceClicked(localCable));
cable.targetButton.onClick.AddListener(() => OnTargetClicked(localCable));
}
PrintAllCableStates("At Start");
}
// ------------------------------
// Button Handlers
// ------------------------------
void OnSourceClicked(Cable cable)
{
Debug.Log($"[SourceClick] Clicked source of cable '{cable.colorName}'");
if (cable.connected)
{
Debug.Log($"[SourceClick] Cable '{cable.colorName}' is already connected, ignoring.");
return;
}
selectedColor = cable.colorName;
Debug.Log($"[SourceClick] Selected color set to '{selectedColor}'");
PrintAllCableStates("After Source Click");
}
void OnTargetClicked(Cable cable)
{
Debug.Log($"[TargetClick] Clicked target of cable '{cable.colorName}'");
if (selectedColor == null)
{
Debug.Log("[TargetClick] No color selected, ignoring click.");
return;
}
if (cable.connected)
{
Debug.Log($"[TargetClick] Cable '{cable.colorName}' already connected, ignoring click.");
return;
}
if (selectedColor == cable.colorName)
{
cable.connected = true;
Debug.Log($"[TargetClick] Correct connection! '{selectedColor}' connected successfully.");
CreateLineUI(cable);
DrawLine(cable);
PrintAllCableStates("After Correct Connection");
CheckCompletion();
}
else
{
Debug.Log($"[TargetClick] Wrong connection attempt! Selected: '{selectedColor}', Target: '{cable.colorName}'");
PrintAllCableStates("Before BlinkAndExit");
StartCoroutine(BlinkAndExit(cable));
}
selectedColor = null;
Debug.Log("[TargetClick] Reset selected color to null");
}
// ------------------------------
// Cable Rendering
// ------------------------------
private void CreateLineUI(Cable cable)
{
if (cable.lineObject != null) return;
GameObject lineGO = new GameObject($"Line_{cable.colorName}");
lineGO.transform.SetParent(canvas.transform, false);
lineGO.transform.SetAsLastSibling();
Image img = lineGO.AddComponent<Image>();
img.color = Color.white;
img.raycastTarget = false;
RectTransform rt = lineGO.GetComponent<RectTransform>();
rt.pivot = new Vector2(0, 0.5f);
rt.localScale = Vector3.one;
rt.sizeDelta = new Vector2(0, cableWidth);
rt.localPosition = Vector3.zero;
cable.lineObject = lineGO;
cable.lineRect = rt;
cable.lineImage = img;
lineGO.SetActive(false);
}
private void DrawLine(Cable cable)
{
if (!cable.connected || cable.lineRect == null) return;
RectTransform canvasRect = canvas.transform as RectTransform;
Vector2 startPos, endPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(
canvasRect,
cable.sourceButton.transform.position,
canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera,
out startPos
);
RectTransformUtility.ScreenPointToLocalPointInRectangle(
canvasRect,
cable.targetButton.transform.position,
canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera,
out endPos
);
Vector2 diff = endPos - startPos;
float distance = diff.magnitude;
cable.lineRect.sizeDelta = new Vector2(distance, cableWidth);
cable.lineRect.anchoredPosition = startPos;
cable.lineRect.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg);
cable.lineImage.color = GetColorFromName(cable.colorName);
cable.lineObject.SetActive(true);
}
// ------------------------------
// Completion Check
// ------------------------------
void CheckCompletion()
{
foreach (var cable in cables)
{
if (!cable.connected)
{
Debug.Log($"[CheckCompletion] Cable '{cable.colorName}' not yet connected.");
return;
}
}
Debug.Log("[CheckCompletion] All cables connected!");
Complete();
}
// ------------------------------
// Helpers
// ------------------------------
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;
default: return Color.white;
}
}
IEnumerator BlinkAndExit(Cable cable)
{
if (cable.lineImage == null) CreateLineUI(cable);
Debug.Log("[BlinkAndExit] Wrong attempt, blinking...");
Color original = cable.lineImage.color;
cable.lineImage.color = wrongAttemptColor;
yield return new WaitForSeconds(blinkDuration);
cable.lineImage.color = original;
Debug.Log("[BlinkAndExit] Restored original color, exiting task.");
ExitTask(_onExit);
if (!string.IsNullOrEmpty(previousSceneName))
SceneManager.LoadScene(previousSceneName);
}
void PrintAllCableStates(string context)
{
string states = $"[CableStates] {context} => ";
foreach (var cable in cables)
{
states += $"'{cable.colorName}': {(cable.connected ? "Connected" : "Not Connected")}, ";
}
Debug.Log(states.TrimEnd(' ', ','));
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d3fa58532362d2547ab6cd0ab17d7bde

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1df04c245c361e941955db9527a21afe
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: