78 lines
2.1 KiB
C#
78 lines
2.1 KiB
C#
using UnityEngine;
|
|
namespace Gameplay.LegionBattle.View
|
|
{
|
|
public class FightLineStandUnit
|
|
{
|
|
public Vector2Int standPos;
|
|
|
|
public int side;
|
|
|
|
public int maxDistance = 3;
|
|
|
|
private GameObject _viewObj;
|
|
|
|
public float moveSpeed = 5;
|
|
|
|
private Vector3 _targetPos;
|
|
private bool _needMove;
|
|
|
|
public bool isStandChanged;
|
|
|
|
public bool isMainUnit;
|
|
|
|
public void CreateView(GameObject sampleObj, Transform parent)
|
|
{
|
|
_viewObj = Object.Instantiate(sampleObj, parent);
|
|
_viewObj.transform.localPosition = new Vector3(standPos.x, standPos.y, 0);
|
|
|
|
var renderer = _viewObj.transform.GetChild(0).GetComponent<MeshRenderer>();
|
|
renderer.material.color = side < 0 ? Color.red : Color.blue;
|
|
_needMove = false;
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
if (!_needMove) return;
|
|
var nowPos = _viewObj.transform.position;
|
|
var toTarget = _targetPos - nowPos;
|
|
toTarget.z = 0;
|
|
var distance = toTarget.magnitude;
|
|
var moveDt = dt * moveSpeed;
|
|
if (distance > moveDt)
|
|
{
|
|
var moveDir = toTarget.normalized;
|
|
var movePos = nowPos + moveDir * moveDt;
|
|
_viewObj.transform.position = movePos;
|
|
}
|
|
else
|
|
{
|
|
_viewObj.transform.position = _targetPos;
|
|
_needMove = false;
|
|
}
|
|
|
|
_UpdateStandPos();
|
|
}
|
|
|
|
private void _UpdateStandPos()
|
|
{
|
|
var newStandPos = FightLineControl.GetGridPosFromWorld(_viewObj.transform.position);
|
|
if (newStandPos != standPos)
|
|
{
|
|
standPos = newStandPos;
|
|
isStandChanged = true;
|
|
}
|
|
}
|
|
|
|
public void MoveTo(Vector3 targetPos)
|
|
{
|
|
if (isMainUnit)
|
|
{
|
|
DebugUtil.LogError("主单位不能移动");
|
|
return;
|
|
}
|
|
_targetPos = targetPos;
|
|
_needMove = true;
|
|
}
|
|
}
|
|
}
|