TH1/Unity/Assets/Scripts/UI/BottomInfoUI.cs
2025-08-15 00:39:22 +08:00

1036 lines
47 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.Collections.Generic;
using Logic;
using Logic.Action;
using Logic.CrashSight;
using UnityEngine;
using TMPro; // 如果使用TextMeshPro
using RuntimeData;
using Logic.Multilingual;
using Logic.Skill;
using MongoDB.Driver.Linq;
using TH1Renderer;
using TH1Resource;
using UI;
using UI.HintUI;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;
public class BottomInfoUI// : MonoBehaviour
{
private MapData _mapData;
private Main _main;
public GameObject ROBottomInfoUI;
public GameObject
_skillInfo;
private GameObject
_unitBaseInfo,
_gridBaseInfo,
_cityBaseInfo,
_gridCamera,
_cityCamera,
_actionInfo,
_unitPreviewArea, //grid/unit预览框
_gridPreviewArea,
_cityPreviewArea,
_unitTitle,
_unitDesc,
_gridTitle,
_gridDesc,
_cityTitle,
_cityDesc,
_closeButton,
_actionArea,
_debugGrid,
_debugUnit,
_debugCity;
//public Tilemap borderTilemap;
//public GameObject unitPrefab;
//private GameObject bottomInfoRenderedUnit;//用来存放角色预览的临时对象
//缓存玩家的金币情况用来判断是否要刷新bottominfo界面
private int _playerWealthCache;
//动画控制数据
private float _slideDuration = 0.2f;
private float _slideTime;
private Vector2 _hiddenPosition = new Vector2(-530, 0);
private Vector2 _visiblePosition = new Vector2(0, 0);
//private Vector2[] _visiblePosition = { new Vector2(100, 93), new Vector2(100, 100) };
private Vector2 _startPos, _endPos;
private float _startAlpha, _endAlpha;
//private Vector2[] _size = { new Vector2(1300,300),new Vector2(1300,686)};
//private Vector2[] _size = { new Vector2(1300,686),new Vector2(1300,686)};
private bool _isAnimating = false; // 动画是否正在进行
private bool _isShow = false;
public Sprite ActionBGAvailable;
public Sprite ActionBGExpensive;
public Sprite ActionBGUnavailable;
//-------- 表现层数据UI部分 --------//
public bool UIBottomInfoStatus = false;
public bool UIBottomInfoIsGrid = false;
public bool UIBottomInfoHideFirst = false;
public uint UIBottomInfoStatusObjectId;
public List<ActionLogicBase> ActionList;
public bool HasAction;
//--------- 对象的实际显示情况 ----------//
public BottomInfoUI(Main main,MapData mapData)
{
_main = main;
_mapData = mapData;
ROBottomInfoUI = UIManager.Instance.ROUIManager.transform.Find("BottomInfoPanel").gameObject;
_gridBaseInfo = ROBottomInfoUI.transform.Find("InfoGroup/GridBaseInfo").gameObject;
_unitBaseInfo = ROBottomInfoUI.transform.Find("InfoGroup/UnitBaseInfo").gameObject;
_cityBaseInfo = ROBottomInfoUI.transform.Find("InfoGroup/CityBaseInfo").gameObject;
_gridCamera = ROBottomInfoUI.transform.Find("InfoGroup/GridBaseInfo/PreviewArea/TileMapPreviewCamera").gameObject;
_cityCamera = ROBottomInfoUI.transform.Find("InfoGroup/CityBaseInfo/PreviewArea/TileMapPreviewCamera").gameObject;
_actionInfo = ROBottomInfoUI.transform.Find("InfoGroup/ActionInfo").gameObject;
_skillInfo = ROBottomInfoUI.transform.Find("InfoGroup/UnitBaseInfo/SkillIconArea").gameObject;
_closeButton = ROBottomInfoUI.transform.Find("CloseButton").gameObject; // 收起按钮
_debugGrid = UIManager.Instance.ROUIManager.transform.Find("DebugPanel/PanelGroup/Selected/DebugGrid").gameObject;
_debugUnit = UIManager.Instance.ROUIManager.transform.Find("DebugPanel/PanelGroup/Selected/DebugUnit").gameObject;
_debugCity = UIManager.Instance.ROUIManager.transform.Find("DebugPanel/PanelGroup/Selected/DebugCity").gameObject;
_unitTitle = ROBottomInfoUI.transform.Find("InfoGroup/UnitBaseInfo/Title").gameObject;
_gridTitle = ROBottomInfoUI.transform.Find("InfoGroup/GridBaseInfo/Title").gameObject;
_cityTitle = ROBottomInfoUI.transform.Find("InfoGroup/CityBaseInfo/Title").gameObject;
_unitDesc = ROBottomInfoUI.transform.Find("InfoGroup/UnitBaseInfo/Desc").gameObject;
_gridDesc = ROBottomInfoUI.transform.Find("InfoGroup/GridBaseInfo/Desc").gameObject;
_cityDesc = ROBottomInfoUI.transform.Find("InfoGroup/CityBaseInfo/Desc").gameObject;
_unitPreviewArea = ROBottomInfoUI.transform.Find("InfoGroup/UnitBaseInfo/PreviewArea").gameObject;
_gridPreviewArea = ROBottomInfoUI.transform.Find("InfoGroup/GridBaseInfo/PreviewArea/Grid").gameObject;
_cityPreviewArea = ROBottomInfoUI.transform.Find("InfoGroup/CityBaseInfo/PreviewArea/Grid").gameObject;
_actionArea = ROBottomInfoUI.transform.Find("InfoGroup/ActionInfo/ActionArea").gameObject;
ActionBGAvailable = Resources.Load<Sprite>("ArtResources/TH1UI/Common/bgAvailable");
ActionBGExpensive = Resources.Load<Sprite>("ArtResources/TH1UI/Common/bgExpensive");
ActionBGUnavailable = Resources.Load<Sprite>("ArtResources/TH1UI/Common/bgUnavailable");
Init();
}
public void Update()
{
if (_isAnimating)
{
_slideTime += Time.deltaTime / _slideDuration;
if (_isShow)
{
ROBottomInfoUI.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(_hiddenPosition, _visiblePosition, _slideTime);
ROBottomInfoUI.GetComponent<CanvasGroup>().alpha = Mathf.Lerp(0f, 1f, _slideTime);
}
else
{
ROBottomInfoUI.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(_visiblePosition, _hiddenPosition, _slideTime);
ROBottomInfoUI.GetComponent<CanvasGroup>().alpha = Mathf.Lerp(1f, 0f, _slideTime);
}
if (_slideTime >= 1f)
{
_isAnimating = false;
_slideTime = 0f;
if(!_isShow)
ROBottomInfoUI.SetActive(false);
}
else
return;
}
//如果需要显示
if (UIBottomInfoStatus)
{
//如果金钱发生了变化,需要更新金币的显示情况
if (_playerWealthCache != Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth)
{
_playerWealthCache = Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth;
UpdateInfo();
}
//如果不需要下滑再上滑,且已经显示了,退出,否则就说明要更新内容
if (!UIBottomInfoHideFirst && ROBottomInfoUI.activeSelf)
return;
//如果需要先下滑再上滑
if (UIBottomInfoHideFirst)
{
//设定播放下滑动画的初始参数
if (ROBottomInfoUI.activeSelf)
{
_isAnimating = true;
_isShow = false;
_slideTime = 0f;
}
UIBottomInfoHideFirst = false;
}
else
{
//设定播放上滑动画的初始参数
_isAnimating = true;
_isShow = true;
_slideTime = 0f;
UpdateInfo();
ROBottomInfoUI.SetActive(true);
}
}
//如果需要关闭
else
{
//如果已经关闭了,退出
if (!ROBottomInfoUI.activeSelf)
return;
_isAnimating = true;
_isShow = false;
_slideTime = 0f;
}
}
public void Init()
{
ROBottomInfoUI.GetComponent<RectTransform>().anchoredPosition = _hiddenPosition;
ROBottomInfoUI.GetComponent<CanvasGroup>().alpha = 0;
_isAnimating = false;
ROBottomInfoUI.SetActive(false);
//处理hint trigger然后需要根据玩家civ替换图标
foreach (Transform childAction in _actionArea.transform)
{
//step #0 获取基础信息
var timg = childAction.Find("Image").GetComponent<Image>();
var ttxt = childAction.Find("Text (TMP)").GetComponent<TextMeshProUGUI>();
var actionId = childAction.GetComponent<ActionIdMono>().ActionId;
var hintTrigger = childAction.GetComponent<HintTrigger>().DataProvider;
//step #1 处理hint trigger
hintTrigger.ActionIdData = actionId;
//step #2 处理需要根据文明替换资源的图标
//如果是根据文明变化的图标,就替换。否则,暂时就不动了,就交给默认设置好的
if (!Table.Instance.ActionDataAssets.GetActionInfo(actionId, out var actionInfo))
continue;
if (actionInfo == null)
continue;
if (actionInfo.VarientIcon)
{
timg.sprite = Table.Instance.QueryActionIconSprite(actionId,Main.MapData.PlayerMap.SelfPlayerData);
//timg.SetNativeSize();
//childAction.Find("Image").localScale = new Vector3(0.3f, 0.3f, 0.3f);
//childAction.Find("Image").localPosition = new Vector3(0f, -40f, 0f);
}
if (actionId.ActionType == CommonActionType.BuildWonder)
{
Table.Instance.GridAndResourceDataAssets.GetWonderInfoByType(actionId.WonderType,
_mapData.PlayerMap.SelfPlayerData,out var c);
timg.sprite = c.Sprite;
MultilingualManager.Instance.SetUIText(ttxt, c.Name);
//childAction.Find("Image").localScale = new Vector3(0.2f, 0.2f, 0.2f);
//childAction.Find("Image").localPosition = new Vector3(0f, -130f, 0f);
}
}
//监听所有的Action按钮
ActionAreaClickHandler();
_closeButton.GetComponent<Button>().onClick.RemoveAllListeners();
_closeButton.GetComponent<Button>().onClick.AddListener(SetBottomInfoHide);
}
public void UpdateInfoUnit()
{
Main.MapData.UnitMap.GetUnitDataByUnitId(UIBottomInfoStatusObjectId,out var unitData);
Main.MapData.GetPlayerDataByUnitId(unitData.Id,out var playerData);
Table.Instance.UnitTypeDataAssets.GetUnitSprite(Main.MapData,unitData, out var sprite);
//筹备unit预览格子
var t = _unitPreviewArea.transform.Find("UnitImage").GetComponent<Image>().sprite = sprite;
_cityBaseInfo.SetActive(false);
_gridBaseInfo.SetActive(false);
_unitBaseInfo.SetActive(true);
_skillInfo.SetActive(true);
//如果是己方单位考虑生成action
if (playerData.Id == Main.MapData.PlayerMap.SelfPlayerId)
{
var param = new CommonActionParams(mapData:_mapData,playerData:playerData,unitData:unitData,mainObjectType:MainObjectType.Unit);
HasAction = ActionLogicFactory.MainObjectCanShowAction(param, out ActionList);
_actionInfo.SetActive(HasAction);
if(HasAction)
GenerateActionOption();
}
string[] showText = Table.Instance.unitInfo(unitData);
if (!Table.Instance.UnitTypeDataAssets.GetUnitTypeInfo(unitData.UnitType, unitData.GiantType,unitData.UnitLevel, out var info))
{
LogSystem.LogError("BottomInfoUI can't find unittype dataasset");
return;
}
if (unitData.GetSkill(SkillType.CARRY, out var _))
{
if (!Table.Instance.UnitTypeDataAssets.GetUnitTypeInfo(unitData.CarryUnitType, unitData.CarryGiantType,unitData.CarryUnitLevel,
out var carryInfo))
{
LogSystem.LogError("BottomInfoUI can't find carryunittype dataasset");
return;
}
showText[0] += "(" + MultilingualManager.Instance.GetMultilingualText(uint.Parse(carryInfo.Name)) + ")";
}
if (unitData.Veteran) showText[0] += "(已升级)";
else if (!unitData.GetSkill(SkillType.STATIC,out var _))
showText[0] += " " + unitData.Exp + "/3";
if (unitData.UnitType == UnitType.Giant)
showText[0] += "Lv." +unitData.UnitLevel;
//MultilingualManager.Instance.SetUIText(_unitTitle.GetComponent<TextMeshProUGUI>(),info.Name);
_unitTitle.GetComponent<TextMeshProUGUI>().text = showText[0];
_unitDesc.GetComponent<TextMeshProUGUI>().text = showText[1];
_unitBaseInfo.transform.Find("AdviseInfo/Text").GetComponent<TextMeshProUGUI>().text = "<color=yellow>军事顾问建议:</color>" + showText[2];
_unitBaseInfo.transform.Find("DataInfo/Title1").GetComponent<TextMeshProUGUI>().text = unitData.Health + "/" + unitData.GetMaxHealth();
_unitBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text = unitData.GetAttackShowString(_mapData);
_unitBaseInfo.transform.Find("DataInfo/Title3").GetComponent<TextMeshProUGUI>().text = unitData.GetDefenseShowString(_mapData);
_unitBaseInfo.transform.Find("DataInfo/Title4").GetComponent<TextMeshProUGUI>().text = " " + unitData.Exp + "/3";
if (unitData.GetSkill(SkillType.STATIC, out var _))
{
var txt = _unitBaseInfo.transform.Find("DataInfo/Title4").GetComponent<TextMeshProUGUI>();
txt.text = "-/-";
if (unitData.UnitType == UnitType.Giant)
{
txt.text = playerData.giantExp[(uint)unitData.ChessType] + "/" + Table.Instance.GiantLevelNeedExp((int)unitData.UnitLevel);
}
}
_unitBaseInfo.transform.Find("DataInfo/Title5").GetComponent<TextMeshProUGUI>().text = unitData.GetMoveRange().ToString();
_unitBaseInfo.transform.Find("DataInfo/Title6").GetComponent<TextMeshProUGUI>().text = unitData.GetAttackRange().ToString();
//处理Skillinfo
var skillSet = new HashSet<SkillType>();
foreach (var skill in unitData.Skills)
skillSet.Add(skill.GetSkillType());
int rk = 0;
foreach (Transform child in _skillInfo.transform)
{
child.gameObject.SetActive(false);
var skillMono = child.GetComponent<SkillIdMono>();
if (skillMono == null) continue;
if (skillSet.Contains(skillMono.SkillTpe))
{
child.gameObject.SetActive(true);
var rect = child.GetComponent<RectTransform>();
if (rect != null)
rect.anchoredPosition = new Vector2(50 + rk * 60, -35);
rk++;
}
//设置skill的hinttrigger
var skillHintTrigger = child.GetComponent<HintTrigger>();
if (skillHintTrigger != null)
skillHintTrigger.DataProvider.SkillTypeData = skillMono.SkillTpe;
}
if (rk == 0)
{
_skillInfo.SetActive(false);
var rect = _unitBaseInfo.transform.Find("AdviseInfo").GetComponent<RectTransform>();
if (rect != null)
{
var tt = rect.anchoredPosition;
tt.y += 70;
rect.anchoredPosition = tt;
}
}
}
//更新预览框的内容
private void UpdateInfo()
{
foreach (Transform child in _gridPreviewArea.transform)
GameObject.Destroy(child.gameObject);
foreach (Transform child in _cityPreviewArea.transform)
GameObject.Destroy(child.gameObject);
//如果预览的是grid或者city
if (UIBottomInfoIsGrid && Main.MapData.GridMap.GetGridDataByGid(UIBottomInfoStatusObjectId,out var gridData))
{
//现获取对应的playerData
Main.MapData.GetPlayerDataByTerritoryGridId(UIBottomInfoStatusObjectId, out var player);
//如果是city
if (Main.MapData.GetCityDataByGid(gridData.Id, out var cityData))
{
GameObject clone =
GameObject.Instantiate(MapRenderer.Instance.ROGridMap[UIBottomInfoStatusObjectId].GetROGrid() as GameObject);
clone.transform.SetParent(_cityPreviewArea.transform);
clone.transform.localPosition = new Vector3(0,15,0);
clone.transform.Find("SelectHighlight").gameObject.SetActive(false);
SetLayerRecursively(clone, LayerMask.NameToLayer("Preview"));
_cityBaseInfo.SetActive(true);
_cityCamera.SetActive(true);
Timer.Instance.TimerRegister(_cityCamera, () => { _cityCamera.SetActive(false); }, 0.001f,"HideCamera");
_gridBaseInfo.SetActive(false);
_unitBaseInfo.SetActive(false);
var param = new CommonActionParams(mapData:_mapData,playerData:_mapData.PlayerMap.SelfPlayerData,cityData:cityData,mainObjectType:MainObjectType.City);
HasAction = ActionLogicFactory.MainObjectCanShowAction(param,out ActionList);
Table.Instance.CivDataAssets.GetCityInfo(cityData.Name,out var cityInfo);
_cityTitle.GetComponent<TextMeshProUGUI>().text =
MultilingualManager.Instance.GetMultilingualText(uint.Parse(cityInfo.CityName)) + " Lv." + cityData.Level;
MultilingualManager.Instance.SetUIText(_cityDesc.GetComponent<TextMeshProUGUI>(),cityInfo.CityDescription);
//显示城市边界
HashSet<uint> gridList = new HashSet<uint>();
cityData.Territory.GetAllTerritoryArea(gridList);
foreach (var gid in gridList)
{
if (_mapData.GridMap.GetGridDataByGid(gid, out var grid))
grid.CityBorderRenderMark = true;
}
//text1 城市所属
//如果是有主领土
Table.Instance.CivDataAssets.GetCivInfo(player.PlayerCivId, out var civInfo);
MultilingualManager.Instance.SetUIText(
_cityBaseInfo.transform.Find("DataInfo/Title1").GetComponent<TextMeshProUGUI>(), civInfo.CivName);
//如果是自己的城市
if (player.Id == _mapData.PlayerMap.SelfPlayerData.Id)
{
//text2 每回合收入
_cityBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text = "+" + Main.CityLogic.GetCityStarsPerTurn(_mapData,cityData) + " 每回合";
//text3 人口情况
_cityBaseInfo.transform.Find("DataInfo/Title3").GetComponent<TextMeshProUGUI>().text = "人口 " + _mapData.GetUnitCount(cityData.Id) + "/" + cityData.Level;
//text4 征税所情况
int pp = 0;
if (cityData.Workshop) pp++;
pp += cityData.ParkCount;
var par = new List<string>();
par.Add(pp.ToString());
MultilingualManager.Instance.SetUIText(_cityBaseInfo.transform.Find("DataInfo/Title4").GetComponent<TextMeshProUGUI>(),par);
//text5 联通情况
if (cityData.IsCapital)
_cityBaseInfo.transform.Find("DataInfo/Title5").GetComponent<TextMeshProUGUI>().text = "首都城市";
else if (cityData.IsConnectedCapital)
_cityBaseInfo.transform.Find("DataInfo/Title5").GetComponent<TextMeshProUGUI>().text = "已联通首都贸易";
else
_cityBaseInfo.transform.Find("DataInfo/Title5").GetComponent<TextMeshProUGUI>().text = "未联通首都贸易";
//text6 奢侈品情况
//_cityBaseInfo.transform.Find("DataInfo/Title6").GetComponent<TextMeshProUGUI>().text = "奢侈品 " + cityData.ParkCount + " 种";
_cityBaseInfo.transform.Find("AdviseInfo/Text").GetComponent<TextMeshProUGUI>().text = "<color=yellow>咲夜的建议:</color>" + "获得城市经验,提升城市等级,从而提高人口上限,获得更多金币。";
}
//如果是敌方的城市
else
{
_cityBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text = "-";
_cityBaseInfo.transform.Find("DataInfo/Title3").GetComponent<TextMeshProUGUI>().text = "-";
_cityBaseInfo.transform.Find("DataInfo/Title4").GetComponent<TextMeshProUGUI>().text = "-";
_cityBaseInfo.transform.Find("DataInfo/Title5").GetComponent<TextMeshProUGUI>().text = "-";
_cityBaseInfo.transform.Find("DataInfo/Title6").GetComponent<TextMeshProUGUI>().text = "-";
_cityBaseInfo.transform.Find("AdviseInfo/Text").GetComponent<TextMeshProUGUI>().text = "<color=yellow>军事顾问建议:</color>" + "攻下敌方城市,将富庶之地纳入自己的领土吧!";
}
}
//如果是grid
else
{
GameObject clone =
GameObject.Instantiate(MapRenderer.Instance.ROGridMap[UIBottomInfoStatusObjectId].GetROGrid() as GameObject);
clone.transform.SetParent(_gridPreviewArea.transform);
clone.transform.localPosition = new Vector3(0,15,0);
clone.transform.Find("SelectHighlight").gameObject.SetActive(false);
SetLayerRecursively(clone, LayerMask.NameToLayer("Preview"));
_cityBaseInfo.SetActive(false);
_gridBaseInfo.SetActive(true);
_gridCamera.SetActive(true);
Timer.Instance.TimerRegister(_gridCamera, () => { _gridCamera.SetActive(false); }, 0.001f,"HideCamera");
_unitBaseInfo.SetActive(false);
//text0 title和desc
var title = _gridTitle.GetComponent<TextMeshProUGUI>();
var desc = _gridDesc.GetComponent<TextMeshProUGUI>();
var param = new CommonActionParams(mapData:_mapData,_mapData.PlayerMap.SelfPlayerData,gridData:gridData,mainObjectType:MainObjectType.Grid);
HasAction = ActionLogicFactory.MainObjectCanShowAction(param,out ActionList);
string[] showText = Table.Instance.tileInfo(gridData);
_gridBaseInfo.transform.Find("AdviseInfo/Text").GetComponent<TextMeshProUGUI>().text = "<color=yellow>咲夜的建议:</color>" + showText[2];
var resource = gridData.Resource;
//处理隐藏没有科技情况下 metal、starfish、crop的情况
if (gridData.Resource == ResourceType.Crop)
if (!_mapData.PlayerMap.SelfPlayerData.TechTree.CheckIfHasTech(TechType.Organization) &&
!_mapData.PlayerMap.SelfPlayerData.TechTree.CheckIfHasTech(TechType.Farming))
resource = ResourceType.None;
if (gridData.Resource == ResourceType.Metal)
if (!_mapData.PlayerMap.SelfPlayerData.TechTree.CheckIfHasTech(TechType.Mining) &&
!_mapData.PlayerMap.SelfPlayerData.TechTree.CheckIfHasTech(TechType.Climbing))
resource = ResourceType.None;
if (gridData.Resource == ResourceType.Starfish)
if (!_mapData.PlayerMap.SelfPlayerData.TechTree.CheckIfHasTech(TechType.Navigation) &&
!_mapData.PlayerMap.SelfPlayerData.TechTree.CheckIfHasTech(TechType.Sailing))
resource = ResourceType.None;
//用玩家看得到的resource来获取rsourceInfo
if (!Table.Instance.GridAndResourceDataAssets.GetResourceInfo(resource, out var resourceInfo))
{
LogSystem.LogError("BottomUIInfo Cant' find resource Info!!!!");
return;
}
string levelSuffix = $" Lv.{gridData.buildingLevel}/{resourceInfo.MaxLevel}";
if (gridData.Resource == ResourceType.Wonder)
levelSuffix =
":" + MultilingualManager.Instance.GetMultilingualText(uint.Parse(Table.Instance.GridAndResourceDataAssets.GetWonderName(gridData.Wonder, player)));
//step #3 现在的到了显示在玩家面前的resource真实情况开始处理
if (resource != ResourceType.None)
{
MultilingualManager.Instance.SetUIText(
title,
resourceInfo.ResourceName,
(resourceInfo.HasLevel || resourceInfo.Resource == ResourceType.Wonder) ? new List<string> { levelSuffix } : null
);
MultilingualManager.Instance.SetUIText(desc,resourceInfo.ResourceDesc);
}
else if(gridData.Vegetation == Vegetation.Trees)
{
if (Table.Instance.GridAndResourceDataAssets.GetVegetationInfo(out var info))
{
MultilingualManager.Instance.SetUIText(title,info.ResourceName);
MultilingualManager.Instance.SetUIText(desc,info.ResourceDesc);
}
else
{
LogSystem.LogError("BottomInfoUI vegetation can't find in data asset");
}
}
else if (gridData.Feature == TerrainFeature.Mountain)
{
if (Table.Instance.GridAndResourceDataAssets.GetMountainInfo(out var info))
{
MultilingualManager.Instance.SetUIText(title,info.ResourceName);
MultilingualManager.Instance.SetUIText(desc,info.ResourceDesc);
}
else
{
LogSystem.LogError("BottomInfoUI mountain can't find in data asset");
}
}
else
{
if (Table.Instance.GridAndResourceDataAssets.GetTerrainInfo(gridData.Terrain,out var info))
{
MultilingualManager.Instance.SetUIText(title,info.ResourceName);
MultilingualManager.Instance.SetUIText(desc,info.ResourceDesc);
}
else
{
LogSystem.LogError("BottomInfoUI terrain can't find in data asset");
}
}
//step #4 设置下方的信息表格
//text1 城市
//如果是有主领土
if (player != null)
{
Table.Instance.CivDataAssets.GetCivInfo(player.PlayerCivId, out var civInfo);
MultilingualManager.Instance.SetUIText(
_gridBaseInfo.transform.Find("DataInfo/Title1").GetComponent<TextMeshProUGUI>(), civInfo.CivName);
}
//如果是无主领土
else
MultilingualManager.Instance.SetUIText(
_gridBaseInfo.transform.Find("DataInfo/Title1").GetComponent<TextMeshProUGUI>(), Table.Instance.TextDataAssets.BottomInfoGridNoPlayerGridSubTitle1);
//_gridBaseInfo.transform.Find("DataInfo/Title1").GetComponent<TextMeshProUGUI>().text = "-";
//text2 地貌
_gridBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text =
gridData.Terrain switch
{
TerrainType.Land => "陆地",
TerrainType.ShallowSea => "浅海",
TerrainType.DeepSea => "深海",
_ => "外太空"
};
if(gridData.Feature == TerrainFeature.Mountain)
_gridBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text +=
",山脉";
if(gridData.Feature == TerrainFeature.Road)
_gridBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text +=
",道路";
if(gridData.Vegetation == Vegetation.Trees)
_gridBaseInfo.transform.Find("DataInfo/Title2").GetComponent<TextMeshProUGUI>().text +=
",树林";
//text3 建筑
MultilingualManager.Instance.SetUIText(_gridBaseInfo.transform.Find("DataInfo/Title3").GetComponent<TextMeshProUGUI>(),
Table.Instance.GridAndResourceDataAssets.GetResourceName(gridData.Resource));
MultilingualManager.Instance.SetUIText(
_gridBaseInfo.transform.Find("DataInfo/Title3").GetComponent<TextMeshProUGUI>(),
resourceInfo.ResourceName,
(resourceInfo.HasLevel || resource == ResourceType.Wonder) ? new List<string> { levelSuffix } : null
);
//text4 资源
_gridBaseInfo.transform.Find("DataInfo/Title4").GetComponent<TextMeshProUGUI>().text =
gridData.Resource switch
{
ResourceType.Fish => "渔产",
ResourceType.Starfish => "鲸鱼",
ResourceType.Animal => "动物",
ResourceType.Fruit => "蔬果",
ResourceType.Metal => "矿产",
ResourceType.Crop => "庄稼",
_ => "-"
};
}
_actionInfo.SetActive(HasAction);
if(HasAction)
GenerateActionOption();
}
//如果是unit
else
UpdateInfoUnit();
//更新Debug模块的显示
UpdateDebugInfo();
}
//让每个action挂一个点击监听事件
public void ActionAreaClickHandler()
{
ActionClickedEvent[] items = _actionArea.GetComponentsInChildren<ActionClickedEvent>(true);//这里设为true会遍历所有inactive的对象
foreach (ActionClickedEvent item in items)
{
item.ClearListeners();
item.OnItemClicked += ActionAreaClicked;
}
}
//处理一个action被点击后的情况
public void ActionAreaClicked(GameObject clickedItem)
{
CommonActionId fakeActionId = clickedItem.GetComponent<ActionIdMono>().ActionId;
//如果钱不够,退出
if(Table.Instance.ActionDataAssets.GetActionInfo(fakeActionId, out var info) && info.Cost > Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth)
return;
//如果没科技,退出
/*if (!Main.MapData.PlayerMap.SelfPlayerData.TechTree.CheckActionCan(fakeActionId) )
return;*/
UnitData unitData = null;
CityData cityData = null;
GridData gridData = null;
MainObjectType mainObjectType = MainObjectType.None;
if (UIBottomInfoIsGrid)
{
mainObjectType = MainObjectType.Grid;
//如果是训练单位
if (fakeActionId.ActionType == CommonActionType.TrainUnit)
{
//如果是城市训练单位
if(Main.MapData.GetCityDataByGid(UIBottomInfoStatusObjectId,out cityData))
{
//城市满人口
if (Main.MapData.CheckIfCityFullPopulation(cityData)) return;
mainObjectType = MainObjectType.City;
}
else if (Main.MapData.GetCityDataByTerritoryGid(UIBottomInfoStatusObjectId, out cityData))
{
//城市满人口
if (Main.MapData.CheckIfCityFullPopulation(cityData)) return;
}
}
Main.MapData.GridMap.GetGridDataByGid(UIBottomInfoStatusObjectId,out gridData);
}
else
{
Main.MapData.UnitMap.GetUnitDataByUnitId(UIBottomInfoStatusObjectId,out unitData);
mainObjectType = MainObjectType.Unit;
}
CommonActionParams actionParams = new CommonActionParams(_mapData,
Main.MapData.PlayerMap.SelfPlayerData,unitData,cityData,gridData,null,null,mainObjectType);
//Debug.Log($"{actionId.ActionType},{actionId.WonderType},{actionId.ResourceType},{actionId.FeatureType},{actionId.UnitType},{actionId.Vegetation}");
//如果成功执行,隐藏当前面板
if (ActionLogicFactory.GetActionLogic(fakeActionId).Execute(actionParams, false))
{
//执行视觉
ActionLogicFactory.GetActionLogic(fakeActionId).ExecuteViewAfter(actionParams);
SetBottomInfoHide();
_main.MapInteractionLogic.CancelAllHighlight();
}
//Debug.Log($"父对象收到通知:{clickedItem.name} 被点击了");
}
//生产选项板的若干选项
public void GenerateActionOption()
{
int rk = 0;
//处理城市满员提示
var fullPopHint = ROBottomInfoUI.transform.Find("InfoGroup/ActionInfo/FullPopHint").gameObject;
fullPopHint.SetActive(false);
foreach (Transform child in _actionArea.transform)
{
CommonActionId tActionId = child.GetComponent<ActionIdMono>().ActionId;
child.gameObject.SetActive(false);
foreach (var actionLogic in ActionList){
if (actionLogic.CheckEqualActionId(tActionId))
{
child.gameObject.SetActive(true);
RectTransform rectTransform = child.GetComponent<RectTransform>();
if (rectTransform != null)
{
float x = (rk % 4) * 110;
float y = (rk / 4) * (-130);
rectTransform.anchoredPosition = new Vector2(x, y);
}
rk++;
TextMeshProUGUI textUIComponent = child.gameObject.transform.Find("costNumber")
.GetComponent<TextMeshProUGUI>();
Image starImage = child.gameObject.transform.Find("star").GetComponent<Image>();
starImage.gameObject.SetActive(true);
textUIComponent.gameObject.SetActive(true);
Image backImage = child.gameObject.GetComponent<Image>();
textUIComponent.color = Color.white;
backImage.sprite = ActionBGAvailable;
//如果是city的action
if (UIBottomInfoIsGrid)
{
//如果科技未解锁 且不是firstHero
if (!Main.MapData.PlayerMap.SelfPlayerData.TechTree.CheckActionCan(tActionId)
&& (tActionId.GiantType != Main.MapData.PlayerMap.SelfPlayerData.FirstHero))
{
starImage.gameObject.SetActive(false);
textUIComponent.gameObject.SetActive(false);
backImage.sprite = ActionBGUnavailable;
}
//如果是firstHero或者其他hero要考虑因为冷却时间导致的锁
if (tActionId.GiantType != GiantType.None)
{
Table.Instance.UnitTypeDataAssets.GetUnitTypeInfo(UnitType.Giant, tActionId.GiantType, 0,
out var info);
//如果在冷却死亡惩罚时间内,也要显示灰色,并且加上漏斗
if (info != null && Main.MapData.PlayerMap.SelfPlayerData.giantPenalty[(uint)info.ChessType] > 0)
{
Transform cd = child.Find("ColdDown");
TextMeshProUGUI txt = cd?.Find("Text")?.GetComponent<TextMeshProUGUI>();
if(cd != null) cd.gameObject.SetActive(true);
if (txt != null) txt.text = Main.MapData.PlayerMap.SelfPlayerData.giantPenalty[(uint)info.ChessType].ToString();
backImage.sprite = ActionBGUnavailable;
}
else
{
Transform cd = child.Find("ColdDown");
if(cd != null) cd.gameObject.SetActive(false);
backImage.sprite = ActionBGAvailable;
}
}
CityData cityData;
//如果是一个满员城市在grid上生产单位要额外判断一下这个是不是生产单位的行为
if (!Main.MapData.GetCityDataByGid(UIBottomInfoStatusObjectId,out cityData)){
if(Main.MapData.GetCityDataByTerritoryGid(UIBottomInfoStatusObjectId,out cityData)
&& Main.MapData.CheckIfCityFullPopulation(cityData)
&& tActionId.ActionType == CommonActionType.TrainUnit)
{
textUIComponent.color = Color.gray;
backImage.sprite = ActionBGUnavailable;
starImage.gameObject.SetActive(false);
textUIComponent.gameObject.SetActive(false);
fullPopHint.SetActive(true);
}
}
//如果是一个满员城市在市中心生产单位(不用在判断是不是生产单位了)
else if (Main.MapData.CheckIfCityFullPopulation(cityData) && tActionId.ActionType == CommonActionType.TrainUnit)
{
textUIComponent.color = Color.gray;
backImage.sprite = ActionBGUnavailable;
starImage.gameObject.SetActive(false);
textUIComponent.gameObject.SetActive(false);
fullPopHint.SetActive(true);
}
//如果钱不够 并且不是满员城市的生产单位行为
if (Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth < actionLogic.GetCost() &&
!(Main.MapData.CheckIfCityFullPopulation(cityData) && tActionId.ActionType == CommonActionType.TrainUnit))
{
textUIComponent.color = Color.red;
backImage.sprite = ActionBGExpensive;
}
break;
}
//如果是unit的action
else
{
starImage.gameObject.SetActive(false);
textUIComponent.gameObject.SetActive(false);
//如果是变形为其他type的单位
if (tActionId.UnitType !=UnitType.None)
{
starImage.gameObject.SetActive(true);
textUIComponent.gameObject.SetActive(true);
if (Main.MapData.PlayerMap.SelfPlayerData.PlayerWealth < actionLogic.GetCost())
{
textUIComponent.color = Color.red;
backImage.sprite = ActionBGExpensive;
}
}
}
}
}
}
//如果数量小于3个要居中显示
if (rk < 4)
{
int rkk = 0;
foreach (Transform child in _actionArea.transform)
{
if (child.gameObject.activeSelf)
{
RectTransform rectTransform = child.GetComponent<RectTransform>();
if (rectTransform != null)
{
float x = rkk * 110 + (4 - rk) * 55f;
rectTransform.anchoredPosition = new Vector2(x, 0);
}
rkk++;
}
}
}
}
//隐藏BottomInfoHid
public void SetBottomInfoHide()
{
UIBottomInfoStatus = false;
}
public void SetBottomInfoShowGrid(uint gridId)
{
UIBottomInfoHideFirst = UIBottomInfoStatus;
UIBottomInfoStatus = true;
UIBottomInfoIsGrid = true;
UIBottomInfoStatusObjectId = gridId;
}
public void SetBottomInfoShowUnit(uint unitId)
{
UIBottomInfoHideFirst = UIBottomInfoStatus;
UIBottomInfoStatus = true;
UIBottomInfoIsGrid = false;
UIBottomInfoStatusObjectId = unitId;
}
//更新Debug模块的信息
public void UpdateDebugInfo()
{
if (!DebugCenter.Instance.DebugMode)
return;
if (_mapData == null)
return;
//如果是城市格子
if (UIBottomInfoIsGrid && _mapData.GetCityDataByGid(UIBottomInfoStatusObjectId,out var city))
{
_debugGrid.SetActive(false);
_debugUnit.SetActive(false);
_debugCity.SetActive(true);
}
//如果是非城市格子
else if (UIBottomInfoIsGrid)
{
_debugGrid.SetActive(true);
_debugUnit.SetActive(false);
_debugCity.SetActive(false);
if (!Main.MapData.GridMap.GetGridDataByGid(UIBottomInfoStatusObjectId, out var gridData))
return;
_debugGrid.transform.Find("BuildingLevelValue").GetComponent<TextMeshProUGUI>().text = gridData.buildingLevel.ToString();
}
//如果是unit
else{
_debugGrid.SetActive(false);
_debugUnit.SetActive(true);
_debugCity.SetActive(false);
// 获取单位数据
if (!Main.MapData.UnitMap.GetUnitDataByUnitId(UIBottomInfoStatusObjectId, out var unitData))
return;
// 填充单位类型信息
_debugUnit.transform.Find("UnitTypeValue").GetComponent<TextMeshProUGUI>().text = unitData.UnitType.ToString();
// 填充巨人类型信息
_debugUnit.transform.Find("GiantTypeValue").GetComponent<TextMeshProUGUI>().text = unitData.GiantType.ToString();
// 填充单位ID
_debugUnit.transform.Find("UnitIdValue").GetComponent<TextMeshProUGUI>().text = unitData.Id.ToString();
// 填充AP值
_debugUnit.transform.Find("APValue").GetComponent<TextMeshProUGUI>().text = unitData.AP.ToString();
// 填充MP值
_debugUnit.transform.Find("MPValue").GetComponent<TextMeshProUGUI>().text = unitData.MP.ToString();
// 填充携带单位类型信息
if (_debugUnit.transform.Find("CarryUnitTypeValue") != null)
{
_debugUnit.transform.Find("CarryUnitTypeValue").GetComponent<TextMeshProUGUI>().text =
unitData.CarryUnitType != UnitType.None ? unitData.CarryUnitType.ToString() : "None";
}
// 填充携带巨人类型信息
if (_debugUnit.transform.Find("CarryGiantTypeValue") != null)
{
_debugUnit.transform.Find("CarryGiantTypeValue").GetComponent<TextMeshProUGUI>().text =
unitData.CarryGiantType != GiantType.None ? unitData.CarryGiantType.ToString() : "None";
}
// 填充携带单位经验值
if (_debugUnit.transform.Find("CarryExpValue") != null)
{
_debugUnit.transform.Find("CarryExpValue").GetComponent<TextMeshProUGUI>().text =
unitData.CarryUnitType != UnitType.None ? unitData.CarryExp.ToString() : "0";
}
// 填充携带单位老兵状态
if (_debugUnit.transform.Find("CarryVeteranValue") != null)
{
_debugUnit.transform.Find("CarryVeteranValue").GetComponent<TextMeshProUGUI>().text =
unitData.CarryUnitType != UnitType.None ? unitData.CarryVeteran.ToString() : "False";
}
// 获取技能组
Transform skillGroup = _debugUnit.transform.Find("SkillGroup");
// 初始化所有技能文本为空
for (int i = 0; i < 21; i++)
{
Transform skillTransform = skillGroup.Find($"Skill ({i})");
if (skillTransform != null)
{
skillTransform.GetComponent<TextMeshProUGUI>().text = "";
}
}
// 填充技能信息
int skillIndex = 0;
foreach (var skill in unitData.Skills)
{
if (skillIndex >= 9) break; // 最多显示9个技能
Transform skillTransform = skillGroup.Find($"Skill ({skillIndex})");
if (skillTransform != null)
{
// 获取技能名称
string skillName = skill.GetSkillType().ToString();
skillTransform.GetComponent<TextMeshProUGUI>().text = skillName;
}
skillIndex++;
}
}
}
// 根据索引执行对应的操作按钮点击
public void ExecuteActionButtonByIndex(int index)
{
if (!ROBottomInfoUI.activeSelf || !HasAction)
return;
// 获取所有激活的操作按钮
List<GameObject> activeButtons = new List<GameObject>();
foreach (Transform child in _actionArea.transform)
{
if (child.gameObject.activeSelf)
{
activeButtons.Add(child.gameObject);
}
}
// 检查索引是否有效
if (index >= 0 && index < activeButtons.Count)
{
GameObject targetButton = activeButtons[index];
// 直接调用ActionAreaClicked方法传入目标按钮
ActionAreaClicked(targetButton);
}
}
// 递归设置层级的辅助方法
private void SetLayerRecursively(GameObject obj, int newLayer)
{
obj.layer = newLayer;
foreach (Transform child in obj.transform)
{
SetLayerRecursively(child.gameObject, newLayer);
}
}
}