using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting.Antlr3.Runtime; public class UIPathEffect : MonoBehaviour { public static UIPathEffect instance; // Pour accès statique [Header("Setup")] //public RectTransform canvasRect; static RectTransform lightPointUI; static List pathPoints = new(); static float moveSpeed = 800f; public GameObject PathUI; static float totalLength; static List segmentLengths = new(); // ✅ Suppression des valeurs par défaut static bool moveCoroutine; void Awake() { instance = this; // ✅ Initialisation des points du chemin (exemple) InitializePathPoints(); } void Start() { moveCoroutine = true; ComputePathLength(); } // ✅ Méthode pour initialiser les points du chemin static void InitializePathPoints() { if (pathPoints.Count == 0) { // Exemple de points - remplacez par vos vrais points pathPoints.Add(new Vector2(-640, 375)); pathPoints.Add(new Vector2(640, 375)); pathPoints.Add(new Vector2(640, -375)); pathPoints.Add(new Vector2(-640, -375)); pathPoints.Add(new Vector2(-640, 375)); } } static void ComputePathLength() { totalLength = 0f; segmentLengths.Clear(); // ✅ Vérification que nous avons assez de points if (pathPoints.Count < 2) { Debug.LogWarning("Pas assez de points dans le chemin pour calculer la longueur"); return; } for (int i = 0; i < pathPoints.Count - 1; i++) { float segLength = Vector2.Distance(pathPoints[i], pathPoints[i + 1]); segmentLengths.Add(segLength); totalLength += segLength; } Debug.Log($"Chemin calculé: {pathPoints.Count} points, longueur totale: {totalLength}"); } public static void StartMovement() { InitializePathPoints(); moveCoroutine = true; ComputePathLength(); GameObject PathUI = Get.o2("PNL", "PathUI"); PathUI.SetActive(true); lightPointUI =PathUI.transform.Find("LihtgGreen").GetComponent(); // Vérification de sécurité if (pathPoints.Count == 0) { Debug.LogError("Aucun point défini dans le chemin !"); return; } if (lightPointUI == null) { Debug.LogError("lightPointUI n'est pas assigné !"); return; } Get.o2("PNL", "PathUI").SetActive(true); // ✅ Remet le point au début du tracé lightPointUI.anchoredPosition = pathPoints[0]; if (moveCoroutine) StaticCoroutine.Start(MoveLightAlongPathUI()); moveCoroutine = true; } public static void StopMovement() { moveCoroutine = false; Get.o2("PNL", "PathUI").SetActive(false); } // ✅ Méthode publique pour définir les points du chemin public static void SetPathPoints(List newPoints) { pathPoints.Clear(); pathPoints.AddRange(newPoints); // Recalculer la longueur du chemin // if (instance != null) // instance.ComputePathLength(); } // ✅ Méthode publique pour assigner le lightPointUI public static void SetLightPointUI(RectTransform lightPoint) { lightPointUI = lightPoint; } static IEnumerator MoveLightAlongPathUI() { float t = 0f; while (moveCoroutine && pathPoints.Count > 0 && lightPointUI != null) { Vector2 pos = GetPointAtDistance(t); lightPointUI.anchoredPosition = pos; t += moveSpeed * Time.deltaTime; if (t > totalLength) t = 0f; yield return null; } } static Vector2 GetPointAtDistance(float distance) { // ✅ Vérifications de sécurité if (pathPoints.Count == 0) return Vector2.zero; if (pathPoints.Count == 1) return pathPoints[0]; if (segmentLengths.Count == 0) return pathPoints[0]; float traveled = 0f; for (int i = 0; i < segmentLengths.Count; i++) { // ✅ Vérification des indices if (i >= pathPoints.Count - 1) break; if (traveled + segmentLengths[i] >= distance) { // ✅ Protection contre division par zéro if (segmentLengths[i] == 0) return pathPoints[i]; float localT = (distance - traveled) / segmentLengths[i]; localT = Mathf.Clamp01(localT); // ✅ S'assurer que t est entre 0 et 1 return Vector2.Lerp(pathPoints[i], pathPoints[i + 1], localT); } traveled += segmentLengths[i]; } // ✅ Retourne le dernier point si le parcours dépasse la fin return pathPoints[pathPoints.Count - 1]; } }