继续重构调整(UI)
- 引入 UniTask 和异步版本的 UIModule - 调整 Runtime/UIBase 和 Presentation 下的脚本命名空间到 SepCore.UI - 将原来的 5 层 UI 迁移到异步实现 - 规范各层的 UI 脚本命名
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SepCore.DataTable;
|
||||
using SepCore.Definition;
|
||||
using Entity;
|
||||
using Random = UnityEngine.Random;
|
||||
using UnityGameFramework.Runtime;
|
||||
using CustomUtility;
|
||||
using GameFramework.DataTable;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class LevelUpUseCase : IUIUseCase
|
||||
{
|
||||
private const int BaseRefreshPrice = 2;
|
||||
private readonly Action _onCompleted;
|
||||
private readonly DRLevelUpReward[] _allRewards;
|
||||
private readonly IDataTable<DRLevelRarity> _levelRarityTable;
|
||||
private readonly Dictionary<ItemRarity, List<DRLevelUpReward>> _rewardsByRarity = new();
|
||||
private readonly List<DRLevelUpReward> _validRewards = new();
|
||||
|
||||
private int _refreshCount;
|
||||
private LevelUpRawData _currentModel;
|
||||
|
||||
private readonly Player _player;
|
||||
|
||||
public LevelUpUseCase(Player player, Action onCompleted)
|
||||
{
|
||||
_player = player;
|
||||
_onCompleted = onCompleted;
|
||||
_allRewards = GameEntry.DataTable.GetDataTable<DRLevelUpReward>().ToArray();
|
||||
_levelRarityTable = GameEntry.DataTable.GetDataTable<DRLevelRarity>();
|
||||
|
||||
BuildRewardRarityPools();
|
||||
}
|
||||
|
||||
public LevelUpRawData CreateInitialModel(int count = 4)
|
||||
{
|
||||
_refreshCount = 0;
|
||||
|
||||
if (_player == null || _player.PendingLevelPoints <= 0)
|
||||
{
|
||||
_currentModel = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
_currentModel = BuildModel(BaseRefreshPrice, count);
|
||||
return _currentModel;
|
||||
}
|
||||
|
||||
public LevelUpRawData TryRefresh(int refreshCost, int count = 4)
|
||||
{
|
||||
if (_currentModel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_player == null || _player.Coin < refreshCost)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_player.Coin -= refreshCost;
|
||||
|
||||
_refreshCount++;
|
||||
_currentModel = BuildModel((_refreshCount + 1) * BaseRefreshPrice, count);
|
||||
return _currentModel;
|
||||
}
|
||||
|
||||
public LevelUpRawData SelectReward(int selectedIndex, int count = 4)
|
||||
{
|
||||
if (_currentModel?.Rewards == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (selectedIndex < 0 || selectedIndex >= _currentModel.Rewards.Count)
|
||||
{
|
||||
Log.Warning("LevelUpFormUseCase::SelectReward: Invalid index");
|
||||
return null;
|
||||
}
|
||||
|
||||
ApplyReward(_currentModel.Rewards[selectedIndex]);
|
||||
|
||||
if (--_player.PendingLevelPoints > 0)
|
||||
{
|
||||
_refreshCount = 0;
|
||||
_currentModel = BuildModel(BaseRefreshPrice, count);
|
||||
return _currentModel;
|
||||
}
|
||||
|
||||
_onCompleted?.Invoke();
|
||||
_currentModel = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
private LevelUpRawData BuildModel(int refreshPrice, int count)
|
||||
{
|
||||
if (_allRewards == null || _allRewards.Length == 0)
|
||||
{
|
||||
Log.Error("LevelUpFormUseCase::BuildModel(): _allRewards is empty");
|
||||
return null;
|
||||
}
|
||||
|
||||
int finalCount = count > 0 ? System.Math.Min(count, _allRewards.Length) : _allRewards.Length;
|
||||
List<DRLevelUpReward> selections = new List<DRLevelUpReward>(finalCount);
|
||||
|
||||
int currentLevel = _player != null ? _player.CurrentLevel : 1;
|
||||
for (int i = 0; i < finalCount; i++)
|
||||
{
|
||||
ItemRarity rarity = RarityUtility.SelectRarityForLevel(_levelRarityTable, currentLevel);
|
||||
DRLevelUpReward reward = PickRewardByRarity(rarity);
|
||||
if (reward == null)
|
||||
{
|
||||
Log.Warning("LevelUpFormUseCase::BuildModel(): No available reward for selection.");
|
||||
break;
|
||||
}
|
||||
|
||||
selections.Add(reward);
|
||||
}
|
||||
|
||||
return new LevelUpRawData
|
||||
{
|
||||
Rewards = selections,
|
||||
RefreshPrice = refreshPrice
|
||||
};
|
||||
}
|
||||
|
||||
private void ApplyReward(DRLevelUpReward reward)
|
||||
{
|
||||
if (reward == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_player == null)
|
||||
{
|
||||
Log.Warning("LevelUpFormUseCase::ApplyReward: Player is null");
|
||||
return;
|
||||
}
|
||||
|
||||
if (reward.Modifiers == null || reward.Modifiers.Length == 0)
|
||||
{
|
||||
Log.Warning("LevelUpFormUseCase::ApplyReward: Reward modifiers are empty");
|
||||
return;
|
||||
}
|
||||
|
||||
_player.AddProp(new PropItem(reward.Modifiers, reward.Rarity, reward.Title, reward.IconAssetName));
|
||||
}
|
||||
|
||||
private void BuildRewardRarityPools()
|
||||
{
|
||||
_rewardsByRarity.Clear();
|
||||
_validRewards.Clear();
|
||||
|
||||
if (_allRewards == null) return;
|
||||
|
||||
foreach (DRLevelUpReward reward in _allRewards)
|
||||
{
|
||||
if (reward == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemRarity rarity = reward.Rarity;
|
||||
_validRewards.Add(reward);
|
||||
|
||||
if (!_rewardsByRarity.TryGetValue(rarity, out List<DRLevelUpReward> list))
|
||||
{
|
||||
list = new List<DRLevelUpReward>();
|
||||
_rewardsByRarity.Add(rarity, list);
|
||||
}
|
||||
|
||||
list.Add(reward);
|
||||
}
|
||||
}
|
||||
|
||||
private DRLevelUpReward PickRewardByRarity(ItemRarity rarity)
|
||||
{
|
||||
if (_rewardsByRarity.TryGetValue(rarity, out List<DRLevelUpReward> list) && list.Count > 0)
|
||||
{
|
||||
return list[Random.Range(0, list.Count)];
|
||||
}
|
||||
|
||||
if (_validRewards.Count > 0)
|
||||
{
|
||||
return _validRewards[Random.Range(0, _validRewards.Count)];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user