using System; using System.Collections.Generic; using InnerNet; using UnityEngine; using UnityEngine.SceneManagement; public class FindAGameManager : DestroyableSingleton, IGameListHandler { private const float RefreshTime = 5f; private float timer; public ObjectPoolBehavior buttonPool; public SpinAnimator RefreshSpinner; public Scroller TargetArea; public float ButtonStart = 1.75f; public float ButtonHeight = 0.6f; public const bool showPrivate = false; private class GameSorter : IComparer { public static readonly FindAGameManager.GameSorter Instance = new FindAGameManager.GameSorter(); public int Compare(GameListing x, GameListing y) { return -x.PlayerCount.CompareTo(y.PlayerCount); } } public void ResetTimer() { this.timer = 5f; this.RefreshSpinner.Appear(); this.RefreshSpinner.StartPulse(); } public void Start() { if (!AmongUsClient.Instance) { AmongUsClient.Instance = UnityEngine.Object.FindObjectOfType(); if (!AmongUsClient.Instance) { SceneManager.LoadScene("MMOnline"); return; } } AmongUsClient.Instance.GameListHandlers.Add(this); AmongUsClient.Instance.RequestGameList(false, SaveManager.GameSearchOptions); } public void Update() { this.timer += Time.deltaTime; GameOptionsData gameSearchOptions = SaveManager.GameSearchOptions; if ((this.timer < 0f || this.timer > 5f) && gameSearchOptions.MapId != 0) { this.RefreshSpinner.Appear(); } else { this.RefreshSpinner.Disappear(); } if (Input.GetKeyUp(KeyCode.Escape)) { this.ExitGame(); } } public void RefreshList() { if (this.timer > 5f) { this.timer = -5f; this.RefreshSpinner.Play(); AmongUsClient.Instance.RequestGameList(false, SaveManager.GameSearchOptions); } } public override void OnDestroy() { if (AmongUsClient.Instance) { AmongUsClient.Instance.GameListHandlers.Remove(this); } base.OnDestroy(); } // 收到游戏房间后的处理 public void HandleList(int totalGames, List availableGames) { Debug.Log(string.Format("TotalGames: {0}\tAvailable: {1}", totalGames, availableGames.Count)); this.RefreshSpinner.Disappear(); this.timer = 0f; availableGames.Sort(FindAGameManager.GameSorter.Instance); while (this.buttonPool.activeChildren.Count > availableGames.Count) { PoolableBehavior poolableBehavior = this.buttonPool.activeChildren[this.buttonPool.activeChildren.Count - 1]; poolableBehavior.OwnerPool.Reclaim(poolableBehavior); } while (this.buttonPool.activeChildren.Count < availableGames.Count) { this.buttonPool.Get().transform.SetParent(this.TargetArea.Inner); } Vector3 vector = new Vector3(0f, this.ButtonStart, -1f); for (int i = 0; i < this.buttonPool.activeChildren.Count; i++) { MatchMakerGameButton matchMakerGameButton = (MatchMakerGameButton)this.buttonPool.activeChildren[i]; matchMakerGameButton.SetGame(availableGames[i]); matchMakerGameButton.transform.localPosition = vector; vector.y -= this.ButtonHeight; } this.TargetArea.YBounds.max = Mathf.Max(0f, -vector.y - this.ButtonStart); } public void ExitGame() { AmongUsClient.Instance.ExitGame(DisconnectReasons.ExitGame); } }