185 lines
5.6 KiB
C#
185 lines
5.6 KiB
C#
using System.Collections.Generic;
|
|
using Gameplay.Common;
|
|
using Gameplay.Level;
|
|
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
|
|
{
|
|
|
|
public GameObject rootObj;
|
|
|
|
public GameUnit owner;
|
|
|
|
private LineRenderer _lineRender;
|
|
|
|
private int _lastMoveIndex = -1;
|
|
|
|
private readonly bool _useSingleLine = false;
|
|
|
|
public async void Init()
|
|
{
|
|
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>("Assets/_Test/Prefab/RoadLine.prefab");
|
|
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()
|
|
{
|
|
rootObj.Destroy();
|
|
rootObj = null;
|
|
}
|
|
|
|
public void UpdateLine(GameUnit u)
|
|
{
|
|
if (!LevelManager.Instance.CurrentLevel.isInBattle) return;
|
|
|
|
_lastMoveIndex = -1;
|
|
|
|
owner = u;
|
|
if (owner == null)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
public void HideLine()
|
|
{
|
|
_lastMoveIndex = -1;
|
|
owner = null;
|
|
if (rootObj != null)
|
|
{
|
|
rootObj.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private List<Vector3> _GetPointList()
|
|
{
|
|
switch (owner.gameunitType)
|
|
{
|
|
case EGameUnitType.Character:
|
|
{
|
|
var c = owner as Character;
|
|
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;
|
|
return state.CalcRoadLine();
|
|
}
|
|
case EGameUnitType.Vehicle:
|
|
{
|
|
var v = owner as NormalVehicle;
|
|
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;
|
|
return state.CalcRoadLine();
|
|
}
|
|
|
|
}
|
|
return null;
|
|
}
|
|
|
|
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;
|
|
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;
|
|
return state.GetCurrentIndex();
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
if (owner == null) return;
|
|
var pointList = _GetPointList();
|
|
if (pointList == null)
|
|
{
|
|
rootObj.SetActive(false);
|
|
return;
|
|
}
|
|
if (!rootObj.activeSelf)
|
|
{
|
|
rootObj.SetActive(true);
|
|
}
|
|
var newIndex = _GetNewIndex();
|
|
|
|
if (_useSingleLine)
|
|
{
|
|
_lastMoveIndex = newIndex;
|
|
// 更新全部的
|
|
_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);
|
|
}
|
|
else
|
|
{
|
|
_lastMoveIndex = newIndex;
|
|
// 更新全部的
|
|
var totalCount = pointList.Count - newIndex;
|
|
_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);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|