NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Area/RoadLineView.cs

347 lines
11 KiB
C#

using System.Collections.Generic;
using Gameplay.Common;
using Gameplay.Fight.Protect;
using Gameplay.Level;
using Gameplay.Unit;
using Gameplay.Unit.Data;
using Gameplay.Vehicle.Impl;
using Gameplay.Vehicle.State;
using PhxhSDK;
using Constants = Framework.Constants;
namespace Gameplay.Area
{
using UnityEngine;
using Character;
public class RoadLineView
{
public GameObject rootObj;
public GameUnit owner;
private LineRenderer _lineRender;
private readonly bool _useSingleLine = false;
private GameObject _endNoticeObj;
public void Init()
{
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(Constants.ROAD_LINE_PATH);
rootObj = Object.Instantiate(sampleObj);
rootObj.transform.position = Vector3.zero;
rootObj.transform.rotation = Quaternion.Euler(90, 0, 0);
_lineRender = rootObj.GetComponent<LineRenderer>();
rootObj.SetActive(false);
}
public void Dispose()
{
if (rootObj != null)
{
Object.Destroy(rootObj);
rootObj = null;
}
if (_endNoticeObj != null)
{
Object.Destroy(_endNoticeObj);
_endNoticeObj = null;
}
}
public void UpdateLine(GameUnit u)
{
owner = u;
}
public void CreateEndNotice(int unitIndex)
{
var loadPath = Constants.END_TARGET_OBJ;
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(loadPath);
_endNoticeObj = Object.Instantiate(sampleObj);
_endNoticeObj.SetActive(false);
}
public void HideLine()
{
owner = null;
if (rootObj != null)
{
rootObj.SetActive(false);
}
if (_endNoticeObj != null)
{
_endNoticeObj.SetActive(false);
}
}
private void _SetActive(bool active)
{
if (rootObj != null)
{
rootObj.SetActive(active);
}
if (_endNoticeObj != null)
{
_endNoticeObj.SetActive(active);
}
}
private List<Vector3> _GetPointList()
{
List<Vector3> result = null;
switch (owner.gameunitType)
{
case EGameUnitType.Character:
{
var c = owner as Character;
if (c == null)
{
DebugUtil.LogError("Character is null");
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;
}
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;
case EGameUnitType.BeProtect:
{
var b = owner as BeProtectUnit;
if (b == null)
{
DebugUtil.LogError("BeProtectUnit is null");
return null;
}
if (b.pathCellIndexs.Count == 0) return null;
if (owner.statusData.isDead)
{
return null;
}
result = b.displayPath;
}
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();
}
case EGameUnitType.BeProtect:
{
var b = owner as BeProtectUnit;
if (b == null)
{
DebugUtil.LogError("BeProtectUnit is null");
return -1;
}
if (b.pathCellIndexs.Count == 0) return -1;
return b.currentMoveIndex;
}
}
return -1;
}
public void LogicUpdate(float dt)
{
if (owner == null) return;
var pointList = _GetPointList();
if (pointList == null)
{
// 没有路径的时候, 需要判断是否需要显示直线连接
_UpdateDirectLine();
return;
}
var newIndex = _GetNewIndex();
if (newIndex == -1)
{
_SetActive(false);
return;
}
if (!rootObj.activeSelf)
{
_SetActive(true);
}
_UpdateRealLine(newIndex, pointList);
}
private void _UpdateDirectLine()
{
var targetCellIndex = owner.controlData.targetCellIndex;
if (targetCellIndex == -1)
{
_SetActive(false);
return;
}
var nowPos = owner.transform.position;
var targetPos = AreaManager.instance.GetCellPosByIndex(targetCellIndex);
var toTarget = targetPos - nowPos;
toTarget.y = 0;
var distance = toTarget.magnitude;
if (distance < 0.5f)
{
_SetActive(false);
return;
}
if (!rootObj.activeSelf)
{
_SetActive(true);
}
// 更新直线
var posList = new List<Vector3>();
posList.Add(nowPos);
posList.Add(targetPos);
_lineRender.positionCount = 2;
// 更新全部的
for (var i = 0; i < posList.Count; ++i)
{
var setPos = posList[i];
setPos.y = 0.1f;
var wrapPos = rootObj.transform.InverseTransformPoint(setPos);
_lineRender.SetPosition(i, wrapPos);
}
if (_endNoticeObj != null)
{
_endNoticeObj.transform.position = targetPos;
}
}
private void _UpdateRealLine(int newIndex, List<Vector3> pointList)
{
if (_useSingleLine)
{
// 更新全部的
_lineRender.positionCount = 2;
var endPos = pointList[0];
endPos.y = 0.1f;
var wrapPos = rootObj.transform.InverseTransformPoint(endPos);
_lineRender.SetPosition(0, wrapPos);
var currentPos = owner.transData.pos;
currentPos.y = 0.1f;
currentPos = rootObj.transform.InverseTransformPoint(currentPos);
_lineRender.SetPosition(1, currentPos);
if (_endNoticeObj != null)
{
_endNoticeObj.transform.position = endPos;
}
}
else
{
// 更新全部的
var totalCount = pointList.Count - newIndex;
if (totalCount <= 0)
{
_lineRender.positionCount = 0;
return;
}
_lineRender.positionCount = totalCount + 1;
for (var i = 0; i < totalCount; ++i)
{
var setPos = pointList[i];
setPos.y = 0.1f;
var wrapPos = rootObj.transform.InverseTransformPoint(setPos);
_lineRender.SetPosition(i, wrapPos);
}
var currentPos = owner.transData.pos;
currentPos.y = 0.1f;
currentPos = rootObj.transform.InverseTransformPoint(currentPos);
_lineRender.SetPosition(totalCount, currentPos);
if (_endNoticeObj != null)
{
_endNoticeObj.transform.position = pointList[0];
}
}
}
}
}