- 重构 `RepoItem`,统一点击事件
- 重构 `PlayerInventoryComponent`:
    - `PlayerInventoryStateStore`:状态与库存写模型
    - `PlayerInventoryTowerAssemblyService`:炮塔组装域
    - `PlayerInventoryTowerRosterService`:编队/耐久域
    - `InventoryParticipantUtility`/`InventoryCloneUtility`/`InventorySeedUtility`:通用工具类
This commit is contained in:
SepComet
2026-03-04 22:04:51 +08:00
parent a62309e8c1
commit 9db9aeabcd
27 changed files with 1755 additions and 1665 deletions
@@ -0,0 +1,128 @@
using GeometryTD.CustomUtility;
using GeometryTD.Definition;
using UnityGameFramework.Runtime;
namespace GeometryTD.CustomComponent
{
public class PlayerInventoryComponent : GameFrameworkComponent
{
private const int MaxParticipantTowerCount = 4;
private PlayerInventoryState _state;
private PlayerInventoryQueryModel _queryModel;
private PlayerInventoryCommandModel _commandModel;
private PlayerInventoryTowerRosterService _towerRosterService;
private PlayerInventoryTowerAssemblyService _towerAssemblyService;
public int Gold
{
get
{
EnsureInitialized();
return _queryModel.Gold;
}
}
public void OnInit()
{
EnsureServices();
_commandModel.Initialize(InventorySeedUtility.CreateSampleInventory(), MaxParticipantTowerCount);
BackpackInventoryData inventory = _queryModel.Inventory;
Log.Info(
"PlayerInventory initialized. Gold={0}, Tower={1}, Muzzle={2}, Bearing={3}, Base={4}.",
inventory.Gold,
inventory.Towers.Count,
inventory.MuzzleComponents.Count,
inventory.BearingComponents.Count,
inventory.BaseComponents.Count);
}
public BackpackInventoryData GetInventorySnapshot()
{
EnsureInitialized();
return _queryModel.GetSnapshot();
}
public void MergeInventory(BackpackInventoryData gainedInventory)
{
EnsureInitialized();
PlayerInventoryMergeSummary summary = _commandModel.MergeInventory(gainedInventory);
if (!summary.HasAnyGain)
{
return;
}
Log.Info(
"PlayerInventory merged reward. Gold+{0}, Tower+{1}, Muzzle+{2}, Bearing+{3}, Base+{4}.",
summary.GainedGold,
summary.GainedTowerCount,
summary.GainedMuzzleCount,
summary.GainedBearingCount,
summary.GainedBaseCount);
}
public bool TryConsumeGold(int costGold)
{
EnsureInitialized();
return _commandModel.TryConsumeGold(costGold);
}
public void AddGold(int gainGold)
{
EnsureInitialized();
_commandModel.AddGold(gainGold);
}
public bool TryAddParticipantTower(long towerInstanceId, int maxCount = 4)
{
EnsureInitialized();
return _towerRosterService.TryAddParticipantTower(towerInstanceId, maxCount);
}
public bool TryRemoveParticipantTower(long towerInstanceId)
{
EnsureInitialized();
return _towerRosterService.TryRemoveParticipantTower(towerInstanceId);
}
public bool TryAssembleTower(
long muzzleInstanceId,
long bearingInstanceId,
long baseInstanceId,
out TowerItemData assembledTower)
{
EnsureInitialized();
return _towerAssemblyService.TryAssembleTower(
muzzleInstanceId,
bearingInstanceId,
baseInstanceId,
out assembledTower);
}
public int ReduceAllTowerEndurance(float enduranceLoss)
{
EnsureInitialized();
return _towerRosterService.ReduceAllTowerEndurance(enduranceLoss);
}
private void EnsureInitialized()
{
if (_queryModel.IsInitialized)
{
return;
}
OnInit();
}
private void EnsureServices()
{
_state ??= new PlayerInventoryState();
_queryModel ??= new PlayerInventoryQueryModel(_state);
_commandModel ??= new PlayerInventoryCommandModel(_state);
_towerRosterService ??= new PlayerInventoryTowerRosterService(_queryModel, MaxParticipantTowerCount);
_towerAssemblyService ??= new PlayerInventoryTowerAssemblyService(_queryModel, _commandModel);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5db8d7bb243947fdbb9fe75a9d234d10
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,270 @@
using System;
using System.Collections.Generic;
using GeometryTD.CustomUtility;
using GeometryTD.Definition;
using UnityEngine;
namespace GeometryTD.CustomComponent
{
internal struct PlayerInventoryMergeSummary
{
public int GainedGold;
public int GainedMuzzleCount;
public int GainedBearingCount;
public int GainedBaseCount;
public int GainedTowerCount;
public bool HasAnyGain =>
GainedGold > 0 || GainedMuzzleCount > 0 || GainedBearingCount > 0 || GainedBaseCount > 0 ||
GainedTowerCount > 0;
}
internal sealed class PlayerInventoryState
{
public BackpackInventoryData Inventory = new BackpackInventoryData();
public long NextInstanceId = 1;
public bool IsInitialized;
}
internal sealed class PlayerInventoryQueryModel
{
private readonly PlayerInventoryState _state;
public PlayerInventoryQueryModel(PlayerInventoryState state)
{
_state = state;
}
public BackpackInventoryData Inventory => _state.Inventory;
public bool IsInitialized => _state.IsInitialized;
public int Gold => _state.Inventory.Gold;
public BackpackInventoryData GetSnapshot()
{
return InventoryCloneUtility.CloneInventory(_state.Inventory);
}
public bool TryGetTowerById(long towerInstanceId, out TowerItemData tower)
{
return InventoryParticipantUtility.TryGetTowerById(_state.Inventory, towerInstanceId, out tower);
}
public bool TryGetComponentById<TComp>(List<TComp> components, long instanceId, out TComp result)
where TComp : TowerCompItemData
{
result = null;
if (components == null || instanceId <= 0)
{
return false;
}
for (int i = 0; i < components.Count; i++)
{
TComp component = components[i];
if (component != null && component.InstanceId == instanceId)
{
result = component;
return true;
}
}
return false;
}
}
internal sealed class PlayerInventoryCommandModel
{
private readonly PlayerInventoryState _state;
public PlayerInventoryCommandModel(PlayerInventoryState state)
{
_state = state;
}
public void Initialize(BackpackInventoryData sourceInventory, int maxParticipantTowerCount)
{
_state.Inventory = InventoryCloneUtility.CloneInventory(sourceInventory);
InventoryParticipantUtility.NormalizeParticipantState(_state.Inventory, maxParticipantTowerCount);
RebuildNextInstanceId();
_state.IsInitialized = true;
}
public PlayerInventoryMergeSummary MergeInventory(BackpackInventoryData gainedInventory)
{
PlayerInventoryMergeSummary summary = default;
if (gainedInventory == null)
{
return summary;
}
summary.GainedGold = Mathf.Max(0, gainedInventory.Gold);
if (summary.GainedGold > 0)
{
_state.Inventory.Gold += summary.GainedGold;
}
if (gainedInventory.MuzzleComponents != null)
{
for (int i = 0; i < gainedInventory.MuzzleComponents.Count; i++)
{
MuzzleCompItemData source = gainedInventory.MuzzleComponents[i];
if (source == null)
{
continue;
}
MuzzleCompItemData cloned = InventoryCloneUtility.CloneMuzzleComp(source);
cloned.InstanceId = AllocateInstanceId();
_state.Inventory.MuzzleComponents.Add(cloned);
summary.GainedMuzzleCount++;
}
}
if (gainedInventory.BearingComponents != null)
{
for (int i = 0; i < gainedInventory.BearingComponents.Count; i++)
{
BearingCompItemData source = gainedInventory.BearingComponents[i];
if (source == null)
{
continue;
}
BearingCompItemData cloned = InventoryCloneUtility.CloneBearingComp(source);
cloned.InstanceId = AllocateInstanceId();
_state.Inventory.BearingComponents.Add(cloned);
summary.GainedBearingCount++;
}
}
if (gainedInventory.BaseComponents != null)
{
for (int i = 0; i < gainedInventory.BaseComponents.Count; i++)
{
BaseCompItemData source = gainedInventory.BaseComponents[i];
if (source == null)
{
continue;
}
BaseCompItemData cloned = InventoryCloneUtility.CloneBaseComp(source);
cloned.InstanceId = AllocateInstanceId();
_state.Inventory.BaseComponents.Add(cloned);
summary.GainedBaseCount++;
}
}
if (gainedInventory.Towers != null)
{
for (int i = 0; i < gainedInventory.Towers.Count; i++)
{
TowerItemData source = gainedInventory.Towers[i];
if (source == null)
{
continue;
}
TowerItemData cloned = InventoryCloneUtility.CloneTower(source);
cloned.InstanceId = AllocateInstanceId();
_state.Inventory.Towers.Add(cloned);
summary.GainedTowerCount++;
}
}
return summary;
}
public bool TryConsumeGold(int costGold)
{
int resolvedCost = Mathf.Max(0, costGold);
if (resolvedCost <= 0)
{
return true;
}
if (_state.Inventory.Gold < resolvedCost)
{
return false;
}
_state.Inventory.Gold -= resolvedCost;
return true;
}
public void AddGold(int gainGold)
{
int resolvedGain = Mathf.Max(0, gainGold);
if (resolvedGain <= 0)
{
return;
}
_state.Inventory.Gold += resolvedGain;
}
public long AllocateInstanceId()
{
if (_state.NextInstanceId < 1)
{
_state.NextInstanceId = 1;
}
return _state.NextInstanceId++;
}
private void RebuildNextInstanceId()
{
long maxInstanceId = 0;
BackpackInventoryData inventory = _state.Inventory;
if (inventory.Towers != null)
{
for (int i = 0; i < inventory.Towers.Count; i++)
{
TowerItemData item = inventory.Towers[i];
if (item != null)
{
maxInstanceId = Math.Max(maxInstanceId, item.InstanceId);
}
}
}
if (inventory.MuzzleComponents != null)
{
for (int i = 0; i < inventory.MuzzleComponents.Count; i++)
{
MuzzleCompItemData item = inventory.MuzzleComponents[i];
if (item != null)
{
maxInstanceId = Math.Max(maxInstanceId, item.InstanceId);
}
}
}
if (inventory.BearingComponents != null)
{
for (int i = 0; i < inventory.BearingComponents.Count; i++)
{
BearingCompItemData item = inventory.BearingComponents[i];
if (item != null)
{
maxInstanceId = Math.Max(maxInstanceId, item.InstanceId);
}
}
}
if (inventory.BaseComponents != null)
{
for (int i = 0; i < inventory.BaseComponents.Count; i++)
{
BaseCompItemData item = inventory.BaseComponents[i];
if (item != null)
{
maxInstanceId = Math.Max(maxInstanceId, item.InstanceId);
}
}
}
_state.NextInstanceId = Math.Max(1, maxInstanceId + 1);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4aa101c4dd7c30845b0a84b2a04f9b9e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,210 @@
using System.Collections.Generic;
using GameFramework.DataTable;
using GeometryTD.CustomUtility;
using GeometryTD.DataTable;
using GeometryTD.Definition;
using UnityEngine;
namespace GeometryTD.CustomComponent
{
internal sealed class PlayerInventoryTowerAssemblyService
{
private const int TowerLevelCount = 5;
private readonly PlayerInventoryQueryModel _queryModel;
private readonly PlayerInventoryCommandModel _commandModel;
private IDataTable<DRMuzzleComp> _drMuzzleComp;
private IDataTable<DRBearingComp> _drBearingComp;
private IDataTable<DRBaseComp> _drBaseComp;
public PlayerInventoryTowerAssemblyService(
PlayerInventoryQueryModel queryModel,
PlayerInventoryCommandModel commandModel)
{
_queryModel = queryModel;
_commandModel = commandModel;
}
public bool TryAssembleTower(
long muzzleInstanceId,
long bearingInstanceId,
long baseInstanceId,
out TowerItemData assembledTower)
{
assembledTower = null;
BackpackInventoryData inventory = _queryModel.Inventory;
if (muzzleInstanceId <= 0 || bearingInstanceId <= 0 || baseInstanceId <= 0)
{
return false;
}
if (!_queryModel.TryGetComponentById(inventory.MuzzleComponents, muzzleInstanceId, out MuzzleCompItemData muzzleComp) ||
!_queryModel.TryGetComponentById(inventory.BearingComponents, bearingInstanceId, out BearingCompItemData bearingComp) ||
!_queryModel.TryGetComponentById(inventory.BaseComponents, baseInstanceId, out BaseCompItemData baseComp))
{
return false;
}
if (muzzleComp.IsAssembledIntoTower || bearingComp.IsAssembledIntoTower || baseComp.IsAssembledIntoTower)
{
return false;
}
if (!TryBuildTowerStats(muzzleComp, bearingComp, baseComp, out TowerStatsData stats))
{
return false;
}
long towerInstanceId = _commandModel.AllocateInstanceId();
TowerItemData tower = new TowerItemData
{
InstanceId = towerInstanceId,
Name = $"组装防御塔-{towerInstanceId}",
Rarity = ResolveAverageRarity(muzzleComp.Rarity, bearingComp.Rarity, baseComp.Rarity),
MuzzleComponentInstanceId = muzzleComp.InstanceId,
BearingComponentInstanceId = bearingComp.InstanceId,
BaseComponentInstanceId = baseComp.InstanceId,
Stats = stats
};
muzzleComp.IsAssembledIntoTower = true;
bearingComp.IsAssembledIntoTower = true;
baseComp.IsAssembledIntoTower = true;
inventory.Towers.Add(tower);
assembledTower = InventoryCloneUtility.CloneTower(tower);
return true;
}
private bool TryBuildTowerStats(
MuzzleCompItemData muzzleComp,
BearingCompItemData bearingComp,
BaseCompItemData baseComp,
out TowerStatsData stats)
{
stats = null;
if (muzzleComp == null || bearingComp == null || baseComp == null)
{
return false;
}
DRMuzzleComp muzzleConfig = EnsureMuzzleTable()?.GetDataRow(muzzleComp.ConfigId);
DRBearingComp bearingConfig = EnsureBearingTable()?.GetDataRow(bearingComp.ConfigId);
DRBaseComp baseConfig = EnsureBaseTable()?.GetDataRow(baseComp.ConfigId);
if (muzzleConfig == null || bearingConfig == null || baseConfig == null)
{
return false;
}
stats = new TowerStatsData
{
AttackDamage = BuildLevelIntArray(muzzleComp.AttackDamage, muzzleComp.Rarity, muzzleConfig.AttackDamagePerLevel),
DamageRandomRate = Mathf.Max(0f, muzzleComp.DamageRandomRate),
RotateSpeed = BuildLevelFloatArray(bearingComp.RotateSpeed, bearingComp.Rarity, bearingConfig.RotateSpeedPerLevel),
AttackRange = BuildLevelFloatArray(bearingComp.AttackRange, bearingComp.Rarity, bearingConfig.AttackRangePerLevel),
AttackSpeed = BuildLevelFloatArray(baseComp.AttackSpeed, baseComp.Rarity, baseConfig.AttackSpeedPerLevel),
AttackMethodType = muzzleComp.AttackMethodType,
AttackPropertyType = baseComp.AttackPropertyType,
Tags = MergeTags(muzzleComp.Tags, bearingComp.Tags, baseComp.Tags)
};
return true;
}
private static int[] BuildLevelIntArray(int[] rarityBaseArray, RarityType rarity, int perLevel)
{
int baseValue = ResolveRarityBaseValue(rarityBaseArray, rarity);
int[] values = new int[TowerLevelCount];
for (int i = 0; i < values.Length; i++)
{
values[i] = baseValue + perLevel * i;
}
return values;
}
private static float[] BuildLevelFloatArray(float[] rarityBaseArray, RarityType rarity, float perLevel)
{
float baseValue = ResolveRarityBaseValue(rarityBaseArray, rarity);
float[] values = new float[TowerLevelCount];
for (int i = 0; i < values.Length; i++)
{
values[i] = baseValue + perLevel * i;
}
return values;
}
private static int ResolveRarityBaseValue(int[] rarityBaseArray, RarityType rarity)
{
if (rarityBaseArray == null || rarityBaseArray.Length <= 0)
{
return 0;
}
int rarityIndex = Mathf.Clamp((int)rarity - 1, 0, rarityBaseArray.Length - 1);
return rarityBaseArray[rarityIndex];
}
private static float ResolveRarityBaseValue(float[] rarityBaseArray, RarityType rarity)
{
if (rarityBaseArray == null || rarityBaseArray.Length <= 0)
{
return 0f;
}
int rarityIndex = Mathf.Clamp((int)rarity - 1, 0, rarityBaseArray.Length - 1);
return rarityBaseArray[rarityIndex];
}
private static TagType[] MergeTags(params TagType[][] sources)
{
HashSet<TagType> uniqueTags = new HashSet<TagType>();
if (sources != null)
{
for (int i = 0; i < sources.Length; i++)
{
TagType[] tags = sources[i];
if (tags == null)
{
continue;
}
for (int j = 0; j < tags.Length; j++)
{
uniqueTags.Add(tags[j]);
}
}
}
TagType[] mergedTags = new TagType[uniqueTags.Count];
uniqueTags.CopyTo(mergedTags);
return mergedTags;
}
private static RarityType ResolveAverageRarity(RarityType muzzleRarity, RarityType bearingRarity, RarityType baseRarity)
{
float avg = ((int)muzzleRarity + (int)bearingRarity + (int)baseRarity) / 3f;
int rounded = Mathf.RoundToInt(avg);
int clamped = Mathf.Clamp(rounded, (int)RarityType.White, (int)RarityType.Red);
return (RarityType)clamped;
}
private IDataTable<DRMuzzleComp> EnsureMuzzleTable()
{
_drMuzzleComp ??= GameEntry.DataTable.GetDataTable<DRMuzzleComp>();
return _drMuzzleComp;
}
private IDataTable<DRBearingComp> EnsureBearingTable()
{
_drBearingComp ??= GameEntry.DataTable.GetDataTable<DRBearingComp>();
return _drBearingComp;
}
private IDataTable<DRBaseComp> EnsureBaseTable()
{
_drBaseComp ??= GameEntry.DataTable.GetDataTable<DRBaseComp>();
return _drBaseComp;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b20e5ad5952075449bc89c483cd4723b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,125 @@
using System.Collections.Generic;
using GeometryTD.CustomUtility;
using GeometryTD.Definition;
using UnityEngine;
namespace GeometryTD.CustomComponent
{
internal sealed class PlayerInventoryTowerRosterService
{
private readonly PlayerInventoryQueryModel _queryModel;
private readonly int _maxParticipantTowerCount;
public PlayerInventoryTowerRosterService(PlayerInventoryQueryModel queryModel, int maxParticipantTowerCount)
{
_queryModel = queryModel;
_maxParticipantTowerCount = Mathf.Max(1, maxParticipantTowerCount);
}
public bool TryAddParticipantTower(long towerInstanceId, int maxCount)
{
int resolvedMaxCount = Mathf.Max(1, maxCount);
resolvedMaxCount = Mathf.Min(resolvedMaxCount, _maxParticipantTowerCount);
return InventoryParticipantUtility.TryAddParticipantTower(
_queryModel.Inventory,
towerInstanceId,
resolvedMaxCount);
}
public bool TryRemoveParticipantTower(long towerInstanceId)
{
return InventoryParticipantUtility.TryRemoveParticipantTower(
_queryModel.Inventory,
towerInstanceId,
_maxParticipantTowerCount);
}
public int ReduceAllTowerEndurance(float enduranceLoss)
{
float resolvedLoss = Mathf.Max(0f, enduranceLoss);
BackpackInventoryData inventory = _queryModel.Inventory;
if (resolvedLoss <= 0f || inventory.Towers == null || inventory.Towers.Count <= 0)
{
return 0;
}
Dictionary<long, MuzzleCompItemData> muzzleMap = BuildComponentMap(inventory.MuzzleComponents);
Dictionary<long, BearingCompItemData> bearingMap = BuildComponentMap(inventory.BearingComponents);
Dictionary<long, BaseCompItemData> baseMap = BuildComponentMap(inventory.BaseComponents);
int affectedCount = 0;
for (int i = 0; i < inventory.Towers.Count; i++)
{
TowerItemData tower = inventory.Towers[i];
if (tower == null)
{
continue;
}
bool towerAffected = false;
if (muzzleMap.TryGetValue(tower.MuzzleComponentInstanceId, out MuzzleCompItemData muzzleComp))
{
towerAffected |= TryReduceComponentEndurance(muzzleComp, resolvedLoss);
}
if (bearingMap.TryGetValue(tower.BearingComponentInstanceId, out BearingCompItemData bearingComp))
{
towerAffected |= TryReduceComponentEndurance(bearingComp, resolvedLoss);
}
if (baseMap.TryGetValue(tower.BaseComponentInstanceId, out BaseCompItemData baseComp))
{
towerAffected |= TryReduceComponentEndurance(baseComp, resolvedLoss);
}
if (towerAffected)
{
affectedCount++;
}
}
return affectedCount;
}
private static bool TryReduceComponentEndurance(TowerCompItemData component, float enduranceLoss)
{
if (component == null)
{
return false;
}
float originalEndurance = component.Endurance;
float nextEndurance = Mathf.Clamp(originalEndurance - Mathf.Max(0f, enduranceLoss), 0f, 100f);
if (nextEndurance >= originalEndurance)
{
return false;
}
component.Endurance = nextEndurance;
return true;
}
private static Dictionary<long, TComp> BuildComponentMap<TComp>(List<TComp> components)
where TComp : TowerCompItemData
{
Dictionary<long, TComp> map = new Dictionary<long, TComp>();
if (components == null || components.Count <= 0)
{
return map;
}
for (int i = 0; i < components.Count; i++)
{
TComp component = components[i];
if (component == null || component.InstanceId <= 0)
{
continue;
}
map[component.InstanceId] = component;
}
return map;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 45c950a847f55a04f9f780218f735d42
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: