diff --git a/DungeonShooting_Godot/src/game/item/package/Holster.cs b/DungeonShooting_Godot/src/game/item/package/Holster.cs index 606b519..a737b40 100644 --- a/DungeonShooting_Godot/src/game/item/package/Holster.cs +++ b/DungeonShooting_Godot/src/game/item/package/Holster.cs @@ -1,3 +1,4 @@ +using System; using Godot; /// <summary> @@ -39,6 +40,9 @@ /// </summary> public int ActiveIndex { get; private set; } = 0; + /// <summary> + /// 武器插槽 + /// </summary> public WeaponSlot[] SlotList { get; } = new WeaponSlot[4]; public Holster(Role master) @@ -92,6 +96,24 @@ } /// <summary> + /// 通过回调函数查询武器在武器袋中的位置, 如果没有, 则返回 -1 + /// </summary> + /// <param name="handler"></param> + /// <returns></returns> + public int FindWeapon(Func<Weapon, bool> handler) + { + for (int i = 0; i < SlotList.Length; i++) + { + var item = SlotList[i]; + if (item.Weapon != null && handler(item.Weapon)) + { + return i; + } + } + return -1; + } + + /// <summary> /// 返回是否能放入武器 /// </summary> /// <param name="weapon">武器对象</param> diff --git a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs index f165656..4bbf70a 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs @@ -285,17 +285,21 @@ //这把武器被扔在地上, 或者当前武器没有被使用 if (Master == null || Master.Holster.ActiveWeapon != this) { + //_triggerTimer _triggerTimer = _triggerTimer > 0 ? _triggerTimer - delta : 0; + //攻击冷却计时 _attackTimer = _attackTimer > 0 ? _attackTimer - delta : 0; + //武器的当前散射半径 CurrScatteringRange = Mathf.Max(CurrScatteringRange - Attribute.ScatteringRangeBackSpeed * delta, Attribute.StartScatteringRange); //松开扳机 if (_triggerFlag || _downTimer > 0) { UpTrigger(); - _triggerFlag = false; _downTimer = 0; } + + _triggerFlag = false; //重置数据 if (_dirtyFlag) @@ -406,6 +410,9 @@ /// </summary> public void Trigger() { + //这一帧已经按过了, 不需要再按下 + if (_triggerFlag) return; + //是否第一帧按下 var justDown = _downTimer == 0; //是否能发射 @@ -454,11 +461,14 @@ fireFlag = false; } } - else if (CurrAmmo <= 0) + else if (CurrAmmo <= 0) //子弹不够 { fireFlag = false; - //子弹不够 - Reload(); + if (justDown) + { + //第一帧按下, 触发换弹 + Reload(); + } } if (fireFlag) @@ -631,15 +641,23 @@ /// <summary> /// 返回弹药是否到达上限 /// </summary> - public bool IsFullAmmo() + public bool IsAmmoFull() { return CurrAmmo + ResidueAmmo >= Attribute.MaxAmmoCapacity; } /// <summary> + /// 返回弹夹是否打空 + /// </summary> + public bool IsAmmoEmpty() + { + return CurrAmmo == 0; + } + + /// <summary> /// 返回是否弹药耗尽 /// </summary> - public bool IsEmptyAmmo() + public bool IsTotalAmmoEmpty() { return CurrAmmo + ResidueAmmo == 0; } @@ -751,7 +769,7 @@ if (CurrAmmo + ResidueAmmo != 0) //子弹不为空 { var targetWeapon = roleMaster.Holster.GetWeapon(index); - if (!targetWeapon.IsFullAmmo()) //背包里面的武器子弹未满 + if (!targetWeapon.IsAmmoFull()) //背包里面的武器子弹未满 { //可以互动拾起弹药 result.CanInteractive = true; @@ -913,7 +931,7 @@ } /// <summary> - /// 触发移除 + /// 触发移除, 这个函数由 Holster 对象调用 /// </summary>a public void Remove() { diff --git a/DungeonShooting_Godot/src/game/role/StateController.cs b/DungeonShooting_Godot/src/game/role/StateController.cs index f349561..ce64474 100644 --- a/DungeonShooting_Godot/src/game/role/StateController.cs +++ b/DungeonShooting_Godot/src/game/role/StateController.cs @@ -66,6 +66,14 @@ } /// <summary> + /// 返回该状态机控制器中是否存在指定的状态实例 + /// </summary> + public bool HasState(S state) + { + return _states.ContainsKey(state); + } + + /// <summary> /// 获取指定状态对应的实例对象 /// </summary> public StateBase<T, S> GetState(S state) diff --git a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs index e27189d..098fb0e 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs @@ -155,17 +155,38 @@ var weapon = Holster.ActiveWeapon; if (weapon != null) { - if (weapon.Attribute.ContinuousShoot) //连发 + if (weapon.IsTotalAmmoEmpty()) //当前武器弹药打空 { - Attack(); + //扔掉当前武器 + ThrowWeapon(); + // var index = Holster.FindWeapon(we => !we.IsTotalAmmoEmpty()); + // if (index != -1) + // { + // + // } } - else //单发 + else if (weapon.Reloading) //换弹中 { - if (_enemyAttackTimer <= 0) + + } + else if (weapon.IsAmmoEmpty()) //弹夹已经打空 + { + Reload(); + } + else //正常射击 + { + if (weapon.Attribute.ContinuousShoot) //连发 { - _enemyAttackTimer = 60f / weapon.Attribute.StartFiringSpeed; Attack(); } + else //单发 + { + if (_enemyAttackTimer <= 0) + { + _enemyAttackTimer = 60f / weapon.Attribute.StartFiringSpeed; + Attack(); + } + } } } }