CultivationOfBrewing-2/Assets/Scripts/CXX/Devices//连线/Line_item.cs

178 lines
5.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/// <summary>
/// 线
/// </summary>
public class Line_item : MonoBehaviour
{
LineRenderer lineRenderer;
Line_group line_Group;
/// <summary>
/// linerender
/// </summary>
List<Vector3> points=new List<Vector3>() {Vector3.zero};
/// <summary>
/// 端点
/// </summary>
public Line_end[] line_Ends = new Line_end[2];
/// <summary>
/// 中间节点
/// </summary>
private List<Line_mid> mids= new List<Line_mid>();
[HideInInspector]
public Material mat;
/// <summary>
/// 初始化线
/// </summary>
/// <param name="mat">颜色</param>
/// <param name="line_Group"></param>
/// <param name="endpoint">起点</param>
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>();
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);
}
/// <summary>
/// 添加中间点
/// </summary>
/// <param name="line_Mid"></param>
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为空");
}
}
/// <summary>
/// 画终点
/// </summary>
/// <param name="line_End"></param>
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<MeshCollider>().sharedMesh = lineMesh;
lineRenderer.gameObject.AddComponent<MeshFilter>().mesh = lineMesh;
lineRenderer.gameObject.AddComponent<MeshRenderer>().material=mat;
lineRenderer.enabled = false;
transform.parent = line_Group.transform;
transform.GetComponent<HighlightPlus.HighlightEffect>().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);
}
}
}
/// <summary>
/// 刷新到手里的线
/// </summary>
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());
}
/// <summary>
/// 取消到手里的线
/// </summary>
public void SetNoHandPoint()
{
points[points.Count - 1] = points[points.Count - 2];
lineRenderer.positionCount = points.Count;
lineRenderer.SetPositions(points.ToArray());
}
/// <summary>
/// 判断线是否正确
/// </summary>
/// <param name="color"></param>
/// <param name="pointTriggerName1"></param>
/// <param name="pointTriggerName2"></param>
/// <returns></returns>
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;
}
}