181 lines
6.2 KiB
C#
181 lines
6.2 KiB
C#
using System.Collections.Generic;
|
|
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;
|
|
private PlayerInventoryTradeService _tradeService;
|
|
|
|
public int Gold
|
|
{
|
|
get
|
|
{
|
|
EnsureInitialized();
|
|
return _queryModel.Gold;
|
|
}
|
|
}
|
|
|
|
public void OnInit(BackpackInventoryData initialInventory = null)
|
|
{
|
|
EnsureServices();
|
|
_commandModel.Initialize(initialInventory ?? 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 ReplaceInventorySnapshot(BackpackInventoryData inventorySnapshot)
|
|
{
|
|
EnsureServices();
|
|
_commandModel.ReplaceInventorySnapshot(inventorySnapshot, MaxParticipantTowerCount);
|
|
}
|
|
|
|
public IReadOnlyList<TowerItemData> GetParticipantTowerSnapshot()
|
|
{
|
|
EnsureInitialized();
|
|
|
|
BackpackInventoryData inventory = _queryModel.Inventory;
|
|
if (inventory?.ParticipantTowerInstanceIds == null || inventory.ParticipantTowerInstanceIds.Count <= 0)
|
|
{
|
|
return new List<TowerItemData>();
|
|
}
|
|
|
|
List<TowerItemData> result = new List<TowerItemData>(inventory.ParticipantTowerInstanceIds.Count);
|
|
for (int i = 0; i < inventory.ParticipantTowerInstanceIds.Count && result.Count < MaxParticipantTowerCount; i++)
|
|
{
|
|
long towerId = inventory.ParticipantTowerInstanceIds[i];
|
|
if (!_queryModel.TryGetTowerById(towerId, out TowerItemData tower) || tower == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
result.Add(InventoryCloneUtility.CloneTower(tower));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
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 bool TryPurchaseComponent(TowerCompItemData item, int price)
|
|
{
|
|
EnsureInitialized();
|
|
return _tradeService.TryPurchaseComponent(item, price);
|
|
}
|
|
|
|
public void AddGold(int gainGold)
|
|
{
|
|
EnsureInitialized();
|
|
_commandModel.AddGold(gainGold);
|
|
}
|
|
|
|
public ParticipantTowerAssignResult 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 ReduceTowerEndurance(IReadOnlyList<long> towerInstanceIds, float enduranceLoss)
|
|
{
|
|
EnsureInitialized();
|
|
return _towerRosterService.ReduceTowerEndurance(towerInstanceIds, enduranceLoss);
|
|
}
|
|
|
|
public bool TryGetSaleCandidate(long itemId, out PlayerInventorySaleCandidate candidate)
|
|
{
|
|
EnsureInitialized();
|
|
return _tradeService.TryGetSaleCandidate(itemId, out candidate);
|
|
}
|
|
|
|
public bool TrySellItems(IReadOnlyCollection<long> itemIds, out PlayerInventorySaleResult result)
|
|
{
|
|
EnsureInitialized();
|
|
return _tradeService.TrySellItems(itemIds, out result);
|
|
}
|
|
|
|
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);
|
|
_tradeService ??= new PlayerInventoryTradeService(_queryModel, _commandModel);
|
|
}
|
|
}
|
|
}
|