122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using Gameplay.Level;
|
|
using Gameplay.Unit;
|
|
using PhxhSDK;
|
|
using UnityEngine;
|
|
using Constants = Framework.Constants;
|
|
namespace Gameplay.Area.AttackArea
|
|
{
|
|
public class AttackAreaManager
|
|
{
|
|
|
|
private Dictionary<int, AttackAreaPoint> _attackAreaPointMap = new Dictionary<int, AttackAreaPoint>();
|
|
|
|
public RaycastHit[] raycastHits = new RaycastHit[10];
|
|
public int hitMask;
|
|
|
|
public GameObject rootObj;
|
|
|
|
private Dictionary<int, TempBlockData> _tempBlockAttackCellMap = new Dictionary<int, TempBlockData>();
|
|
public Dictionary<int, TempBlockData> tempBlockAttackCellMap
|
|
{
|
|
get
|
|
{
|
|
return _tempBlockAttackCellMap;
|
|
}
|
|
}
|
|
private bool _isMapChanged;
|
|
|
|
public AttackAreaManager()
|
|
{
|
|
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
hitMask = 1 << LayerMask.NameToLayer("BlockAttack");
|
|
rootObj = new GameObject("[BlockAttackArea]");
|
|
var allCell = LevelManager.Instance.CurrentLevel.Map.MapData.BlocksData;
|
|
for (int i = 0; i < allCell.Count; i++)
|
|
{
|
|
var cell = allCell[i];
|
|
if (cell.GetTerrainTypeProperty().banAttack)
|
|
{
|
|
var worldPos = AreaManager.instance.GetCellPosByIndex(i);
|
|
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(Constants.FIGHT_CELL_COLLIDER_PATH);
|
|
var instanceObj = Object.Instantiate(sampleObj, rootObj.transform);
|
|
instanceObj.transform.position = worldPos;
|
|
}
|
|
}
|
|
_isMapChanged = false;
|
|
Physics.Simulate(0.016f);
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
if (_isMapChanged)
|
|
{
|
|
_isMapChanged = false;
|
|
Physics.Simulate(dt);
|
|
// 清空攻击区域
|
|
foreach (var attackAreaPoint in _attackAreaPointMap.Values)
|
|
{
|
|
attackAreaPoint.attackAreaMap.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
public TempBlockData CreateBlockAttackCell(int cellIndex)
|
|
{
|
|
if (_tempBlockAttackCellMap.ContainsKey(cellIndex))
|
|
{
|
|
DebugUtil.LogError("已经存在的阻挡攻击的格子:{0}", cellIndex);
|
|
return null;
|
|
}
|
|
var worldPos = AreaManager.instance.GetCellPosByIndex(cellIndex);
|
|
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(Constants.FIGHT_CELL_COLLIDER_PATH);
|
|
var instanceObj = Object.Instantiate(sampleObj, rootObj.transform);
|
|
instanceObj.transform.position = worldPos;
|
|
var tempData = new TempBlockData(cellIndex, instanceObj);
|
|
_tempBlockAttackCellMap.Add(cellIndex, tempData);
|
|
_isMapChanged = true;
|
|
return tempData;
|
|
}
|
|
|
|
public void RemoveBlockAttackCell(int cellIndex)
|
|
{
|
|
if (_tempBlockAttackCellMap.TryGetValue(cellIndex, out var searchObj))
|
|
{
|
|
Object.Destroy(searchObj.blockObj);
|
|
_tempBlockAttackCellMap.Remove(cellIndex);
|
|
_isMapChanged = true;
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError("不存在的阻挡攻击的格子:{0}", cellIndex);
|
|
}
|
|
}
|
|
|
|
public AttackAreaData GetAttackArea(int centerCellIndex,
|
|
int attackDistance)
|
|
{
|
|
if (_attackAreaPointMap.TryGetValue(centerCellIndex, out var attackAreaPoint))
|
|
{
|
|
return attackAreaPoint.GetAttackArea(attackDistance);
|
|
}
|
|
var newAttackAreaPoint = new AttackAreaPoint(centerCellIndex);
|
|
_attackAreaPointMap.Add(centerCellIndex, newAttackAreaPoint);
|
|
return newAttackAreaPoint.GetAttackArea(attackDistance);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (rootObj != null)
|
|
{
|
|
Object.Destroy(rootObj);
|
|
rootObj = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|