From 08f48856542f64a4e051d7e78b0f137d33997164 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Fri, 10 Jun 2022 04:21:38 +0200 Subject: [PATCH 1/4] Help me tomat hihi --- CalamityMod.cs | 1 + ForegroundDrawing/ForegroundManager.cs | 58 +++++++++++++++++++++++++ ILEditing/ILChangesLoading.cs | 2 + ILEditing/MechanicILChanges.cs | 15 ++++++- Tiles/BaseTiles/BaseBossRelic.cs | 60 ++++++++++++++++++++++++-- 5 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 ForegroundDrawing/ForegroundManager.cs diff --git a/CalamityMod.cs b/CalamityMod.cs index 87daf4267e..21c6d03b56 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -194,6 +194,7 @@ namespace CalamityMod { LoadClient(); GeneralParticleHandler.Load(); + ForegroundDrawing.ForegroundManager.Load(); } CooldownRegistry.Load(); diff --git a/ForegroundDrawing/ForegroundManager.cs b/ForegroundDrawing/ForegroundManager.cs new file mode 100644 index 0000000000..1375271bfa --- /dev/null +++ b/ForegroundDrawing/ForegroundManager.cs @@ -0,0 +1,58 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ReLogic.Content; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using Terraria; +using Terraria.ModLoader; +using Terraria.ModLoader.Core; + +namespace CalamityMod.ForegroundDrawing +{ + interface IForegroundTile + { + void ForegroundDraw(int x, int y, SpriteBatch spriteBatch); + } + + public static class ForegroundManager + { + private static List _foregroundElements; + private static int _foregroundElementCount; + public static bool drawToScreenLastFrame = false; + + internal static void Load() + { + _foregroundElements = new List(1000); + _foregroundElementCount = 0; + } + + public static void AddForegroundDrawingPoint(int x, int y) + { + if (drawToScreenLastFrame) + { + _foregroundElements.Add(new Point(x, y)); + _foregroundElementCount++; + } + } + + + public static void DrawTiles() + { + drawToScreenLastFrame = Main.drawToScreen; + + for (int i = 0; i < _foregroundElementCount; i++) + { + ushort type = Main.tile[_foregroundElements[i]].TileType; + if (TileLoader.GetTile(type) is IForegroundTile fgTile) + fgTile.ForegroundDraw(_foregroundElements[i].X, _foregroundElements[i].Y, Main.spriteBatch); + } + + if (!Main.drawToScreen) return; + _foregroundElementCount = 0; + _foregroundElements.Clear(); + + } + } +} diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 01d86af731..95975898c1 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -54,6 +54,7 @@ namespace CalamityMod.ILEditing IL.Terraria.GameContent.Drawing.TileDrawing.DrawPartialLiquid += DrawCustomLava; IL.Terraria.GameContent.Liquid.LiquidRenderer.InternalDraw += DrawCustomLava2; IL.Terraria.Main.oldDrawWater += DrawCustomLava3; + On.Terraria.Main.DrawGore += DrawForegroundStuff; // TODO -- Revisit this. It's not an extremely important thing, but it'd be ideal to not just abandon it. // IL.Terraria.WaterfallManager.DrawWaterfall += DrawCustomLavafalls; @@ -150,6 +151,7 @@ namespace CalamityMod.ILEditing IL.Terraria.WaterfallManager.DrawWaterfall -= DrawCustomLavafalls; On.Terraria.NPC.Collision_DecideFallThroughPlatforms -= EnableCalamityBossPlatformCollision; IL.Terraria.Wiring.HitWireSingle -= AddTwinklersToStatue; + On.Terraria.Main.DrawGore -= DrawForegroundStuff; // Damage and health balance IL.Terraria.Main.DamageVar -= AdjustDamageVariance; diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 91380988f1..7fd2971ffd 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -2,6 +2,7 @@ using CalamityMod.Buffs.DamageOverTime; using CalamityMod.CalPlayer; using CalamityMod.Cooldowns; +using CalamityMod.ForegroundDrawing; using CalamityMod.FluidSimulation; using CalamityMod.Items.Dyes; using CalamityMod.NPCs.Astral; @@ -32,6 +33,7 @@ using Terraria.ModLoader; using Terraria.UI.Gamepad; using Terraria.Utilities; + namespace CalamityMod.ILEditing { public partial class ILChanges @@ -876,8 +878,8 @@ namespace CalamityMod.ILEditing c.EmitDelegate>(arr => { // resize the array and add our custom firefly - Array.Resize(ref arr, arr.Length+1); - arr[arr.Length-1] = (short)ModContent.NPCType(); + Array.Resize(ref arr, arr.Length + 1); + arr[arr.Length - 1] = (short)ModContent.NPCType(); return arr; }); @@ -889,5 +891,14 @@ namespace CalamityMod.ILEditing throw new Exception("Hook location not found, switch(*) { case 54: ..."); } #endregion Statue Additions + + #region Foreground tiles drawing + private static void DrawForegroundStuff(On.Terraria.Main.orig_DrawGore orig, Main self) + { + orig(self); + if (Main.PlayerLoaded && !Main.gameMenu) + ForegroundManager.DrawTiles(); + } + #endregion } } diff --git a/Tiles/BaseTiles/BaseBossRelic.cs b/Tiles/BaseTiles/BaseBossRelic.cs index 3437446e5d..6267dd9b41 100644 --- a/Tiles/BaseTiles/BaseBossRelic.cs +++ b/Tiles/BaseTiles/BaseBossRelic.cs @@ -9,11 +9,11 @@ using Terraria.Enums; using System; using ReLogic.Content; using Terraria.Localization; - +using CalamityMod.ForegroundDrawing; namespace CalamityMod.Tiles.BaseTiles { - public abstract class BaseBossRelic : ModTile + public abstract class BaseBossRelic : ModTile, IForegroundTile { public const int FrameWidth = 18 * 3; public const int FrameHeight = 18 * 4; @@ -94,7 +94,61 @@ namespace CalamityMod.Tiles.BaseTiles // This allows us to draw things in SpecialDraw if (drawData.tileFrameX % FrameWidth == 0 && drawData.tileFrameY % FrameHeight == 0) { - Main.instance.TilesRenderer.AddSpecialLegacyPoint(i, j); + ForegroundManager.AddForegroundDrawingPoint(i, j); + + //Main.instance.TilesRenderer.AddSpecialLegacyPoint(i, j); + } + } + + public void ForegroundDraw(int i, int j, SpriteBatch spriteBatch) + { + /* + // This is lighting-mode specific, always include this if you draw tiles manually + Vector2 offScreen = new Vector2(Main.offScreenRange); + if (Main.drawToScreen) + { + offScreen = Vector2.Zero; + } + */ + + // Take the tile, check if it actually exists + Point p = new Point(i, j); + Tile tile = Main.tile[p.X, p.Y]; + if (tile == null || !tile.HasTile) + { + return; + } + + // Get the initial draw parameters + Texture2D texture = RelicTexture.Value; + + int frameY = tile.TileFrameX / FrameWidth; // Picks the frame on the sheet based on the placeStyle of the item + Rectangle frame = texture.Frame(HorizontalFrames, VerticalFrames, 0, frameY); + + Vector2 origin = frame.Size() / 2f; + Vector2 worldPos = p.ToWorldCoordinates(24f, 64f); + + Color color = Lighting.GetColor(p.X, p.Y); + + bool direction = tile.TileFrameY / FrameHeight != 0; // This is related to the alternate tile data we registered before + SpriteEffects effects = direction ? SpriteEffects.FlipHorizontally : SpriteEffects.None; + + // Some math magic to make it smoothly move up and down over time + const float TwoPi = (float)Math.PI * 2f; + float offset = (float)Math.Sin(Main.GlobalTimeWrappedHourly * TwoPi / 5f); + Vector2 drawPos = worldPos - Main.screenPosition + new Vector2(0f, -40f) + new Vector2(0f, offset * 4f); + + // Draw the main texture + spriteBatch.Draw(texture, drawPos, frame, color, 0f, origin, 1f, effects, 0f); + + // Draw the periodic glow effect + float scale = (float)Math.Sin(Main.GlobalTimeWrappedHourly * TwoPi / 2f) * 0.3f + 0.7f; + Color effectColor = color; + effectColor.A = 0; + effectColor = effectColor * 0.1f * scale; + for (float num5 = 0f; num5 < 1f; num5 += 355f / (678f * (float)Math.PI)) + { + spriteBatch.Draw(texture, drawPos + (TwoPi * num5).ToRotationVector2() * (6f + offset * 2f), frame, effectColor, 0f, origin, 1f, effects, 0f); } } From 5846f50cea732cfd7f90cb06d45dfd27ba983248 Mon Sep 17 00:00:00 2001 From: Tomat Date: Thu, 9 Jun 2022 20:54:32 -0700 Subject: [PATCH 2/4] Fix foreground drawing, delegate clearing to a different detour. --- ForegroundDrawing/ForegroundManager.cs | 19 ++++++++----------- ILEditing/ILChangesLoading.cs | 4 ++++ ILEditing/MechanicILChanges.cs | 8 ++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ForegroundDrawing/ForegroundManager.cs b/ForegroundDrawing/ForegroundManager.cs index 1375271bfa..dfb5c285af 100644 --- a/ForegroundDrawing/ForegroundManager.cs +++ b/ForegroundDrawing/ForegroundManager.cs @@ -20,7 +20,6 @@ namespace CalamityMod.ForegroundDrawing { private static List _foregroundElements; private static int _foregroundElementCount; - public static bool drawToScreenLastFrame = false; internal static void Load() { @@ -28,31 +27,29 @@ namespace CalamityMod.ForegroundDrawing _foregroundElementCount = 0; } + private static bool DrawToScreen() => Lighting.UpdateEveryFrame || Main.drawToScreen; + public static void AddForegroundDrawingPoint(int x, int y) { - if (drawToScreenLastFrame) - { - _foregroundElements.Add(new Point(x, y)); - _foregroundElementCount++; - } + _foregroundElements.Add(new Point(x, y)); + _foregroundElementCount++; } public static void DrawTiles() { - drawToScreenLastFrame = Main.drawToScreen; - for (int i = 0; i < _foregroundElementCount; i++) { ushort type = Main.tile[_foregroundElements[i]].TileType; if (TileLoader.GetTile(type) is IForegroundTile fgTile) fgTile.ForegroundDraw(_foregroundElements[i].X, _foregroundElements[i].Y, Main.spriteBatch); } + } - if (!Main.drawToScreen) return; - _foregroundElementCount = 0; + public static void ClearTiles() + { _foregroundElements.Clear(); - + _foregroundElementCount = 0; } } } diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 95975898c1..54834f371d 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -55,6 +55,8 @@ namespace CalamityMod.ILEditing IL.Terraria.GameContent.Liquid.LiquidRenderer.InternalDraw += DrawCustomLava2; IL.Terraria.Main.oldDrawWater += DrawCustomLava3; On.Terraria.Main.DrawGore += DrawForegroundStuff; + //On.Terraria.GameContent.Drawing.TileDrawing.ClearLegacyCachedDraws += ClearForegroundStuff; + On.Terraria.GameContent.Drawing.TileDrawing.PreDrawTiles += ClearForegroundStuff; // TODO -- Revisit this. It's not an extremely important thing, but it'd be ideal to not just abandon it. // IL.Terraria.WaterfallManager.DrawWaterfall += DrawCustomLavafalls; @@ -152,6 +154,8 @@ namespace CalamityMod.ILEditing On.Terraria.NPC.Collision_DecideFallThroughPlatforms -= EnableCalamityBossPlatformCollision; IL.Terraria.Wiring.HitWireSingle -= AddTwinklersToStatue; On.Terraria.Main.DrawGore -= DrawForegroundStuff; + //On.Terraria.GameContent.Drawing.TileDrawing.ClearLegacyCachedDraws -= ClearForegroundStuff; + On.Terraria.GameContent.Drawing.TileDrawing.PreDrawTiles -= ClearForegroundStuff; // Damage and health balance IL.Terraria.Main.DamageVar -= AdjustDamageVariance; diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 7fd2971ffd..8c3ca498a2 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -899,6 +899,14 @@ namespace CalamityMod.ILEditing if (Main.PlayerLoaded && !Main.gameMenu) ForegroundManager.DrawTiles(); } + + private static void ClearForegroundStuff(On.Terraria.GameContent.Drawing.TileDrawing.orig_PreDrawTiles orig, Terraria.GameContent.Drawing.TileDrawing self, bool solidLayer, bool forRenderTargets, bool intoRenderTargets) + { + orig(self, solidLayer, forRenderTargets, intoRenderTargets); + + if (!solidLayer && (intoRenderTargets || Lighting.UpdateEveryFrame)) + ForegroundManager.ClearTiles(); + } #endregion } } From f3b5cb09f93f91cfe840bd8ff82b6b10c21c270e Mon Sep 17 00:00:00 2001 From: Tomat Date: Thu, 9 Jun 2022 20:55:50 -0700 Subject: [PATCH 3/4] Remove unused method from previous commit. --- ForegroundDrawing/ForegroundManager.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ForegroundDrawing/ForegroundManager.cs b/ForegroundDrawing/ForegroundManager.cs index dfb5c285af..01c279313a 100644 --- a/ForegroundDrawing/ForegroundManager.cs +++ b/ForegroundDrawing/ForegroundManager.cs @@ -27,8 +27,6 @@ namespace CalamityMod.ForegroundDrawing _foregroundElementCount = 0; } - private static bool DrawToScreen() => Lighting.UpdateEveryFrame || Main.drawToScreen; - public static void AddForegroundDrawingPoint(int x, int y) { _foregroundElements.Add(new Point(x, y)); From bed43ff85fde77037704d3c360bbac69053610a5 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Wed, 13 Jul 2022 02:09:16 +0200 Subject: [PATCH 4/4] Fix & removed test code --- ILEditing/MechanicILChanges.cs | 6 ++-- Tiles/BaseTiles/BaseBossRelic.cs | 58 ++------------------------------ 2 files changed, 6 insertions(+), 58 deletions(-) diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 342d6853e1..a681c72ab0 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -934,13 +934,15 @@ namespace CalamityMod.ILEditing if (Main.PlayerLoaded && !Main.gameMenu) ForegroundManager.DrawTiles(); } - + private static void ClearForegroundStuff(On.Terraria.GameContent.Drawing.TileDrawing.orig_PreDrawTiles orig, Terraria.GameContent.Drawing.TileDrawing self, bool solidLayer, bool forRenderTargets, bool intoRenderTargets) { orig(self, solidLayer, forRenderTargets, intoRenderTargets); - + if (!solidLayer && (intoRenderTargets || Lighting.UpdateEveryFrame)) ForegroundManager.ClearTiles(); + } + #endregion #region Tile ping overlay private static void ClearTilePings(On.Terraria.GameContent.Drawing.TileDrawing.orig_Draw orig, Terraria.GameContent.Drawing.TileDrawing self, bool solidLayer, bool forRenderTargets, bool intoRenderTargets, int waterStyleOverride) diff --git a/Tiles/BaseTiles/BaseBossRelic.cs b/Tiles/BaseTiles/BaseBossRelic.cs index 700edca932..ccbdd0b207 100644 --- a/Tiles/BaseTiles/BaseBossRelic.cs +++ b/Tiles/BaseTiles/BaseBossRelic.cs @@ -13,7 +13,7 @@ using CalamityMod.ForegroundDrawing; namespace CalamityMod.Tiles.BaseTiles { - public abstract class BaseBossRelic : ModTile, IForegroundTile + public abstract class BaseBossRelic : ModTile { public const int FrameWidth = 18 * 3; public const int FrameHeight = 18 * 4; @@ -94,61 +94,7 @@ namespace CalamityMod.Tiles.BaseTiles // This allows us to draw things in SpecialDraw if (drawData.tileFrameX % FrameWidth == 0 && drawData.tileFrameY % FrameHeight == 0) { - ForegroundManager.AddForegroundDrawingPoint(i, j); - - //Main.instance.TilesRenderer.AddSpecialLegacyPoint(i, j); - } - } - - public void ForegroundDraw(int i, int j, SpriteBatch spriteBatch) - { - /* - // This is lighting-mode specific, always include this if you draw tiles manually - Vector2 offScreen = new Vector2(Main.offScreenRange); - if (Main.drawToScreen) - { - offScreen = Vector2.Zero; - } - */ - - // Take the tile, check if it actually exists - Point p = new Point(i, j); - Tile tile = Main.tile[p.X, p.Y]; - if (tile == null || !tile.HasTile) - { - return; - } - - // Get the initial draw parameters - Texture2D texture = RelicTexture.Value; - - int frameY = tile.TileFrameX / FrameWidth; // Picks the frame on the sheet based on the placeStyle of the item - Rectangle frame = texture.Frame(HorizontalFrames, VerticalFrames, 0, frameY); - - Vector2 origin = frame.Size() / 2f; - Vector2 worldPos = p.ToWorldCoordinates(24f, 64f); - - Color color = Lighting.GetColor(p.X, p.Y); - - bool direction = tile.TileFrameY / FrameHeight != 0; // This is related to the alternate tile data we registered before - SpriteEffects effects = direction ? SpriteEffects.FlipHorizontally : SpriteEffects.None; - - // Some math magic to make it smoothly move up and down over time - const float TwoPi = (float)Math.PI * 2f; - float offset = (float)Math.Sin(Main.GlobalTimeWrappedHourly * TwoPi / 5f); - Vector2 drawPos = worldPos - Main.screenPosition + new Vector2(0f, -40f) + new Vector2(0f, offset * 4f); - - // Draw the main texture - spriteBatch.Draw(texture, drawPos, frame, color, 0f, origin, 1f, effects, 0f); - - // Draw the periodic glow effect - float scale = (float)Math.Sin(Main.GlobalTimeWrappedHourly * TwoPi / 2f) * 0.3f + 0.7f; - Color effectColor = color; - effectColor.A = 0; - effectColor = effectColor * 0.1f * scale; - for (float num5 = 0f; num5 < 1f; num5 += 355f / (678f * (float)Math.PI)) - { - spriteBatch.Draw(texture, drawPos + (TwoPi * num5).ToRotationVector2() * (6f + offset * 2f), frame, effectColor, 0f, origin, 1f, effects, 0f); + Main.instance.TilesRenderer.AddSpecialLegacyPoint(i, j); } }