using System.Collections.Generic; 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 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; _hintColor = ColorUtility.TryParseHtmlString("#FFCB54", 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; _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(); } // 创建新的 var attackArea = AreaManager.instance.attackAreaManager.GetAttackArea(owner.transData.cellIndex, owner.fightData.attackDistance); var areaCellList = owner.fightData.isAttackIgnoreBlock ? attackArea.totalCells : attackArea.canAttackCells; _cacheTerritory = _map.TerrainGridSystem.TerritoryCreate(areaCellList); var obj = _map.TerrainGridSystem.ForceDrawTerritoryDrawInteriorBorder(_cacheTerritory, 0, 1, _hintColor, _hintColor2); var getMat = obj.transform.GetChild(0).GetComponent().material; getMat.SetColor(Color1, _hintColor); getMat.SetColor(SecondColor, _hintColor2); _cacheObjBorader = obj; isAreaShowed = true; } } else { if (isAreaShowed) { _HideArea(); } } } } }