geometry-tower-defense/Assets/GameMain/Scripts/CustomComponent/PlayerInventory/PlayerInventoryComponent.cs

162 lines
5.3 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;
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 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 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 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);
}
}
}