using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace DongWuYiXue.DaoNiaoShu { public class PointLineManager : MonoBehaviour { List points = new(); List lines = new(); public Line m_LinePfb; public RectTransform m_LineCon; public Color color; private List selectPoints = new(); private void Start() { points = GetComponentsInChildren().ToList(); for (int i = 0; i < points.Count; i++) { points[i].Init(this); } } /// /// 画线 /// public void DrawLine(Point p) { selectPoints.Add(p); if (selectPoints.Count == 2 && selectPoints[0].group != selectPoints[1].group)//凑齐两点,且两点不同区域 { if (TwoPointsAlreadyConnected(selectPoints))//两点之间是否已经连线 { UnselectPoints(); return; } if (OnePointsAlreadyConnected(selectPoints[0]) && OnePointsAlreadyConnected(selectPoints[1]))//两点是否都与第三点有连线 { if (selectPoints[0].multip && selectPoints[1].multip) { CreateLine(selectPoints[0], selectPoints[1]); } UnselectPoints(); return; } if (OnePointsAlreadyConnected(selectPoints[0]))//点1是否都与第三点有连线 { if (selectPoints[0].multip) { CreateLine(selectPoints[0], selectPoints[1]); } UnselectPoints(); return; } if (OnePointsAlreadyConnected(selectPoints[1]))//点2是否都与第三点有连线 { if (selectPoints[1].multip) { CreateLine(selectPoints[0], selectPoints[1]); } UnselectPoints(); return; } CreateLine(selectPoints[0], selectPoints[1]); UnselectPoints(); return; } if (selectPoints.Count == 2 && selectPoints[0].group == selectPoints[1].group)//凑齐两点,且两点同一区域 { UnselectPoints(); p.Select(); selectPoints.Add(p); return; } } /// /// 两点之间是否已经有连线 /// public bool TwoPointsAlreadyConnected(List p) { for (int i = 0; i < lines.Count; i++) { if (lines[i].points.SequenceEqual(p)) { return true; } } return false; } /// /// 一点是否已经连线 /// public bool OnePointsAlreadyConnected(Point p) { for (int i = 0; i < lines.Count; i++) { if (lines[i].points.Contains(p)) { return true; } } return false; } /// /// 取消所有点击 /// public void UnselectPoints() { for (int i = 0; i < selectPoints.Count; i++) { selectPoints[i].UnSelect(); } selectPoints.Clear(); } /// /// 创建⼀条两点之间的线 /// private void CreateLine(Point p1, Point p2) { //实例化需要显⽰的线段图⽚pfb Line line = DrawLineTool.DrawLine(p1.transform.position, p2.transform.position, color, m_LineCon).GetComponent(); line.AddPoint(p1); line.AddPoint(p2); line.gameObject.SetActive(true); lines.Add(line); } /// /// 清除线 /// public void ClearLine() { for (int i = 0; i < lines.Count; i++) { Destroy(lines[i].gameObject); } lines.Clear(); } /// /// 判断对错 /// public bool JudgeLine() { bool trueOrFalse = true; for (int i = 0; i < lines.Count; i++) { if (lines[i].Judge()) { lines[i].Correct(); } else { lines[i].Error(); trueOrFalse = false; } } for (int i = 0; i < points.Count; i++) { Point p = points[i]; if (p.answers != null && !OnePointsAlreadyConnected(p)) { trueOrFalse = false; } } return trueOrFalse; } /// /// 禁用按钮 /// public void ButtonForbidden() { for (int i = 0; i < points.Count; i++) { points[i].btn.interactable = false; } } /// /// 启用按钮 /// public void ButtonOnEnable() { for (int i = 0; i < points.Count; i++) { points[i].btn.interactable = true; } } } }