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/CalamityMod.cs b/CalamityMod.cs index c97cc09828..779c660bcf 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; @@ -1598,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/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/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 561044bbee..731bd5765e 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -50,6 +50,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; @@ -125,6 +126,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 49af526275..031179cf16 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 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); + 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/BaseParticleSet.cs b/Particles/BaseParticleSet.cs index f5c48fdf59..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++; } @@ -66,11 +83,18 @@ 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 + { + 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/BloomCircle.png b/Particles/BloomCircle.png new file mode 100644 index 0000000000..a1c0bd7510 Binary files /dev/null and b/Particles/BloomCircle.png differ diff --git a/Particles/EnchantedParticle.cs b/Particles/EnchantedParticle.cs new file mode 100644 index 0000000000..899da6a2e5 --- /dev/null +++ b/Particles/EnchantedParticle.cs @@ -0,0 +1,50 @@ +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; + 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 new file mode 100644 index 0000000000..2be3028795 --- /dev/null +++ b/Particles/GeneralParticleHandler.cs @@ -0,0 +1,150 @@ +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 +{ + public static class GeneralParticleHandler + { + private static List particles; + private static int nextVacantIndex; + //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 List(); + particleTypes = new Dictionary(); + particleTextures = new Dictionary(); + particleInstances = new List(); + + batchedAlphaBlendParticles = new List(); + batchedAdditiveBlendParticles = new List(); + + 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 (particles.Count >= CalamityConfig.Instance.ParticleLimit && !particle.Important) + return; + + particles.Add(particle); + particle.Type = particleTypes[particle.GetType()]; + } + + public static void Update() + { + foreach (Particle particle in particles) + { + if (particle == null) + continue; + particle.Position += particle.Velocity; + particle.Time++; + particle.Update(); + } + //Clear out particles whose time is up + particles.RemoveAll(particle => particle.Time >= particle.Lifetime && particle.SetLifetime); + } + + public static void RemoveParticle(Particle particle) + { + particles.Remove(particle); + } + + 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]; + + 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"; + } +} diff --git a/Particles/GenericSparkle.cs b/Particles/GenericSparkle.cs new file mode 100644 index 0000000000..e4b45fbd4c --- /dev/null +++ b/Particles/GenericSparkle.cs @@ -0,0 +1,55 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +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; + + 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 = (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); + spriteBatch.Draw(starTexture, Position - Main.screenPosition, null, Color * opacity, Rotation, starTexture.Size() / 2f, Scale, SpriteEffects.None, 0); + } + } +} 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 cf3bac931f..74671fda7e 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -1,21 +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; - public int Lifetime; - public int Variant; - public float Scale; + + /// + /// 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 + /// + 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 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 + /// public Vector2 RelativeOffset; - public Particle(int lifetime, Vector2 relativeOffset, float scale, int variant = 0) - { - Time = 0; - Lifetime = lifetime; - Variant = variant; - Scale = scale; - RelativeOffset = 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 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. + /// + 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(this); + } } \ 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/Sparkle.png b/Particles/Sparkle.png new file mode 100644 index 0000000000..79930b6e99 Binary files /dev/null and b/Particles/Sparkle.png differ diff --git a/Particles/ThanatosSmokeParticle.cs b/Particles/ThanatosSmokeParticle.cs new file mode 100644 index 0000000000..27235a3071 --- /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, LifetimeCompletion, 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