From faea741cd95b4dfd34077f37aaa1c6cf78a7c3b8 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 13:18:06 +0100 Subject: [PATCH 1/9] Starte --- CalamityMod.cs | 4 ++ ILEditing/ILChangesLoading.cs | 2 + ILEditing/MechanicILChanges.cs | 12 ++++ Particles/GeneralParticleHandler.cs | 98 +++++++++++++++++++++++++++++ Particles/Particle.cs | 2 + 5 files changed, 118 insertions(+) create mode 100644 Particles/GeneralParticleHandler.cs 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; From cb04b03ec3848f90a6d0e87e733c09c407f6f9cf Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 16:24:52 +0100 Subject: [PATCH 2/9] More work. I really need to make the particle set system more compatible with this --- CalamityMod.cs | 10 ++++ ILEditing/MechanicILChanges.cs | 2 +- Particles/BaseParticleSet.cs | 13 +++-- Particles/GeneralParticleHandler.cs | 80 +++++++++++++++++++++++++++-- Particles/Particle.cs | 69 ++++++++++++++++++++++++- 5 files changed, 163 insertions(+), 11 deletions(-) diff --git a/CalamityMod.cs b/CalamityMod.cs index 556b726308..779c660bcf 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -1602,5 +1602,15 @@ namespace CalamityMod #region Speedrun Timer Stopper public override void PreSaveAndQuit() => SpeedrunTimer?.Stop(); #endregion + + #region Particles updating + public override void PostUpdateEverything() + { + if (!Main.dedServ) + { + GeneralParticleHandler.Update(); + } + } + #endregion } } diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 9256e5f4bd..626acb6980 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -541,7 +541,7 @@ namespace CalamityMod.ILEditing #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); + Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, default, null, null, Main.GameViewMatrix.ZoomMatrix); GeneralParticleHandler.DrawAllParticles(Main.spriteBatch); Main.spriteBatch.End(); diff --git a/Particles/BaseParticleSet.cs b/Particles/BaseParticleSet.cs index f5c48fdf59..a172223884 100644 --- a/Particles/BaseParticleSet.cs +++ b/Particles/BaseParticleSet.cs @@ -66,11 +66,16 @@ namespace CalamityMod.Particles foreach (Particle particle in Particles.OrderBy(p => p.Time)) { - Vector2 drawPosition = basePosition - Main.screenPosition + particle.RelativeOffset; - Color particleColor = DetermineParticleColor(particle.Time / (float)particle.Lifetime); - Rectangle frame = ParticleTexture.Frame(1, ParticleFrameVariants, 0, particle.Variant); + if (particle.UseCustomDraw) + particle.CustomDraw(Main.spriteBatch); + else + { + Vector2 drawPosition = basePosition - Main.screenPosition + particle.RelativeOffset; + Color particleColor = DetermineParticleColor(particle.Time / (float)particle.Lifetime); + Rectangle frame = ParticleTexture.Frame(1, ParticleFrameVariants, 0, particle.Variant); - Main.spriteBatch.Draw(ParticleTexture, drawPosition, frame, particleColor, 0f, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f); + Main.spriteBatch.Draw(ParticleTexture, drawPosition, frame, particleColor, 0f, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f); + } } } } diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index 43d5c56284..aee0bff66a 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -1,10 +1,12 @@ -using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework; +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; using Terraria.ModLoader; namespace CalamityMod.Particles @@ -69,30 +71,100 @@ namespace CalamityMod.Particles 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) + if (activeParticles == particleCap) return; particles[nextVacantIndex] = particle; particle.ID = nextVacantIndex; particle.Type = particleTypes[particle.GetType()]; + //If the next index is also vacant if (nextVacantIndex + 1 < particles.Length && particles[nextVacantIndex + 1] == null) nextVacantIndex++; + //If it isnt, go check for the first empty spot. else for (int i = 0; i < particles.Length; i++) if (particles[i] == null) + { nextVacantIndex = i; + break; + } activeParticles++; } + public static void Update() + { + foreach (Particle particle in particles) + { + if (particle == null) + continue; + particle.Position += particle.Velocity; + particle.Time++; + particle.Update(); + } + } + public static void RemoveParticle(int position) + { + particles[position] = null; + nextVacantIndex = position; + activeParticles--; + } + + public static void DrawAllParticles(SpriteBatch sb) + { + //Batch the particles to avoid constant restarting of the spritebatch + foreach (Particle particle in particles) + { + if (particle == null) + continue; + + if (particle.UseAdditiveBlend) + batchedAdditiveBlendParticles.Add(particle); + else + batchedAlphaBlendParticles.Add(particle); + } + + foreach (Particle particle in batchedAlphaBlendParticles) + { + if (particle.UseCustomDraw) + particle.CustomDraw(sb); + else + { + Rectangle frame = particleTextures[particle.Type].Frame(1, particle.FrameVariants, 0, particle.Variant); + sb.Draw(particleTextures[particle.Type], particle.Position - Main.screenPosition, frame, particle.Color, particle.Rotation, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f); + } + } + + sb.End(); + sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, null, null, Main.GameViewMatrix.ZoomMatrix); + + foreach (Particle particle in batchedAdditiveBlendParticles) + { + if (particle.UseCustomDraw) + particle.CustomDraw(sb); + else + { + Rectangle frame = particleTextures[particle.Type].Frame(1, particle.FrameVariants, 0, particle.Variant); + sb.Draw(particleTextures[particle.Type], particle.Position - Main.screenPosition, frame, particle.Color, particle.Rotation, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f); + } + } + + sb.End(); + sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); + + batchedAlphaBlendParticles.Clear(); + batchedAdditiveBlendParticles.Clear(); + } + /// + /// Gives you the texture of the particle type. Useful for custom drawing + /// + public static Texture2D GetTexture(int type) => particleTextures[type] } } diff --git a/Particles/Particle.cs b/Particles/Particle.cs index 24ea429b0f..9655c90f7d 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -1,15 +1,80 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; namespace CalamityMod.Particles { public class Particle { + /// + /// The ID of the particle inside of the general particle handler's array. This is set automatically when the particle is created + /// + public int ID; + /// + /// The ID of the particle type as registered by the general particle handler. This is set automatically when the particle handler loads + /// + public int Type; + /// + /// The amount of frames this particle has existed for. You shouldn't have to touch this manually. + /// public int Time; + /// + /// The maximum amount of frames a particle may stay alive when used in a particle set. + /// Keep in mind the general particle handler will not automatically clear particles past their lifetime, nor will it care if the Lifetime isnt set at all. + /// public int Lifetime; - public int Variant; - public float Scale; + + public int FrameVariants = 1; + public int Variant = 0; + + /// + /// The offset of the particle in relation to the origin of the set it belongs to. This is only used in the context of a Particle Set + /// public Vector2 RelativeOffset; + /// + /// The inworld position of a particle. Keep in mind this isn't used in the context of a Particle Set, since all the particles work off their relative position to the set's origin + /// + public Vector2 Position; + public Vector2 Velocity; + + + public Vector2 Origin; + public Color Color; + public float Rotation; + public float Scale; + public virtual string Texture => ""; + /// + /// Set this to true to disable default particle drawing, thus calling Particle.CustomDraw() instead. + /// + public virtual bool UseCustomDraw => false; + /// + /// Use this method if you want to handle the particle drawing yourself. Only called if Particle.UseCustomDraw is set to true. + /// + public virtual void CustomDraw(SpriteBatch spriteBatch) { } + /// + /// Called for every update of the particle handler. + /// The particle's velocity gets automatically added to its position, and its time automatically increases. + /// + public virtual void Update() { } + + /// + /// Set this to true to make your particle use additive blending instead of alphablend. + /// + public virtual bool UseAdditiveBlend => false; + + /// + /// Removes the particle from the handler + /// + public void Kill() => GeneralParticleHandler.RemoveParticle(ID); + + public Particle(int lifetime, Vector2 relativeOffset, float scale, int variant = 0) + { + Time = 0; + Lifetime = lifetime; + Variant = variant; + Scale = scale; + RelativeOffset = relativeOffset; + } public Particle(int lifetime, Vector2 relativeOffset, float scale, int variant = 0) { From 447d8d78a0fa55910df8b63cece17be7c902d717 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 18:30:56 +0100 Subject: [PATCH 3/9] Rewired particleSets --- Dusts/PumplerDust.cs | 4 +- Particles/BaseParticleSet.cs | 49 +++++++++++------ Particles/EnchantedParticle.cs | 51 ++++++++++++++++++ Particles/EnchantedParticleSet.cs | 21 +------- .../Particles => Particles}/Fire.png | Bin Particles/FireParticle.cs | 48 +++++++++++++++++ Particles/FireParticleSet.cs | 21 +------- Particles/GeneralParticleHandler.cs | 4 ++ Particles/GenericSparkle.cs | 25 +++++++++ .../Particles => Particles}/Light.png | Bin .../MediumSmoke.png | Bin Particles/Particle.cs | 33 ++++-------- {Dusts => Particles}/SmallSmoke.png | Bin Particles/ThanatosSmokeParticle.cs | 48 +++++++++++++++++ Particles/ThanatosSmokeParticleSet.cs | 26 ++------- 15 files changed, 229 insertions(+), 101 deletions(-) create mode 100644 Particles/EnchantedParticle.cs rename {ExtraTextures/Particles => Particles}/Fire.png (100%) create mode 100644 Particles/FireParticle.cs create mode 100644 Particles/GenericSparkle.cs rename {ExtraTextures/Particles => Particles}/Light.png (100%) rename ExtraTextures/ThanatosVentParticle.png => Particles/MediumSmoke.png (100%) rename {Dusts => Particles}/SmallSmoke.png (100%) create mode 100644 Particles/ThanatosSmokeParticle.cs diff --git a/Dusts/PumplerDust.cs b/Dusts/PumplerDust.cs index a83524b6cc..ea6b3fc929 100644 --- a/Dusts/PumplerDust.cs +++ b/Dusts/PumplerDust.cs @@ -4,11 +4,11 @@ using Terraria.ModLoader; namespace CalamityMod.Dusts { - public class PumplerDust : ModDust + public class PumplerDust : ModDust //TURN THAT INTO A PARTICLE WHEN I REMEMBER { public override bool Autoload(ref string name, ref string texture) { - texture = "CalamityMod/Dusts/SmallSmoke"; + texture = "CalamityMod/Particles/SmallSmoke"; return true; } public override void OnSpawn(Dust dust) diff --git a/Particles/BaseParticleSet.cs b/Particles/BaseParticleSet.cs index a172223884..ba60271776 100644 --- a/Particles/BaseParticleSet.cs +++ b/Particles/BaseParticleSet.cs @@ -5,21 +5,38 @@ using System.Collections.Generic; using System.Linq; using Terraria; using Terraria.ID; +using Terraria.ModLoader; namespace CalamityMod.Particles { + /// + /// A set of particles that aren't attached to any particular position in the world, but instead an arbitrary center point defined in the draw function + /// The particles in particle sets don't count towards the general particle handler's particle cap since it doesn't process them at all. + /// You'll have to manually update and draw these sets. + /// TLDR: Groups of particles that work with relative positions instead of world positions and have to be manually updated and drawn + /// public abstract class BaseParticleSet { public int LocalTimer { get; internal set; } public int SetLifetime { get; internal set; } + /// + /// The amount of frames it takes for a new particle to be spawned in the set. + /// public int ParticleSpawnRate; + /// + /// The particles in the set + /// public List Particles = new List(); + /// + /// The lifetime of the particles in the set. + /// If the particles spawned don't have this same lifetime set they may get cut off when the set dies. + /// public abstract int ParticleLifetime { get; } - public abstract void UpdateBehavior(Particle particle); + /// + /// The particle spawned by the set + /// + /// public abstract Particle SpawnParticle(); - public abstract Color DetermineParticleColor(float lifetimeCompletion); - public abstract Texture2D ParticleTexture { get; } - public virtual int ParticleFrameVariants => 1; public virtual Func OrderFunction { get; } = null; public BaseParticleSet(int setLifetime, int particleSpawnRate) @@ -34,24 +51,24 @@ namespace CalamityMod.Particles if (Main.netMode == NetmodeID.Server) return; + //Spawn new particles if time remains bool closeToDeath = LocalTimer >= SetLifetime - ParticleLifetime && SetLifetime > 0; if (LocalTimer % ParticleSpawnRate == ParticleSpawnRate - 1 && !closeToDeath) { Particle particle = SpawnParticle(); - particle.Lifetime = ParticleLifetime; - particle.Variant = Main.rand.Next(ParticleFrameVariants); Particles.Add(particle); } - // Update and increment the time of all particles. - for (int i = 0; i < Particles.Count; i++) + // Update and increment the time of all particles, alongside modifying their offset based on their velocity. + foreach(Particle particle in Particles) { - Particles[i].Time++; - UpdateBehavior(Particles[i]); + particle.RelativeOffset += particle.Velocity; + particle.Time++; + particle.Update(); } // Clear all expired particles. - Particles.RemoveAll(particle => particle.Time >= particle.Lifetime); + Particles.RemoveAll(particle => particle.Time >= particle.Lifetime && particle.SetLifetime); LocalTimer++; } @@ -70,11 +87,13 @@ namespace CalamityMod.Particles particle.CustomDraw(Main.spriteBatch); else { - Vector2 drawPosition = basePosition - Main.screenPosition + particle.RelativeOffset; - Color particleColor = DetermineParticleColor(particle.Time / (float)particle.Lifetime); - Rectangle frame = ParticleTexture.Frame(1, ParticleFrameVariants, 0, particle.Variant); + var tex = ModContent.GetTexture(particle.Texture); - Main.spriteBatch.Draw(ParticleTexture, drawPosition, frame, particleColor, 0f, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f); + Vector2 drawPosition = basePosition - Main.screenPosition + particle.RelativeOffset; + Color particleColor = particle.Color; + Rectangle frame = tex.Frame(1, particle.FrameVariants, 0, particle.Variant); + + Main.spriteBatch.Draw(tex, drawPosition, frame, particle.Color, 0f, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f); } } } diff --git a/Particles/EnchantedParticle.cs b/Particles/EnchantedParticle.cs new file mode 100644 index 0000000000..8d03ea184b --- /dev/null +++ b/Particles/EnchantedParticle.cs @@ -0,0 +1,51 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Particles +{ + public class EnchantedParticle : Particle + { + public float RelativePower; + public override bool SetLifetime => true; + + public float InterpolationSpeed; + public float EdgeOffset; + public Color EdgeColor; + public Color CenterColor; + + public override string Texture => "CalamityMod/Particles/Light"; + + public EnchantedParticle(Vector2 relativePosition, int lifetime, float scale, Color edgeColor, Color centerColor, float interpolationSpeed, float edgeOffset) + { + RelativeOffset = relativePosition; + Velocity = Vector2.Zero; + Scale = scale; + Variant = Main.rand.Next(3); + Lifetime = lifetime; + EdgeColor = edgeColor; + CenterColor = centerColor; + InterpolationSpeed = interpolationSpeed; + EdgeOffset = edgeOffset; + } + + public override void Update() + { + float distanceToCenter = RelativeOffset.Length(); + Scale = MathHelper.SmoothStep(0.05f, 0.125f, Utils.InverseLerp(EdgeOffset, 6f, distanceToCenter, true)); + Scale *= Utils.InverseLerp(Lifetime, Lifetime - 10f, Time, true); + + if (distanceToCenter > 4.5f) + RelativeOffset = Vector2.Lerp(RelativeOffset, Vector2.Zero, InterpolationSpeed); + + Color = Color.Lerp(EdgeColor, CenterColor, Utils.InverseLerp(0f, 0.67f, LifetimeCompletion, true)); + Color.A = 50; + } + } +} diff --git a/Particles/EnchantedParticleSet.cs b/Particles/EnchantedParticleSet.cs index 4feb625237..82ef4e3c9a 100644 --- a/Particles/EnchantedParticleSet.cs +++ b/Particles/EnchantedParticleSet.cs @@ -12,7 +12,6 @@ namespace CalamityMod.Particles public Color EdgeColor; public Color CenterColor; public override int ParticleLifetime => 50; - public override Texture2D ParticleTexture => ModContent.GetTexture("CalamityMod/ExtraTextures/Particles/Light"); public ChargingEnergyParticleSet(int setLifetime, int particleSpawnRate, Color edgeColor, Color centerColor, float interpolationSpeed, float edgeOffset) : base(setLifetime, particleSpawnRate) { @@ -22,24 +21,6 @@ namespace CalamityMod.Particles EdgeOffset = edgeOffset; } - public override void UpdateBehavior(Particle particle) - { - float distanceToCenter = particle.RelativeOffset.Length(); - particle.Scale = MathHelper.SmoothStep(0.05f, 0.125f, Utils.InverseLerp(EdgeOffset, 6f, distanceToCenter, true)); - particle.Scale *= Utils.InverseLerp(ParticleLifetime, ParticleLifetime - 10f, particle.Time, true); - - if (distanceToCenter > 4.5f) - particle.RelativeOffset = Vector2.Lerp(particle.RelativeOffset, Vector2.Zero, InterpolationSpeed); - } - - public override Color DetermineParticleColor(float lifetimeCompletion) - { - Color particleColor = Color.Lerp(EdgeColor, CenterColor, Utils.InverseLerp(0f, 0.67f, lifetimeCompletion, true)); - particleColor.A = 50; - - return particleColor; - } - - public override Particle SpawnParticle() => new Particle(ParticleLifetime, Main.rand.NextVector2CircularEdge(1f, 1f) * EdgeOffset, 0.1f); + public override Particle SpawnParticle() => new EnchantedParticle(Main.rand.NextVector2CircularEdge(1f, 1f) * EdgeOffset, ParticleLifetime, 0.1f, EdgeColor, CenterColor, InterpolationSpeed, EdgeOffset); } } \ No newline at end of file diff --git a/ExtraTextures/Particles/Fire.png b/Particles/Fire.png similarity index 100% rename from ExtraTextures/Particles/Fire.png rename to Particles/Fire.png diff --git a/Particles/FireParticle.cs b/Particles/FireParticle.cs new file mode 100644 index 0000000000..5aa8e54ccd --- /dev/null +++ b/Particles/FireParticle.cs @@ -0,0 +1,48 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Particles +{ + public class FireParticle : Particle + { + public float RelativePower; + public override bool SetLifetime => true; + public override int FrameVariants => 3; + + public Color BrightColor; + public Color DarkColor; + + public override string Texture => "CalamityMod/Particles/Fire"; + + public FireParticle(Vector2 relativePosition, int lifetime, float scale, float relativePower, Color brightColor, Color darkColor) + { + RelativeOffset = relativePosition; + Velocity = Vector2.Zero; + Scale = scale; + Variant = Main.rand.Next(3); + Lifetime = lifetime; + RelativePower = relativePower; + BrightColor = brightColor; + DarkColor = darkColor; + } + + public override void Update() + { + Scale += RelativePower * 0.01f; + RelativeOffset.Y -= RelativePower * 3f; + + Color = Color.Lerp(BrightColor, DarkColor, LifetimeCompletion); + Color = Color.Lerp(Color, Color.SaddleBrown, Utils.InverseLerp(0.95f, 0.7f, LifetimeCompletion, true)); + Color = Color.Lerp(Color, Color.White, Utils.InverseLerp(0.1f, 0.25f, LifetimeCompletion, true) * Utils.InverseLerp(0.4f, 0.25f, LifetimeCompletion, true) * 0.7f); + Color *= Utils.InverseLerp(0f, 0.15f, LifetimeCompletion, true) * Utils.InverseLerp(1f, 0.8f, LifetimeCompletion, true) * 0.6f; + Color.A = 50; + } + } +} diff --git a/Particles/FireParticleSet.cs b/Particles/FireParticleSet.cs index fd362b1a76..4dbfe1275b 100644 --- a/Particles/FireParticleSet.cs +++ b/Particles/FireParticleSet.cs @@ -12,7 +12,7 @@ namespace CalamityMod.Particles public Color BrightColor; public Color DarkColor; public override int ParticleLifetime => 50; - public override Texture2D ParticleTexture => ModContent.GetTexture("CalamityMod/ExtraTextures/Particles/Fire"); + public FireParticleSet(int setLifetime, int particleSpawnRate, Color brightColor, Color darkColor, float spawnAreaCompactness, float relativePower) : base(setLifetime, particleSpawnRate) { @@ -22,23 +22,6 @@ namespace CalamityMod.Particles RelativePower = relativePower; } - public override void UpdateBehavior(Particle particle) - { - particle.Scale += RelativePower * 0.01f; - particle.RelativeOffset.Y -= RelativePower * 3f; - } - - public override Color DetermineParticleColor(float lifetimeCompletion) - { - Color particleColor = Color.Lerp(BrightColor, DarkColor, lifetimeCompletion); - particleColor = Color.Lerp(particleColor, Color.SaddleBrown, Utils.InverseLerp(0.95f, 0.7f, lifetimeCompletion, true)); - particleColor = Color.Lerp(particleColor, Color.White, Utils.InverseLerp(0.1f, 0.25f, lifetimeCompletion, true) * Utils.InverseLerp(0.4f, 0.25f, lifetimeCompletion, true) * 0.7f); - particleColor *= Utils.InverseLerp(0f, 0.15f, lifetimeCompletion, true) * Utils.InverseLerp(1f, 0.8f, lifetimeCompletion, true) * 0.6f; - particleColor.A = 50; - - return particleColor; - } - - public override Particle SpawnParticle() => new Particle(ParticleLifetime, Main.rand.NextVector2Circular(1f, 1f) * SpawnAreaCompactness, 0.06f); + public override Particle SpawnParticle() => new FireParticle(Main.rand.NextVector2Circular(1f, 1f) * SpawnAreaCompactness, ParticleLifetime, 0.06f, RelativePower, BrightColor, DarkColor); } } \ No newline at end of file diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index aee0bff66a..fb18af3fb1 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -107,6 +107,10 @@ namespace CalamityMod.Particles particle.Position += particle.Velocity; particle.Time++; particle.Update(); + + //Clear out particles whose time is up + if (particle.Time >= particle.Lifetime && particle.SetLifetime) + particle.Kill(); } } diff --git a/Particles/GenericSparkle.cs b/Particles/GenericSparkle.cs new file mode 100644 index 0000000000..a83c687b6d --- /dev/null +++ b/Particles/GenericSparkle.cs @@ -0,0 +1,25 @@ +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria; + +namespace CalamityMod.Particles +{ + public class GenericSparkle : Particle + { + public GenericSparkle(Vector2 position, Vector2 velocity, Color color, float scale, int lifeTime, float rotationSpeed = 1f) + { + Position = position; + Velocity = velocity; + Color = color; + Scale = scale; + Lifetime = lifeTime; + Rotation = Main.rand.NextFloat(MathHelper.TwoPi); + } + + + } +} diff --git a/ExtraTextures/Particles/Light.png b/Particles/Light.png similarity index 100% rename from ExtraTextures/Particles/Light.png rename to Particles/Light.png diff --git a/ExtraTextures/ThanatosVentParticle.png b/Particles/MediumSmoke.png similarity index 100% rename from ExtraTextures/ThanatosVentParticle.png rename to Particles/MediumSmoke.png diff --git a/Particles/Particle.cs b/Particles/Particle.cs index 9655c90f7d..70bfe50976 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -17,14 +17,17 @@ namespace CalamityMod.Particles /// The amount of frames this particle has existed for. You shouldn't have to touch this manually. /// public int Time; + /// - /// The maximum amount of frames a particle may stay alive when used in a particle set. - /// Keep in mind the general particle handler will not automatically clear particles past their lifetime, nor will it care if the Lifetime isnt set at all. + /// Set this to true if you want your particle to automatically get removed when its time reaches its maximum lifetime /// - public int Lifetime; + public virtual bool SetLifetime => false; + /// + /// The maximum amount of frames a particle may stay alive if Particle.SetLifeTime is set to true + /// + public int Lifetime = 0; - public int FrameVariants = 1; - public int Variant = 0; + public float LifetimeCompletion => Lifetime != 0 ? Time / Lifetime : 0; /// /// The offset of the particle in relation to the origin of the set it belongs to. This is only used in the context of a Particle Set @@ -36,12 +39,13 @@ namespace CalamityMod.Particles public Vector2 Position; public Vector2 Velocity; - public Vector2 Origin; public Color Color; public float Rotation; public float Scale; + public virtual int FrameVariants => 1; + public int Variant = 0; public virtual string Texture => ""; /// /// Set this to true to disable default particle drawing, thus calling Particle.CustomDraw() instead. @@ -67,22 +71,5 @@ namespace CalamityMod.Particles /// public void Kill() => GeneralParticleHandler.RemoveParticle(ID); - public Particle(int lifetime, Vector2 relativeOffset, float scale, int variant = 0) - { - Time = 0; - Lifetime = lifetime; - Variant = variant; - Scale = scale; - RelativeOffset = relativeOffset; - } - - public Particle(int lifetime, Vector2 relativeOffset, float scale, int variant = 0) - { - Time = 0; - Lifetime = lifetime; - Variant = variant; - Scale = scale; - RelativeOffset = relativeOffset; - } } } \ No newline at end of file diff --git a/Dusts/SmallSmoke.png b/Particles/SmallSmoke.png similarity index 100% rename from Dusts/SmallSmoke.png rename to Particles/SmallSmoke.png diff --git a/Particles/ThanatosSmokeParticle.cs b/Particles/ThanatosSmokeParticle.cs new file mode 100644 index 0000000000..cd039bb100 --- /dev/null +++ b/Particles/ThanatosSmokeParticle.cs @@ -0,0 +1,48 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Particles +{ + public class ThanatosSmokeParticle : Particle + { + public float RelativePower; + public float BaseMoveRotation; + public override bool SetLifetime => true; + public override int FrameVariants => 3; + + public override string Texture => "CalamityMod/Particles/MediumSmoke"; + + public ThanatosSmokeParticle(Vector2 relativePosition, int lifetime, float scale, float relativePower, float baseMoveRotation) + { + RelativeOffset = relativePosition; + Velocity = Vector2.Zero; + Scale = scale; + Variant = Main.rand.Next(3); + Lifetime = lifetime; + RelativePower = relativePower; + BaseMoveRotation = baseMoveRotation; + } + + public override void Update() + { + if (Scale < 1.25f) + Scale += RelativePower * 0.04f; + RelativeOffset -= (BaseMoveRotation + Main.rand.NextFloat(-0.18f, 0.18f)).ToRotationVector2() * RelativePower * 4.5f; + + Color = Color.DarkRed; + Color = Color.Lerp(Color, new Color(154, 139, 138), (float)Math.Pow(Utils.InverseLerp(0f, 0.57f, LifetimeCompletion, true), 2D) * 0.5f + 0.5f); + Color.A = 92; + + float opacity = Utils.InverseLerp(1f, 0.85f, Time / Lifetime, true); + Color *= opacity; + } + + } +} diff --git a/Particles/ThanatosSmokeParticleSet.cs b/Particles/ThanatosSmokeParticleSet.cs index abff9d70c8..3037de7bb2 100644 --- a/Particles/ThanatosSmokeParticleSet.cs +++ b/Particles/ThanatosSmokeParticleSet.cs @@ -8,13 +8,12 @@ namespace CalamityMod.Particles { public class ThanatosSmokeParticleSet : BaseParticleSet { - public float SpawnAreaCompactness; + public override int ParticleLifetime => 30; + public float SpawnAreaCompactness; public float RelativePower; public float BaseMoveRotation; public float MoveRotationOffset; - public override int ParticleLifetime => 30; - public override int ParticleFrameVariants => 3; - public override Texture2D ParticleTexture => ModContent.GetTexture("CalamityMod/ExtraTextures/ThanatosVentParticle"); + public ThanatosSmokeParticleSet(int setLifetime, int particleSpawnRate, float baseMoveRotation, float spawnAreaCompactness, float relativePower) : base(setLifetime, particleSpawnRate) { @@ -24,23 +23,6 @@ namespace CalamityMod.Particles RelativePower = relativePower; } - public override void UpdateBehavior(Particle particle) - { - if (particle.Scale < 1.25f) - particle.Scale += RelativePower * 0.04f; - particle.RelativeOffset -= (BaseMoveRotation + Main.rand.NextFloat(-0.18f, 0.18f)).ToRotationVector2() * RelativePower * 4.5f; - } - - public override Color DetermineParticleColor(float lifetimeCompletion) - { - Color color = Color.DarkRed; - color = Color.Lerp(color, new Color(154, 139, 138), (float)Math.Pow(Utils.InverseLerp(0f, 0.57f, lifetimeCompletion, true), 2D) * 0.5f + 0.5f); - color.A = 92; - - float opacity = Utils.InverseLerp(1f, 0.85f, lifetimeCompletion, true); - return color * opacity; - } - - public override Particle SpawnParticle() => new Particle(ParticleLifetime, Main.rand.NextVector2Circular(1f, 1f) * SpawnAreaCompactness, 0.06f, Main.rand.Next(3)); + public override Particle SpawnParticle() => new ThanatosSmokeParticle(Main.rand.NextVector2Circular(1f, 1f) * SpawnAreaCompactness, ParticleLifetime, 0.06f, RelativePower, BaseMoveRotation); } } \ No newline at end of file From 603cefa60365c0aa631b20c1bb013bb7c2c55729 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 18:34:58 +0100 Subject: [PATCH 4/9] Fix --- ILEditing/MechanicILChanges.cs | 2 +- Particles/GeneralParticleHandler.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 626acb6980..3208003620 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -539,7 +539,7 @@ 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) + private static void DrawGeneralParticles(On.Terraria.Main.orig_DrawInterface orig, Main self, GameTime gameTime) { Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, default, null, null, Main.GameViewMatrix.ZoomMatrix); GeneralParticleHandler.DrawAllParticles(Main.spriteBatch); diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index fb18af3fb1..af14671fcf 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -169,6 +169,6 @@ namespace CalamityMod.Particles /// /// Gives you the texture of the particle type. Useful for custom drawing /// - public static Texture2D GetTexture(int type) => particleTextures[type] + public static Texture2D GetTexture(int type) => particleTextures[type]; } } From 7cab3cea2035845fa8e6357ad50d4c91e6b9333d Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 19:06:24 +0100 Subject: [PATCH 5/9] Accidental int division! --- Particles/GenericSparkle.cs | 2 ++ Particles/Particle.cs | 2 +- Particles/Sparkle.png | Bin 0 -> 707 bytes Particles/ThanatosSmokeParticle.cs | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 Particles/Sparkle.png diff --git a/Particles/GenericSparkle.cs b/Particles/GenericSparkle.cs index a83c687b6d..7e8b44a156 100644 --- a/Particles/GenericSparkle.cs +++ b/Particles/GenericSparkle.cs @@ -10,6 +10,8 @@ namespace CalamityMod.Particles { public class GenericSparkle : Particle { + public override string Texture => "CalamityMod/Particles/Sparkle"; + public GenericSparkle(Vector2 position, Vector2 velocity, Color color, float scale, int lifeTime, float rotationSpeed = 1f) { Position = position; diff --git a/Particles/Particle.cs b/Particles/Particle.cs index 70bfe50976..ee9467d316 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -27,7 +27,7 @@ namespace CalamityMod.Particles /// public int Lifetime = 0; - public float LifetimeCompletion => Lifetime != 0 ? Time / Lifetime : 0; + public float LifetimeCompletion => Lifetime != 0 ? Time / (float)Lifetime : 0; /// /// The offset of the particle in relation to the origin of the set it belongs to. This is only used in the context of a Particle Set diff --git a/Particles/Sparkle.png b/Particles/Sparkle.png new file mode 100644 index 0000000000000000000000000000000000000000..79930b6e9941d798bfd88216c66f9d6f9e005eb2 GIT binary patch literal 707 zcmV;!0zCbRP)Px%eMv+?R9J=0n7wWjK@i7(dp_HIB~cnW+JHcY4hf0E&%v8epeUl?O?V25AR!^q z0RlltJQ1)!g3f?fiCj_QQ5`6z{#$V*&J~SHMCV=bZ1- zXORiu^z9|!rF5b9w%z$d7>N+Qn(pL$69}hL2k05w)*uRC0PIK?Y!M=gH_ivDXUQfI z17Hr6(u3TN%8YN~R9pe}<$PbZDH#!I9SO7Sya0OqnUp=f_qF!t2Vta7ipGerM~p~` z+3(t6D+n{HlC(?i0hfVm(tD&`w5)yCC}%m*;)4Ph%E=CJA9y5N)Gr8Ix0hZu^&7YM zq9s6_?+9=}82MQCvFZo1_epv4SBZYhHRnMcDDDG8Lga1&w}IVSyIsBaz&islM+ckr zuXo;i?_BLvw8fL=c&O)lz%9a(JX|xlBj7vm33w|i!72SDkZ^8)b-@8EhA^XYr1@UTd3>JA-suNW z&h;)vVXFF^ur#NbW!FVEx#-5rZAH5DX2~WHj Date: Thu, 6 Jan 2022 20:03:09 +0100 Subject: [PATCH 6/9] Unculled two thirds of the enchantment particles & extra note --- Particles/EnchantedParticle.cs | 1 - Particles/GeneralParticleHandler.cs | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Particles/EnchantedParticle.cs b/Particles/EnchantedParticle.cs index 8d03ea184b..899da6a2e5 100644 --- a/Particles/EnchantedParticle.cs +++ b/Particles/EnchantedParticle.cs @@ -27,7 +27,6 @@ namespace CalamityMod.Particles RelativeOffset = relativePosition; Velocity = Vector2.Zero; Scale = scale; - Variant = Main.rand.Next(3); Lifetime = lifetime; EdgeColor = edgeColor; CenterColor = centerColor; diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index af14671fcf..52788d3e79 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -170,5 +170,7 @@ namespace CalamityMod.Particles /// Gives you the texture of the particle type. Useful for custom drawing /// public static Texture2D GetTexture(int type) => particleTextures[type]; + + private static string noteToEveryone = "This particle system was inspired by spirit mod's own particle system, with permission granted by Yuyutsu. Love you spirit mod! -Iban"; } } From e24c9afe14f520113d9fbcf89067b04d76c994c1 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 21:56:17 +0100 Subject: [PATCH 7/9] Basic sparkle particle --- Particles/BloomCircle.png | Bin 0 -> 7607 bytes Particles/GenericSparkle.cs | 38 +++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 Particles/BloomCircle.png diff --git a/Particles/BloomCircle.png b/Particles/BloomCircle.png new file mode 100644 index 0000000000000000000000000000000000000000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!$waJJ+ zQ8!K=AZsm`viJq@3U;&?SH08=h&9Z9>Z8U}?}nsql;oaduPI&LPFIv#G>uiR2zgZ5 z^^Wy^Azjq&1Q`X~!BzNuSs#VL+D>Z6LlW};zPG>; z8YS3Gru{7)4k_B-mmqR;m%eM#FL^VG%YA_HXYdp`R6gwz@v$5HYW~yCW53Xb-7A3Q zXTZhsAY;jCiC7TI|E zAX2F%4vKIw)?OaDR-sH{zv**qKV8FKLKK}a-q5R5NjPZ2kL?fv%c7@#WV7r?ka#Jl zdh;$JjOqdLWvT(^-d+|yZUFqFIWBJJZ>to7ti6OtSpUHz*Z7Lk2oM>SW2=8v?#EV# zx&`uKyI6LLf^MPOo0C5@0Gb^{Otdy@CjS?A?!^OI8yk1*#WG1G)c-3Svkhc5Og-md Sl%9}SmaB%>4660r68;B;tCYV0 literal 0 HcmV?d00001 diff --git a/Particles/GenericSparkle.cs b/Particles/GenericSparkle.cs index 7e8b44a156..0b5ce64c20 100644 --- a/Particles/GenericSparkle.cs +++ b/Particles/GenericSparkle.cs @@ -1,27 +1,55 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Terraria; +using Terraria.ModLoader; namespace CalamityMod.Particles { public class GenericSparkle : Particle { public override string Texture => "CalamityMod/Particles/Sparkle"; + public override bool UseAdditiveBlend => true; + public override bool UseCustomDraw => true; + public override bool SetLifetime => true; - public GenericSparkle(Vector2 position, Vector2 velocity, Color color, float scale, int lifeTime, float rotationSpeed = 1f) + private float Spin; + private float opacity; + private Color Bloom; + private Color LightColor => Bloom * opacity; + private float BloomScale; + + public GenericSparkle(Vector2 position, Vector2 velocity, Color color, Color bloom, float scale, int lifeTime, float rotationSpeed = 1f, float bloomScale = 1f) { Position = position; Velocity = velocity; Color = color; + Bloom = bloom; Scale = scale; Lifetime = lifeTime; Rotation = Main.rand.NextFloat(MathHelper.TwoPi); + Spin = rotationSpeed; + BloomScale = bloomScale; } + public override void Update() + { + opacity = (float)Math.Sin(LifetimeCompletion * MathHelper.Pi); + Lighting.AddLight(Position, LightColor.R / 255f, LightColor.G / 255f, LightColor.B / 255f); + Velocity *= 0.95f; + Rotation += Spin * ((Velocity.X > 0) ? 1f : -1f); + } + public override void CustomDraw(SpriteBatch spriteBatch) + { + Texture2D starTexture = GeneralParticleHandler.GetTexture(Type); + Texture2D bloomTexture = ModContent.GetTexture("CalamityMod/Particles/BloomCircle"); + //Ajust the bloom's texture to be the same size as the star's + float properBloomSize = starTexture.Height / bloomTexture.Height; + + spriteBatch.Draw(bloomTexture, Position - Main.screenPosition, null, Bloom * opacity * 0.5f, 0, bloomTexture.Size()/2f, Scale * BloomScale * properBloomSize, SpriteEffects.None, 0); + spriteBatch.Draw(starTexture, Position - Main.screenPosition, null, Color * opacity * 0.5f, Rotation + MathHelper.PiOver4, starTexture.Size() / 2f, Scale * 0.75f, SpriteEffects.None, 0); + spriteBatch.Draw(starTexture, Position - Main.screenPosition, null, Color * opacity, Rotation, starTexture.Size() / 2f, Scale, SpriteEffects.None, 0); + } } } From ae92e313beda3205ec0499ad76c41536e70106ed Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Thu, 6 Jan 2022 23:08:55 +0100 Subject: [PATCH 8/9] Particle cap config --- CalamityConfig.cs | 8 ++++++++ Particles/GeneralParticleHandler.cs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CalamityConfig.cs b/CalamityConfig.cs index 4a38eca484..e8b9591f41 100644 --- a/CalamityConfig.cs +++ b/CalamityConfig.cs @@ -30,6 +30,14 @@ namespace CalamityMod [Tooltip("Enables rendering afterimages for Calamity NPCs, projectiles, etc.\nDisable to improve performance.")] public bool Afterimages { get; set; } + [Label("Maximum particles")] + [BackgroundColor(192, 54, 64, 192)] + [SliderColor(224, 165, 56, 128)] + [Range(0, 1000)] + [DefaultValue(500)] + [Tooltip("Sets the maximum of particle effects that can exist at once.\nParticles are separate from dust and gores.\nTurn down to improve performance.")] + public int ParticleLimit { get; set; } + [Label("Disable Screenshakes")] [BackgroundColor(192, 54, 64, 192)] [DefaultValue(false)] diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index 52788d3e79..a0beecd252 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -17,7 +17,7 @@ namespace CalamityMod.Particles /// 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 readonly int particleCap = CalamityConfig.Instance.ParticleLimit; private static Particle[] particles; private static int nextVacantIndex; From 8afe663d7e6236ffc89dbef1d292e3db07e903a9 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Fri, 7 Jan 2022 01:13:52 +0100 Subject: [PATCH 9/9] Configurable particle cap better now --- Particles/GeneralParticleHandler.cs | 46 +++++++---------------------- Particles/GenericSparkle.cs | 2 +- Particles/Particle.cs | 9 ++++-- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index a0beecd252..2be3028795 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -13,15 +13,8 @@ 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 = CalamityConfig.Instance.ParticleLimit; - - private static Particle[] particles; + private static List 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; @@ -32,13 +25,13 @@ namespace CalamityMod.Particles internal static void Load() { - particles = new Particle[particleCap]; + particles = new List(); particleTypes = new Dictionary(); particleTextures = new Dictionary(); particleInstances = new List(); - batchedAlphaBlendParticles = new List(particleCap); - batchedAdditiveBlendParticles = new List(particleCap); + batchedAlphaBlendParticles = new List(); + batchedAdditiveBlendParticles = new List(); Type baseParticleType = typeof(Particle); CalamityMod calamity = ModContent.GetInstance(); @@ -76,26 +69,11 @@ namespace CalamityMod.Particles /// public static void SpawnParticle(Particle particle) { - if (activeParticles == particleCap) + if (particles.Count >= CalamityConfig.Instance.ParticleLimit && !particle.Important) return; - particles[nextVacantIndex] = particle; - particle.ID = nextVacantIndex; + particles.Add(particle); particle.Type = particleTypes[particle.GetType()]; - - //If the next index is also vacant - if (nextVacantIndex + 1 < particles.Length && particles[nextVacantIndex + 1] == null) - nextVacantIndex++; - //If it isnt, go check for the first empty spot. - else - for (int i = 0; i < particles.Length; i++) - if (particles[i] == null) - { - nextVacantIndex = i; - break; - } - - activeParticles++; } public static void Update() @@ -107,18 +85,14 @@ namespace CalamityMod.Particles particle.Position += particle.Velocity; particle.Time++; particle.Update(); - - //Clear out particles whose time is up - if (particle.Time >= particle.Lifetime && particle.SetLifetime) - particle.Kill(); } + //Clear out particles whose time is up + particles.RemoveAll(particle => particle.Time >= particle.Lifetime && particle.SetLifetime); } - public static void RemoveParticle(int position) + public static void RemoveParticle(Particle particle) { - particles[position] = null; - nextVacantIndex = position; - activeParticles--; + particles.Remove(particle); } public static void DrawAllParticles(SpriteBatch sb) diff --git a/Particles/GenericSparkle.cs b/Particles/GenericSparkle.cs index 0b5ce64c20..e4b45fbd4c 100644 --- a/Particles/GenericSparkle.cs +++ b/Particles/GenericSparkle.cs @@ -45,7 +45,7 @@ namespace CalamityMod.Particles Texture2D starTexture = GeneralParticleHandler.GetTexture(Type); Texture2D bloomTexture = ModContent.GetTexture("CalamityMod/Particles/BloomCircle"); //Ajust the bloom's texture to be the same size as the star's - float properBloomSize = starTexture.Height / bloomTexture.Height; + float properBloomSize = (float)starTexture.Height / (float)bloomTexture.Height; spriteBatch.Draw(bloomTexture, Position - Main.screenPosition, null, Bloom * opacity * 0.5f, 0, bloomTexture.Size()/2f, Scale * BloomScale * properBloomSize, SpriteEffects.None, 0); spriteBatch.Draw(starTexture, Position - Main.screenPosition, null, Color * opacity * 0.5f, Rotation + MathHelper.PiOver4, starTexture.Size() / 2f, Scale * 0.75f, SpriteEffects.None, 0); diff --git a/Particles/Particle.cs b/Particles/Particle.cs index ee9467d316..74671fda7e 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -17,7 +17,12 @@ namespace CalamityMod.Particles /// The amount of frames this particle has existed for. You shouldn't have to touch this manually. /// public int Time; - + + /// + /// Set this to true if you NEED the particle to render even if the particle cap is reached. + /// + public virtual bool Important => false; + /// /// Set this to true if you want your particle to automatically get removed when its time reaches its maximum lifetime /// @@ -69,7 +74,7 @@ namespace CalamityMod.Particles /// /// Removes the particle from the handler /// - public void Kill() => GeneralParticleHandler.RemoveParticle(ID); + public void Kill() => GeneralParticleHandler.RemoveParticle(this); } } \ No newline at end of file