using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
///
/// 线
///
public class Line_item : MonoBehaviour
{
LineRenderer lineRenderer;
Line_group line_Group;
///
/// linerender
///
List points=new List() {Vector3.zero};
///
/// 端点
///
public Line_end[] line_Ends = new Line_end[2];
///
/// 中间节点
///
private List mids= new List();
[HideInInspector]
public Material mat;
///
/// 初始化线
///
/// 颜色
///
/// 起点
public void Init(Material mat, Line_group line_Group,Line_end endpoint)
{
this.mat = mat;
this.line_Group = line_Group;
line_Group.line_Items.Add(this);
lineRenderer = GetComponent();
lineRenderer.material = mat;
lineRenderer.startWidth = 0.0025f;
lineRenderer.endWidth = 0.0025f;
points.Insert(points.Count - 1, endpoint.transform.position);
line_Ends[0] = endpoint;
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
Debug.Log("开始连线");
line_Group.drawEndPointAction?.Invoke(this);
}
///
/// 添加中间点
///
///
public void AddPoint(Line_mid line_Mid)
{
if (line_Mid != null)
{
points.Insert(points.Count - 1, line_Mid.transform.position);
mids.Add(line_Mid);
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
Debug.Log("连到中点");
}
else
{
Debug.LogError("mid为空");
}
}
///
/// 画终点
///
///
public void AddPoint(Line_end line_End)
{
if (line_End != null)
{
points.Insert(points.Count - 1, line_End.transform.position);
points.RemoveAt(points.Count - 1);
line_Ends[1] = line_End;
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
//生成mesh
var lineMesh = new Mesh();
lineRenderer.BakeMesh(lineMesh,true);
lineRenderer.gameObject.AddComponent().sharedMesh = lineMesh;
lineRenderer.gameObject.AddComponent().mesh = lineMesh;
lineRenderer.gameObject.AddComponent().material=mat;
lineRenderer.enabled = false;
transform.parent = line_Group.transform;
transform.GetComponent().Refresh();
line_Group.currentLine_Item = null;
Debug.Log("结束连线");
line_Group.drawEndPointAction?.Invoke(this);
ScoreManager.instance?.Check("线_" + line_Ends[0].triggerName + "_" + line_Ends[1].triggerName,true);
}
else
{
Debug.LogError("end为空");
}
}
public void MyOnMouseDown()
{
if (GameManager.RunModelMgr?.ModeType != E_ModeType.Study)
{
if (line_Group.currentLine_Item == null || line_Group.currentLine_Item.line_Ends[1] != null)
{
line_Group.ChoseLine(this);
}
}
}
///
/// 刷新到手里的线
///
public void SetHandPoint()
{
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
points[points.Count - 1]= ray.origin + ray.direction*0.5f;
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
}
///
/// 取消到手里的线
///
public void SetNoHandPoint()
{
points[points.Count - 1] = points[points.Count - 2];
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
}
///
/// 判断线是否正确
///
///
///
///
///
public bool Check(string color,string pointTriggerName1,string pointTriggerName2)
{
//判断颜色
string tmp = "";
if (color == "黑")
tmp = "black";
else if (color == "红")
tmp = "red";
else if (color == "绿")
tmp = "green";
else if (color == "黄")
tmp = "yellow";
if(mat.name.Contains(tmp))
{
//判断端点
if (line_Ends[0]!=null && line_Ends[1]!=null)
{
if(line_Ends[0].triggerName== pointTriggerName1 && line_Ends[1].triggerName== pointTriggerName2)
{
return true;
}
else if(line_Ends[0].triggerName == pointTriggerName2 && line_Ends[1].triggerName == pointTriggerName1)
{
return true;
}
}
}
return false;
}
}