NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/View/FightLine.cs

103 lines
2.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;
namespace Gameplay.LegionBattle.View
{
public class FightLine
{
public int side;
public List<FightLineCell> cells = new List<FightLineCell>();
private FightLineCell _workCell;
public FightLine()
{
}
public void AddCell(FightLineCell cell)
{
cells.Add(cell);
_workCell = cell;
}
public FightLineCell SearchNext(List<FightLineCell> remainCellList, out int outIndex)
{
var minDistance = float.MaxValue;
FightLineCell selectCell = null;
int selectIndex = -1;
var neighbors = _workCell.fullNeighbors;
for (int i = 0; i < neighbors.Count; i++)
{
var nCell = neighbors[i];
if (nCell.standValue != _workCell.standValue)
{
continue;
}
var indexOfCell = remainCellList.IndexOf(nCell);
if (indexOfCell < 0)
{
continue;
}
var nowDistance = Vector3.Distance(_workCell.boardPos, nCell.boardPos);
if (nowDistance < minDistance)
{
minDistance = nowDistance;
selectCell = nCell;
selectIndex = indexOfCell;
}
}
outIndex = selectIndex;
return selectCell;
}
public void Reverse()
{
// 反转
cells.Reverse();
}
public void AddLine(FightLine nLine)
{
nLine.cells.AddRange(nLine.cells);
}
public void Clear()
{
cells.Clear();
}
public void UpdateSpline(Spline spLine)
{
spLine.Closed = true;
spLine.Clear();
var workCell = cells[0];
spLine.Add(workCell.boardPos, TangentMode.Linear);
for (int i = 1; i < cells.Count; i++)
{
var checkCell = cells[i];
var nextIndex = i + 1;
if (nextIndex >= cells.Count)
{
nextIndex = 0;
}
var nextCell = cells[nextIndex];
var workToCheck = checkCell.boardPos - workCell.boardPos;
var checkToNext = nextCell.boardPos - checkCell.boardPos;
var angle = Vector3.Angle(workToCheck, checkToNext);
if (angle > 5)
{
// 是关键点
spLine.Add(checkCell.boardPos, TangentMode.Linear);
}
workCell = checkCell;
}
}
}
}