77 lines
1.9 KiB
C#
77 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace Gameplay.LegionBattle.View
|
|
{
|
|
public class FightLineStandUnit2 : MonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public int standCellIndex = -1;
|
|
|
|
public int maxDistance = 3;
|
|
public int side = 1;
|
|
|
|
public bool isBuild = false;
|
|
public float moveSpeed = 5f;
|
|
|
|
private bool _needMove;
|
|
private Vector3 _targetPos;
|
|
|
|
public bool isPosChanged
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public void MoveTo(Vector3 worldPos)
|
|
{
|
|
_targetPos = worldPos;
|
|
_targetPos.y = 0;
|
|
_needMove = true;
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
_UpdateStandPos();
|
|
|
|
var worldPos = FightLineControl2.instance.m_tgs.CellGetPosition(standCellIndex);
|
|
transform.position = worldPos;
|
|
}
|
|
|
|
public void ViewUpdate(float dt)
|
|
{
|
|
if (!_needMove) return;
|
|
var nowPos = transform.position;
|
|
var toTarget = _targetPos - nowPos;
|
|
toTarget.y = 0;
|
|
var distance = toTarget.magnitude;
|
|
var moveDt = dt * moveSpeed;
|
|
if (distance > moveDt)
|
|
{
|
|
var moveDir = toTarget.normalized;
|
|
var movePos = nowPos + moveDir * moveDt;
|
|
transform.position = movePos;
|
|
}
|
|
else
|
|
{
|
|
transform.position = _targetPos;
|
|
_needMove = false;
|
|
}
|
|
_UpdateStandPos();
|
|
}
|
|
|
|
private void _UpdateStandPos()
|
|
{
|
|
var newCell = FightLineControl2.instance.m_tgs.CellGetAtPosition(transform.position, true);
|
|
if (newCell == null)
|
|
{
|
|
return;
|
|
}
|
|
if (standCellIndex != newCell.index)
|
|
{
|
|
standCellIndex = newCell.index;
|
|
isPosChanged = true;
|
|
}
|
|
}
|
|
}
|
|
}
|