添加商店 UI
This commit is contained in:
@@ -0,0 +1,360 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GameFramework.DataTable;
|
||||
using GeometryTD.CustomUtility;
|
||||
using GeometryTD.DataTable;
|
||||
using GeometryTD.Definition;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public sealed class ShopFormUseCase : IUIUseCase
|
||||
{
|
||||
private const int GoodsCount = 4;
|
||||
private long _nextTempInstanceId = 1000000;
|
||||
|
||||
private readonly List<GoodsItemRawData> _currentGoods = new List<GoodsItemRawData>(GoodsCount);
|
||||
private readonly List<DRShopPrice> _shopPriceRows = new List<DRShopPrice>();
|
||||
|
||||
private IDataTable<DRShopPrice> _shopPriceTable;
|
||||
private IDataTable<DRMuzzleComp> _muzzleCompTable;
|
||||
private IDataTable<DRBearingComp> _bearingCompTable;
|
||||
private IDataTable<DRBaseComp> _baseCompTable;
|
||||
|
||||
public bool PrepareForOpen()
|
||||
{
|
||||
if (!EnsureTables())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_currentGoods.Clear();
|
||||
for (int i = 0; i < GoodsCount; i++)
|
||||
{
|
||||
if (!TryBuildRandomGoodsItem(i, out GoodsItemRawData goodsItem))
|
||||
{
|
||||
Log.Warning("ShopFormUseCase.PrepareForOpen() failed to build goods item {0}.", i);
|
||||
return false;
|
||||
}
|
||||
|
||||
_currentGoods.Add(goodsItem);
|
||||
}
|
||||
|
||||
return _currentGoods.Count == GoodsCount;
|
||||
}
|
||||
|
||||
public ShopFormRawData CreateInitialModel()
|
||||
{
|
||||
return new ShopFormRawData
|
||||
{
|
||||
PlayerGold = GameEntry.PlayerInventory != null ? GameEntry.PlayerInventory.Gold : 0,
|
||||
GoodsItems = CloneGoodsItems(_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.TryConsumeGold(goodsItem.Price))
|
||||
{
|
||||
Log.Warning("ShopFormUseCase.TryPurchase() failed. Not enough gold for goods item {0}.", goodsIndex);
|
||||
return false;
|
||||
}
|
||||
|
||||
BackpackInventoryData inventoryDelta = WrapSingleItem(goodsItem.SourceItem);
|
||||
GameEntry.PlayerInventory.MergeInventory(inventoryDelta);
|
||||
goodsItem.IsPurchased = true;
|
||||
updatedRawData = CreateInitialModel();
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool EnsureTables()
|
||||
{
|
||||
_shopPriceTable ??= GameEntry.DataTable.GetDataTable<DRShopPrice>();
|
||||
_muzzleCompTable ??= GameEntry.DataTable.GetDataTable<DRMuzzleComp>();
|
||||
_bearingCompTable ??= GameEntry.DataTable.GetDataTable<DRBearingComp>();
|
||||
_baseCompTable ??= GameEntry.DataTable.GetDataTable<DRBaseComp>();
|
||||
|
||||
if (_shopPriceTable == null || _muzzleCompTable == null || _bearingCompTable == null || _baseCompTable == null)
|
||||
{
|
||||
Log.Warning("ShopFormUseCase.EnsureTables() failed. Missing required data tables.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_shopPriceRows.Count <= 0)
|
||||
{
|
||||
DRShopPrice[] rows = _shopPriceTable.GetAllDataRows();
|
||||
if (rows == null || rows.Length <= 0)
|
||||
{
|
||||
Log.Warning("ShopFormUseCase.EnsureTables() failed. Shop price table is empty.");
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var price in rows)
|
||||
{
|
||||
if (price != null)
|
||||
{
|
||||
_shopPriceRows.Add(price);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _shopPriceRows.Count > 0 &&
|
||||
_muzzleCompTable.Count > 0 &&
|
||||
_bearingCompTable.Count > 0 &&
|
||||
_baseCompTable.Count > 0;
|
||||
}
|
||||
|
||||
private bool TryBuildRandomGoodsItem(int goodsIndex, out GoodsItemRawData goodsItem)
|
||||
{
|
||||
goodsItem = null;
|
||||
TowerCompItemData sourceItem = BuildRandomComponentItem();
|
||||
if (sourceItem == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
goodsItem = new GoodsItemRawData
|
||||
{
|
||||
GoodsIndex = goodsIndex,
|
||||
Title = sourceItem.Name,
|
||||
TypeText = BuildTypeText(sourceItem.SlotType),
|
||||
Description = BuildDescription(sourceItem),
|
||||
Price = ResolveRandomPrice(sourceItem.Rarity),
|
||||
Tags = sourceItem.Tags != null ? (TagType[])sourceItem.Tags.Clone() : Array.Empty<TagType>(),
|
||||
IconAreaContext = BuildIconAreaContext(sourceItem),
|
||||
SourceItem = sourceItem,
|
||||
IsPurchased = false
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
private TowerCompItemData BuildRandomComponentItem()
|
||||
{
|
||||
int slotRoll = UnityEngine.Random.Range(0, 3);
|
||||
DRShopPrice priceRow = _shopPriceRows[UnityEngine.Random.Range(0, _shopPriceRows.Count)];
|
||||
RarityType rarity = priceRow != null ? priceRow.Rarity : RarityType.White;
|
||||
|
||||
switch (slotRoll)
|
||||
{
|
||||
case 0:
|
||||
return BuildRandomMuzzleItem(rarity);
|
||||
case 1:
|
||||
return BuildRandomBearingItem(rarity);
|
||||
default:
|
||||
return BuildRandomBaseItem(rarity);
|
||||
}
|
||||
}
|
||||
|
||||
private MuzzleCompItemData BuildRandomMuzzleItem(RarityType rarity)
|
||||
{
|
||||
DRMuzzleComp[] rows = _muzzleCompTable.GetAllDataRows();
|
||||
DRMuzzleComp config = rows[UnityEngine.Random.Range(0, rows.Length)];
|
||||
return new MuzzleCompItemData
|
||||
{
|
||||
InstanceId = _nextTempInstanceId++,
|
||||
ConfigId = config.Id,
|
||||
Name = config.Name,
|
||||
Rarity = rarity,
|
||||
Endurance = 100f,
|
||||
Constraint = config.Constraint,
|
||||
Tags = config.PossibleTag != null ? (TagType[])config.PossibleTag.Clone() : Array.Empty<TagType>(),
|
||||
AttackDamage = config.AttackDamage != null ? (int[])config.AttackDamage.Clone() : Array.Empty<int>(),
|
||||
DamageRandomRate = config.DamageRandomRate,
|
||||
AttackMethodType = config.AttackMethodType
|
||||
};
|
||||
}
|
||||
|
||||
private BearingCompItemData BuildRandomBearingItem(RarityType rarity)
|
||||
{
|
||||
DRBearingComp[] rows = _bearingCompTable.GetAllDataRows();
|
||||
DRBearingComp config = rows[UnityEngine.Random.Range(0, rows.Length)];
|
||||
return new BearingCompItemData
|
||||
{
|
||||
InstanceId = _nextTempInstanceId++,
|
||||
ConfigId = config.Id,
|
||||
Name = config.Name,
|
||||
Rarity = rarity,
|
||||
Endurance = 100f,
|
||||
Constraint = config.Constraint,
|
||||
Tags = config.PossibleTag != null ? (TagType[])config.PossibleTag.Clone() : Array.Empty<TagType>(),
|
||||
RotateSpeed = config.RotateSpeed != null ? (float[])config.RotateSpeed.Clone() : Array.Empty<float>(),
|
||||
AttackRange = config.AttackRange != null ? (float[])config.AttackRange.Clone() : Array.Empty<float>()
|
||||
};
|
||||
}
|
||||
|
||||
private BaseCompItemData BuildRandomBaseItem(RarityType rarity)
|
||||
{
|
||||
DRBaseComp[] rows = _baseCompTable.GetAllDataRows();
|
||||
DRBaseComp config = rows[UnityEngine.Random.Range(0, rows.Length)];
|
||||
return new BaseCompItemData
|
||||
{
|
||||
InstanceId = _nextTempInstanceId++,
|
||||
ConfigId = config.Id,
|
||||
Name = config.Name,
|
||||
Rarity = rarity,
|
||||
Endurance = 100f,
|
||||
Constraint = config.Constraint,
|
||||
Tags = config.PossibleTag != null ? (TagType[])config.PossibleTag.Clone() : Array.Empty<TagType>(),
|
||||
AttackSpeed = config.AttackSpeed != null ? (float[])config.AttackSpeed.Clone() : Array.Empty<float>(),
|
||||
AttackPropertyType = config.AttackPropertyType
|
||||
};
|
||||
}
|
||||
|
||||
private int ResolveRandomPrice(RarityType rarity)
|
||||
{
|
||||
for (int i = 0; i < _shopPriceRows.Count; i++)
|
||||
{
|
||||
DRShopPrice row = _shopPriceRows[i];
|
||||
if (row != null && row.Rarity == rarity)
|
||||
{
|
||||
int min = Mathf.Max(0, row.MinPrice);
|
||||
int max = Mathf.Max(min, row.MaxPrice);
|
||||
return UnityEngine.Random.Range(min, max + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static IconAreaContext BuildIconAreaContext(TowerCompItemData item)
|
||||
{
|
||||
return new IconAreaContext
|
||||
{
|
||||
Rarity = item.Rarity,
|
||||
ComponentSlotType = item.SlotType,
|
||||
Color = IconColorGenerator.GenerateForComponent(item)
|
||||
};
|
||||
}
|
||||
|
||||
private static string BuildTypeText(TowerCompSlotType slotType)
|
||||
{
|
||||
return slotType switch
|
||||
{
|
||||
TowerCompSlotType.Muzzle => "枪口组件",
|
||||
TowerCompSlotType.Bearing => "轴承组件",
|
||||
TowerCompSlotType.Base => "底座组件",
|
||||
_ => "组件"
|
||||
};
|
||||
}
|
||||
|
||||
private static string BuildDescription(TowerCompItemData item)
|
||||
{
|
||||
if (item is MuzzleCompItemData muzzleComp)
|
||||
{
|
||||
return ItemDescUtility.BuildMuzzleDesc(muzzleComp);
|
||||
}
|
||||
|
||||
if (item is BearingCompItemData bearingComp)
|
||||
{
|
||||
return ItemDescUtility.BuildBearingDesc(bearingComp);
|
||||
}
|
||||
|
||||
if (item is BaseCompItemData baseComp)
|
||||
{
|
||||
return ItemDescUtility.BuildBaseDesc(baseComp);
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private static BackpackInventoryData WrapSingleItem(TowerCompItemData item)
|
||||
{
|
||||
BackpackInventoryData inventory = new BackpackInventoryData();
|
||||
switch (item)
|
||||
{
|
||||
case MuzzleCompItemData muzzleComp:
|
||||
inventory.MuzzleComponents.Add(InventoryCloneUtility.CloneMuzzleComp(muzzleComp));
|
||||
break;
|
||||
case BearingCompItemData bearingComp:
|
||||
inventory.BearingComponents.Add(InventoryCloneUtility.CloneBearingComp(bearingComp));
|
||||
break;
|
||||
case BaseCompItemData baseComp:
|
||||
inventory.BaseComponents.Add(InventoryCloneUtility.CloneBaseComp(baseComp));
|
||||
break;
|
||||
}
|
||||
|
||||
return inventory;
|
||||
}
|
||||
|
||||
private static List<GoodsItemRawData> CloneGoodsItems(List<GoodsItemRawData> source)
|
||||
{
|
||||
List<GoodsItemRawData> result = new List<GoodsItemRawData>(source?.Count ?? 0);
|
||||
if (source == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
result.Add(new GoodsItemRawData
|
||||
{
|
||||
GoodsIndex = item.GoodsIndex,
|
||||
Title = item.Title,
|
||||
TypeText = item.TypeText,
|
||||
Description = item.Description,
|
||||
Price = item.Price,
|
||||
Tags = item.Tags != null ? (TagType[])item.Tags.Clone() : Array.Empty<TagType>(),
|
||||
IconAreaContext = item.IconAreaContext == null
|
||||
? null
|
||||
: new IconAreaContext
|
||||
{
|
||||
Rarity = item.IconAreaContext.Rarity,
|
||||
ComponentSlotType = item.IconAreaContext.ComponentSlotType,
|
||||
Color = item.IconAreaContext.Color,
|
||||
Icon = item.IconAreaContext.Icon
|
||||
},
|
||||
SourceItem = CloneSourceItem(item.SourceItem),
|
||||
IsPurchased = item.IsPurchased
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static TowerCompItemData CloneSourceItem(TowerCompItemData item)
|
||||
{
|
||||
switch (item)
|
||||
{
|
||||
case MuzzleCompItemData muzzleComp:
|
||||
return InventoryCloneUtility.CloneMuzzleComp(muzzleComp);
|
||||
case BearingCompItemData bearingComp:
|
||||
return InventoryCloneUtility.CloneBearingComp(bearingComp);
|
||||
case BaseCompItemData baseComp:
|
||||
return InventoryCloneUtility.CloneBaseComp(baseComp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7f8df16c6f56467aa56bbd520d6f784e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user