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

220 lines
6.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace Gameplay.LegionBattle.View
{
public class FightLineCell
{
public int standValue = 0;
public Vector2Int pos;
private GameObject _viewObj;
private Material _mat;
public bool isMainCell;
public bool isVisited = false;
public List<FightLineCell> neighbors = new List<FightLineCell>();
public int halfNeighborCount = 0;
public List<FightLineCell> fullNeighbors = new List<FightLineCell>();
public FightLineCell upCell;
public FightLineCell downCell;
public FightLineCell leftCell;
public FightLineCell rightCell;
public Vector3 boardPos;
public int iteration = 0;
public FightLineCell(int x,
int y)
{
pos = new Vector2Int(x, y);
}
public void BuildNeighbors(FightLineCell[,] cellMap)
{
var x = pos.x;
var y = pos.y;
if (x > 0)
{
leftCell = cellMap[x - 1, y];
neighbors.Add(leftCell);
}
if (x < cellMap.GetLength(0) - 1)
{
rightCell = cellMap[x + 1, y];
neighbors.Add(rightCell);
}
if (y > 0)
{
downCell = cellMap[x, y - 1];
neighbors.Add(downCell);
}
if (y < cellMap.GetLength(1) - 1)
{
upCell = cellMap[x, y + 1];
neighbors.Add(upCell);
}
fullNeighbors.AddRange(neighbors);
// 斜角也算
if (x > 0 && y > 0)
{
fullNeighbors.Add(cellMap[x - 1, y - 1]);
}
if (x > 0 && y < cellMap.GetLength(1) - 1)
{
fullNeighbors.Add(cellMap[x - 1, y + 1]);
}
if (x < cellMap.GetLength(0) - 1 && y > 0)
{
fullNeighbors.Add(cellMap[x + 1, y - 1]);
}
if (x < cellMap.GetLength(0) - 1 && y < cellMap.GetLength(1) - 1)
{
fullNeighbors.Add(cellMap[x + 1, y + 1]);
}
halfNeighborCount = Mathf.FloorToInt(neighbors.Count / 2f);
}
public void BindViewObj(GameObject viewObj)
{
_viewObj = viewObj;
_mat = viewObj.transform.GetChild(0).GetComponent<Renderer>().material;
}
private static Color _colorLeft = new Color(1f, 0f, 0f, 0.2f);
private static Color _colorRight = new Color(0f, 0f, 1f, 0.2f);
private static Color _colorDefault = new Color(1f, 1f, 1f, 0.2f);
public void UpdateColor()
{
if (standValue == 0)
{
_mat.color = _colorDefault;
}
_mat.color = standValue < 0 ? _colorLeft : _colorRight;
}
public bool CheckIsAlone()
{
if (standValue < 0)
{
foreach (var cell in fullNeighbors)
{
if (cell.standValue >= 0)
{
return false;
}
}
return true;
}
else
{
foreach (var cell in fullNeighbors)
{
if (cell.standValue < 0)
{
return false;
}
}
return true;
}
}
public bool CheckIsNotBoarder()
{
if (((upCell!= null && standValue == upCell.standValue)
|| downCell != null && standValue == downCell.standValue)
&& (leftCell != null && standValue == leftCell.standValue
|| rightCell != null && standValue == rightCell.standValue))
{
return true;
}
return false;
}
public bool CheckIsTuDian()
{
var noSameCount = 0;
foreach (var cell in neighbors)
{
if (cell.standValue != standValue)
{
noSameCount++;
}
}
if (noSameCount > halfNeighborCount)
{
return true;
}
return false;
}
public void CalcBoardPos()
{
boardPos.x = pos.x;
boardPos.y = pos.y;
// if (upCell == null || upCell.standValue != standValue)
// {
// boardPos.y += 0.5f;
// }
// if (downCell == null || downCell.standValue != standValue)
// {
// boardPos.y -= 0.5f;
// }
// if (leftCell == null || leftCell.standValue != standValue)
// {
// boardPos.x -= 0.5f;
// }
// if (rightCell == null || rightCell.standValue != standValue)
// {
// boardPos.x += 0.5f;
// }
}
public bool CheckIsCenter()
{
if (neighbors.Count < 4) return false;
// 自己和所有相邻格子都是同一方
if (standValue < 0)
{
foreach (var cell in neighbors)
{
if (cell.standValue >= 0)
{
return false;
}
}
return true;
}
else
{
foreach (var cell in neighbors)
{
if (cell.standValue < 0)
{
return false;
}
}
return true;
}
}
public bool CheckIsConnect(FightLineCell anotherCell)
{
if (neighbors.Contains(anotherCell))
{
return true;
}
return false;
}
}
}