diff --git a/CalPlayer/CalamityPlayer.cs b/CalPlayer/CalamityPlayer.cs index d22c30d001..a70b0b50ae 100644 --- a/CalPlayer/CalamityPlayer.cs +++ b/CalPlayer/CalamityPlayer.cs @@ -901,6 +901,7 @@ namespace CalamityMod.CalPlayer public bool kamiBoost = false; public bool avertorBonus = false; public bool divineBless = false; + public bool infiniteFlight = false; #endregion #region Minion @@ -2060,6 +2061,8 @@ namespace CalamityMod.CalPlayer AbleToSelectExoMech = false; + infiniteFlight = false; + EnchantHeldItemEffects(Player, Player.Calamity(), Player.ActiveItem()); } #endregion diff --git a/CalPlayer/CalamityPlayerMiscEffects.cs b/CalPlayer/CalamityPlayerMiscEffects.cs index cb88248316..3c774a492a 100644 --- a/CalPlayer/CalamityPlayerMiscEffects.cs +++ b/CalPlayer/CalamityPlayerMiscEffects.cs @@ -2419,6 +2419,12 @@ namespace CalamityMod.CalPlayer #region Other Buff Effects private void OtherBuffEffects() { + //Infinite flight granted by some boss attacks + if (infiniteFlight) + { + Player.wingTime = Player.wingTimeMax; + } + if (gravityNormalizer) { Player.buffImmune[BuffID.VortexDebuff] = true; diff --git a/CalamityConfig.cs b/CalamityConfig.cs index 99ad203ee3..8b6efb2f85 100644 --- a/CalamityConfig.cs +++ b/CalamityConfig.cs @@ -160,6 +160,22 @@ namespace CalamityMod [Range(0f, 100f)] [DefaultValue(SpeedrunTimerUI.DefaultTimerPosY)] public float SpeedrunTimerPosY { get; set; } + + [BackgroundColor(192, 54, 64, 192)] + [DefaultValue(true)] + public bool FlightBar { get; set; } + + [BackgroundColor(192, 54, 64, 192)] + [SliderColor(224, 165, 56, 128)] + [Range(0f, 100f)] + [DefaultValue(UI.FlightBar.DefaultFlightPosX)] + public float FlightBarPosX { get; set; } + + [BackgroundColor(192, 54, 64, 192)] + [SliderColor(224, 165, 56, 128)] + [Range(0f, 100f)] + [DefaultValue(UI.FlightBar.DefaultFlightPosY)] + public float FlightBarPosY { get; set; } #endregion #region General Gameplay Changes diff --git a/CalamityMod.cs b/CalamityMod.cs index 6f9d637a86..ad25c527e8 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -364,6 +364,7 @@ namespace CalamityMod StealthUI.Load(); ChargeMeterUI.Load(); AstralArcanumUI.Load(this); + FlightBar.Load(); // TODO -- Is this not possible to place in ModNPC.Load or ModNPC.SetStaticDefaults ? // Centralizing head texture registration like this seems absurdly stiff @@ -467,6 +468,7 @@ namespace CalamityMod StealthUI.Unload(); ChargeMeterUI.Unload(); AstralArcanumUI.Unload(); + FlightBar.Unload(); if (!Main.dedServ) { diff --git a/Localization/en-US/Mods.CalamityMod.Configs.hjson b/Localization/en-US/Mods.CalamityMod.Configs.hjson index 1db017a6d1..412b2070e4 100644 --- a/Localization/en-US/Mods.CalamityMod.Configs.hjson +++ b/Localization/en-US/Mods.CalamityMod.Configs.hjson @@ -170,6 +170,29 @@ CalamityConfig: { ''' } + FlightBar: { + Label: "[i:CalamityMod/DrewsWings] Display Flight Bar" + Tooltip: Enables the Flight Bar UI, which displays the player's flight capabilities + } + + FlightBarPosX: { + Label: "[i:LaserRuler] Flight Bar X Position" + Tooltip: + ''' + The X position of the Flight Bar, measured as a % of your screen height. + The meter can be dragged with the mouse if Lock Meter Positions is disabled. + ''' + } + + FlightBarPosY: { + Label: "[i:LaserRuler] Flight Bar Y Position" + Tooltip: + ''' + The Y position of the Flight Bar, measured as a % of your screen height. + The meter can be dragged with the mouse if Lock Meter Positions is disabled. + ''' + } + ForceTownSafety: { Label: "[i:Sunflower] Force Town Safety" Tooltip: diff --git a/Localization/en-US/Mods.CalamityMod.UI.hjson b/Localization/en-US/Mods.CalamityMod.UI.hjson index 88759dc5b6..eaa5e63043 100644 --- a/Localization/en-US/Mods.CalamityMod.UI.hjson +++ b/Localization/en-US/Mods.CalamityMod.UI.hjson @@ -3,6 +3,7 @@ SmallWorldWarning: "[c/FF0000:SMALL WORLDS NOT SUPPORTED BY CALAMITY]" Adrenaline: Adrenaline Nanomachines: Nanomachines Rage: Rage +Flight: "{0} Flight Remaining" Charge: Current Charge DefenseDamage: Defense Damage SulphurousWater: Sulphuric Poisoning diff --git a/NPCs/DevourerofGods/DevourerofGodsHead.cs b/NPCs/DevourerofGods/DevourerofGodsHead.cs index 76dcf7751f..68b78f813b 100644 --- a/NPCs/DevourerofGods/DevourerofGodsHead.cs +++ b/NPCs/DevourerofGods/DevourerofGodsHead.cs @@ -992,8 +992,7 @@ namespace CalamityMod.NPCs.DevourerofGods { if (!Main.player[Main.myPlayer].dead && Main.player[Main.myPlayer].active && Vector2.Distance(Main.player[Main.myPlayer].Center, NPC.Center) < CalamityGlobalNPC.CatchUpDistance350Tiles) { - if (Main.player[Main.myPlayer].wingTime < Main.player[Main.myPlayer].wingTimeMax) - Main.player[Main.myPlayer].wingTime = Main.player[Main.myPlayer].wingTimeMax; + Main.player[Main.myPlayer].Calamity().infiniteFlight = true; } } } diff --git a/NPCs/ExoMechs/Ares/AresBody.cs b/NPCs/ExoMechs/Ares/AresBody.cs index cef9013d2c..d6f4b04efd 100644 --- a/NPCs/ExoMechs/Ares/AresBody.cs +++ b/NPCs/ExoMechs/Ares/AresBody.cs @@ -811,8 +811,7 @@ namespace CalamityMod.NPCs.ExoMechs.Ares { if (!Main.player[Main.myPlayer].dead && Main.player[Main.myPlayer].active && Vector2.Distance(Main.player[Main.myPlayer].Center, NPC.Center) < DeathrayEnrageDistance) { - if (Main.player[Main.myPlayer].wingTime < Main.player[Main.myPlayer].wingTimeMax) - Main.player[Main.myPlayer].wingTime = Main.player[Main.myPlayer].wingTimeMax; + Main.player[Main.myPlayer].Calamity().infiniteFlight = true; } } diff --git a/Systems/UIManagementSystem.cs b/Systems/UIManagementSystem.cs index bb9155904d..a678d6e40c 100644 --- a/Systems/UIManagementSystem.cs +++ b/Systems/UIManagementSystem.cs @@ -96,6 +96,13 @@ namespace CalamityMod.Systems SulphurousWaterMeterUI.Draw(Main.spriteBatch, Main.LocalPlayer); return true; }, InterfaceScaleType.None)); + + //Flight bar + layers.Insert(mouseIndex, new LegacyGameInterfaceLayer("Flight UI", () => + { + FlightBar.Draw(Main.spriteBatch, Main.LocalPlayer); + return true; + }, InterfaceScaleType.None)); // Charge meter layers.Insert(mouseIndex, new LegacyGameInterfaceLayer("Charge UI", () => diff --git a/UI/FlightBar/FlightBar.cs b/UI/FlightBar/FlightBar.cs new file mode 100644 index 0000000000..c68ace19da --- /dev/null +++ b/UI/FlightBar/FlightBar.cs @@ -0,0 +1,241 @@ +using System; +using System.Text; +using CalamityMod.CalPlayer; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using ReLogic.Content; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.UI +{ + // TODO -- This can be made into a ModSystem with simple OnModLoad and Unload hooks. + public class FlightBar + { + // These values were handpicked on a 1080p screen by Amber. Please disregard the bizarre precision. + internal const float DefaultFlightPosX = 40.9375f; + internal const float DefaultFlightPosY = 7.2222223f; //yes really, that many twos were handpicked, how? your guess is as good as mine lmao + private const float MouseDragEpsilon = 0.05f; // 0.05% + private const int FlightAnimFrameDelay = 5; + private const int FlightAnimFrames = 17; + + private static int FlightAnimFrame = -1; + private static int FlightAnimTimer = 0; + + private static Vector2? dragOffset = null; + private static Texture2D borderTexture, flightBarAnimTexture, barTexture, disabledBarTexture, infiniteBarTexture, limitedBarTexture; + private static bool completedAnimation = false; + + private static Texture2D GetApplicableBorder(CalamityPlayer modPlayer) + { + if (modPlayer.Player.equippedWings != null && modPlayer.Player.wingTimeMax == 0) + return disabledBarTexture; + if (modPlayer.infiniteFlight && completedAnimation) + return infiniteBarTexture; + if (modPlayer.weakPetrification || modPlayer.vHex || modPlayer.icarusFolly || modPlayer.DoGExtremeGravity) + return limitedBarTexture; + return borderTexture; + } + + private static object GetFlightTime(CalamityPlayer modPlayer) + { + object result; + if (modPlayer.Player.equippedWings != null && modPlayer.Player.wingTimeMax == 0) + result = 0; + if (modPlayer.infiniteFlight && completedAnimation) + { + result = "∞"; //infinite flight + } + else + { + int currentFlight = (int)modPlayer.Player.wingTime; + int maxFlight = modPlayer.Player.wingTimeMax; + string percent = (100f * currentFlight / maxFlight).ToString("0.00"); + StringBuilder sb = new StringBuilder(32); + sb.Append(percent); + return sb.ToString(); + } + + return result; + } + + internal static void Load() + { + borderTexture = ModContent.Request("CalamityMod/UI/FlightBar/FlightBarBorder", AssetRequestMode.ImmediateLoad).Value; + flightBarAnimTexture = ModContent.Request("CalamityMod/UI/FlightBar/FlightBarAnim", AssetRequestMode.ImmediateLoad).Value; + barTexture = ModContent.Request("CalamityMod/UI/FlightBar/FlightBar", AssetRequestMode.ImmediateLoad).Value; + disabledBarTexture = ModContent.Request("CalamityMod/UI/FlightBar/FlightBarBorderDisabled", AssetRequestMode.ImmediateLoad).Value; + infiniteBarTexture = ModContent.Request("CalamityMod/UI/FlightBar/FlightBarBorderInfinite", AssetRequestMode.ImmediateLoad).Value; + limitedBarTexture = ModContent.Request("CalamityMod/UI/FlightBar/FlightBarBorderReduced", AssetRequestMode.ImmediateLoad).Value; + Reset(); + } + + internal static void Unload() + { + Reset(); + borderTexture = flightBarAnimTexture = barTexture = disabledBarTexture = infiniteBarTexture = limitedBarTexture = null; + FlightAnimFrame = -1; + FlightAnimTimer = 0; + completedAnimation = false; + } + + private static void Reset() => dragOffset = null; + + + + public static void Draw(SpriteBatch spriteBatch, Player player) + { + // Sanity check the planned position before drawing + Vector2 screenRatioPosition = new Vector2(CalamityConfig.Instance.FlightBarPosX, CalamityConfig.Instance.FlightBarPosY); + if (screenRatioPosition.X < 0f || screenRatioPosition.X > 100f) + screenRatioPosition.X = DefaultFlightPosX; + if (screenRatioPosition.Y < 0f || screenRatioPosition.Y > 100f) + screenRatioPosition.Y = DefaultFlightPosY; + + // Convert the screen ratio position to an absolute position in pixels + // Cast to integer to prevent blurriness which results from decimal pixel positions + float uiScale = Main.UIScale; + Vector2 screenPos = screenRatioPosition; + screenPos.X = (int)(screenPos.X * 0.01f * Main.screenWidth); + screenPos.Y = (int)(screenPos.Y * 0.01f * Main.screenHeight); + + CalamityPlayer modPlayer = player.Calamity(); + + // If not drawing the flight bar, save its latest position to config and leave. + if (CalamityConfig.Instance.FlightBar && player.wingsLogic > 0) + { + DrawFlightBar(spriteBatch, modPlayer, screenPos); + } + else + { + bool changed = false; + if (CalamityConfig.Instance.FlightBarPosX != screenRatioPosition.X) + { + CalamityConfig.Instance.FlightBarPosX = screenRatioPosition.X; + changed = true; + } + if (CalamityConfig.Instance.FlightBarPosY != screenRatioPosition.Y) + { + CalamityConfig.Instance.FlightBarPosY = screenRatioPosition.Y; + changed = true; + } + + if (changed) + CalamityMod.SaveConfig(CalamityConfig.Instance); + } + + Rectangle mouseHitbox = new Rectangle((int)Main.MouseScreen.X, (int)Main.MouseScreen.Y, 8, 8); + Rectangle flightBar = Utils.CenteredRectangle(screenPos, borderTexture.Size() * uiScale); + + MouseState ms = Mouse.GetState(); + Vector2 mousePos = Main.MouseScreen; + + // Handle mouse dragging + if (flightBar.Intersects(mouseHitbox)) + { + if (!CalamityConfig.Instance.MeterPosLock) + Main.LocalPlayer.mouseInterface = true; + + if (modPlayer.Player.equippedWings != null && modPlayer.Player.wingTimeMax > 0) //equipped wings and max wingtime above 0 (so not disabled bar) + { + string textToDisplay = CalamityUtils.GetText("UI.Flight").Format((GetFlightTime(modPlayer).ToString() + (modPlayer.infiniteFlight ? "" : "%"))); //the percent is here and not in localisation otherwise it looks like a dick when it's infinite flight + Main.instance.MouseText(textToDisplay, 0, 0, -1, -1, -1, -1); + } + + Vector2 newScreenRatioPosition = screenRatioPosition; + // As long as the mouse button is held down, drag the meter along with an offset. + if (!CalamityConfig.Instance.MeterPosLock && ms.LeftButton == ButtonState.Pressed) + { + // If the drag offset doesn't exist yet, create it. + if (!dragOffset.HasValue) + dragOffset = mousePos - screenPos; + + // Given the mouse's absolute current position, compute where the corner of the flight bar should be based on the original drag offset. + Vector2 newCorner = mousePos - dragOffset.GetValueOrDefault(Vector2.Zero); + + // Convert the new corner position into a screen ratio position. + newScreenRatioPosition.X = (100f * newCorner.X) / Main.screenWidth; + newScreenRatioPosition.Y = (100f * newCorner.Y) / Main.screenHeight; + } + + // Compute the change in position. If it is large enough, actually move the meter + Vector2 delta = newScreenRatioPosition - screenRatioPosition; + if (Math.Abs(delta.X) >= MouseDragEpsilon || Math.Abs(delta.Y) >= MouseDragEpsilon) + { + CalamityConfig.Instance.FlightBarPosX = newScreenRatioPosition.X; + CalamityConfig.Instance.FlightBarPosY = newScreenRatioPosition.Y; + } + + // When the mouse is released, save the config and destroy the drag offset. + if (ms.LeftButton == ButtonState.Released) + { + dragOffset = null; + CalamityMod.SaveConfig(CalamityConfig.Instance); + } + } + } + + /** + private const int FlightAnimFrameDelay = 5; + private const int FlightAnimFrames = 17; + + private static int FlightAnimFrame = -1; + private static int FlightAnimTimer = 0; + */ + private static void DrawFlightBar(SpriteBatch spriteBatch, CalamityPlayer modPlayer, Vector2 screenPos) + { + float uiScale = Main.UIScale; + Player player = modPlayer.Player; + float flightRatio = player.wingTime / player.wingTimeMax; + if (!completedAnimation && FlightAnimFrame == -1 && modPlayer.infiniteFlight) + FlightAnimFrame++; + if (FlightAnimFrame > -1) //animation started, complete it. + { + FlightAnimTimer++; + if (FlightAnimTimer >= FlightAnimFrameDelay) + { + if (FlightAnimFrame >= FlightAnimFrames) + { + FlightAnimFrame = -1; + FlightAnimTimer = 0; + completedAnimation = modPlayer.infiniteFlight; //completed animation sets to true if infinite flight still exists + } + else + { + FlightAnimTimer = 0; + FlightAnimFrame++; + } + } + } + Texture2D correctBorder = GetApplicableBorder(modPlayer); //Fetch texture after animation calculations in case update to infinite flight + + if (completedAnimation && !modPlayer.infiniteFlight && correctBorder != infiniteBarTexture) + completedAnimation = false; //reset flight anim once infinite flight expires. + + + float offset = (correctBorder.Width - barTexture.Width) * 0.5f; + spriteBatch.Draw(correctBorder, screenPos, null, Color.White, 0f, correctBorder.Size() * 0.5f, uiScale, SpriteEffects.None, 0); + if (correctBorder != disabledBarTexture && correctBorder != infiniteBarTexture) //neither requires an internal bar to be drawn + { + int correctHeight = (correctBorder == limitedBarTexture ? barTexture.Height / 2 : barTexture.Height); + Rectangle barRectangle = barTexture.Bounds; + barRectangle.Height = (int)(correctHeight * flightRatio); + Vector2 origin = correctBorder.Size() * 0.5f; + + spriteBatch.Draw(barTexture, screenPos - new Vector2(offset * uiScale, 11), barRectangle, Color.White, MathHelper.ToRadians(180), origin, uiScale, SpriteEffects.None, 0); + } + if (!completedAnimation && FlightAnimFrame >= 0) + { + Vector2 origin = new Vector2(correctBorder.Width * 0.5f, (correctBorder.Height / FlightAnimFrames) * 0.5f); + float animOffset = 5f; + float xOffset = (correctBorder.Width - flightBarAnimTexture.Width) / 2f; + int frameHeight = (flightBarAnimTexture.Height / FlightAnimFrames) - 1; + float yOffset = FlightAnimFrame == 0 ? 0 : ((correctBorder.Height / FlightAnimFrame) - frameHeight) / 2f ; + Vector2 sizeDiffOffset = new Vector2(xOffset, yOffset); + Rectangle animCropRect = new Rectangle(0, (frameHeight + 1) * FlightAnimFrame, flightBarAnimTexture.Width, frameHeight); + spriteBatch.Draw(flightBarAnimTexture, screenPos + sizeDiffOffset, animCropRect, Color.White, 0f, origin, uiScale, SpriteEffects.None, 0); + } + } + } +} diff --git a/UI/FlightBar/FlightBar.png b/UI/FlightBar/FlightBar.png new file mode 100644 index 0000000000..e41e4f2892 Binary files /dev/null and b/UI/FlightBar/FlightBar.png differ diff --git a/UI/FlightBar/FlightBarAnim.png b/UI/FlightBar/FlightBarAnim.png new file mode 100644 index 0000000000..30f87ea581 Binary files /dev/null and b/UI/FlightBar/FlightBarAnim.png differ diff --git a/UI/FlightBar/FlightBarBorder.png b/UI/FlightBar/FlightBarBorder.png new file mode 100644 index 0000000000..cbaad78eb1 Binary files /dev/null and b/UI/FlightBar/FlightBarBorder.png differ diff --git a/UI/FlightBar/FlightBarBorderDisabled.png b/UI/FlightBar/FlightBarBorderDisabled.png new file mode 100644 index 0000000000..be56fce589 Binary files /dev/null and b/UI/FlightBar/FlightBarBorderDisabled.png differ diff --git a/UI/FlightBar/FlightBarBorderInfinite.png b/UI/FlightBar/FlightBarBorderInfinite.png new file mode 100644 index 0000000000..932e19936c Binary files /dev/null and b/UI/FlightBar/FlightBarBorderInfinite.png differ diff --git a/UI/FlightBar/FlightBarBorderReduced.png b/UI/FlightBar/FlightBarBorderReduced.png new file mode 100644 index 0000000000..563b5cc4a7 Binary files /dev/null and b/UI/FlightBar/FlightBarBorderReduced.png differ