317 lines
11 KiB
C#
317 lines
11 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using Logic;
|
||
using RuntimeData;
|
||
using Animancer;
|
||
using Logic.AI;
|
||
using UnityEngine.UI;
|
||
using Logic.Audio;
|
||
using TH1_Core.Managers;
|
||
using TH1_Logic.Core;
|
||
using TH1_Logic.Net;
|
||
using TH1Renderer;
|
||
using TH1Resource;
|
||
using TMPro;
|
||
using UI;
|
||
|
||
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 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(
|
||
() =>
|
||
{
|
||
Hide();
|
||
UIManager.Instance.GameUI.MainUI.NeedShow = true;
|
||
});
|
||
ROChooseUI.transform.Find("ChooseSecond/ModeButton/Button").GetComponent<Button>().onClick.AddListener(ShowLoadingAndStartGame);
|
||
ROChooseUI.transform.Find("ChooseSecond/CloseButton").GetComponent<Button>().onClick.AddListener(HideSecondChoose);
|
||
|
||
ROChooseUI.gameObject.SetActive(false);
|
||
Init();
|
||
}
|
||
|
||
public void Init()
|
||
{
|
||
|
||
//绑定每个阵营选项和左边的panel的点击关系
|
||
for (int i = 0; i < 2; i++)
|
||
{
|
||
int index = i; // 闭包用局部变量
|
||
_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");
|
||
_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);
|
||
}
|
||
|
||
private void HideSecondChoose()
|
||
{
|
||
var anim = _secondChoosePanel.GetComponent<AnimancerComponent>();
|
||
if (anim != null)
|
||
{
|
||
anim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut);
|
||
Timer.Instance.TimerRegister(_secondChoosePanel, () =>
|
||
{
|
||
_secondChoosePanel.SetActive(false);
|
||
},ResourceCache.Instance.AnimCache.UICommonPanelFadeOut.length,"ChooseUI_HideSecond");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
//当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,"ChooseUI_ShowLoading");
|
||
|
||
//播放loading消失的动画
|
||
Timer.Instance.TimerRegister(loading,()=> { loadingAnim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut); },loadingTime,"ChooseUI_ShowLoading2");
|
||
//将loading设置为false
|
||
Timer.Instance.TimerRegister(loading,()=> { loading.gameObject.SetActive(false); },loadingTime + 0.2f,"ChooseUI_ShowLoading3");
|
||
}
|
||
//点击开始游戏后,
|
||
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
|
||
{
|
||
"ButtonNormal" => AIDifficult.NORMAL,
|
||
"ButtonHard" => AIDifficult.HARD,
|
||
"ButtonLunatic" => 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;
|
||
|
||
//传递玩家选择的阵营civ和force
|
||
uint civ = _civ[_currentPanelIndex];
|
||
uint force = _force[_currentPanelIndex];
|
||
|
||
//Debug.Log($"mapSize ={mapSize}, playerCount = {playerCount} civ = {civ}, force = {force}");
|
||
|
||
_main.MapConfig.Width = (uint)mapSize;
|
||
_main.MapConfig.Height = (uint)mapSize;
|
||
_main.MapConfig.PlayerCount = (uint)playerCount;
|
||
_main.MapConfig.selfCivId = civ;
|
||
_main.MapConfig.selfForceId = force;
|
||
_main.MapConfig.AIDiff = diff;
|
||
if (LobbyManager.Instance.Lobby.IsLobbyOwner() && LobbyManager.Instance.Lobby.GetMemberCount() > 1)
|
||
{
|
||
_main.MainMemberStartGame();
|
||
}
|
||
else
|
||
{
|
||
_main.StartGame();
|
||
}
|
||
Hide();
|
||
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
}
|
||
|
||
public void Show()
|
||
{
|
||
if (_isShowing || _isAnimating) return;
|
||
AudioManager.Instance.PlayMusic("RemiliaEgyptian",0.3f,0.3f,true);
|
||
//设置默认点开的
|
||
_currentPanelIndex = 0;
|
||
|
||
_isShowing = true;
|
||
_isAnimating = true;
|
||
ROChooseUI.SetActive(true);
|
||
_secondChoosePanel.SetActive(false);
|
||
AnimancerComponent animancer = ROChooseUI.GetComponent<AnimancerComponent>();
|
||
animancer.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
|
||
SwitchToPanel(_currentPanelIndex,force:true);
|
||
Timer.Instance.TimerRegister(ROChooseUI, () =>
|
||
{
|
||
_isAnimating = false;
|
||
}, _fadeDuration,"ChooseUI_Show");
|
||
}
|
||
|
||
public void Hide()
|
||
{
|
||
if (!_isShowing || _isAnimating) return;
|
||
_isShowing = false;
|
||
_isAnimating = true;
|
||
|
||
AnimancerComponent animancer = ROChooseUI.GetComponent<AnimancerComponent>();
|
||
animancer.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut);
|
||
|
||
Timer.Instance.TimerRegister(ROChooseUI, () =>
|
||
{
|
||
ROChooseUI.SetActive(false);
|
||
_isAnimating = false;
|
||
}, _fadeDuration,"ChooseUI_Hide");
|
||
}
|
||
|
||
private void SwitchToPanel(int index,bool force = false)
|
||
{
|
||
//Step #1 如果当前存储的就是要switch的,并且不是强制刷新,就return
|
||
if(index == _currentPanelIndex && !force) return;
|
||
|
||
//Step #2 隐藏目前的面板
|
||
_isAnimating = true;
|
||
for(int i =0;i < 2;i ++)
|
||
if (_panels[i].activeSelf)
|
||
{
|
||
if (i == index) continue;
|
||
var pn = _panels[i];
|
||
var animancer = pn.GetComponent<AnimancerComponent>();
|
||
if(animancer != null)
|
||
animancer.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeOut);
|
||
Timer.Instance.TimerRegister(pn, () =>
|
||
{
|
||
pn.SetActive(false);
|
||
}, _fadeDuration,"ChooseUI_SwitchPanel");
|
||
}
|
||
|
||
|
||
//Step #3 切换到新面板
|
||
_currentPanelIndex = index;
|
||
var targetPanel = _panels[index];
|
||
|
||
targetPanel.SetActive(true); // 提前激活才能播放动画
|
||
//调整所有按钮的颜色
|
||
for (int i = 0; i < 2; i++)
|
||
{
|
||
var img = _buttons[i].transform.Find("TribeIconMask/Mask")?.GetComponent<Image>();
|
||
if(img != null)
|
||
{
|
||
var c = img.color;
|
||
c.a = i == _currentPanelIndex ? 1f : 0.6f;
|
||
img.color = c;
|
||
}
|
||
var diag = _buttons[i].transform.Find("TribeIconMask/TribeDiagBG");
|
||
if (diag != null)
|
||
{
|
||
diag.gameObject.SetActive(i == _currentPanelIndex);
|
||
}
|
||
|
||
}
|
||
if(index == 0)
|
||
AudioManager.Instance.PlayMusic("RemiliaEgyptian",0.3f,0.3f,true);
|
||
else if(index == 1)
|
||
AudioManager.Instance.PlayMusic("KaguyaFrench",0.3f,0.3f,true);
|
||
|
||
var anim = targetPanel.GetComponent<AnimancerComponent>();
|
||
if(anim != null)
|
||
anim.Play(ResourceCache.Instance.AnimCache.UICommonPanelFadeIn);
|
||
|
||
Timer.Instance.TimerRegister(targetPanel, () =>
|
||
{
|
||
_isAnimating = false;
|
||
}, _fadeDuration,"ChooseUI_SwitchPanel2");
|
||
}
|
||
|
||
|
||
}
|