diff --git a/CalamityMod.cs b/CalamityMod.cs index c97cc09828..556b726308 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -207,7 +207,10 @@ namespace CalamityMod ExoChairSlowdownHotkey = RegisterHotKey("Exo Chair Slow Down", "RightShift"); if (!Main.dedServ) + { LoadClient(); + GeneralParticleHandler.Load(); + } BossRushEvent.Load(); BossHealthBarManager.Load(this); @@ -396,6 +399,7 @@ namespace CalamityMod Main.rainTexture = rainOriginal; Main.manaTexture = manaOriginal; Main.flyingCarpetTexture = carpetOriginal; + GeneralParticleHandler.Unload(); } Mount.mounts[Mount.Unicorn].dashSpeed /= CalamityPlayer.UnicornSpeedNerfPower; Mount.mounts[Mount.Unicorn].runSpeed /= CalamityPlayer.UnicornSpeedNerfPower; diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index ef2a89146b..bf553dd284 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -46,6 +46,7 @@ namespace CalamityMod.ILEditing IL.Terraria.Player.AddBuff += AllowBuffTimeStackingForManaBurn; IL.Terraria.Main.DoDraw += DrawFusableParticles; IL.Terraria.Main.DoDraw += DrawAshParticles; + On.Terraria.Main.DrawInterface += DrawGeneralParticles; On.Terraria.Main.SetDisplayMode += ResetRenderTargetSizes; IL.Terraria.Main.DrawTiles += DrawCustomLava; IL.Terraria.GameContent.Liquid.LiquidRenderer.InternalDraw += DrawCustomLava2; @@ -121,6 +122,7 @@ namespace CalamityMod.ILEditing IL.Terraria.Player.AddBuff -= AllowBuffTimeStackingForManaBurn; IL.Terraria.Main.DoDraw -= DrawFusableParticles; IL.Terraria.Main.DoDraw -= DrawAshParticles; + On.Terraria.Main.DrawInterface -= DrawGeneralParticles; On.Terraria.Main.SetDisplayMode -= ResetRenderTargetSizes; IL.Terraria.Main.DrawTiles -= DrawCustomLava; IL.Terraria.GameContent.Liquid.LiquidRenderer.InternalDraw -= DrawCustomLava2; diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 39de7df419..9256e5f4bd 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -538,6 +538,18 @@ namespace CalamityMod.ILEditing } #endregion Ash Particle Rendering + #region General Particle Rendering + private static void DrawParticles(On.Terraria.Main.orig_DrawInterface orig, Main self, GameTime gameTime) + { + Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, default, default, default, default, Main.GameViewMatrix.ZoomMatrix); + GeneralParticleHandler.DrawAllParticles(Main.spriteBatch); + Main.spriteBatch.End(); + + orig(self, gameTime); + } + + #endregion General Particle Rendering + #region Custom Lava Visuals private static void ResetRenderTargetSizes(On.Terraria.Main.orig_SetDisplayMode orig, int width, int height, bool fullscreen) { diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs new file mode 100644 index 0000000000..43d5c56284 --- /dev/null +++ b/Particles/GeneralParticleHandler.cs @@ -0,0 +1,98 @@ +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using Terraria.ModLoader; + +namespace CalamityMod.Particles +{ + public static class GeneralParticleHandler + { + /// + /// This is the maximum of particles allowed. Particles marked as important may replace nonimportant particles when the cap is reached + /// + // May be a good idea to make this into a config option + private static readonly int particleCap = 500; + + private static Particle[] particles; + private static int nextVacantIndex; + private static int activeParticles; + //Static list for details concerning every particle type + private static Dictionary particleTypes; + private static Dictionary particleTextures; + private static List particleInstances; + //Lists used when drawing particles batched + private static List batchedAlphaBlendParticles; + private static List batchedAdditiveBlendParticles; + + internal static void Load() + { + particles = new Particle[particleCap]; + particleTypes = new Dictionary(); + particleTextures = new Dictionary(); + particleInstances = new List(); + + batchedAlphaBlendParticles = new List(particleCap); + batchedAdditiveBlendParticles = new List(particleCap); + + Type baseParticleType = typeof(Particle); + CalamityMod calamity = ModContent.GetInstance(); + + foreach (Type type in calamity.Code.GetTypes()) + { + if (type.IsSubclassOf(baseParticleType) && !type.IsAbstract && type != baseParticleType) + { + int ID = particleTypes.Count; //Get the ID of the particle + particleTypes[type] = ID; + + Particle instance = (Particle)FormatterServices.GetUninitializedObject(type); + particleInstances.Add(instance); + + string texturePath = type.Namespace.Replace('.', '/') + "/" + type.Name; + if (instance.Texture != "") + texturePath = instance.Texture; + particleTextures[ID] = ModContent.GetTexture(texturePath); + } + } + } + + internal static void Unload() + { + particles = null; + particleTypes = null; + particleTextures = null; + particleInstances = null; + batchedAlphaBlendParticles = null; + batchedAdditiveBlendParticles = null; + } + + + + /// + /// Spawns the particle instance provided into the world. If the particle limit is reached but the particle is marked as important, it will try to replace a non important particle. + /// + public static void SpawnParticle(Particle particle) + { + if (activeParticles == particleCap && !particle.Important) + return; + + particles[nextVacantIndex] = particle; + particle.ID = nextVacantIndex; + particle.Type = particleTypes[particle.GetType()]; + + if (nextVacantIndex + 1 < particles.Length && particles[nextVacantIndex + 1] == null) + nextVacantIndex++; + else + for (int i = 0; i < particles.Length; i++) + if (particles[i] == null) + nextVacantIndex = i; + + activeParticles++; + } + + + } +} diff --git a/Particles/Particle.cs b/Particles/Particle.cs index cf3bac931f..24ea429b0f 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -9,6 +9,8 @@ namespace CalamityMod.Particles public int Variant; public float Scale; public Vector2 RelativeOffset; + public virtual string Texture => ""; + public Particle(int lifetime, Vector2 relativeOffset, float scale, int variant = 0) { Time = 0;