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

116 lines
3.7 KiB
C#
Raw Normal View History

2023-12-12 12:49:49 +08:00
using System.Collections.Generic;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
2023-08-22 19:08:45 +08:00
namespace Gameplay.Area
{
using UnityEngine;
using Gameplay.Level;
public class AttackAreaView
{
public GameUnit owner;
2023-10-19 15:00:19 +08:00
public bool isAreaShowed;
2023-08-22 19:08:45 +08:00
private Map _map;
private int _lastCellIndex;
2024-05-17 14:20:53 +08:00
private int _lastAttackDistance;
2023-08-22 19:08:45 +08:00
private Color _hintColor;
private Color _hintColor2;
private bool _forceHide;
private TGS.Territory _cacheTerritory;
private GameObject _cacheObjBorader;
2023-10-19 15:00:19 +08:00
private static readonly int Color1 = Shader.PropertyToID("_Color");
private static readonly int SecondColor = Shader.PropertyToID("_SecondColor");
2023-08-22 19:08:45 +08:00
public void Init()
{
_map = LevelManager.Instance.CurrentLevel.Map;
2024-06-13 14:42:12 +08:00
_hintColor = ColorUtility.TryParseHtmlString("#FFCB54", out Color color) ? color : Color.red;
2023-08-22 19:08:45 +08:00
_hintColor2 = ColorUtility.TryParseHtmlString("#c26b36", out Color color2) ? color2 : Color.red;
_hintColor2.a = 0;
}
public void SetOwner(GameUnit u)
{
owner = u;
_lastCellIndex = -1;
2024-05-17 14:20:53 +08:00
_lastAttackDistance = -1;
2023-08-22 19:08:45 +08:00
}
public void HideArea()
{
owner = null;
_HideArea();
}
public void ForceHide()
{
_forceHide = true;
_HideArea();
}
public void CancelForce()
{
_forceHide = false;
if (owner != null)
{
SetOwner(owner);
}
2023-08-22 19:08:45 +08:00
}
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;
2024-05-17 14:20:53 +08:00
var currentAttackDistance = owner.fightData.attackDistance;
if (currentCellIndex != _lastCellIndex || _lastAttackDistance != currentAttackDistance)
2023-08-22 19:08:45 +08:00
{
_lastCellIndex = currentCellIndex;
2024-05-17 14:20:53 +08:00
_lastAttackDistance = currentAttackDistance;
2023-08-22 19:08:45 +08:00
if (isAreaShowed)
{
// 隐藏区域信息
_HideArea();
}
// 创建新的
var attackArea = AreaManager.instance.attackAreaManager.GetAttackArea(owner.transData.cellIndex, owner.fightData.attackDistance);
var areaCellList = owner.fightData.isAttackIgnoreBlock ? attackArea.totalCells : attackArea.canAttackCells;
2024-07-12 17:06:39 +08:00
_cacheTerritory = _map.TerrainGridSystem.TerritoryCreate(areaCellList);
2023-08-22 19:08:45 +08:00
var obj = _map.TerrainGridSystem.ForceDrawTerritoryDrawInteriorBorder(_cacheTerritory, 0, 1, _hintColor, _hintColor2);
var getMat = obj.transform.GetChild(0).GetComponent<MeshRenderer>().material;
2023-10-19 15:00:19 +08:00
getMat.SetColor(Color1, _hintColor);
getMat.SetColor(SecondColor, _hintColor2);
2023-08-22 19:08:45 +08:00
_cacheObjBorader = obj;
isAreaShowed = true;
}
}
else
{
if (isAreaShowed)
{
_HideArea();
}
}
}
}
}