using System.Collections.Generic; using Gameplay.Common; using Gameplay.Unit; using Gameplay.Unit.Data; using Gameplay.Vehicle.Impl; using Gameplay.Vehicle.State; using PhxhSDK; namespace Gameplay.Area { using UnityEngine; using Character; public class RoadLineView { private GameObject _rootObj; private GameObject _rootObjStraight; public GameUnit owner; private LineRenderer _workLineRender; private LineRenderer _lineRender; private LineRenderer _lineRenderStraight; private readonly bool _useSingleLine = false; private GameObject _endNoticeObj; private bool _isAttackMove; private bool _lastIsAttackMove; private bool _showStraightLine; public void Init() { _CreateRootObj(); _CreateRootObjStraight(); } private void _CreateRootObj() { var sampleObj = AssetManager.Instance.GetPreLoadResult(PathDefine.RoadLine.ROAD_LINE); _rootObj = Object.Instantiate(sampleObj, AreaManager.instance.parentGroup.parentRoadLine); _rootObj.transform.position = Vector3.zero; _rootObj.transform.rotation = Quaternion.Euler(90, 0, 0); _lineRender = _rootObj.GetComponent(); _rootObj.SetActive(false); } private void _CreateRootObjStraight() { var sampleObj = AssetManager.Instance.GetPreLoadResult(PathDefine.RoadLine.ROAD_LINE_STRAIGHT); _rootObjStraight = Object.Instantiate(sampleObj, AreaManager.instance.parentGroup.parentRoadLine); _rootObjStraight.transform.position = Vector3.zero; _rootObjStraight.transform.rotation = Quaternion.Euler(90, 0, 0); _lineRenderStraight = _rootObjStraight.GetComponent(); _rootObjStraight.SetActive(false); } public void Dispose() { if (_rootObj != null) { Object.Destroy(_rootObj); _rootObj = null; } if (_rootObjStraight != null) { Object.Destroy(_rootObjStraight); _rootObjStraight = null; } if (_endNoticeObj != null) { Object.Destroy(_endNoticeObj); _endNoticeObj = null; } } public void UpdateLine(GameUnit u) { owner = u; } public void CreateEndNotice(int unitIndex) { unitIndex++; DebugUtil.Log("CreateEndNotice: " + unitIndex); var loadPath = PathDefine.RoadLine.END_NOTICES[unitIndex]; var sampleObj = AssetManager.Instance.GetPreLoadResult(loadPath); _endNoticeObj = Object.Instantiate(sampleObj, AreaManager.instance.parentGroup.parentRoadLine); _endNoticeObj.SetActive(false); } public void HideLine() { owner = null; _SetActive(false); } private void _SetActive(bool active) { if (_rootObj != null) { _rootObj.SetActive(active); } if (_rootObjStraight != null) { _rootObjStraight.SetActive(active); } if (_endNoticeObj != null) { _endNoticeObj.SetActive(active); } } private List _GetPointList() { _showStraightLine = false; List result = null; _isAttackMove = owner.controlData.markEnemy != null; switch (owner.gameunitType) { case EGameUnitType.Character: { var c = owner as Character; if (c == null) { DebugUtil.LogError("Character is null"); return null; } if (!c.statusData.isSelected) { // 没有选中的时候, 不显示详细路径 _showStraightLine = true; return null; } var downFsm = c.downFSM; if (downFsm == null) return null; if (downFsm.currentState == null) return null; if (downFsm.currentState.id != EDownState.Move) { return null; } var state = downFsm.currentState as DownStateMove; if (state == null) { DebugUtil.LogError("DownStateMove is null"); return null; } result = state.CalcRoadLine(); } break; case EGameUnitType.Vehicle: { var v = owner as NormalVehicle; if (v == null) { DebugUtil.LogError("NormalVehicle is null"); return null; } if (!v.statusData.isSelected) { // 没有选中的时候, 不显示详细路径 _showStraightLine = true; return null; } var fsm = v.fsm; if (fsm == null) return null; if (fsm.currentState == null) return null; if (fsm.currentState.id != EVehicleState.Move) { return null; } var state = fsm.currentState as VehicleStateMove; if (state == null) { DebugUtil.LogError("VehicleStateMove is null"); return null; } result = state.CalcRoadLine(); } break; } if (result == null || result.Count == 0) return null; return result; } private int _GetNewIndex() { switch (owner.gameunitType) { case EGameUnitType.Character: { var c = owner as Character; var downFsm = c.downFSM; if (downFsm.currentState.id != EDownState.Move) { return -1; } var state = downFsm.currentState as DownStateMove; if (state == null) { DebugUtil.LogError("DownStateMove is null"); return -1; } return state.GetCurrentIndex(); } case EGameUnitType.Vehicle: { var v = owner as NormalVehicle; var fsm = v.fsm; if (fsm.currentState.id != EVehicleState.Move) { return -1; } var state = fsm.currentState as VehicleStateMove; if (state == null) { DebugUtil.LogError("VehicleStateMove is null"); return -1; } return state.GetCurrentIndex(); } } return -1; } public void LogicUpdate(float dt) { if (owner == null) return; if (owner.statusData.isDead || !owner.statusData.isOnFloor) { _SetActive(false); return; } var pointList = _GetPointList(); if (pointList == null) { // 没有路径的时候, 需要判断是否需要显示直线连接 _UpdateDirectLine(); return; } var newIndex = _GetNewIndex(); if (newIndex == -1) { _SetActive(false); return; } _UpdateObjShow(); _UpdateRealLine(newIndex, pointList); } private void _UpdateObjShow() { if (_rootObj.activeSelf == _showStraightLine) { _rootObj.SetActive(!_showStraightLine); } if (_rootObjStraight.activeSelf == !_showStraightLine) { _rootObjStraight.SetActive(_showStraightLine); } if (_showStraightLine) { _workLineRender = _lineRenderStraight; } else { _workLineRender = _lineRender; } if (!_endNoticeObj.activeSelf) { _endNoticeObj.SetActive(true); } } private void _UpdateDirectLine() { var targetCellIndex = owner.controlData.targetCellIndex; if (targetCellIndex == -1) { _SetActive(false); return; } var nowPos = owner.transform.position; var targetPos = AreaManager.instance.GetWorldPosByIndex(targetCellIndex); var toTarget = targetPos - nowPos; toTarget.y = 0; var distance = toTarget.magnitude; if (distance < 0.5f) { _SetActive(false); return; } _UpdateObjShow(); // 更新直线 var posList = new List(); posList.Add(targetPos); posList.Add(nowPos); _workLineRender.positionCount = 2; // 更新全部的 for (var i = 0; i < posList.Count; ++i) { var setPos = posList[i]; setPos.y = 0.1f; var wrapPos = _rootObj.transform.InverseTransformPoint(setPos); _workLineRender.SetPosition(i, wrapPos); } if (_endNoticeObj != null) { _endNoticeObj.transform.position = targetPos; } if (_isAttackMove != _lastIsAttackMove) { _lastIsAttackMove = _isAttackMove; // 设置颜色 var color = _isAttackMove ? Color.red : Color.white; _workLineRender.startColor = color; _workLineRender.endColor = color; } } private void _UpdateRealLine(int newIndex, List pointList) { if (_useSingleLine) { // 更新全部的 _workLineRender.positionCount = 2; var endPos = pointList[0]; endPos.y = 0.1f; var wrapPos = _rootObj.transform.InverseTransformPoint(endPos); _workLineRender.SetPosition(0, wrapPos); var currentPos = owner.transData.pos; currentPos.y = 0.1f; currentPos = _rootObj.transform.InverseTransformPoint(currentPos); _workLineRender.SetPosition(1, currentPos); if (_endNoticeObj != null) { _endNoticeObj.transform.position = endPos; } } else { // 更新全部的 var totalCount = pointList.Count - newIndex; if (totalCount <= 0) { _workLineRender.positionCount = 0; return; } _workLineRender.positionCount = totalCount + 1; for (var i = 0; i < totalCount; ++i) { var setPos = pointList[i]; setPos.y = 0.1f; var wrapPos = _rootObj.transform.InverseTransformPoint(setPos); _workLineRender.SetPosition(i, wrapPos); } var currentPos = owner.transData.pos; currentPos.y = 0.1f; currentPos = _rootObj.transform.InverseTransformPoint(currentPos); _workLineRender.SetPosition(totalCount, currentPos); if (_endNoticeObj != null) { _endNoticeObj.transform.position = pointList[0]; } } } } }