From d96088060a5301292b5d2ddeab2fc5941dc1cba0 Mon Sep 17 00:00:00 2001 From: DominicKarma42 <51094361+DominicKarma@users.noreply.github.com> Date: Sat, 1 Aug 2020 01:45:24 -0500 Subject: [PATCH] Cryogen Sky Aurora effect --- CalPlayer/CalamityPlayer.cs | 14 ++++ CalamityMod.cs | 2 + Skies/CryogenSky.cs | 147 ++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 Skies/CryogenSky.cs diff --git a/CalPlayer/CalamityPlayer.cs b/CalPlayer/CalamityPlayer.cs index 94d5253249..f7a1d7894f 100644 --- a/CalPlayer/CalamityPlayer.cs +++ b/CalPlayer/CalamityPlayer.cs @@ -2447,6 +2447,20 @@ namespace CalamityMod.CalPlayer bool inAstral = ZoneAstral; player.ManageSpecialBiomeVisuals("CalamityMod:Astral", inAstral); + + bool cryogenActive = NPC.AnyNPCs(ModContent.NPCType()); + + if (SkyManager.Instance["CalamityMod:Cryogen"] != null && cryogenActive != SkyManager.Instance["CalamityMod:Cryogen"].IsActive()) + { + if (cryogenActive) + { + SkyManager.Instance.Activate("CalamityMod:Cryogen"); + } + else + { + SkyManager.Instance.Deactivate("CalamityMod:Cryogen"); + } + } } public override void UpdateBiomes() diff --git a/CalamityMod.cs b/CalamityMod.cs index 81ab98224a..fa7c047563 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -337,6 +337,8 @@ namespace CalamityMod GameShaders.Misc["CalamityMod:SubsumingTentacle"] = new MiscShaderData(new Ref(TentacleShader), "BurstPass"); GameShaders.Misc["CalamityMod:LightDistortion"] = new MiscShaderData(new Ref(LightDistortionShader), "DistortionPass"); + SkyManager.Instance["CalamityMod:Cryogen"] = new CryogenSky(); + RipperUI.Reset(); AstralArcanumUI.Load(this); diff --git a/Skies/CryogenSky.cs b/Skies/CryogenSky.cs new file mode 100644 index 0000000000..bd4fd5ca4f --- /dev/null +++ b/Skies/CryogenSky.cs @@ -0,0 +1,147 @@ +using CalamityMod.NPCs.Cryogen; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using Terraria; +using Terraria.Graphics.Effects; +using Terraria.ModLoader; + +namespace CalamityMod.Skies +{ + public class CryogenSky : CustomSky + { + public struct CryogenAurora + { + public float Depth; + public float ColorHueOffset; + public float CenterOffsetRatio; + public Vector2 Center; + public SpriteEffects DirectionEffect; + } + private int cryogenIndex = -1; + private float fadeIn = 0f; + private float fadeOut = 0f; + private CryogenAurora[] Auroras; + + internal const int TimePerRotation = 75; + internal static Texture2D AuroraTexture => ModContent.GetTexture("CalamityMod/ExtraTextures/AuroraTexture"); + + public override void Update(GameTime gameTime) + { + if (fadeIn > 0) + fadeIn--; + + if (cryogenIndex == -1) + { + UpdateCryogenIndex(); + if (fadeOut == 0) + fadeOut = 45f; + fadeOut--; + if (fadeOut <= 0f) + Deactivate(); + } + for (int i = 0; i < Auroras.Length; i++) + { + Vector2 centerRange = new Vector2(Main.screenWidth * 0.5f, Main.screenHeight * 0.1f / Auroras[i].Depth); + Auroras[i].Center = centerRange * (Auroras[i].CenterOffsetRatio * MathHelper.TwoPi).ToRotationVector2() + Vector2.UnitX * Main.screenWidth * 0.5f; + Auroras[i].Center.Y -= 180f + (float)Math.Cos(Main.GlobalTime * 1.2f + Auroras[i].CenterOffsetRatio * MathHelper.Pi) * 50f; + Auroras[i].CenterOffsetRatio += 1f / 1800f * MathHelper.Lerp(0.2f, 1f, 1f - GetCryogenLifeRatio()) * (float)Math.Sin(Main.GlobalTime * 0.9f); + } + } + + private float GetCryogenLifeRatio() + { + if (UpdateCryogenIndex()) + { + float localIntensity = 0f; + if (cryogenIndex != -1) + { + localIntensity = Main.npc[cryogenIndex].life / (float)Main.npc[cryogenIndex].lifeMax; + } + return localIntensity; + } + return 0f; + } + + private bool UpdateCryogenIndex() + { + int cryogenType = ModContent.NPCType(); + if (cryogenIndex >= 0 && Main.npc[cryogenIndex].active && Main.npc[cryogenIndex].type == cryogenType) + { + return true; + } + cryogenIndex = -1; + for (int i = 0; i < Main.npc.Length; i++) + { + if (Main.npc[i].active && Main.npc[i].type == cryogenType) + { + cryogenIndex = i; + break; + } + } + return cryogenIndex != -1; + } + + public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth) + { + if (maxDepth >= float.MaxValue && minDepth < float.MaxValue) + { + float drawPower = 1f - GetCryogenLifeRatio(); + for (int i = 0; i < Auroras.Length; i++) + { + float hue = 0.5f + Auroras[i].ColorHueOffset * 0.5f * MathHelper.Lerp(0.15f, 0.95f, drawPower); + hue += (float)Math.Cos(Main.GlobalTime * 1.2f) * 0.25f; + hue %= 1f; + float scale = 1.4f / Auroras[i].Depth; + scale += (float)Math.Cos(Main.GlobalTime * 0.7f + Auroras[i].CenterOffsetRatio * MathHelper.TwoPi) * 0.2f; + + Color auroraColor = Main.hslToRgb(hue, 1f, 0.825f) * 0.85f; + auroraColor *= MathHelper.Lerp(0.3f, 1f, drawPower); + + if (fadeIn > 0f) + auroraColor *= Utils.InverseLerp(45f, 0f, fadeIn); + if (fadeOut > 0f) + auroraColor *= Utils.InverseLerp(0f, 45f, fadeOut); + if (Main.dayTime) + auroraColor *= 0.4f; + + float yBrightness = MathHelper.Lerp(1.5f, 0.5f, 1f - MathHelper.Clamp((Auroras[i].Center.Y + 300f) / 200f, 0f, 1f)) * 1.3f; + yBrightness *= MathHelper.Lerp(0.6f, 1.1f, (float)Math.Sin(Main.GlobalTime / 1.8f) * 0.5f + 0.5f); + if (yBrightness > 1.3f) + yBrightness = 1.3f; + auroraColor *= yBrightness; + + spriteBatch.Draw(AuroraTexture, + Auroras[i].Center, + null, + auroraColor * 1.1f, + MathHelper.PiOver2, + AuroraTexture.Size() * 0.5f, + scale, + Auroras[i].DirectionEffect, + 0f); + } + } + } + + public override void Activate(Vector2 position, params object[] args) + { + fadeIn = 45f; + Auroras = new CryogenAurora[150]; + for (int i = 0; i < Auroras.Length; i++) + { + Auroras[i].Depth = Main.rand.NextFloat(1f, 2.2f); + Auroras[i].ColorHueOffset = Main.rand.NextFloat(-1f, 1f); + Auroras[i].DirectionEffect = Utils.SelectRandom(Main.rand, SpriteEffects.None, SpriteEffects.FlipHorizontally); + Auroras[i].CenterOffsetRatio = i / (float)Auroras.Length + Main.rand.NextFloat(3f / Auroras.Length); + } + } + + public override void Deactivate(params object[] args) + { + } + + public override void Reset() { } // a + public override bool IsActive() => (cryogenIndex != -1 || fadeOut > 0 || fadeIn > 0) && !Main.gameMenu; + } +}