243 lines
7.3 KiB
C#
243 lines
7.3 KiB
C#
using System.Collections.Generic;
|
|
using Framework;
|
|
using Gameplay.Level;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Unit.Data;
|
|
using PhxhSDK;
|
|
using UnityEngine;
|
|
using Constants = Framework.Constants;
|
|
namespace Gameplay.Area
|
|
{
|
|
public class MoveAreaView
|
|
{
|
|
public GameUnit owner;
|
|
|
|
public List<int> moveCellList = new List<int>();
|
|
|
|
public GameObject rootObj;
|
|
|
|
private bool _needRefresh;
|
|
|
|
private List<GameUnit> _teamUnits = new List<GameUnit>();
|
|
|
|
public void Init()
|
|
{
|
|
rootObj = new GameObject("[MoveArea]");
|
|
}
|
|
|
|
public void Show(GameUnit setOwner)
|
|
{
|
|
owner = setOwner;
|
|
|
|
_UpdateTeamUnits();
|
|
_RefreshMoveArea();
|
|
_needRefresh = false;
|
|
|
|
rootObj.SetActive(true);
|
|
|
|
_RegisterEvent();
|
|
}
|
|
|
|
private void _OnCellIndexChange(GameUnit unit)
|
|
{
|
|
if (unit == owner) return;
|
|
if (unit.commonData.troopId != owner.commonData.troopId) return;
|
|
_needRefresh = true;
|
|
}
|
|
|
|
private void _RegisterEvent()
|
|
{
|
|
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_CELLINDEX_CHANGE, _OnCellIndexChange);
|
|
}
|
|
|
|
private void _UnRegisterEvent()
|
|
{
|
|
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_CELLINDEX_CHANGE, _OnCellIndexChange);
|
|
}
|
|
|
|
private void _UpdateTeamUnits()
|
|
{
|
|
|
|
_teamUnits.Clear();
|
|
var allUnits = GameUnitManager.instance.infightUnits.unitList;
|
|
for (int i = 0; i < allUnits.Count; i++)
|
|
{
|
|
var unit = allUnits[i];
|
|
if (unit == owner)
|
|
{
|
|
continue;
|
|
}
|
|
if (unit.commonData.troopId != owner.commonData.troopId)
|
|
{
|
|
continue;
|
|
}
|
|
// 只要角色
|
|
if (unit.gameunitType != EGameUnitType.Character)
|
|
{
|
|
continue;
|
|
}
|
|
_teamUnits.Add(unit);
|
|
}
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
_UnRegisterEvent();
|
|
owner = null;
|
|
rootObj.SetActive(false);
|
|
|
|
}
|
|
|
|
public void LogicUpdate()
|
|
{
|
|
if (owner == null) return;
|
|
if (_needRefresh)
|
|
{
|
|
_needRefresh = false;
|
|
_RefreshMoveArea();
|
|
}
|
|
}
|
|
|
|
public bool CheckIsInArea(int cellIndex)
|
|
{
|
|
return moveCellList.Contains(cellIndex);
|
|
}
|
|
|
|
private bool _CheckCanPass(int cellIndex)
|
|
{
|
|
if (cellIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (cellIndex == owner.transData.cellIndex)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (!PathFinder.CanBasePass(cellIndex, owner))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// 判断所有队友在不在这个格子上
|
|
// 判断所有队友的targetcell在不在这个格子上
|
|
for (int i = 0; i < _teamUnits.Count; i++)
|
|
{
|
|
var unit = _teamUnits[i];
|
|
if (unit.statusData.isOnVehicle)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (unit.transData.cellIndex == cellIndex)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (unit.controlData.targetCellIndex == cellIndex)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void _RefreshMoveArea()
|
|
{
|
|
moveCellList.Clear();
|
|
|
|
var centerIndex = owner.transData.cellIndex;
|
|
var workList = new List<int>();
|
|
var blockedSet = new HashSet<int>();
|
|
var map = LevelManager.Instance.CurrentLevel.Map;
|
|
|
|
workList.Add(centerIndex);
|
|
while (workList.Count > 0)
|
|
{
|
|
var lastValue = workList[^1];
|
|
workList.RemoveAt(workList.Count - 1);
|
|
|
|
// 判断当前格子是否可通行
|
|
if (!_CheckCanPass(lastValue))
|
|
{
|
|
blockedSet.Add(lastValue);
|
|
// DebugUtil.LogError("blockedSet.Add:" + lastValue);
|
|
continue;
|
|
}
|
|
// 把自己加入到移动列表
|
|
moveCellList.Add(lastValue);
|
|
|
|
// 把周围的格子加入到工作列表
|
|
var aroundList = map.TerrainGridSystem.CellGetNeighboursWithinRange(lastValue, 0, 1, canCrossCheckType: TGS.CanCrossCheckType.IgnoreCanCrossCheckOnAllCells);
|
|
for (int i = 0; i < aroundList.Count; i++)
|
|
{
|
|
var cellIndex = aroundList[i];
|
|
if (moveCellList.Contains(cellIndex))
|
|
{
|
|
continue;
|
|
}
|
|
if (blockedSet.Contains(cellIndex))
|
|
{
|
|
continue;
|
|
}
|
|
if (workList.Contains(cellIndex))
|
|
{
|
|
continue;
|
|
}
|
|
workList.Add(cellIndex);
|
|
}
|
|
}
|
|
|
|
// 刷新显示
|
|
if (moveCellList.Count != rootObj.transform.childCount)
|
|
{
|
|
// 需要匹配个数
|
|
if (moveCellList.Count < rootObj.transform.childCount)
|
|
{
|
|
// 多了, 需要删除
|
|
var needRemoveCount = rootObj.transform.childCount - moveCellList.Count;
|
|
for (int i = 0; i < needRemoveCount; i++)
|
|
{
|
|
var child = rootObj.transform.GetChild(moveCellList.Count + i);
|
|
Object.Destroy(child.gameObject);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 少了, 需要添加
|
|
var needAddCount = moveCellList.Count - rootObj.transform.childCount;
|
|
for (int i = 0; i < needAddCount; i++)
|
|
{
|
|
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(Constants.MOVE_AREA_POINT_PATH);
|
|
var instanceObj = Object.Instantiate(sampleObj, rootObj.transform);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 刷新位置
|
|
for (int i = 0; i < moveCellList.Count; i++)
|
|
{
|
|
var cellIndex = moveCellList[i];
|
|
var worldPos = AreaManager.instance.GetCellPosByIndex(cellIndex);
|
|
var child = rootObj.transform.GetChild(i);
|
|
child.position = worldPos;
|
|
}
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
owner = null;
|
|
moveCellList.Clear();
|
|
|
|
if (rootObj != null)
|
|
{
|
|
Object.Destroy(rootObj);
|
|
rootObj = null;
|
|
}
|
|
}
|
|
}
|
|
}
|