using System.Collections.Generic; using Definition.DataStruct; using Entity.Weapon; using Entity; using UnityEngine; namespace Components { public class BackpackComponent : MonoBehaviour { /// /// 武器槽容量,由DRRole决定。 /// private int _weaponCapacity; /// /// 玩家类引用。 /// private Player _player; /// /// 武器列表。 /// private List _weapons; /// /// 道具列表。 /// private List _props; /// /// 玩家状态组件引用,用于道具效果的应用。 /// private StatComponent _statComponent; /// /// 武器装备槽,在 Editor 中进行配置,表示武器在世界中的显示位置。 /// [SerializeField] private Transform[] _weaponSlots; public IReadOnlyList Weapons => _weapons; public IReadOnlyList Props => _props; public int WeaponCapacity => _weaponCapacity; public void OnInit(Player player, int weaponCapacity) { _weaponCapacity = weaponCapacity; _player = player; _weapons = new List(_weaponCapacity); _props = new List(); _statComponent = GetComponent(); } public void OnReset() { _weapons.Clear(); _props.Clear(); } public bool AttachWeapon(WeaponBase weapon) { if (_weapons.Count == _weaponCapacity) return false; GameEntry.Entity.AttachEntity(weapon.Id, _player.Id, _weaponSlots[_weapons.Count]); _weapons.Add(weapon); weapon.SetEnabled(true); return true; } public bool DetachWeapon(WeaponBase weapon) { bool result = _weapons.Remove(weapon); if (result) { GameEntry.Entity.DetachEntity(weapon.Id, _player.Id); } return result; } public bool AttachProp(PropItem prop) { _props.Add(prop); prop.OnAttach(_statComponent); return true; } public bool DetachProp(PropItem prop) { _props.Remove(prop); prop.OnDetach(_statComponent); return true; } public void SetWeaponState(bool state) { if (_weapons == null) return; foreach (var weapon in _weapons) { weapon.SetEnabled(state); } } } }