diff --git a/CalamityMod.cs b/CalamityMod.cs index a597d47b12..2c8a68bef2 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -275,8 +275,8 @@ namespace CalamityMod SkyManager.Instance["CalamityMod:StormWeaverFlash"] = new StormWeaverFlashSky(); CalamityShaders.LoadShaders(); - BaseFusableParticleSet.LoadParticleRenderTargets(); - Main.OnPreDraw += _ => BaseFusableParticleSet.PrepareFusableParticleTargets(); + FusableParticleManager.LoadParticleRenderTargets(); + Main.OnPreDraw += _ => FusableParticleManager.PrepareFusableParticleTargets(); RipperUI.Load(); AstralArcanumUI.Load(this); @@ -353,7 +353,7 @@ namespace CalamityMod TileFraming.Unload(); - Main.OnPreDraw -= _ => BaseFusableParticleSet.PrepareFusableParticleTargets(); + Main.OnPreDraw -= _ => FusableParticleManager.PrepareFusableParticleTargets(); RipperUI.Unload(); AstralArcanumUI.Unload(); @@ -409,7 +409,7 @@ namespace CalamityMod { // Update all fusable particles. // These are really only visual and as such don't really need any complex netcode. - foreach (BaseFusableParticleSet.FusableParticleRenderCollection particleSet in BaseFusableParticleSet.ParticleSets) + foreach (BaseFusableParticleSet.FusableParticleRenderCollection particleSet in FusableParticleManager.ParticleSets) { foreach (BaseFusableParticleSet.FusableParticle particle in particleSet.ParticleSet.Particles) particleSet.ParticleSet.UpdateBehavior(particle); diff --git a/ILEditing/ILChanges.cs b/ILEditing/ILChanges.cs index ac6a6fc181..c0738357cb 100644 --- a/ILEditing/ILChanges.cs +++ b/ILEditing/ILChanges.cs @@ -572,7 +572,7 @@ namespace CalamityMod.ILEditing return; } - cursor.EmitDelegate(() => BaseFusableParticleSet.RenderAllFusableParticles()); + cursor.EmitDelegate(() => FusableParticleManager.RenderAllFusableParticles()); } #endregion diff --git a/Particles/BaseFusableParticleSet.cs b/Particles/BaseFusableParticleSet.cs index 82811f11cb..d697ac8cca 100644 --- a/Particles/BaseFusableParticleSet.cs +++ b/Particles/BaseFusableParticleSet.cs @@ -1,11 +1,8 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using System; using System.Collections.Generic; -using System.Linq; using Terraria; using Terraria.ID; -using Terraria.ModLoader; namespace CalamityMod.Particles { @@ -38,9 +35,7 @@ namespace CalamityMod.Particles } } - internal static readonly List ParticleSets = new List(); - - public FusableParticleRenderCollection RenderCollection => GetParticleSetByType(GetType()); + public FusableParticleRenderCollection RenderCollection => FusableParticleManager.GetParticleRenderCollectionByType(GetType()); public List Particles = new List(); @@ -57,29 +52,6 @@ namespace CalamityMod.Particles public abstract void UpdateBehavior(FusableParticle particle); public abstract void DrawParticles(); - internal static void LoadParticleRenderTargets() - { - // Look through every type in the mod, and check if it's derived from BaseFusableParticleSet. - // If it is, create a default instance of said particle, save it, and create a RenderTarget2D for it to use. - foreach (Type type in typeof(CalamityMod).Assembly.GetTypes()) - { - // Don't load abstract classes; they cannot have instances. - if (type.IsAbstract) - continue; - - if (type.IsSubclassOf(typeof(BaseFusableParticleSet))) - { - BaseFusableParticleSet instance = Activator.CreateInstance(type) as BaseFusableParticleSet; - RenderTarget2D renderTarget = null; - if (Main.netMode != NetmodeID.Server) - renderTarget = new RenderTarget2D(Main.instance.GraphicsDevice, Main.screenWidth, Main.screenHeight, false, default, default, 0, RenderTargetUsage.PreserveContents); - - FusableParticleRenderCollection particleRenderCollection = new FusableParticleRenderCollection(instance, renderTarget); - ParticleSets.Add(particleRenderCollection); - } - } - } - internal void PrepareRenderTargetForDrawing() { // Don't bother doing anything if this method is called serverside. @@ -102,51 +74,5 @@ namespace CalamityMod.Particles // Return to using the previous render targets after done drawing everything to this target. Main.instance.GraphicsDevice.SetRenderTarget(null); } - - internal static void PrepareFusableParticleTargets() - { - // Don't attempt to draw anything serverside. - if (Main.netMode == NetmodeID.Server) - return; - - // Prepare the render target for all fusable particles. - foreach (FusableParticleRenderCollection particleSet in ParticleSets) - particleSet.ParticleSet.PrepareRenderTargetForDrawing(); - } - - internal static void RenderAllFusableParticles() - { - foreach (FusableParticleRenderCollection particleRenderSet in ParticleSets) - { - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.instance.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - - BaseFusableParticleSet particleSet = particleRenderSet.ParticleSet; - - // Draw the current target with the specified shader. - particleSet.EdgeShader.Parameters["edgeBorderSize"].SetValue(particleSet.BorderSize); - particleSet.EdgeShader.Parameters["borderShouldBeSolid"].SetValue(particleSet.BorderShouldBeSolid); - particleSet.EdgeShader.Parameters["edgeBorderColor"].SetValue(particleSet.BorderColor.ToVector3()); - particleSet.EdgeShader.Parameters["screenArea"].SetValue(new Vector2(Main.screenWidth, Main.screenHeight) / Main.GameViewMatrix.Zoom); - particleSet.EdgeShader.Parameters["screenMoveOffset"].SetValue(Main.screenPosition - Main.screenLastPosition); - particleSet.EdgeShader.Parameters["uWorldPosition"].SetValue(Main.screenPosition); - particleSet.EdgeShader.Parameters["renderTargetArea"].SetValue(new Vector2(particleSet.GetRenderTarget.Width, particleSet.GetRenderTarget.Height)); - particleSet.EdgeShader.Parameters["invertedScreen"].SetValue(Main.LocalPlayer.gravDir == -1f); - - Main.graphics.GraphicsDevice.Textures[1] = particleSet.BackgroundTexture; - particleSet.EdgeShader.Parameters["uImageSize1"].SetValue(particleSet.BackgroundTexture.Size()); - particleSet.EdgeShader.CurrentTechnique.Passes[0].Apply(); - - particleSet.PrepareOptionalShaderData(particleSet.EdgeShader); - - Main.spriteBatch.Draw(particleSet.GetRenderTarget, Vector2.Zero, Color.White); - - Main.spriteBatch.End(); - } - } - - public static FusableParticleRenderCollection GetParticleSetByType(Type type) - { - return ParticleSets.First(s => s.ParticleSet.GetType() == type); - } } } diff --git a/Particles/FusableParticleManager.cs b/Particles/FusableParticleManager.cs new file mode 100644 index 0000000000..eee7419a0b --- /dev/null +++ b/Particles/FusableParticleManager.cs @@ -0,0 +1,98 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using Terraria; +using Terraria.ID; + +using static CalamityMod.Particles.BaseFusableParticleSet; + +namespace CalamityMod.Particles +{ + public static class FusableParticleManager + { + internal static readonly List ParticleSets = new List(); + + internal static void LoadParticleRenderTargets() + { + // Look through every type in the mod, and check if it's derived from BaseFusableParticleSet. + // If it is, create a default instance of said particle, save it, and create a RenderTarget2D for it to use. + foreach (Type type in typeof(CalamityMod).Assembly.GetTypes()) + { + // Don't load abstract classes; they cannot have instances. + if (type.IsAbstract) + continue; + + if (type.IsSubclassOf(typeof(BaseFusableParticleSet))) + { + BaseFusableParticleSet instance = Activator.CreateInstance(type) as BaseFusableParticleSet; + RenderTarget2D renderTarget = null; + if (Main.netMode != NetmodeID.Server) + renderTarget = new RenderTarget2D(Main.instance.GraphicsDevice, Main.screenWidth, Main.screenHeight, false, default, default, 0, RenderTargetUsage.PreserveContents); + + FusableParticleRenderCollection particleRenderCollection = new FusableParticleRenderCollection(instance, renderTarget); + ParticleSets.Add(particleRenderCollection); + } + } + } + + internal static void PrepareFusableParticleTargets() + { + // Don't attempt to draw anything serverside. + if (Main.netMode == NetmodeID.Server) + return; + + // Prepare the render target for all fusable particles. + foreach (FusableParticleRenderCollection particleSet in ParticleSets) + particleSet.ParticleSet.PrepareRenderTargetForDrawing(); + } + + internal static void RenderAllFusableParticles() + { + foreach (FusableParticleRenderCollection particleRenderSet in ParticleSets) + { + // Restart the sprite batch. This must be done with an immediate sort mode since a shader is going to be applied. + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.instance.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); + + BaseFusableParticleSet particleSet = particleRenderSet.ParticleSet; + Effect edgeShader = particleSet.EdgeShader; + + // Draw the current target with the specified shader. + edgeShader.Parameters["edgeBorderSize"].SetValue(particleSet.BorderSize); + edgeShader.Parameters["borderShouldBeSolid"].SetValue(particleSet.BorderShouldBeSolid); + edgeShader.Parameters["edgeBorderColor"].SetValue(particleSet.BorderColor.ToVector3()); + edgeShader.Parameters["screenArea"].SetValue(new Vector2(Main.screenWidth, Main.screenHeight) / Main.GameViewMatrix.Zoom); + edgeShader.Parameters["screenMoveOffset"].SetValue(Main.screenPosition - Main.screenLastPosition); + edgeShader.Parameters["uWorldPosition"].SetValue(Main.screenPosition); + edgeShader.Parameters["renderTargetArea"].SetValue(new Vector2(particleSet.GetRenderTarget.Width, particleSet.GetRenderTarget.Height)); + edgeShader.Parameters["invertedScreen"].SetValue(Main.LocalPlayer.gravDir == -1f); + + // Prepare the background texture for loading. + Main.graphics.GraphicsDevice.Textures[1] = particleSet.BackgroundTexture; + edgeShader.Parameters["uImageSize1"].SetValue(particleSet.BackgroundTexture.Size()); + + // Do any optional shader operations prior to applying the shader. + particleSet.PrepareOptionalShaderData(edgeShader); + + // Apply the shader. This is what is used to connect separated blobs together. + edgeShader.CurrentTechnique.Passes[0].Apply(); + + // And draw the particle set's render target with said shader. + Main.spriteBatch.Draw(particleSet.GetRenderTarget, Vector2.Zero, Color.White); + + Main.spriteBatch.End(); + } + } + + internal static FusableParticleRenderCollection GetParticleRenderCollectionByType(Type type) + { + return ParticleSets.First(s => s.ParticleSet.GetType() == type); + } + + public static T GetParticleSetByType() where T : BaseFusableParticleSet + { + return ParticleSets.First(s => s.ParticleSet.GetType() == typeof(T)).ParticleSet as T; + } + } +} diff --git a/Particles/GhostlyFusableParticleSet.cs b/Particles/GhostlyFusableParticleSet.cs index ccc0aae61b..3cd60ea05f 100644 --- a/Particles/GhostlyFusableParticleSet.cs +++ b/Particles/GhostlyFusableParticleSet.cs @@ -9,7 +9,6 @@ namespace CalamityMod.Particles { public class GhostlyFusableParticleSet : BaseFusableParticleSet { - public static GhostlyFusableParticleSet Instance => GetParticleSetByType(typeof(GhostlyFusableParticleSet)).ParticleSet as GhostlyFusableParticleSet; public override float BorderSize => 5f; public override bool BorderShouldBeSolid => false; public override Color BorderColor => Color.Lerp(Color.LightBlue, Color.Red, 0.35f) * 1.4f; diff --git a/Projectiles/Magic/SpiritCongregation.cs b/Projectiles/Magic/SpiritCongregation.cs index 8fd45512af..c421392fb5 100644 --- a/Projectiles/Magic/SpiritCongregation.cs +++ b/Projectiles/Magic/SpiritCongregation.cs @@ -189,12 +189,12 @@ namespace CalamityMod.Projectiles.Magic { // Summon a base particle. Vector2 spawnPosition = projectile.Center + Main.rand.NextVector2Circular(1f, 1f) * particleSize / 26f; - GhostlyFusableParticleSet.Instance.SpawnParticle(spawnPosition, particleSize); + FusableParticleManager.GetParticleSetByType().SpawnParticle(spawnPosition, particleSize); // And an "ahead" particle that spawns based on current movement. // This causes the "head" of the overall thing to have bumps when moving. spawnPosition += projectile.velocity.RotatedByRandom(1.38f) * particleSize / 65f; - GhostlyFusableParticleSet.Instance.SpawnParticle(spawnPosition, particleSize * 0.4f); + FusableParticleManager.GetParticleSetByType().SpawnParticle(spawnPosition, particleSize * 0.4f); } // Release gas projectiles randomly. This does not happen when dying. diff --git a/Projectiles/Magic/SpiritDust.cs b/Projectiles/Magic/SpiritDust.cs index 4ba7794f89..f4caf1f1e1 100644 --- a/Projectiles/Magic/SpiritDust.cs +++ b/Projectiles/Magic/SpiritDust.cs @@ -25,7 +25,7 @@ namespace CalamityMod.Projectiles.Magic { float radius = MathHelper.SmoothStep(80f, 48f, 1f - projectile.timeLeft / 90f); Vector2 spawnPosition = projectile.Center + Main.rand.NextVector2Circular(5f, 5f) * radius / 130f; - GhostlyFusableParticleSet.Instance.SpawnParticle(spawnPosition, radius); + FusableParticleManager.GetParticleSetByType().SpawnParticle(spawnPosition, radius); } } }