using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using Terraria; using Terraria.ID; namespace CalamityMod.Particles { public abstract class BaseFusableParticleSet { public class FusableParticleRenderCollection { public BaseFusableParticleSet ParticleSet; public List BackgroundTargets; public FusableParticleRenderCollection(BaseFusableParticleSet set, List backgroundTargets) { ParticleSet = set; BackgroundTargets = backgroundTargets; } } public class FusableParticle { public Vector2 Center; public float Size; public Vector2 TopLeft => Center + new Vector2(-1f, -1f) * Size * 0.5f; public Vector2 TopRight => Center + new Vector2(1f, -1f) * Size * 0.5f; public Vector2 BottomLeft => Center + new Vector2(-1f, 1f) * Size * 0.5f; public Vector2 BottomRight => Center + new Vector2(1f, 1f) * Size * 0.5f; public FusableParticle(Vector2 center, float size) { Center = center; Size = size; } } public int LayerCount { get { // Not doing this will result in inevitable crashes down the line as the graphics engine fails. if (BackgroundShaders.Count != BackgroundTextures.Count) throw new InvalidOperationException("The number of texture maps and shaders are not equivalent."); return BackgroundShaders.Count; } } public List Particles = new(); public FusableParticleRenderCollection RenderCollection => FusableParticleManager.GetParticleRenderCollectionByType(GetType()); public List GetBackgroundTargets => RenderCollection.BackgroundTargets; public virtual float BorderSize => 0f; public virtual bool BorderShouldBeSolid => false; public virtual Color BorderColor => Color.Transparent; public virtual void PrepareOptionalShaderData(Effect effect, int index) { } public abstract FusableParticleRenderLayer RenderLayer { get; } public abstract List BackgroundShaders { get; } public abstract List BackgroundTextures { get; } public abstract FusableParticle SpawnParticle(Vector2 center, float sizeStrength); public abstract void UpdateBehavior(FusableParticle particle); public abstract void DrawParticles(); /// /// Temporarily moves from the backbuffer to a specialized render target, draws all particles, and caches the resulting render target for drawing later. /// internal void PrepareRenderTargetForDrawing() { // Don't bother doing anything if this method is called serverside. if (Main.netMode == NetmodeID.Server) return; // Don't bother doing anything if the set has no particles to render at the moment, for the sake of optimization. if (Particles.Count <= 0) return; // Go through each background render target in the set and clear the entire thing to use a base of transparent pixels. foreach (RenderTarget2D backgroundTarget in GetBackgroundTargets) { Main.instance.GraphicsDevice.SetRenderTarget(backgroundTarget); Main.instance.GraphicsDevice.Clear(Color.Transparent); // Clear away any particles that shouldn't exist anymore. Particles.RemoveAll(p => p.Size <= 5f); // Prepare the sprite batch for specialized drawing in prepration that the graphics device will draw to new render targets. Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.EffectMatrix); // Draw the surviving particles. DrawParticles(); Main.spriteBatch.End(); } // Return to using the backbuffer after done drawing everything to the background targets. Main.instance.GraphicsDevice.SetRenderTarget(null); } } }