77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using GeometryTD.CustomUtility;
|
|
using GeometryTD.Definition;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public sealed class ShopFormUseCase : IUIUseCase
|
|
{
|
|
private const int GoodsCount = 4;
|
|
|
|
private readonly List<GoodsItemRawData> _currentGoods = new List<GoodsItemRawData>(GoodsCount);
|
|
|
|
public bool PrepareForOpen(int runSeed = 0, int sequenceIndex = -1)
|
|
{
|
|
if (GameEntry.InventoryGeneration == null)
|
|
{
|
|
Log.Warning("ShopFormUseCase.PrepareForOpen() inventory generation component is null.");
|
|
return false;
|
|
}
|
|
|
|
_currentGoods.Clear();
|
|
_currentGoods.AddRange(GameEntry.InventoryGeneration.BuildShopGoods(GoodsCount, runSeed, sequenceIndex));
|
|
return _currentGoods.Count == GoodsCount;
|
|
}
|
|
|
|
public ShopFormRawData CreateInitialModel()
|
|
{
|
|
return new ShopFormRawData
|
|
{
|
|
PlayerGold = GameEntry.PlayerInventory != null ? GameEntry.PlayerInventory.Gold : 0,
|
|
GoodsItems = new List<GoodsItemRawData>(_currentGoods)
|
|
};
|
|
}
|
|
|
|
public bool TryPurchase(int goodsIndex, out ShopFormRawData updatedRawData)
|
|
{
|
|
updatedRawData = null;
|
|
if (goodsIndex < 0 || goodsIndex >= _currentGoods.Count)
|
|
{
|
|
Log.Warning("ShopFormUseCase.TryPurchase() goods index is invalid: {0}", goodsIndex);
|
|
return false;
|
|
}
|
|
|
|
if (GameEntry.PlayerInventory == null)
|
|
{
|
|
Log.Warning("ShopFormUseCase.TryPurchase() player inventory is null.");
|
|
return false;
|
|
}
|
|
|
|
GoodsItemRawData goodsItem = _currentGoods[goodsIndex];
|
|
if (goodsItem == null || goodsItem.SourceItem == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (goodsItem.IsPurchased)
|
|
{
|
|
Log.Warning("ShopFormUseCase.TryPurchase() goods item {0} already purchased.", goodsIndex);
|
|
return false;
|
|
}
|
|
|
|
if (!GameEntry.PlayerInventory.TryPurchaseComponent(goodsItem.SourceItem, goodsItem.Price))
|
|
{
|
|
Log.Warning("ShopFormUseCase.TryPurchase() failed. Purchase command was rejected for goods item {0}.", goodsIndex);
|
|
return false;
|
|
}
|
|
|
|
goodsItem.IsPurchased = true;
|
|
updatedRawData = CreateInitialModel();
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|