Files
Cielonos/Assets/Scripts/MainGame/Characters/Player/Inventory/PlayerInventorySubcontroller.cs

381 lines
14 KiB
C#
Raw Normal View History

2026-06-05 04:21:00 -04:00
using System;
2026-01-03 18:19:39 -05:00
using System.Collections.Generic;
using System.Linq;
2026-05-23 08:27:50 -04:00
using Cielonos.MainGame.Inventory;
using Cielonos.MainGame.Inventory.Collection;
using Cielonos.MainGame.Inventory.Collections;
2026-05-10 11:47:55 -04:00
using UnityEngine;
2025-11-25 08:19:33 -05:00
namespace Cielonos.MainGame.Characters
{
2026-02-13 09:22:11 -05:00
public partial class PlayerInventorySubcontroller : SubcontrollerBase<Player>
2025-11-25 08:19:33 -05:00
{
public Player player => owner;
private PlayerInputSubcontroller inputSc => player.inputSc;
private PlayerOperationSubcontroller operationSc => player.operationSc;
2026-05-10 11:47:55 -04:00
public BackpackSubmodule backpackSm;
public EquipmentSubmodule equipmentSm;
public Transform mainWeaponContainer;
public Transform supportEquipmentContainer;
public Transform passiveEquipmentContainer;
public Transform consumableContainer;
2025-11-25 08:19:33 -05:00
public override void Initialize()
{
base.Initialize();
RegisterOperations();
2026-05-10 11:47:55 -04:00
backpackSm ??= new BackpackSubmodule(this);
equipmentSm ??= new EquipmentSubmodule(this);
2026-06-12 17:11:39 -04:00
backpackSm.ObtainItem<Polychrome>();
2026-05-26 00:21:27 -04:00
backpackSm.ObtainItem<FutureWand>();
2026-05-23 08:27:50 -04:00
backpackSm.ObtainItem<Ascension>();
2026-06-12 17:11:39 -04:00
backpackSm.ObtainItem<Passion>();
backpackSm.ObtainItem<ThermalDetonator>();
//backpackSm.ObtainItem<PerceptiveMetalPlating>();
//backpackSm.ObtainItem<DualHarmony>();
/*backpackSm.ObtainItem<Polychrome>();
2026-05-26 00:21:27 -04:00
backpackSm.ObtainItem<BlackHoleDisplacer>();
2026-05-23 08:27:50 -04:00
backpackSm.ObtainItem<DecayPropagator>();
2026-05-26 00:21:27 -04:00
backpackSm.ObtainItem<DecayAccelerationCoil>();
2026-05-23 08:27:50 -04:00
backpackSm.ObtainItem<FusionInjector>();
2026-05-26 00:21:27 -04:00
backpackSm.ObtainItem<MissileSeparationMembrane>();
2026-05-27 15:15:28 -04:00
backpackSm.ObtainItem<PhotonPolarizer>();
2026-06-02 12:55:39 -04:00
backpackSm.ObtainItem<PhotonDissociator>();*/
2026-05-10 11:47:55 -04:00
2026-06-12 17:11:39 -04:00
foreach (MainWeaponBase mainWeapon in backpackSm.mainWeapons)
2026-05-10 11:47:55 -04:00
{
equipmentSm.PrepareMainWeapon(mainWeapon);
2026-06-12 17:11:39 -04:00
}
2026-05-10 11:47:55 -04:00
//backpackSm.ObtainItem<BlackHoleDisplacer>();
2025-11-25 08:19:33 -05:00
}
}
2026-01-03 18:19:39 -05:00
#region Functions
public partial class PlayerInventorySubcontroller
{
public void ApplyAttributeChange(string attributeName, ref float numeric, ref float percentageAccumulation, ref float percentageMultiplication)
{
2026-05-10 11:47:55 -04:00
foreach (var mainWeapon in backpackSm.mainWeapons.Where(mainWeapon => mainWeapon.passiveAttributeSm != null))
2026-01-03 18:19:39 -05:00
{
2026-05-23 08:27:50 -04:00
mainWeapon.passiveAttributeSm.ApplyCharacterAttributeChanges(attributeName, ref numeric, ref percentageAccumulation, ref percentageMultiplication);
2026-01-03 18:19:39 -05:00
}
2026-05-10 11:47:55 -04:00
foreach (var supportEquipment in backpackSm.supportEquipments.Where(supportEquipment => supportEquipment.passiveAttributeSm != null))
2026-01-03 18:19:39 -05:00
{
2026-05-23 08:27:50 -04:00
supportEquipment.passiveAttributeSm.ApplyCharacterAttributeChanges(attributeName, ref numeric, ref percentageAccumulation, ref percentageMultiplication);
2026-01-03 18:19:39 -05:00
}
2026-05-10 11:47:55 -04:00
foreach (var passiveEquipment in backpackSm.passiveEquipments.Where(passiveEquipment => passiveEquipment.passiveAttributeSm != null))
2026-01-03 18:19:39 -05:00
{
2026-05-23 08:27:50 -04:00
passiveEquipment.passiveAttributeSm.ApplyCharacterAttributeChanges(attributeName, ref numeric, ref percentageAccumulation, ref percentageMultiplication);
2026-01-03 18:19:39 -05:00
}
2026-05-10 11:47:55 -04:00
foreach (var consumable in backpackSm.consumables.Where(consumable => consumable.passiveAttributeSm != null))
2026-01-03 18:19:39 -05:00
{
2026-05-23 08:27:50 -04:00
consumable.passiveAttributeSm.ApplyCharacterAttributeChanges(attributeName, ref numeric, ref percentageAccumulation, ref percentageMultiplication);
2026-01-03 18:19:39 -05:00
}
2026-05-23 08:27:50 -04:00
currentMainWeapon?.activeAttributeSm?.ApplyCharacterAttributeChanges(attributeName, ref numeric, ref percentageAccumulation, ref percentageMultiplication);
2026-01-03 18:19:39 -05:00
foreach (SupportEquipmentBase supportEquipment in currentSupportEquipments)
{
2026-05-23 08:27:50 -04:00
supportEquipment?.activeAttributeSm?.ApplyCharacterAttributeChanges(attributeName, ref numeric, ref percentageAccumulation, ref percentageMultiplication);
2026-01-03 18:19:39 -05:00
}
}
}
2026-05-10 11:47:55 -04:00
#endregion
#region Rare Materials
public partial class PlayerInventorySubcontroller
{
/// <summary>
/// 获取玩家当前持有的 RareMaterial。
/// </summary>
public RareMaterial GetRareMaterial()
{
return backpackSm.consumables.Find(c => c is RareMaterial) as RareMaterial;
}
/// <summary>
/// 获取玩家当前持有的 RareMaterial 数量。
/// </summary>
public int GetRareMaterialAmount()
{
RareMaterial rm = GetRareMaterial();
return rm != null ? rm.stackAmount : 0;
}
}
2026-01-03 18:19:39 -05:00
#endregion
2025-11-25 08:19:33 -05:00
public partial class PlayerInventorySubcontroller
{
private MainWeaponBase currentMainWeapon => equipmentSm.currentMainWeapon;
2026-01-03 18:19:39 -05:00
private List<SupportEquipmentBase> currentSupportEquipments => equipmentSm.currentSupportEquipments;
2025-11-25 08:19:33 -05:00
private void RegisterOperations()
{
//operationSc.OnMainWeaponSpecialPress += MainWeaponSpecialPress;
//operationSc.OnMainWeaponSpecialRelease += MainWeaponSpecialRelease;
operationSc.OnMainWeaponPrimaryPress += MainWeaponPrimaryPress;
operationSc.OnMainWeaponPrimaryRelease += MainWeaponPrimaryRelease;
operationSc.OnMainWeaponSecondaryPress += MainWeaponSecondaryPress;
operationSc.OnMainWeaponSecondaryRelease += MainWeaponSecondaryRelease;
2026-02-13 09:22:11 -05:00
operationSc.OnMainWeaponSpecialAPress += MainWeaponSpecialAPress;
operationSc.OnMainWeaponSpecialARelease += MainWeaponSpecialARelease;
operationSc.OnMainWeaponSpecialBPress += MainWeaponSpecialBPress;
operationSc.OnMainWeaponSpecialBRelease += MainWeaponSpecialBRelease;
operationSc.OnMainWeaponSpecialCPress += MainWeaponSpecialCPress;
operationSc.OnMainWeaponSpecialCRelease += MainWeaponSpecialCRelease;
2025-11-25 08:19:33 -05:00
2025-12-23 19:47:06 -05:00
operationSc.OnSwitchMainWeapon += SwitchMainWeapon;
2026-01-03 18:19:39 -05:00
operationSc.OnSupportEquipment0Press += () => SupportEquipmentPress(0);
operationSc.OnSupportEquipment1Press += () => SupportEquipmentPress(1);
operationSc.OnSupportEquipment2Press += () => SupportEquipmentPress(2);
operationSc.OnSupportEquipment3Press += () => SupportEquipmentPress(3);
operationSc.OnSupportEquipment0Release += () => SupportEquipmentRelease(0);
operationSc.OnSupportEquipment1Release += () => SupportEquipmentRelease(1);
operationSc.OnSupportEquipment2Release += () => SupportEquipmentRelease(2);
operationSc.OnSupportEquipment3Release += () => SupportEquipmentRelease(3);
2025-11-25 08:19:33 -05:00
}
public virtual void Update()
{
if (inputSc.IsHoldingPrimary)
{
2026-06-05 04:21:00 -04:00
currentMainWeapon?.OnPrimaryHold();
2025-11-25 08:19:33 -05:00
}
if (inputSc.IsHoldingSecondary)
{
2026-06-05 04:21:00 -04:00
currentMainWeapon?.OnSecondaryHold();
2025-11-25 08:19:33 -05:00
}
2025-12-08 05:27:53 -05:00
if (inputSc.IsHoldingSpecialA)
2025-11-25 08:19:33 -05:00
{
2026-06-05 04:21:00 -04:00
currentMainWeapon?.OnSpecialAHold();
2025-11-25 08:19:33 -05:00
}
2025-12-08 05:27:53 -05:00
if (inputSc.IsHoldingSpecialB)
2025-11-25 08:19:33 -05:00
{
2026-06-05 04:21:00 -04:00
currentMainWeapon?.OnSpecialBHold();
2025-11-25 08:19:33 -05:00
}
2026-02-13 09:22:11 -05:00
if (inputSc.IsHoldingSpecialC)
{
2026-06-05 04:21:00 -04:00
currentMainWeapon?.OnSpecialCHold();
2026-02-13 09:22:11 -05:00
}
2025-11-25 08:19:33 -05:00
}
}
public partial class PlayerInventorySubcontroller
{
2025-12-23 19:47:06 -05:00
private void SwitchMainWeapon(int direction)
{
int currentIndex = equipmentSm.preparedMainWeapons.IndexOf(currentMainWeapon);
2026-06-12 17:11:39 -04:00
currentMainWeapon?.OnSwitchOut();
2025-12-23 19:47:06 -05:00
equipmentSm.RemoveMainWeapon();
2026-06-12 17:11:39 -04:00
2025-12-23 19:47:06 -05:00
int newIndex = (currentIndex + direction + equipmentSm.preparedMainWeapons.Count) % equipmentSm.preparedMainWeapons.Count;
2026-06-12 17:11:39 -04:00
MainWeaponBase newWeapon = equipmentSm.preparedMainWeapons[newIndex];
equipmentSm.EquipMainWeapon(newWeapon);
newWeapon.OnSwitchIn();
2025-12-23 19:47:06 -05:00
}
2026-01-03 18:19:39 -05:00
private void MainWeaponPrimaryPress()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
currentMainWeapon.OnPrimaryPress();
2025-12-08 05:27:53 -05:00
player.movementSc.isSprinting = false;
2025-11-25 08:19:33 -05:00
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponPrimaryRelease()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
currentMainWeapon.OnPrimaryRelease();
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponSecondaryPress()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
currentMainWeapon.OnSecondaryPress();
2025-12-08 05:27:53 -05:00
player.movementSc.isSprinting = false;
2025-11-25 08:19:33 -05:00
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponSecondaryRelease()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
currentMainWeapon.OnSecondaryRelease();
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponSpecialAPress()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
2025-12-08 05:27:53 -05:00
currentMainWeapon.OnSpecialAPress();
2025-11-25 08:19:33 -05:00
player.movementSc.isSprinting = false;
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponSpecialARelease()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
2025-12-08 05:27:53 -05:00
currentMainWeapon.OnSpecialARelease();
2025-11-25 08:19:33 -05:00
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponSpecialBPress()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
2025-12-08 05:27:53 -05:00
currentMainWeapon.OnSpecialBPress();
2025-11-25 08:19:33 -05:00
player.movementSc.isSprinting = false;
}
}
2026-01-03 18:19:39 -05:00
private void MainWeaponSpecialBRelease()
2025-11-25 08:19:33 -05:00
{
if (currentMainWeapon != null)
{
2025-12-08 05:27:53 -05:00
currentMainWeapon.OnSpecialBRelease();
2025-11-25 08:19:33 -05:00
}
}
2026-02-13 09:22:11 -05:00
private void MainWeaponSpecialCPress()
{
if (currentMainWeapon != null)
{
currentMainWeapon.OnSpecialCPress();
player.movementSc.isSprinting = false;
}
}
private void MainWeaponSpecialCRelease()
{
if (currentMainWeapon != null)
{
currentMainWeapon.OnSpecialCRelease();
}
}
2025-11-25 08:19:33 -05:00
}
2026-01-03 18:19:39 -05:00
public partial class PlayerInventorySubcontroller
{
private void SupportEquipmentPress(int slotIndex)
{
if (currentSupportEquipments.Count > slotIndex)
{
var equipment = currentSupportEquipments[slotIndex];
if (equipment != null)
{
equipment.OnPress();
}
}
}
private void SupportEquipmentRelease(int slotIndex)
{
if (currentSupportEquipments.Count > slotIndex)
{
var equipment = currentSupportEquipments[slotIndex];
if (equipment != null)
{
equipment.OnRelease();
}
}
}
}
2026-06-05 04:21:00 -04:00
public partial class PlayerInventorySubcontroller
{
/// <summary>
/// 彻底清空并注销玩家身上当前的全部装备和游戏实体。
/// </summary>
private void ClearInventory()
{
// A. 注销当前手持主武器(解绑事件、停止动画、卸下表现层模型)
if (equipmentSm.currentMainWeapon != null)
{
equipmentSm.RemoveMainWeapon();
}
// B. 注销各个槽位上的辅助装备
for (int i = 0; i < equipmentSm.currentSupportEquipments.Count; i++)
{
if (equipmentSm.currentSupportEquipments[i] != null)
{
equipmentSm.RemoveSupportEquipment(i);
}
}
// C. 卸下并触发背包内所有物品的 OnDiscarded 回调以更新属性
var mainWeaponsCopy = new List<MainWeaponBase>(backpackSm.mainWeapons);
foreach (var item in mainWeaponsCopy) { backpackSm.DiscardItem(item); }
var supportCopy = new List<SupportEquipmentBase>(backpackSm.supportEquipments);
foreach (var item in supportCopy) { backpackSm.DiscardItem(item); }
var passiveCopy = new List<PassiveEquipmentBase>(backpackSm.passiveEquipments);
foreach (var item in passiveCopy) { backpackSm.DiscardItem(item); }
var consumablesCopy = new List<ConsumableBase>(backpackSm.consumables);
foreach (var item in consumablesCopy) { backpackSm.DiscardItem(item); }
// D. 摧毁玩家挂载点下生成的所有武器物体实例
if (mainWeaponContainer != null)
{
foreach (Transform child in mainWeaponContainer) { Destroy(child.gameObject); }
}
if (supportEquipmentContainer != null)
{
foreach (Transform child in supportEquipmentContainer) { Destroy(child.gameObject); }
}
if (passiveEquipmentContainer != null)
{
foreach (Transform child in passiveEquipmentContainer) { Destroy(child.gameObject); }
}
if (consumableContainer != null)
{
foreach (Transform child in consumableContainer) { Destroy(child.gameObject); }
}
// E. 清空列表并重新初始化底层集合
backpackSm.mainWeapons.Clear();
backpackSm.supportEquipments.Clear();
backpackSm.passiveEquipments.Clear();
backpackSm.consumables.Clear();
equipmentSm.preparedMainWeapons.Clear();
equipmentSm.currentMainWeapon = null;
for (int i = 0; i < equipmentSm.currentSupportEquipments.Count; i++)
{
equipmentSm.currentSupportEquipments[i] = null;
}
}
private ItemSaveData CreateSaveDataFromItem(ItemBase item)
{
if (item == null) return null;
return item.CreateSaveDataFromItem(item);
}
}
2025-11-25 08:19:33 -05:00
}