using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MeadowGames.UINodeConnect4.GraphicRenderer { [System.Serializable] public class Line { [System.Serializable] public class LineCap { public bool active = false; public Shape.Type shape = Shape.Type.Diamond; public float size = 5; public Color color = new Color(1, 0.81f, 0.3f); public float angleOffset = 0; } public LineCap capStart; public LineCap capEnd; public string ID = "line"; public float startWidth = 3; public float endWidth = 3; public float dashDistance = 5; public Color color = new Color(0.98f, 0.94f, 0.84f); [HideInInspector] public List points = new List(); public LineStyle.Type lineStyle = LineStyle.Type.Solid; public Line() { points = new List(); } public void Draw(UICLineRenderer lineRenderer) { LineStyle.DrawStyle(lineStyle, lineRenderer, this); animation.DrawLineAnimation(lineRenderer, this); } /// /// Replace all points by the newPoints /// /// public void SetPoints(Vector2[] newPoints) { if (newPoints != null) { points.Clear(); points.AddRange(newPoints); } length = GetLength(); } public float length; public LineAnimation animation = new LineAnimation(); float _deg2Rad90 = Mathf.Deg2Rad * 90f; public float GetLength() { float l = 0; for (int i = 1; i < points.Count; i++) { l += Vector2.Distance(points[i - 1], points[i]); } return l; } System.Tuple GetPreviousPointAndLength(float pos01) { System.Tuple t = new System.Tuple(0, 0); float l = 0; float prevL = 0; for (int i = 1; i < points.Count; i++) { l += Vector2.Distance(points[i - 1], points[i]); if (l >= pos01) { t = new System.Tuple(i - 1, prevL); break; } prevL = l; } return t; } /// /// returns point in the line based on percent from 0 to 1 /// /// percent from 0 to 1 /// (Vector2) point and (float) angle at this percent public System.Tuple LerpLine(float pos01) { if (points.Count > 1) { System.Tuple t = GetPreviousPointAndLength(pos01 * length); Vector2 p0 = points[t.Item1]; Vector2 p1 = points[t.Item1 + 1]; float ppDistance = Vector2.Distance(p0, p1); Vector2 position = Vector2.Lerp(p0, p1, ((pos01 * length) - t.Item2) / ppDistance); float angle = Mathf.Atan2(p0.y - p1.y, p0.x - p1.x) + _deg2Rad90; return new System.Tuple(position, angle); } return new System.Tuple(Vector2.zero, 0); } /// /// returns point in the line based on length /// /// target legth /// (Vector2) point and (float) angle at this length public System.Tuple LerpLineLength(float posLength) { if (points.Count > 1) { System.Tuple t = GetPreviousPointAndLength(posLength); Vector2 p0 = points[t.Item1]; Vector2 p1 = points[t.Item1 + 1]; Vector2 position = Vector2.MoveTowards(p0, p1, posLength - t.Item2); float angle = Mathf.Atan2(p0.y - p1.y, p0.x - p1.x) + _deg2Rad90; return new System.Tuple(position, angle); } return new System.Tuple(Vector2.zero, 0); } public Line Clone() { return UICUtility.Clone(this); } } }