157 lines
5.0 KiB
C#
157 lines
5.0 KiB
C#
using System.Collections.Generic;
|
|
using cfg.BulletCfg;
|
|
using Framework;
|
|
using Gameplay.Common;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Unit.Data;
|
|
|
|
namespace Gameplay.Summon
|
|
{
|
|
using Gameplay.Area;
|
|
using Gameplay.Bullet;
|
|
using LTGame;
|
|
|
|
public class SummonStateTent : BaseSummonState
|
|
{
|
|
|
|
private readonly SummonTent _tent;
|
|
|
|
private float _remainTriggerTime;
|
|
|
|
private DataBullet _bulletConfig;
|
|
private List<GameUnit> _cacheUnits = new List<GameUnit>();
|
|
|
|
public SummonStateTent(SummonTent tent) : base(tent, ESummonState.Tent)
|
|
{
|
|
_tent = tent;
|
|
}
|
|
|
|
protected override void _OnEnter(NBaseState exitState, object param)
|
|
{
|
|
base._OnEnter(exitState, param);
|
|
_remainTriggerTime = _tent.triggerInterval;
|
|
_bulletConfig = TableManager.Instance.Tables.BulletConfig.GetOrDefault(_tent.bulletId);
|
|
if (_bulletConfig == null)
|
|
{
|
|
DebugUtil.LogError("SummonStateTent _OnEnter bulletConfig is null:" + _tent.bulletId);
|
|
}
|
|
|
|
_isInBuilding = false;
|
|
|
|
EventManager.Instance.Register<EDExitUnitContainer>(EventManager.EventName.INFIGHT_EXIT_UNITCONTAINER, _OnUnitExitUnitContainer);
|
|
}
|
|
|
|
protected override void _OnRunning()
|
|
{
|
|
base._OnRunning();
|
|
if (_isInBuilding)
|
|
{
|
|
_remainTriggerTime -= deltaTime;
|
|
if (_remainTriggerTime <= 0)
|
|
{
|
|
_remainTriggerTime += _tent.triggerInterval;
|
|
_DoLogic();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_WaitCreateEndAndSetInBuilding();
|
|
}
|
|
}
|
|
|
|
protected override void _OnExit(NBaseState exitState,
|
|
object param)
|
|
{
|
|
base._OnExit(exitState, param);
|
|
|
|
EventManager.Instance.Unregister<EDExitUnitContainer>(EventManager.EventName.INFIGHT_EXIT_UNITCONTAINER, _OnUnitExitUnitContainer);
|
|
}
|
|
|
|
private void _OnUnitExitUnitContainer(EDExitUnitContainer ed)
|
|
{
|
|
if (ed.fromUnit == owner)
|
|
{
|
|
// 创建者退出了帐篷, 帐篷直接销毁
|
|
owner.needRemove = true;
|
|
}
|
|
}
|
|
|
|
private bool _isInBuilding = false;
|
|
private void _WaitCreateEndAndSetInBuilding()
|
|
{
|
|
if (_isInBuilding) return;
|
|
var creator = owner.creator;
|
|
if (creator == null) return;
|
|
if (creator is Character.Character unitCharacter)
|
|
{
|
|
if (creator.statusData.isInSkilling) return;
|
|
unitCharacter.EnterBuilding(owner);
|
|
_isInBuilding = true;
|
|
}
|
|
}
|
|
|
|
private void _DoLogic()
|
|
{
|
|
var checkDistance = _tent.checkDistance;
|
|
|
|
// 根据作用区域筛选目标
|
|
var selectIndexList = AreaManager.instance.GetCellIndexsAround(owner.transData.cellIndex, checkDistance, false);
|
|
_cacheUnits.Clear();
|
|
for (var i = 0; i < selectIndexList.Count; ++i)
|
|
{
|
|
var cellIndex = selectIndexList[i];
|
|
var units = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
|
|
for (int j = 0; j < units.Count; j++)
|
|
{
|
|
var checkUnit = units[j];
|
|
if (!checkUnit.statusData.isOnFloor) continue;
|
|
if (checkUnit.commonData.troopId != owner.creator.commonData.troopId) continue;
|
|
if (BulletManagerHelper.CheckBulletFilter(owner.creator, checkUnit, _bulletConfig))
|
|
{
|
|
_cacheUnits.Add(checkUnit);
|
|
}
|
|
}
|
|
}
|
|
|
|
var pickCount = _tent.pickCount;
|
|
if (pickCount >= _cacheUnits.Count)
|
|
{
|
|
_TryEmitBulletToUnits(_cacheUnits);
|
|
}
|
|
else
|
|
{
|
|
// 随机选取pickCount个单位, 并发射子弹
|
|
_TryEmitToRandomUnits(pickCount);
|
|
}
|
|
|
|
}
|
|
|
|
private void _TryEmitToRandomUnits(int pickCount)
|
|
{
|
|
for (int i = 0; i < pickCount; i++)
|
|
{
|
|
var randomCount = UnityEngine.Random.Range(0, _cacheUnits.Count);
|
|
var randomUnit = _cacheUnits[randomCount];
|
|
_EmitBulletToUnit(randomUnit);
|
|
_cacheUnits.RemoveAt(randomCount);
|
|
}
|
|
}
|
|
|
|
private void _TryEmitBulletToUnits(List<GameUnit> units)
|
|
{
|
|
for (int i = 0; i < units.Count; i++)
|
|
{
|
|
var unit = units[i];
|
|
_EmitBulletToUnit(unit);
|
|
}
|
|
}
|
|
|
|
private void _EmitBulletToUnit(GameUnit unit)
|
|
{
|
|
var bulletId = _tent.bulletId;
|
|
BulletManager.instance.EmitBullet(bulletId, unit, owner.creator);
|
|
}
|
|
|
|
}
|
|
}
|