TH1/Unity/Assets/Scripts/TH1_UI/View/Interaction/UIInteractionCityUpgradeView.cs
2025-08-26 14:32:20 +08:00

104 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using Logic;
using Logic.Action;
using Logic.Multilingual;
using RuntimeData;
using TH1_Logic.Core;
using TH1_UI.View.Announce;
using TMPro;
using UnityEngine;
namespace TH1_UI.View.Interaction
{
public class UIInteractionCityUpgradeView : Base.View
{
public Transform Title;
[Header("Interaction Items")]
[Tooltip("在Editor中将预制件内所有预置的Item拖拽到这里")]
public List<UIInteractionCityUpgradeItem> AllItems;
//当选择了一个选项item后执行的action
public Action<CommonActionId> OnChoiceMade;
public int CurrentCityID { get; private set; }
private Material _grayScaleMat;
private Dictionary<CommonActionId, UIInteractionCityUpgradeItem> _itemMap;
public ViDelegateAssisstant.Dele OnItemClick;
protected override void InitStart()
{
base.InitStart();
//step #2 建立actionId到每一个item选项所挂在的脚本的关联
if (AllItems == null) return;
_itemMap = new Dictionary<CommonActionId, UIInteractionCityUpgradeItem>();
foreach (var item in AllItems)
_itemMap.TryAdd(item.ChoiceId, item);
}
public void SetContent(int cid, int cityLevel)
{
CurrentCityID = cid;
//step #1 获取当前citylevel所有可能得选项
var validChoices = GetValidChoices(cid, cityLevel);
//step #2 将可能得选项显示true没有的选项显示false
foreach (var item in AllItems)
{
bool show = validChoices.TryGetValue(item.ChoiceId, out var _);
item.gameObject.SetActive(show);
if (!show) continue;
item.Setup(HandleItemClicked);
}
//step #3 修改title
var txt = Title.GetComponent<TextMeshProUGUI>();
var mul = Title.GetComponent<MultilingualTextMono>();
if (txt != null && mul != null
&& Main.MapData.CityMap.GetCityById((uint)cid, out var city)
&& Table.Instance.CivDataAssets.GetCityInfo(city.Name, out var cityInfo)
&& uint.TryParse(cityInfo.CityName, out var mid))
MultilingualManager.Instance.SetUIText(txt,mul.ID.ToString(),new List<string>(){MultilingualManager.Instance.GetMultilingualText(mid)});
}
private HashSet<CommonActionId> GetValidChoices(int cid, int level)
{
var ret = new HashSet<CommonActionId>();
var dict = ActionLogicFactory.GetActionLogicDict();
foreach (var actionId in dict.Keys)
{
if (actionId.ActionType != CommonActionType.CityLevelUpAction) continue;
switch (actionId.CityLevelUpActionType)
{
case CityLevelUpActionType.Explorer: if (level == 2) ret.Add(actionId); break;
case CityLevelUpActionType.Workshop: if (level == 2) ret.Add(actionId); break;
case CityLevelUpActionType.CityWall: if (level == 3) ret.Add(actionId); break;
case CityLevelUpActionType.CityWealth: if (level == 3) ret.Add(actionId); break;
case CityLevelUpActionType.Expand: if (level == 4) ret.Add(actionId); break;
case CityLevelUpActionType.Population: if (level == 4) ret.Add(actionId); break;
case CityLevelUpActionType.Park: if (level > 4) ret.Add(actionId); break;
case CityLevelUpActionType.BigGuy: if (level > 4) ret.Add(actionId); break;
}
}
return ret;
}
private void HandleItemClicked(CommonActionId choiceId)
{
OnChoiceMade?.Invoke(choiceId);
}
}
}