using SoulHarvest.Common; using SoulHarvest.Items; using SoulHarvest.Tiles; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System; using System.Collections.Generic; using Terraria; using Terraria.Audio; using Terraria.GameContent; using Terraria.GameContent.UI.Elements; using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; using Terraria.UI; namespace SoulHarvest.UI; [Autoload(Side = ModSide.Client)] public class DeathAltarUISystem : ModSystem { private UserInterface? altarInterface; private DeathAltarUIState? altarState; private bool escapeWasDown; public bool IsVisible => altarInterface?.CurrentState is not null; public override void Load() { altarState = new DeathAltarUIState(); altarState.Activate(); altarInterface = new UserInterface(); } public void Show(int tileX, int tileY) { altarState?.SetAltarPosition(tileX, tileY); altarInterface?.SetState(altarState); } public void Hide() { altarState?.CancelTransientInteraction(); altarInterface?.SetState(null); } public override void UpdateUI(GameTime gameTime) { if (!IsVisible || altarState is null) return; bool escapeIsDown = Main.keyState.IsKeyDown(Keys.Escape); if (escapeIsDown && !escapeWasDown) { SoundEngine.PlaySound(SoundID.MenuClose); Hide(); } escapeWasDown = escapeIsDown; if (!altarState.IsPlayerNearAltar()) { Hide(); return; } altarInterface?.Update(gameTime); } public override void ModifyInterfaceLayers(List layers) { int index = layers.FindIndex(layer => layer.Name == "Vanilla: Mouse Text"); if (index < 0) return; layers.Insert(index, new LegacyGameInterfaceLayer("SoulHarvest: Death Altar Skill Tree", () => { if (IsVisible) altarInterface?.Draw(Main.spriteBatch, new GameTime()); return true; }, InterfaceScaleType.UI)); } internal void RequestUpgrade(DeathAltarUpgradeType type, DeathAltarItemReference itemReference, int tileX, int tileY) { if (Main.netMode == NetmodeID.SinglePlayer) { DeathAltarUpgradeService.TryUpgrade(Main.LocalPlayer, type, itemReference, tileX, tileY); return; } ModPacket packet = ModContent.GetInstance().GetPacket(); packet.Write((byte)SoulHarvest.MessageType.RequestAltarUpgrade); packet.Write((byte)type); packet.Write((byte)itemReference.Storage); packet.Write(itemReference.Slot); packet.Write(itemReference.ExpectedItemType); packet.Write((short)tileX); packet.Write((short)tileY); packet.Send(); } }