vampire-like/Assets/GameMain/Scripts/Components/BackpackComponent.cs

105 lines
2.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using Definition.DataStruct;
using Entity.Weapon;
using Entity;
using UnityEngine;
namespace Components
{
public class BackpackComponent : MonoBehaviour
{
/// <summary>
/// 武器槽容量由DRRole决定。
/// </summary>
private int _weaponCapacity;
/// <summary>
/// 玩家类引用。
/// </summary>
private Player _player;
/// <summary>
/// 武器列表。
/// </summary>
private List<WeaponBase> _weapons;
/// <summary>
/// 道具列表。
/// </summary>
private List<PropItem> _props;
/// <summary>
/// 玩家状态组件引用,用于道具效果的应用。
/// </summary>
private StatComponent _statComponent;
/// <summary>
/// 武器装备槽,在 Editor 中进行配置,表示武器在世界中的显示位置。
/// </summary>
[SerializeField] private Transform[] _weaponSlots;
public IReadOnlyList<WeaponBase> Weapons => _weapons;
public IReadOnlyList<PropItem> Props => _props;
public int WeaponCapacity => _weaponCapacity;
public void OnInit(Player player, int weaponCapacity)
{
_weaponCapacity = weaponCapacity;
_player = player;
_weapons = new List<WeaponBase>(_weaponCapacity);
_props = new List<PropItem>();
_statComponent = GetComponent<StatComponent>();
}
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);
}
}
}
}