diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-05-25 13:11:17 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-05-25 13:11:17 -0400 |
commit | 189fcfc1087764961e61c470ffea642ff46d164d (patch) | |
tree | 77890701aeb8e8e89b1dffb7084559dc6c8e7364 /AnodyneArchipelago/Menu | |
parent | ca34b22c8dbeecda5ac5f66ca24fbe69a617b4dd (diff) | |
download | anodyne-archipelago-189fcfc1087764961e61c470ffea642ff46d164d.tar.gz anodyne-archipelago-189fcfc1087764961e61c470ffea642ff46d164d.tar.bz2 anodyne-archipelago-189fcfc1087764961e61c470ffea642ff46d164d.zip |
You can connect now!
Diffstat (limited to 'AnodyneArchipelago/Menu')
-rw-r--r-- | AnodyneArchipelago/Menu/ConnectionState.cs | 118 | ||||
-rw-r--r-- | AnodyneArchipelago/Menu/MenuState.cs | 38 |
2 files changed, 156 insertions, 0 deletions
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 @@ | |||
1 | using AnodyneSharp.Input; | ||
2 | using AnodyneSharp.Resources; | ||
3 | using AnodyneSharp.Sounds; | ||
4 | using AnodyneSharp.States; | ||
5 | using AnodyneSharp.UI; | ||
6 | using AnodyneSharp.UI.Font; | ||
7 | using AnodyneSharp.UI.Text; | ||
8 | using Archipelago.MultiClient.Net; | ||
9 | using Archipelago.MultiClient.Net.Enums; | ||
10 | using Microsoft.Xna.Framework; | ||
11 | using System.Reflection; | ||
12 | using System.Threading.Tasks; | ||
13 | |||
14 | namespace AnodyneArchipelago.Menu | ||
15 | { | ||
16 | internal class ConnectionState : State | ||
17 | { | ||
18 | public delegate void SuccessEvent(ArchipelagoManager archipelagoManager); | ||
19 | |||
20 | private readonly SuccessEvent _successFunc; | ||
21 | |||
22 | private Task<LoginResult> _connectionTask; | ||
23 | private ArchipelagoManager _archipelago = new(); | ||
24 | |||
25 | private TextWriter _textWriter; | ||
26 | private UIEntity _bgBox; | ||
27 | private readonly SpriteFont _font; | ||
28 | |||
29 | private string _text = "Connecting..."; | ||
30 | |||
31 | public ConnectionState(string apServer, string apSlot, string apPassword, SuccessEvent successFunc) | ||
32 | { | ||
33 | _successFunc = successFunc; | ||
34 | |||
35 | _connectionTask = Task.Run(() => _archipelago.Connect(apServer, apSlot, apPassword)); | ||
36 | |||
37 | _font = FontManager.InitFont(new Color(226, 226, 226), true); | ||
38 | |||
39 | _textWriter = new(20, 44, 128, 100) | ||
40 | { | ||
41 | drawLayer = AnodyneSharp.Drawing.DrawOrder.TEXT | ||
42 | }; | ||
43 | _textWriter.SetSpriteFont(_font, ResourceManager.GetTexture("consoleButtons")); | ||
44 | |||
45 | _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 16, 16, AnodyneSharp.Drawing.DrawOrder.TEXTBOX); | ||
46 | UpdateDisplay(); | ||
47 | } | ||
48 | |||
49 | public override void Update() | ||
50 | { | ||
51 | if (_connectionTask != null && _connectionTask.IsCompleted) | ||
52 | { | ||
53 | LoginResult result = _connectionTask.Result; | ||
54 | |||
55 | if (result.Successful) | ||
56 | { | ||
57 | Exit = true; | ||
58 | _successFunc(_archipelago); | ||
59 | return; | ||
60 | } | ||
61 | else | ||
62 | { | ||
63 | LoginFailure failure = result as LoginFailure; | ||
64 | string errorMessage = ""; | ||
65 | foreach (string error in failure.Errors) | ||
66 | { | ||
67 | errorMessage += error; | ||
68 | errorMessage += "\n"; | ||
69 | } | ||
70 | foreach (ConnectionRefusedError error in failure.ErrorCodes) | ||
71 | { | ||
72 | errorMessage += error.ToString(); | ||
73 | errorMessage += "\n"; | ||
74 | } | ||
75 | |||
76 | if (errorMessage.Length > 0) | ||
77 | { | ||
78 | errorMessage = errorMessage.Substring(0, errorMessage.Length - 1); | ||
79 | } | ||
80 | else | ||
81 | { | ||
82 | errorMessage = "Unknown error during connection."; | ||
83 | } | ||
84 | |||
85 | _text = errorMessage; | ||
86 | _connectionTask = null; | ||
87 | |||
88 | UpdateDisplay(); | ||
89 | } | ||
90 | } | ||
91 | |||
92 | if (KeyInput.JustPressedRebindableKey(KeyFunctions.Accept) || KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel)) | ||
93 | { | ||
94 | Exit = true; | ||
95 | SoundManager.PlaySoundEffect("menu_select"); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | public override void DrawUI() | ||
100 | { | ||
101 | _bgBox.Draw(); | ||
102 | _textWriter.Draw(); | ||
103 | } | ||
104 | |||
105 | private void UpdateDisplay() | ||
106 | { | ||
107 | _textWriter.Text = _text; | ||
108 | _textWriter.ProgressTextToEnd(); | ||
109 | |||
110 | FieldInfo linesField = typeof(TextWriter).GetField("_line", BindingFlags.NonPublic | BindingFlags.Instance); | ||
111 | int lineValue = (int)linesField.GetValue(_textWriter); | ||
112 | |||
113 | int innerHeight = (lineValue + 1) * _font.lineSeparation; | ||
114 | |||
115 | _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 136, innerHeight + 8, AnodyneSharp.Drawing.DrawOrder.TEXTBOX); | ||
116 | } | ||
117 | } | ||
118 | } | ||
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 | |||
33 | 33 | ||
34 | private int _selectorIndex = 0; | 34 | private int _selectorIndex = 0; |
35 | 35 | ||
36 | private bool _fadingOut = false; | ||
37 | private bool _isNewGame; | ||
38 | |||
36 | public override void Create() | 39 | public override void Create() |
37 | { | 40 | { |
38 | _selector = new(); | 41 | _selector = new(); |
@@ -65,6 +68,18 @@ namespace AnodyneArchipelago.Menu | |||
65 | 68 | ||
66 | public override void Update() | 69 | public override void Update() |
67 | { | 70 | { |
71 | if (_fadingOut) | ||
72 | { | ||
73 | GlobalState.black_overlay.ChangeAlpha(0.72f); | ||
74 | |||
75 | if (GlobalState.black_overlay.alpha == 1.0) | ||
76 | { | ||
77 | ChangeStateEvent(_isNewGame ? AnodyneSharp.AnodyneGame.GameState.Intro : AnodyneSharp.AnodyneGame.GameState.Game); | ||
78 | } | ||
79 | |||
80 | return; | ||
81 | } | ||
82 | |||
68 | if (_substate != null) | 83 | if (_substate != null) |
69 | { | 84 | { |
70 | _substate.Update(); | 85 | _substate.Update(); |
@@ -217,6 +232,9 @@ namespace AnodyneArchipelago.Menu | |||
217 | case 2: | 232 | case 2: |
218 | _substate = new TextEntry("Password:", _apPassword, (string value) => { _apPassword = value; UpdateLabels(); }); | 233 | _substate = new TextEntry("Password:", _apPassword, (string value) => { _apPassword = value; UpdateLabels(); }); |
219 | break; | 234 | break; |
235 | case 4: | ||
236 | _substate = new ConnectionState(_apServer, _apSlot, _apPassword, OnConnected); | ||
237 | break; | ||
220 | case 6: | 238 | case 6: |
221 | GlobalState.ClosingGame = true; | 239 | GlobalState.ClosingGame = true; |
222 | break; | 240 | break; |
@@ -231,5 +249,25 @@ namespace AnodyneArchipelago.Menu | |||
231 | { | 249 | { |
232 | 250 | ||
233 | } | 251 | } |
252 | |||
253 | private void OnConnected(ArchipelagoManager archipelagoManager) | ||
254 | { | ||
255 | Plugin.ArchipelagoManager = archipelagoManager; | ||
256 | |||
257 | GlobalState.Save saveFile = GlobalState.Save.GetSave(string.Format("{0}Saves/Save_zzAP{1}_{2}.dat", GameConstants.SavePath, Plugin.ArchipelagoManager.GetSeed(), Plugin.ArchipelagoManager.GetPlayer())); | ||
258 | |||
259 | GlobalState.ResetValues(); | ||
260 | if (saveFile != null) | ||
261 | { | ||
262 | GlobalState.LoadSave(saveFile); | ||
263 | _isNewGame = false; | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | _isNewGame = true; | ||
268 | } | ||
269 | |||
270 | _fadingOut = true; | ||
271 | } | ||
234 | } | 272 | } |
235 | } | 273 | } |