NewN_UAVPlane/Assets/Zion/Scripts/Adam/FiniteStateMachines/LineDrawingState.cs

130 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
//============================================================
//支持中文文件使用UTF-8编码
//@author JiphuTzu
//@create 20220910
//@company Umawerse
//
//@description:
//============================================================
namespace Umawerse.FiniteStateMachines
{
public class LineDrawingState : DrawingState
{
private LineRenderer _line;
public LineDrawingState(ToolsBootstrap root) : base(root, StateName.line, 2, 99)
{
}
protected override void OnTap(Vector3 pos)
{
AddPoint(pos);
}
protected override void OnDoubleTap(Vector3 pos)
{
AddPoint(pos);
}
protected override void UpdatePreview(Vector3 pos)
{
var vs = new Vector3[vertices.Count + 1];
//var h = vertices[0].y > pos.y ? vertices[0].y : pos.y;
for (var i = 0; i < vertices.Count; i++)
{
vs[i] = vertices[i];//new Vector3(vertices[i].x, h, vertices[i].z);
//target.transform.GetChild(i).position = vs[i];
}
vs[vertices.Count] = pos;//new Vector3(pos.x, h, pos.z);
_line.positionCount = vs.Length;
_line.SetPositions(vs);
_line.material.SetFloat("_Cnt", GetLength(vs) / 3);
}
public override void Init()
{
base.Init();
_line = target.AddComponent<LineRenderer>();
_line.sortingOrder = -1;
_line.startWidth = 3f;
_line.endWidth = 3f;
_line.numCornerVertices = 3;
_line.material = root.solidLineMaterial;
_line.material.color = Color.green;
}
public override void Complete()
{
_line.positionCount = vertices.Count;
var vs = vertices.ToArray();
_line.SetPositions(vs);
_line.material.SetFloat("_Cnt", GetLength(vs) / 3);
var lineMesh = new Mesh();
_line.BakeMesh(lineMesh, true);
target.AddComponent<MeshCollider>().sharedMesh = lineMesh;
//
}
public override void AddPoint(Vector3 position, bool showImmediately = true)
{
Vector3 tempY;
if (root.isDistance)
{
tempY = new Vector3(position.x, 580f, position.z);
}
else
{
tempY = position + Vector3.up * 50f;
}
vertices.Add(tempY);
//if (showImmediately)
//{
// var h = vertices[0].y > position.y ? vertices[0].y : position.y;
// for (int i = 0; i < vertices.Count; i++)
// {
// var v = vertices[i];
// v.y = h;
// vertices[i] = v;
// if (i < target.transform.childCount)
// target.transform.GetChild(i).localPosition = v;
// }
//}
var linePoint = Object.Instantiate(root.linePointPrefab, target.transform);
linePoint.transform.localPosition = vertices[vertices.Count - 1];
_line.positionCount = vertices.Count;
var vs = vertices.ToArray();
_line.SetPositions(vs);
}
private void CorrectDirection()
{
var index = vertices.Count - 1;
var last = index - 1;
var dir = vertices[index] - vertices[last];
dir.y = 0;
dir.Normalize();
target.transform.GetChild(index).GetChild(1).forward = dir;
target.transform.GetChild(last).GetChild(1).forward = dir;
}
private float GetLength(Vector3[] vs)
{
var length = 0f;
for (var i = 1; i < vs.Length; i++)
{
length += Vector3.Distance(vs[i], vs[i - 1]);
}
return length;
}
}
}