using System; using System.Collections.Generic; using System.Linq; using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ModLoader; namespace CalamityMod.Graphics.Metaballs { public class SignusMetaball : Metaball { public Color borderColor = Color.Purple; public Vector2 goalDir = Vector2.Zero; public Vector2 scrollDir = Vector2.Zero; public class CosmicParticle { public float Size; public Vector2 Velocity; public Vector2 Center; public Vector2 BaseSquash = Vector2.One; public float Rotation; public float ShrinkSpeed = 0; public int Lifetime; public float SquashPower = 0; public int time = 0; public CosmicParticle(Vector2 center, Vector2 velocity, float size, int lifetime, Vector2 squash, float shrinkSpeed = 0, float velocitySquash = 0) { Center = center; Velocity = velocity; Size = size; Rotation = Velocity.ToRotation() + MathHelper.PiOver2; BaseSquash = squash; Lifetime = lifetime; ShrinkSpeed = shrinkSpeed; SquashPower = velocitySquash; } public void Update() { Center += Velocity; Velocity *= 0.95f; if ((float)time / (float)Lifetime > 0.5f) { Size *= 0.94f; } BaseSquash.X *= (1 - 0.2f * ShrinkSpeed); BaseSquash.Y *= (1 + 0.2f * ShrinkSpeed); Rotation = Velocity.ToRotation() + MathHelper.PiOver2; time++; } } public static Asset LayerAsset { get; private set; } public static List Particles { get; private set; } = new(); public override bool AnythingToDraw => Particles.Any(); public override IEnumerable Layers { get { yield return LayerAsset.Value; } } public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeNPCs; public override void Load() { if (Main.dedServ) return; // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/SignusLayer", AssetRequestMode.ImmediateLoad); } public override void Update() { if (goalDir == Vector2.Zero) goalDir = Vector2.One.RotatedByRandom(Math.PI); if ((int)(Main.GlobalTimeWrappedHourly * 50) % 5 == 0) { goalDir = goalDir.RotatedByRandom(0.13f); } scrollDir = Vector2.Lerp(scrollDir, goalDir, 0.01f); float rate = Main.GlobalTimeWrappedHourly * 19; List colors = new List() { Color.Purple, Color.BlueViolet, Color.DarkViolet }; int colorIndex = (int)(rate / 2 % colors.Count); Color currentColor = colors[colorIndex]; Color nextColor = colors[(colorIndex + 1) % colors.Count]; borderColor = Color.Lerp(currentColor, nextColor, rate % 2f > 1f ? 1f : rate % 1f); // Update all particle instances. // Once sufficiently small, they vanish. for (int i = 0; i < Particles.Count; i++) Particles[i].Update(); Particles.RemoveAll(p => p.Size <= 2f); } public override Color EdgeColor => borderColor with { A = 0 }; public static void SpawnParticle(Vector2 position, Vector2 velocity, float size, int lifetime, Vector2 squash, float shrinkSpeed = 0, float velocitySquash = 0) => Particles.Add(new(position, velocity, size, lifetime, squash, shrinkSpeed, velocitySquash)); // Make the texture scroll. public override Vector2 CalculateManualOffsetForLayer(int layerIndex) { return scrollDir * 0.5f; } public override void DrawInstances() { Texture2D tex = ModContent.Request("CalamityMod/ExtraTextures/BasicMess").Value; foreach (CosmicParticle particle in Particles) { Vector2 squash = Vector2.Lerp(particle.BaseSquash, new Vector2(Utils.Remap(particle.Velocity.Length(), 2, 7, 1, 0.5f), Utils.Remap(particle.Velocity.Length(), 2, 7, 1, 2.5f)), particle.SquashPower); Vector2 drawPosition = particle.Center - Main.screenPosition; Vector2 origin = tex.Size() * 0.5f; Vector2 scale = Vector2.One * particle.Size / tex.Size(); Main.spriteBatch.Draw(tex, drawPosition, null, Color.White, particle.Rotation, origin, squash * scale, SpriteEffects.None, 0f); } } } }