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

119 lines
3.7 KiB
C#

using System.Collections.Generic;
using Gameplay.Common;
using Gameplay.Unit;
namespace Gameplay.Area
{
using UnityEngine;
using Gameplay.Level;
public class AttackAreaView
{
public GameUnit owner;
public bool isAreaShowed;
private Map _map;
private int _lastCellIndex;
private int _lastAttackDistance;
private List<int> _areaInfo;
private Color _hintColor;
private Color _hintColor2;
private bool _forceHide;
private TGS.Territory _cacheTerritory;
private GameObject _cacheObjBorader;
private static readonly int Color1 = Shader.PropertyToID("_Color");
private static readonly int SecondColor = Shader.PropertyToID("_SecondColor");
public void Init()
{
_map = LevelManager.Instance.CurrentLevel.Map;
_areaInfo = new List<int>();
_hintColor = ColorUtility.TryParseHtmlString("#C1C336", out Color color) ? color : Color.red;
_hintColor2 = ColorUtility.TryParseHtmlString("#c26b36", out Color color2) ? color2 : Color.red;
_hintColor2.a = 0;
}
public void SetOwner(GameUnit u)
{
owner = u;
_lastCellIndex = -1;
_lastAttackDistance = -1;
}
public void HideArea()
{
owner = null;
_HideArea();
}
public void ForceHide()
{
_forceHide = true;
_HideArea();
}
public void CancelForce()
{
_forceHide = false;
if (owner != null)
{
SetOwner(owner);
}
}
private void _HideArea()
{
if (!isAreaShowed) return;
isAreaShowed = false;
var tIndex = _map.TerrainGridSystem.TerritoryGetIndex(_cacheTerritory);
_map.TerrainGridSystem.TerritoryDestroy(tIndex);
_cacheTerritory = null;
_areaInfo = null;
_cacheObjBorader.Destroy();
}
public void LogicUpdate(float dt)
{
var needShow = owner != null
&& !owner.statusData.isDead
&& !_forceHide;
if (needShow)
{
var currentCellIndex = owner.transData.cellIndex;
var currentAttackDistance = owner.fightData.attackDistance;
if (currentCellIndex != _lastCellIndex || _lastAttackDistance != currentAttackDistance)
{
_lastCellIndex = currentCellIndex;
_lastAttackDistance = currentAttackDistance;
if (isAreaShowed)
{
// 隐藏区域信息
_HideArea();
}
// 创建新的
_areaInfo = AreaManager.instance.GetCellIndexsAround(currentCellIndex, currentAttackDistance);
_cacheTerritory = _map.TerrainGridSystem.TerritoryCreate(_areaInfo);
var obj = _map.TerrainGridSystem.ForceDrawTerritoryDrawInteriorBorder(_cacheTerritory, 0, 1, _hintColor, _hintColor2);
var getMat = obj.transform.GetChild(0).GetComponent<MeshRenderer>().material;
getMat.SetColor(Color1, _hintColor);
getMat.SetColor(SecondColor, _hintColor2);
_cacheObjBorader = obj;
isAreaShowed = true;
}
}
else
{
if (isAreaShowed)
{
_HideArea();
}
}
}
}
}