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