2025-07-09 17:23:17 +08:00

275 lines
9.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 UnityEngine;
using Logic;
using RuntimeData;
using Animancer;
using Logic.AI;
using UnityEngine.UI;
using Logic.Audio;
using TH1Renderer;
using TH1Resource;
public class ChooseUI
{
private Main _main;
private MapData _mapData;
public GameObject ROChooseUI;
public Button StartButton;
public bool NeedShow = false;
private bool _isShowing = false;
private bool _isAnimating = false;
private float _fadeDuration = 0.2f;
private string[] _name = { "Remilia","Kaguya","Kanako","Satori"};
private uint[] _civ = { 0, 1, 2, 3 };
private uint[] _force = { 0, 1, 2, 3 };
private AnimationClip[] fadeIns = new AnimationClip[4];
private AnimationClip[] fadeOuts = new AnimationClip[4];
private Button[] _buttons = new Button[4];
private GameObject[] _panels = new GameObject[4];
private Button _selectTribe;
private GameObject _secondChoosePanel;
private int _currentPanelIndex = 0;
public ChooseUI(Main main, MapData mapData)
{
_main = main;
_mapData = mapData;
ROChooseUI = _main.UIManager.ROUIManager.transform.Find("GameUI/ChooseUI").gameObject;
ROChooseUI.transform.Find("CloseButton").GetComponent<Button>().onClick.AddListener(
() => { NeedShow = false;
_main.UIManager.GameUI.MainUI.NeedShow = true;
});
ROChooseUI.transform.Find("ChooseSecond/ModeButton/Button").GetComponent<Button>().onClick.AddListener(ShowLoadingAndStartGame);
ROChooseUI.gameObject.SetActive(false);
Init();
}
public void Init()
{
//绑定每个阵营选项和左边的panel的点击关系
for (int i = 0; i < 3; i++)
{
int index = i; // 闭包用局部变量
int row = i / 3;
_buttons[i] = ROChooseUI.transform.Find($"ForceListPanel/TribeList/{_name[i]}Div").GetComponent<Button>();
_panels[i] = ROChooseUI.transform.Find("ForceInfoPanel").Find(_name[i] + "Panel").gameObject;
fadeIns[i] = Resources.Load<AnimationClip>($"Animations/UI/ChooseUI{_name[i]}PanelFadeIn");
fadeOuts[i] = Resources.Load<AnimationClip>($"Animations/UI/ChooseUI{_name[i]}PanelFadeOut");
if(!ROChooseUI.transform.Find($"ForceListPanel/TribeList/{_name[i]}Div").GetComponent<TribeHoverEffect>().IsLocked)
_buttons[i].onClick.AddListener(() => SwitchToPanel(index));
}
_secondChoosePanel = ROChooseUI.transform.Find($"ChooseSecond").gameObject;
_secondChoosePanel.SetActive(false);
//绑定确认选择按钮到弹出ChooseSecond的关系
_selectTribe = ROChooseUI.transform.Find($"ForceListPanel/NextButton").GetComponent<Button>();
_selectTribe.onClick.AddListener(ShowSecondChoose);
}
//点击选择阵营后,显示游戏设置选项
private void ShowSecondChoose()
{
_secondChoosePanel.SetActive(true);
var anim = _secondChoosePanel.GetComponent<AnimancerComponent>();
if(anim != null)
anim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
}
public void ShowLoadingAndStartGame()
{
float loadingTime = 2f;
var loading = _main.UIManager.ROUIManager.transform.Find("LoadingUI");
var loadingAnim = loading.GetComponent<AnimancerComponent>();
if (loadingAnim == null)
{
StartGame();
return;
}
//先播放loading界面然后加载游戏再关掉loading界面
loading.gameObject.SetActive(true);
loadingAnim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
Timer.Instance.TimerRegister(this,StartGame,0.5f);
//播放loading消失的动画
Timer.Instance.TimerRegister(loading,()=> { loadingAnim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut); },loadingTime);
//将loading设置为false
Timer.Instance.TimerRegister(loading,()=> { loading.gameObject.SetActive(false); },loadingTime + 0.2f);
}
//点击开始游戏后,
public void StartGame()
{
//TODO 把游戏难度加上
string mapSizeS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/MapSize").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
string playerCountS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/EnemyCount").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
string diffS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/Difficulty").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
string modeS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/Mode").GetComponent<ToggleButtonGroupController>()
.GetSelectedButtonName();
AIDifficult diff = diffS switch
{
"NORMAL" => AIDifficult.NORMAL,
"HARD" => AIDifficult.HARD,
"LUNATIC" => AIDifficult.LUNATIC,
_ => AIDifficult.EASY
};
_mapData.MapConfig.AIDiff = diff;
GameMode mode = modeS switch
{
"PerfectMode" => GameMode.PERFECT,
"DominationMode" => GameMode.DOMINATION,
_ => GameMode.CREATIVE
};
_mapData.MapConfig.AIDiff = diff;
_mapData.MapConfig.GameMode = mode;
if (!int.TryParse(mapSizeS.Substring(10), out int mapSize))
mapSize = 18;
if (!int.TryParse(playerCountS.Substring(12), out int playerCount))
playerCount = 8;
NeedShow = false;
_currentPanelIndex = 0;
//确认目前选中的是哪个tribe
/*
for (int i = 0; i < 3; i++)
{
if (!ROChooseUI.transform.Find($"ForceListPanel/TribeList/{_name[i]}Div").GetComponent<TribeHoverEffect>()
.CheckIsSelected())
continue;
_currentPanelIndex = i;
break;
}*/
_currentPanelIndex = 0;
uint civ = _civ[_currentPanelIndex];
uint force = _force[_currentPanelIndex];
Debug.Log($"mapSize ={mapSize}, playerCount = {playerCount} civ = {civ}, force = {force}");
_main.StartGame((uint)mapSize, (uint)mapSize,(uint)playerCount, civ, force);
}
public void Update()
{
if (_isAnimating) return;
if (NeedShow && !ROChooseUI.activeSelf)
{
Show();
}
else if (!NeedShow && ROChooseUI.activeSelf)
{
Hide();
}
// 设置界面逻辑(如音量滑块变化、按钮监听等)也可写在这里
}
public void Show()
{
if (_isShowing || _isAnimating) return;
_isShowing = true;
_isAnimating = true;
ROChooseUI.SetActive(true);
AudioManager.Instance.PlayMusic("RemiliaEgyptian",0.3f,0.3f,true);
AnimancerComponent animancer = ROChooseUI.GetComponent<AnimancerComponent>();
AnimationClip fadeInClip = Resources.Load<AnimationClip>("Animations/UI/SettingPanelFadeIn");
if (fadeInClip != null)
animancer.Play(fadeInClip);
Timer.Instance.TimerRegister(ROChooseUI, () => { _isAnimating = false; }, _fadeDuration);
}
public void Hide()
{
if (!_isShowing || _isAnimating) return;
_isShowing = false;
_isAnimating = true;
AnimancerComponent animancer = ROChooseUI.GetComponent<AnimancerComponent>();
AnimationClip fadeOutClip = Resources.Load<AnimationClip>("Animations/UI/SettingPanelFadeOut");
if (fadeOutClip != null)
animancer.Play(fadeOutClip);
Timer.Instance.TimerRegister(ROChooseUI, () =>
{
ROChooseUI.SetActive(false);
_isAnimating = false;
}, _fadeDuration);
}
private void SwitchToPanel(int index)
{
if (index == _currentPanelIndex)
return;
_isAnimating = true;
// 如果当前有面板正在显示,先播放它的 FadeOut 动画
if (_currentPanelIndex != -1)
{
var currentPanel = _panels[_currentPanelIndex];
var fadeOutClip = fadeOuts[_currentPanelIndex];
if (fadeOutClip != null)
{
var animancer = currentPanel.GetComponent<AnimancerComponent>();
animancer.Play(fadeOutClip);
}
// 延迟隐藏当前面板
Timer.Instance.TimerRegister(currentPanel, () =>
{
currentPanel.SetActive(false);
}, _fadeDuration);
}
// 切换到新面板
_currentPanelIndex = index;
var targetPanel = _panels[index];
var fadeInClipNew = fadeIns[index];
targetPanel.SetActive(true); // 提前激活才能播放动画
//if(index == 0)
//AudioManager.Instance.PlayMusic("RemiliaEgyptian",0.3f,0.3f,true);
//else
//AudioManager.Instance.PlayMusic("KaguyaFrench",0.3f,0.3f,true);
if (fadeInClipNew != null)
{
var animancer = targetPanel.GetComponent<AnimancerComponent>();
animancer.Play(fadeInClipNew);
}
Timer.Instance.TimerRegister(targetPanel, () =>
{
_isAnimating = false;
}, _fadeDuration);
}
}