298 lines
10 KiB
C#
298 lines
10 KiB
C#
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 = UIManager.Instance.ROUIManager.transform.Find("GameUI/ChooseUI").gameObject;
|
||
ROChooseUI.transform.Find("CloseButton").GetComponent<Button>().onClick.AddListener(
|
||
() => { NeedShow = false;
|
||
UIManager.Instance.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);
|
||
}
|
||
|
||
//当SecondChoose发生选择之后,更新选项情况
|
||
public void SelectUpdate()
|
||
{
|
||
string mode = _secondChoosePanel.transform.Find("SettingBar/Mode")?.GetComponent<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
string count = _secondChoosePanel.transform.Find("SettingBar/EnemyCount")?.GetComponent<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
string size = _secondChoosePanel.transform.Find("SettingBar/MapSize")?.GetComponent<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
string rightSize = count switch
|
||
{
|
||
"ButtonPlayer2" => "ButtonSize11",
|
||
"ButtonPlayer3" => "ButtonSize14",
|
||
"ButtonPlayer4" => "ButtonSize16",
|
||
_ => "ButtonSize18"
|
||
};
|
||
if (mode == "PerfectMode")
|
||
rightSize = "ButtonSize16";
|
||
if(rightSize != size)
|
||
_secondChoosePanel.transform.Find("SettingBar/MapSize")?.GetComponent<ChooseUISettingBarController>()
|
||
.SetSelectedByButtonName(rightSize);
|
||
}
|
||
|
||
public void ShowLoadingAndStartGame()
|
||
{
|
||
|
||
float loadingTime = 2f;
|
||
var loading = UIManager.Instance.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<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
string playerCountS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/EnemyCount").GetComponent<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
string diffS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/Difficulty").GetComponent<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
string modeS = ROChooseUI.transform.Find("ChooseSecond/SettingBar/Mode").GetComponent<ChooseUISettingBarController>()
|
||
.GetSelectedButtonName();
|
||
|
||
|
||
AIDifficult diff = diffS switch
|
||
{
|
||
"NORMAL" => AIDifficult.NORMAL,
|
||
"HARD" => AIDifficult.HARD,
|
||
"LUNATIC" => AIDifficult.LUNATIC,
|
||
_ => AIDifficult.EASY
|
||
};
|
||
if (_mapData != null) _mapData.MapConfig.AIDiff = diff;
|
||
|
||
GameMode mode = modeS switch
|
||
{
|
||
"PerfectMode" => GameMode.PERFECT,
|
||
"DominationMode" => GameMode.DOMINATION,
|
||
_ => GameMode.CREATIVE
|
||
};
|
||
|
||
if (_mapData != null) _mapData.MapConfig.AIDiff = diff;
|
||
if (_mapData != null) _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);
|
||
}
|
||
|
||
|
||
}
|