168 lines
4.8 KiB
C#
168 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using SepCore.Battle;
|
|
using SepCore.CustomUtility;
|
|
using SepCore.Definition;
|
|
using SepCore.Entity;
|
|
using UnityEngine;
|
|
|
|
namespace SepCore.Components
|
|
{
|
|
public class BackpackComponent : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 玩家类引用。
|
|
/// </summary>
|
|
private Player _player;
|
|
|
|
/// <summary>
|
|
/// 武器列表。
|
|
/// </summary>
|
|
private List<WeaponBase> _weapons;
|
|
|
|
/// <summary>
|
|
/// 道具列表。
|
|
/// </summary>
|
|
private List<PropItem> _props;
|
|
|
|
/// <summary>
|
|
/// 玩家状态组件引用,用于道具效果的应用。
|
|
/// </summary>
|
|
private StatComponent _statComponent;
|
|
|
|
/// <summary>
|
|
/// 武器标签羁绊管理器。
|
|
/// </summary>
|
|
private readonly WeaponSynergyManager _synergyManager = new();
|
|
|
|
/// <summary>
|
|
/// 武器装备槽,在 Editor 中进行配置,表示武器在世界中的显示位置。
|
|
/// </summary>
|
|
[SerializeField] private Transform[] _weaponSlots;
|
|
|
|
public IReadOnlyList<WeaponBase> Weapons => _weapons;
|
|
public IReadOnlyList<PropItem> Props => _props;
|
|
public int WeaponCapacity => GameEntry.Luban.Global.WeaponCapacity;
|
|
public WeaponSynergyManager SynergyManager => _synergyManager;
|
|
|
|
public void OnInit(Player player)
|
|
{
|
|
_player = player;
|
|
|
|
_weapons = new List<WeaponBase>(WeaponCapacity);
|
|
_props = new List<PropItem>();
|
|
_statComponent = player.StatComponent;
|
|
_synergyManager.Initialize();
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_weapons.Clear();
|
|
_props.Clear();
|
|
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
|
|
}
|
|
|
|
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);
|
|
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
|
|
return true;
|
|
}
|
|
|
|
public bool DetachWeapon(WeaponBase weapon)
|
|
{
|
|
bool result = _weapons.Remove(weapon);
|
|
if (result)
|
|
{
|
|
GameEntry.Entity.DetachEntity(weapon.Id, _player.Id);
|
|
RealignWeaponSlots();
|
|
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void RealignWeaponSlots()
|
|
{
|
|
if (_weaponSlots == null || _player == null) return;
|
|
|
|
for (int i = 0; i < _weapons.Count; i++)
|
|
{
|
|
if (i < _weaponSlots.Length && _weapons[i] != null)
|
|
{
|
|
GameEntry.Entity.AttachEntity(_weapons[i].Id, _player.Id, _weaponSlots[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool CanCombineWeapon(int index, out int matchIndex, out WeaponConfig nextTierConfig)
|
|
{
|
|
matchIndex = -1;
|
|
nextTierConfig = null;
|
|
|
|
if (_weapons == null || index < 0 || index >= _weapons.Count)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WeaponBase target = _weapons[index];
|
|
if (target?.WeaponData == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!WeaponUtility.TryGetNextTierConfig(target.WeaponData.Config, out nextTierConfig))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < _weapons.Count; i++)
|
|
{
|
|
if (i == index) continue;
|
|
WeaponBase other = _weapons[i];
|
|
if (other?.WeaponData == null) continue;
|
|
|
|
if (other.WeaponData.WeaponType == target.WeaponData.WeaponType &&
|
|
other.WeaponData.Rarity == target.WeaponData.Rarity)
|
|
{
|
|
matchIndex = i;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool AttachProp(PropItem prop)
|
|
{
|
|
_props.Add(prop);
|
|
foreach (var modifier in prop.Modifiers)
|
|
{
|
|
_statComponent.AddModifier(modifier);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool DetachProp(PropItem prop)
|
|
{
|
|
_props.Remove(prop);
|
|
foreach (var modifier in prop.Modifiers)
|
|
{
|
|
_statComponent.RemoveModifier(modifier);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void SetWeaponState(bool state)
|
|
{
|
|
foreach (var weapon in _weapons)
|
|
{
|
|
weapon.SetEnabled(state);
|
|
}
|
|
}
|
|
}
|
|
}
|