From 189fcfc1087764961e61c470ffea642ff46d164d Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 25 May 2024 13:11:17 -0400 Subject: You can connect now! --- AnodyneArchipelago/Menu/ConnectionState.cs | 118 +++++++++++++++++++++++++++++ AnodyneArchipelago/Menu/MenuState.cs | 38 ++++++++++ 2 files changed, 156 insertions(+) create mode 100644 AnodyneArchipelago/Menu/ConnectionState.cs (limited to 'AnodyneArchipelago/Menu') diff --git a/AnodyneArchipelago/Menu/ConnectionState.cs b/AnodyneArchipelago/Menu/ConnectionState.cs new file mode 100644 index 0000000..877213f --- /dev/null +++ b/AnodyneArchipelago/Menu/ConnectionState.cs @@ -0,0 +1,118 @@ +using AnodyneSharp.Input; +using AnodyneSharp.Resources; +using AnodyneSharp.Sounds; +using AnodyneSharp.States; +using AnodyneSharp.UI; +using AnodyneSharp.UI.Font; +using AnodyneSharp.UI.Text; +using Archipelago.MultiClient.Net; +using Archipelago.MultiClient.Net.Enums; +using Microsoft.Xna.Framework; +using System.Reflection; +using System.Threading.Tasks; + +namespace AnodyneArchipelago.Menu +{ + internal class ConnectionState : State + { + public delegate void SuccessEvent(ArchipelagoManager archipelagoManager); + + private readonly SuccessEvent _successFunc; + + private Task _connectionTask; + private ArchipelagoManager _archipelago = new(); + + private TextWriter _textWriter; + private UIEntity _bgBox; + private readonly SpriteFont _font; + + private string _text = "Connecting..."; + + public ConnectionState(string apServer, string apSlot, string apPassword, SuccessEvent successFunc) + { + _successFunc = successFunc; + + _connectionTask = Task.Run(() => _archipelago.Connect(apServer, apSlot, apPassword)); + + _font = FontManager.InitFont(new Color(226, 226, 226), true); + + _textWriter = new(20, 44, 128, 100) + { + drawLayer = AnodyneSharp.Drawing.DrawOrder.TEXT + }; + _textWriter.SetSpriteFont(_font, ResourceManager.GetTexture("consoleButtons")); + + _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 16, 16, AnodyneSharp.Drawing.DrawOrder.TEXTBOX); + UpdateDisplay(); + } + + public override void Update() + { + if (_connectionTask != null && _connectionTask.IsCompleted) + { + LoginResult result = _connectionTask.Result; + + if (result.Successful) + { + Exit = true; + _successFunc(_archipelago); + return; + } + else + { + LoginFailure failure = result as LoginFailure; + string errorMessage = ""; + foreach (string error in failure.Errors) + { + errorMessage += error; + errorMessage += "\n"; + } + foreach (ConnectionRefusedError error in failure.ErrorCodes) + { + errorMessage += error.ToString(); + errorMessage += "\n"; + } + + if (errorMessage.Length > 0) + { + errorMessage = errorMessage.Substring(0, errorMessage.Length - 1); + } + else + { + errorMessage = "Unknown error during connection."; + } + + _text = errorMessage; + _connectionTask = null; + + UpdateDisplay(); + } + } + + if (KeyInput.JustPressedRebindableKey(KeyFunctions.Accept) || KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel)) + { + Exit = true; + SoundManager.PlaySoundEffect("menu_select"); + } + } + + public override void DrawUI() + { + _bgBox.Draw(); + _textWriter.Draw(); + } + + private void UpdateDisplay() + { + _textWriter.Text = _text; + _textWriter.ProgressTextToEnd(); + + FieldInfo linesField = typeof(TextWriter).GetField("_line", BindingFlags.NonPublic | BindingFlags.Instance); + int lineValue = (int)linesField.GetValue(_textWriter); + + int innerHeight = (lineValue + 1) * _font.lineSeparation; + + _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 136, innerHeight + 8, AnodyneSharp.Drawing.DrawOrder.TEXTBOX); + } + } +} diff --git a/AnodyneArchipelago/Menu/MenuState.cs b/AnodyneArchipelago/Menu/MenuState.cs index b9ee0dd..cabb94b 100644 --- a/AnodyneArchipelago/Menu/MenuState.cs +++ b/AnodyneArchipelago/Menu/MenuState.cs @@ -33,6 +33,9 @@ namespace AnodyneArchipelago.Menu private int _selectorIndex = 0; + private bool _fadingOut = false; + private bool _isNewGame; + public override void Create() { _selector = new(); @@ -65,6 +68,18 @@ namespace AnodyneArchipelago.Menu public override void Update() { + if (_fadingOut) + { + GlobalState.black_overlay.ChangeAlpha(0.72f); + + if (GlobalState.black_overlay.alpha == 1.0) + { + ChangeStateEvent(_isNewGame ? AnodyneSharp.AnodyneGame.GameState.Intro : AnodyneSharp.AnodyneGame.GameState.Game); + } + + return; + } + if (_substate != null) { _substate.Update(); @@ -217,6 +232,9 @@ namespace AnodyneArchipelago.Menu case 2: _substate = new TextEntry("Password:", _apPassword, (string value) => { _apPassword = value; UpdateLabels(); }); break; + case 4: + _substate = new ConnectionState(_apServer, _apSlot, _apPassword, OnConnected); + break; case 6: GlobalState.ClosingGame = true; break; @@ -231,5 +249,25 @@ namespace AnodyneArchipelago.Menu { } + + private void OnConnected(ArchipelagoManager archipelagoManager) + { + Plugin.ArchipelagoManager = archipelagoManager; + + GlobalState.Save saveFile = GlobalState.Save.GetSave(string.Format("{0}Saves/Save_zzAP{1}_{2}.dat", GameConstants.SavePath, Plugin.ArchipelagoManager.GetSeed(), Plugin.ArchipelagoManager.GetPlayer())); + + GlobalState.ResetValues(); + if (saveFile != null) + { + GlobalState.LoadSave(saveFile); + _isNewGame = false; + } + else + { + _isNewGame = true; + } + + _fadingOut = true; + } } } -- cgit 1.4.1