TH1/Unity/Assets/Scripts/TH1_UI/View/Interaction/UIInteractionCityUpgradeView.cs
2025-12-12 00:41:18 +08:00

149 lines
5.6 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;
using UnityEngine.UI;
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 OnInit()
{
base.OnInit();
//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);
//TODO 这里用legacy方法处理了根据不同阵营召唤不同bigguy替换bigguy图标的情况。今后要迭代处理
var actionId = item.GetComponent<ActionIdMono>().ActionId;
var selfPlayer = Main.MapData.PlayerMap.SelfPlayerData;
if (actionId.CityLevelUpActionType == CityLevelUpActionType.BigGuy)
{
//bigGuy特殊处理
var fullType = new UnitFullType(UnitType.BigGuy, GiantType.None, 0);
//处理辉夜阵营的竹林狼
if (selfPlayer.PlayerCivId == 1 && selfPlayer.PlayerForceId == 1)
fullType = new UnitFullType(UnitType.KaguyaFrenchWolf, GiantType.None, 0);
//处理蕾米阵营的小恶魔
if (selfPlayer.PlayerCivId == 0 && selfPlayer.PlayerForceId == 0)
fullType = new UnitFullType(UnitType.RemiliaEgyptianKoakuma, GiantType.None, 0);
Table.Instance.UnitTypeDataAssets.GetUnitTypeInfo(fullType, out var info);
Table.Instance.UnitTypeDataAssets.GetUnitSpriteByInfo(info,selfPlayer,out var sprite);
item.transform.Find("Image").GetComponent<Image>().sprite = sprite;
MultilingualManager.Instance.SetUIText(item.transform.Find("Text").GetComponent<TextMeshProUGUI>(),info.Name);
}
if (actionId.CityLevelUpActionType == CityLevelUpActionType.Expand)
{
var gridSpType = GridSpType.None;
if (selfPlayer.PlayerCivId == 1) gridSpType = GridSpType.KaguyaGrid;
var sprite = Table.Instance.GridAndResourceDataAssets.GetGroundSprite(
Table.Instance.TransCivIdToCivEnum(selfPlayer.PlayerCivId), gridSpType);
item.transform.Find("Image1").GetComponent<Image>().sprite = sprite;
item.transform.Find("Image2").GetComponent<Image>().sprite = sprite;
item.transform.Find("Image3").GetComponent<Image>().sprite = sprite;
item.transform.Find("Image4").GetComponent<Image>().sprite = sprite;
}
if (actionId.CityLevelUpActionType == CityLevelUpActionType.Population)
{
var gridSpType = GridSpType.None;
Table.Instance.PlayerDataAssets.GetPlayerInfoByCivId(selfPlayer.PlayerCivId,
selfPlayer.PlayerForceId, out var info);
var sprite = info?.PopulationSprite ?? null;
item.transform.Find("Image1").GetComponent<Image>().sprite = sprite;
item.transform.Find("Image2").GetComponent<Image>().sprite = sprite;
item.transform.Find("Image3").GetComponent<Image>().sprite = sprite;
}
}
//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);
}
}
}