129 lines
4.1 KiB
C#
129 lines
4.1 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|