diff --git a/DungeonShooting_Godot/src/game/activity/package/Holster.cs b/DungeonShooting_Godot/src/game/activity/package/Holster.cs deleted file mode 100644 index df5dc56..0000000 --- a/DungeonShooting_Godot/src/game/activity/package/Holster.cs +++ /dev/null @@ -1,369 +0,0 @@ -using System; -using System.Collections.Generic; -using Godot; - -/// -/// 角色身上的武器袋, 存储角色携带的武器 -/// -public class Holster -{ - /// - /// 归属者 - /// - public Role Master { get; } - - /// - /// 当前使用的武器对象 - /// - public Weapon ActiveWeapon { get; private set; } - - /// - /// 当前使用的武器的索引 - /// - public int ActiveIndex { get; private set; } = 0; - - /// - /// 武器袋容量 - /// - public int Capacity { get; private set; } = 0; - - /// - /// 武器插槽 - /// - public Weapon[] Weapons { get; private set; } - - public Holster(Role master) - { - Master = master; - //默认容量4 - SetCapacity(4); - } - - /// - /// 修改武器袋容量 - /// - public void SetCapacity(int capacity) - { - if (capacity < 0) - { - capacity = 0; - } - - if (capacity == Capacity) - { - return; - } - - if (Weapons == null) - { - Weapons = new Weapon[capacity]; - } - else if (Weapons.Length > capacity) //删减格子 - { - var newArray = new Weapon[capacity]; - for (var i = 0; i < Weapons.Length; i++) - { - if (i < capacity) - { - newArray[i] = Weapons[i]; - } - else - { - Master.ThrowWeapon(i); - } - } - - Weapons = newArray; - } - else //添加格子 - { - var newArray = new Weapon[capacity]; - for (var i = 0; i < Weapons.Length; i++) - { - newArray[i] = Weapons[i]; - } - Weapons = newArray; - } - Capacity = capacity; - - } - - /// - /// 返回当前武器袋是否是空的 - /// - public bool IsEmpty() - { - for (var i = 0; i < Weapons.Length; i++) - { - if (Weapons[i] != null) - { - return false; - } - } - - return true; - } - - /// - /// 返回当前武器袋是否还有空位 - /// - public bool HasVacancy() - { - for (var i = 0; i < Weapons.Length; i++) - { - if (Weapons[i] == null) - { - return true; - } - } - - return false; - } - - /// - /// 根据索引获取武器 - /// - public Weapon GetWeapon(int index) - { - if (index < 0 || index >= Weapons.Length) - { - return null; - } - return Weapons[index]; - } - - /// - /// 根据武器id查找武器袋中该武器所在的位置, 如果没有, 则返回 -1 - /// - /// 武器id - public int FindWeapon(string id) - { - for (var i = 0; i < Weapons.Length; i++) - { - var item = Weapons[i]; - if (item != null && item.ItemConfig.Id == id) - { - return i; - } - } - return -1; - } - - /// - /// 通过回调函数查询武器在武器袋中的位置, 如果没有, 则返回 -1 - /// - public int FindWeapon(Func handler) - { - for (var i = 0; i < Weapons.Length; i++) - { - var item = Weapons[i]; - if (item != null && handler(item, i)) - { - return i; - } - } - return -1; - } - - /// - /// 遍历所有武器 - /// - public void ForEach(Action handler) - { - for (var i = 0; i < Weapons.Length; i++) - { - var item = Weapons[i]; - if (item != null) - { - handler(item, i); - } - } - } - - /// - /// 从武器袋中移除所有武器, 并返回 - /// - public Weapon[] GetAndClearWeapon() - { - var weapons = new List(); - for (var i = 0; i < Weapons.Length; i++) - { - var weapon = Weapons[i]; - if (weapon != null) - { - weapon.GetParent().RemoveChild(weapon); - weapon.RemoveAt(); - weapons.Add(weapon); - Weapons[i] = null; - } - } - - return weapons.ToArray(); - } - - /// - /// 返回是否能放入武器 - /// - /// 武器对象 - public bool CanPickupWeapon(Weapon weapon) - { - for (var i = 0; i < Weapons.Length; i++) - { - var item = Weapons[i]; - if (item == null) - { - return true; - } - } - return false; - } - - /// - /// 拾起武器, 存入武器袋中, 返回存放在武器袋的位置, 如果容不下这把武器, 则会返回 -1 - /// - /// 武器对象 - /// 是否立即切换到该武器, 默认 true - public int PickupWeapon(Weapon weapon, bool exchange = true) - { - //已经被拾起了 - if (weapon.Master != null) - { - return -1; - } - for (var i = 0; i < Weapons.Length; i++) - { - var item = Weapons[i]; - if (item == null) - { - weapon.Pickup(); - Weapons[i] = weapon; - weapon.PickUpWeapon(Master); - if (exchange) - { - ExchangeByIndex(i); - } - - return i; - } - } - return -1; - } - - /// - /// 移除指定位置的武器, 并返回这个武器对象, 如果移除正在使用的这把武器, 则会自动切换到上一把武器 - /// - /// 所在武器袋的位置索引 - public Weapon RemoveWeapon(int index) - { - if (index < 0 || index >= Weapons.Length) - { - return null; - } - var weapon = Weapons[index]; - if (weapon == null) - { - return null; - } - weapon.GetParent().RemoveChild(weapon); - Weapons[index] = null; - - //如果是当前手持的武器, 就需要调用切换武器操作 - if (index == ActiveIndex) - { - //没有其他武器了 - if (ExchangePrev() == index) - { - ActiveIndex = 0; - ActiveWeapon = null; - } - } - weapon.RemoveAt(); - return weapon; - } - - /// - /// 切换到上一个武器 - /// - public int ExchangePrev() - { - var index = ActiveIndex - 1; - do - { - if (index < 0) - { - index = Weapons.Length - 1; - } - if (ExchangeByIndex(index)) - { - return index; - } - } while (index-- != ActiveIndex); - return -1; - } - - /// - /// 切换到下一个武器, - /// - public int ExchangeNext() - { - var index = ActiveIndex + 1; - do - { - if (index >= Weapons.Length) - { - index = 0; - } - if (ExchangeByIndex(index)) - { - return index; - } - } - while (index++ != ActiveIndex); - return -1; - } - - /// - /// 切换到指定索引的武器 - /// - public bool ExchangeByIndex(int index) - { - if (index == ActiveIndex && ActiveWeapon != null) return true; - if (index < 0 || index > Weapons.Length) return false; - var weapon = Weapons[index]; - if (weapon == null) return false; - - //将上一把武器放到背后 - if (ActiveWeapon != null) - { - var tempParent = ActiveWeapon.GetParentOrNull(); - if (tempParent != null) - { - tempParent.RemoveChild(ActiveWeapon); - Master.BackMountPoint.AddChild(ActiveWeapon); - Master.OnPutBackMount(ActiveWeapon, ActiveIndex); - ActiveWeapon.Conceal(); - } - } - - //更改父节点 - var parent = weapon.GetParentOrNull(); - if (parent == null) - { - Master.MountPoint.AddChild(weapon); - } - else if (parent != Master.MountPoint) - { - parent.RemoveChild(weapon); - Master.MountPoint.AddChild(weapon); - } - - weapon.Position = Vector2.Zero; - weapon.Scale = Vector2.One; - weapon.RotationDegrees = 0; - weapon.Visible = true; - ActiveWeapon = weapon; - ActiveIndex = index; - ActiveWeapon.Active(); - return true; - } -}