diff options
| -rw-r--r-- | ArchipelagoManager.cs | 18 | ||||
| -rw-r--r-- | ManifoldGardenArchipelago.csproj | 6 | ||||
| -rw-r--r-- | Notifications.cs | 205 | ||||
| -rw-r--r-- | Plugin.cs | 13 |
4 files changed, 242 insertions, 0 deletions
| diff --git a/ArchipelagoManager.cs b/ArchipelagoManager.cs index 5e1d867..b295f2a 100644 --- a/ArchipelagoManager.cs +++ b/ArchipelagoManager.cs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | using Archipelago.MultiClient.Net; | 1 | using Archipelago.MultiClient.Net; |
| 2 | using Archipelago.MultiClient.Net.Enums; | 2 | using Archipelago.MultiClient.Net.Enums; |
| 3 | using Archipelago.MultiClient.Net.MessageLog.Messages; | ||
| 3 | using Archipelago.MultiClient.Net.Models; | 4 | using Archipelago.MultiClient.Net.Models; |
| 4 | using Archipelago.MultiClient.Net.Packets; | 5 | using Archipelago.MultiClient.Net.Packets; |
| 5 | using System; | 6 | using System; |
| @@ -22,6 +23,7 @@ namespace ManifoldGardenArchipelago | |||
| 22 | try | 23 | try |
| 23 | { | 24 | { |
| 24 | _session = ArchipelagoSessionFactory.CreateSession(url); | 25 | _session = ArchipelagoSessionFactory.CreateSession(url); |
| 26 | _session.MessageLog.OnMessageReceived += OnMessageReceived; | ||
| 25 | 27 | ||
| 26 | RoomInfoPacket roomInfoPacket = await _session.ConnectAsync(); | 28 | RoomInfoPacket roomInfoPacket = await _session.ConnectAsync(); |
| 27 | 29 | ||
| @@ -83,6 +85,7 @@ namespace ManifoldGardenArchipelago | |||
| 83 | ItemInfo item = _session.Items.AllItemsReceived[i]; | 85 | ItemInfo item = _session.Items.AllItemsReceived[i]; |
| 84 | string itemName = item.ItemName; | 86 | string itemName = item.ItemName; |
| 85 | Plugin.Logger.LogInfo($"Received {itemName}"); | 87 | Plugin.Logger.LogInfo($"Received {itemName}"); |
| 88 | Plugin.notificationManager.AddMessage($"Received {itemName}"); | ||
| 86 | 89 | ||
| 87 | _items.Add(itemName); | 90 | _items.Add(itemName); |
| 88 | 91 | ||
| @@ -111,5 +114,20 @@ namespace ManifoldGardenArchipelago | |||
| 111 | */ | 114 | */ |
| 112 | } | 115 | } |
| 113 | } | 116 | } |
| 117 | |||
| 118 | private void OnMessageReceived(LogMessage message) | ||
| 119 | { | ||
| 120 | if (message is ItemSendLogMessage itemSendLogMessage && | ||
| 121 | itemSendLogMessage.IsSenderTheActivePlayer && | ||
| 122 | !itemSendLogMessage.IsReceiverTheActivePlayer) | ||
| 123 | { | ||
| 124 | string itemName = itemSendLogMessage.Item.ItemName; | ||
| 125 | string otherPlayer = itemSendLogMessage.Receiver.Name; | ||
| 126 | |||
| 127 | string messageText = $"Sent {itemName} to {otherPlayer}"; | ||
| 128 | |||
| 129 | Plugin.notificationManager.AddMessage(messageText); | ||
| 130 | } | ||
| 131 | } | ||
| 114 | } | 132 | } |
| 115 | } | 133 | } |
| diff --git a/ManifoldGardenArchipelago.csproj b/ManifoldGardenArchipelago.csproj index 67e0dab..cc427c9 100644 --- a/ManifoldGardenArchipelago.csproj +++ b/ManifoldGardenArchipelago.csproj | |||
| @@ -36,6 +36,12 @@ | |||
| 36 | <Reference Include="Assembly-CSharp"> | 36 | <Reference Include="Assembly-CSharp"> |
| 37 | <HintPath>D:\SteamLibrary\steamapps\common\Manifold Garden\ManifoldGarden_Data\Managed\Assembly-CSharp.dll</HintPath> | 37 | <HintPath>D:\SteamLibrary\steamapps\common\Manifold Garden\ManifoldGarden_Data\Managed\Assembly-CSharp.dll</HintPath> |
| 38 | </Reference> | 38 | </Reference> |
| 39 | <Reference Include="Unity.TextMeshPro"> | ||
| 40 | <HintPath>D:\SteamLibrary\steamapps\common\Manifold Garden\ManifoldGarden_Data\Managed\Unity.TextMeshPro.dll</HintPath> | ||
| 41 | </Reference> | ||
| 42 | <Reference Include="UnityEngine.UI"> | ||
| 43 | <HintPath>D:\SteamLibrary\steamapps\common\Manifold Garden\ManifoldGarden_Data\Managed\UnityEngine.UI.dll</HintPath> | ||
| 44 | </Reference> | ||
| 39 | </ItemGroup> | 45 | </ItemGroup> |
| 40 | 46 | ||
| 41 | <ItemGroup> | 47 | <ItemGroup> |
| diff --git a/Notifications.cs b/Notifications.cs new file mode 100644 index 0000000..20e5e51 --- /dev/null +++ b/Notifications.cs | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | using System; | ||
| 2 | using System.Collections.Generic; | ||
| 3 | using TMPro; | ||
| 4 | using UnityEngine; | ||
| 5 | |||
| 6 | namespace ManifoldGardenArchipelago | ||
| 7 | { | ||
| 8 | public class NotificationManager : MonoBehaviour | ||
| 9 | { | ||
| 10 | public static NotificationManager InitializeNotifications(UIManager uiManager) | ||
| 11 | { | ||
| 12 | GameObject gameObject = new("Notifications"); | ||
| 13 | gameObject.transform.parent = uiManager.transform; | ||
| 14 | |||
| 15 | Vector3 groupPosition = new(1260f, 680f, 0f); | ||
| 16 | gameObject.transform.set_position_Injected(ref groupPosition); | ||
| 17 | |||
| 18 | NotificationManager notificationManager = gameObject.AddComponent<NotificationManager>(); | ||
| 19 | |||
| 20 | Plugin.Logger.LogInfo("Notification manager initialized"); | ||
| 21 | |||
| 22 | return notificationManager; | ||
| 23 | } | ||
| 24 | |||
| 25 | public const int MAX_VISIBLE = 5; | ||
| 26 | |||
| 27 | private readonly List<NotificationLine> _notifications = []; | ||
| 28 | private readonly List<string> _queuedMessages = []; | ||
| 29 | private int _counter = 0; | ||
| 30 | |||
| 31 | void Update() | ||
| 32 | { | ||
| 33 | while (_notifications.Count > 0 && _notifications[_notifications.Count - 1].ReadyToBeDestroyed()) | ||
| 34 | { | ||
| 35 | NotificationLine toDestroy = _notifications[_notifications.Count - 1]; | ||
| 36 | GameObject.Destroy(toDestroy.gameObject); | ||
| 37 | _notifications.RemoveAt(_notifications.Count - 1); | ||
| 38 | } | ||
| 39 | |||
| 40 | if (_queuedMessages.Count > 0) | ||
| 41 | { | ||
| 42 | if (_notifications.Count > 0 && !_notifications[0].ReadyToBeSurplanted()) | ||
| 43 | { | ||
| 44 | if (!_notifications[0].IsMoving() && | ||
| 45 | !(_notifications.Count >= MAX_VISIBLE && !_notifications[_notifications.Count - 1].IsFadingOut())) | ||
| 46 | { | ||
| 47 | foreach (var notification in _notifications) | ||
| 48 | { | ||
| 49 | notification.SlideDown(); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | else | ||
| 54 | { | ||
| 55 | foreach (var notification in _notifications) | ||
| 56 | { | ||
| 57 | notification.ConfirmMovement(); | ||
| 58 | } | ||
| 59 | |||
| 60 | GameObject newNotify = new($"Message_{_counter}"); | ||
| 61 | _counter += 1; | ||
| 62 | |||
| 63 | newNotify.transform.parent = transform; | ||
| 64 | |||
| 65 | Vector3 freshPos = new(); | ||
| 66 | newNotify.transform.set_localPosition_Injected(ref freshPos); | ||
| 67 | |||
| 68 | TextMeshProUGUI textMeshPro = newNotify.AddComponent<TextMeshProUGUI>(); | ||
| 69 | textMeshPro.alignment = TextAlignmentOptions.TopRight; | ||
| 70 | textMeshPro.enableWordWrapping = false; | ||
| 71 | textMeshPro.text = _queuedMessages[0]; | ||
| 72 | textMeshPro.alpha = 0f; | ||
| 73 | textMeshPro.autoSizeTextContainer = true; | ||
| 74 | |||
| 75 | Vector2 pivot = new(1f, 0.5f); | ||
| 76 | textMeshPro.rectTransform.set_pivot_Injected(ref pivot); | ||
| 77 | |||
| 78 | _queuedMessages.RemoveAt(0); | ||
| 79 | |||
| 80 | NotificationLine newLine = newNotify.AddComponent<NotificationLine>(); | ||
| 81 | _notifications.Insert(0, newLine); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | public void AddMessage(string message) | ||
| 87 | { | ||
| 88 | _queuedMessages.Add(message); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | public class NotificationLine : MonoBehaviour | ||
| 93 | { | ||
| 94 | public const float ALPHA_SPEED = 1f; | ||
| 95 | public const float SLIDE_SPEED = 75f; | ||
| 96 | public const float SLIDE_DISPLACEMENT = 50f; | ||
| 97 | |||
| 98 | private enum FadeState | ||
| 99 | { | ||
| 100 | FadingIn, | ||
| 101 | Waiting, | ||
| 102 | FadingOut, | ||
| 103 | ReadyToDestroy, | ||
| 104 | } | ||
| 105 | |||
| 106 | private enum MoveState | ||
| 107 | { | ||
| 108 | Waiting, | ||
| 109 | Moving, | ||
| 110 | Ready, | ||
| 111 | } | ||
| 112 | |||
| 113 | private TextMeshProUGUI _textMeshPro; | ||
| 114 | |||
| 115 | private float _alphaTarget = 1f; | ||
| 116 | private FadeState _fadeState = FadeState.FadingIn; | ||
| 117 | private float _waitTimer = 10f; | ||
| 118 | |||
| 119 | private MoveState _moveState = MoveState.Waiting; | ||
| 120 | private float _targetY = 0f; | ||
| 121 | |||
| 122 | void Start() | ||
| 123 | { | ||
| 124 | _textMeshPro = GetComponent<TextMeshProUGUI>(); | ||
| 125 | } | ||
| 126 | |||
| 127 | void Update() | ||
| 128 | { | ||
| 129 | if (_textMeshPro.alpha != _alphaTarget) | ||
| 130 | { | ||
| 131 | float alpha = _textMeshPro.alpha; | ||
| 132 | |||
| 133 | if (alpha < _alphaTarget) | ||
| 134 | { | ||
| 135 | alpha = Math.Min(_alphaTarget, alpha + ALPHA_SPEED * Time.deltaTime); | ||
| 136 | } | ||
| 137 | else | ||
| 138 | { | ||
| 139 | alpha = Math.Max(_alphaTarget, alpha - ALPHA_SPEED * Time.deltaTime); | ||
| 140 | } | ||
| 141 | |||
| 142 | _textMeshPro.alpha = alpha; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (_fadeState == FadeState.FadingIn) | ||
| 146 | { | ||
| 147 | if (_textMeshPro.alpha >= _alphaTarget) | ||
| 148 | { | ||
| 149 | _fadeState = FadeState.Waiting; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | else if (_fadeState == FadeState.Waiting) | ||
| 153 | { | ||
| 154 | _waitTimer -= ALPHA_SPEED * Time.deltaTime; | ||
| 155 | |||
| 156 | if (_waitTimer < 0) | ||
| 157 | { | ||
| 158 | _fadeState = FadeState.FadingOut; | ||
| 159 | _alphaTarget = 0f; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | else if (_fadeState == FadeState.FadingOut) | ||
| 163 | { | ||
| 164 | if (_textMeshPro.alpha <= _alphaTarget) | ||
| 165 | { | ||
| 166 | _fadeState = FadeState.ReadyToDestroy; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | if (_moveState == MoveState.Moving) | ||
| 171 | { | ||
| 172 | if (transform.localPosition.y > _targetY) | ||
| 173 | { | ||
| 174 | Vector3 localPos = transform.localPosition; | ||
| 175 | localPos.y = Math.Max(_targetY, localPos.y - SLIDE_SPEED * Time.deltaTime); | ||
| 176 | transform.set_localPosition_Injected(ref localPos); | ||
| 177 | |||
| 178 | if (transform.localPosition.y <= _targetY) | ||
| 179 | { | ||
| 180 | _moveState = MoveState.Ready; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | public bool ReadyToBeSurplanted() => _moveState == MoveState.Ready; | ||
| 187 | |||
| 188 | public bool IsMoving() => _moveState == MoveState.Moving; | ||
| 189 | |||
| 190 | public void SlideDown() | ||
| 191 | { | ||
| 192 | _moveState = MoveState.Moving; | ||
| 193 | _targetY = transform.localPosition.y - SLIDE_DISPLACEMENT; | ||
| 194 | } | ||
| 195 | |||
| 196 | public void ConfirmMovement() | ||
| 197 | { | ||
| 198 | _moveState = MoveState.Waiting; | ||
| 199 | } | ||
| 200 | |||
| 201 | public bool IsFadingOut() => _fadeState == FadeState.FadingOut; | ||
| 202 | |||
| 203 | public bool ReadyToBeDestroyed() => _fadeState == FadeState.ReadyToDestroy; | ||
| 204 | } | ||
| 205 | } | ||
| diff --git a/Plugin.cs b/Plugin.cs index df53e96..a9fb284 100644 --- a/Plugin.cs +++ b/Plugin.cs | |||
| @@ -12,6 +12,7 @@ namespace ManifoldGardenArchipelago | |||
| 12 | 12 | ||
| 13 | public static ArchipelagoManager archipelagoManager = new(); | 13 | public static ArchipelagoManager archipelagoManager = new(); |
| 14 | public static SlotSave slotSave = new(); | 14 | public static SlotSave slotSave = new(); |
| 15 | public static NotificationManager notificationManager = null; | ||
| 15 | 16 | ||
| 16 | private void Awake() | 17 | private void Awake() |
| 17 | { | 18 | { |
| @@ -26,6 +27,18 @@ namespace ManifoldGardenArchipelago | |||
| 26 | } | 27 | } |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 30 | [HarmonyPatch(typeof(GameManager), nameof(GameManager.AttachUI))] | ||
| 31 | static class UIManagerAwakePatch | ||
| 32 | { | ||
| 33 | static void Postfix() | ||
| 34 | { | ||
| 35 | if (Plugin.notificationManager == null) | ||
| 36 | { | ||
| 37 | Plugin.notificationManager = NotificationManager.InitializeNotifications(GameManager.UI); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 29 | [HarmonyPatch(typeof(GameManager), "Update")] | 42 | [HarmonyPatch(typeof(GameManager), "Update")] |
| 30 | static class GameManagerUpdatePatch | 43 | static class GameManagerUpdatePatch |
| 31 | { | 44 | { |
