using Gameplay.Area; using Gameplay.Common; using Gameplay.Unit; using Gameplay.Unit.Data; using LTGame; namespace Gameplay.Summon { public class BulletBoxStateIdle : BaseSummonState { public SummonBulletBox bulletBox; // 站在格子上的单位 private GameUnit _standUnit; public BulletBoxStateIdle(SummonBulletBox bulletBox) : base(bulletBox, ESummonState.Idle) { this.bulletBox = bulletBox; } protected override void _OnEnter(NBaseState exitState, object param) { base._OnEnter(exitState, param); _standUnit = null; } protected override void _OnRunning() { base._OnRunning(); var selfCellIndex = bulletBox.transData.cellIndex; if (_standUnit == null) { // 判断是否有单位站在格子上 var units = AreaManager.instance.GetUnitsByCellIndex(selfCellIndex); for (int i = 0; i < units.Count; i++) { var unit = units[i]; if (unit.commonData.troopId != bulletBox.commonData.troopId) { // 敌方不生效 continue; } if (unit.gameunitType != EGameUnitType.Character) { // 只对角色生效 continue; } _standUnit = unit; break; } if (_standUnit != null) { // 添加buff _AddBuffToUnit(); } } else { // 判断当前单位是否离开格子 var targetCellIndex = _standUnit.transData.cellIndex; if (targetCellIndex != selfCellIndex) { // 移除buff _RemoveBuffFromUnit(); // 置空 _standUnit = null; } } } protected override void _OnExit(NBaseState exitState, object param) { base._OnExit(exitState, param); if (_standUnit != null) { // 移除buff _RemoveBuffFromUnit(); // 置空 _standUnit = null; } } private void _AddBuffToUnit() { for (int i = 0; i < bulletBox.buffIds.Count; i++) { var buffId = bulletBox.buffIds[i]; _standUnit.buffManager.AddBuffById(buffId, owner); } } private void _RemoveBuffFromUnit() { for (var i = 0; i < bulletBox.buffIds.Count; ++i) { var buffId = bulletBox.buffIds[i]; _standUnit.buffManager.RemoveBuffById(buffId); } } } }