using CalamityMod.Buffs.DamageOverTime; using CalamityMod.Events; using CalamityMod.NPCs.ExoMechs.Ares; using CalamityMod.NPCs.Other; using CalamityMod.Projectiles.BaseProjectiles; using CalamityMod.World; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using System; using System.IO; using System.Linq; using Terraria; using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Projectiles.Boss { public class AresDeathBeamStart : BaseLaserbeamProjectile, ILocalizedModType { public new string LocalizationCategory => "Projectiles.Boss"; public override string Texture => "CalamityMod/Projectiles/InvisibleProj"; public int OwnerIndex { get => (int)Projectile.ai[1]; set => Projectile.ai[1] = value; } public override float MaxScale => 1f; public override float MaxLaserLength => 2400f; public override float Lifetime => AresBody.deathrayDuration; public override Color LaserOverlayColor => new Color(250, 250, 250, 100); public override Color LightCastColor => Color.White; public override Texture2D LaserBeginTexture => ModContent.Request("CalamityMod/Projectiles/Boss/AresDeathBeamStart", AssetRequestMode.ImmediateLoad).Value; public override Texture2D LaserMiddleTexture => ModContent.Request("CalamityMod/ExtraTextures/Lasers/AresDeathBeamMiddle", AssetRequestMode.ImmediateLoad).Value; public override Texture2D LaserEndTexture => ModContent.Request("CalamityMod/ExtraTextures/Lasers/AresDeathBeamEnd", AssetRequestMode.ImmediateLoad).Value; public override void SetStaticDefaults() { // Ares' eight-pointed-star (more on higher difficulties) laser beams Main.projFrames[Projectile.type] = 5; ProjectileID.Sets.DrawScreenCheckFluff[Projectile.type] = 10000; // This is its serious name } public override void SetDefaults() { Projectile.Calamity().DealsDefenseDamage = true; Projectile.width = 30; Projectile.height = 30; Projectile.hostile = true; Projectile.alpha = 255; Projectile.penetrate = -1; Projectile.tileCollide = false; Projectile.timeLeft = 600; CooldownSlot = ImmunityCooldownID.Bosses; } public override void SendExtraAI(BinaryWriter writer) { writer.Write(Projectile.localAI[0]); writer.Write(Projectile.localAI[1]); } public override void ReceiveExtraAI(BinaryReader reader) { Projectile.localAI[0] = reader.ReadSingle(); Projectile.localAI[1] = reader.ReadSingle(); } public override void AttachToSomething() { if (Main.npc[OwnerIndex].active && (Main.npc[OwnerIndex].type == ModContent.NPCType() || Main.npc[OwnerIndex].type == ModContent.NPCType())) { Vector2 fireFrom = new Vector2(Main.npc[OwnerIndex].Center.X - 1f, Main.npc[OwnerIndex].Center.Y + 23f); fireFrom += Projectile.velocity.SafeNormalize(Vector2.UnitY) * MathHelper.Lerp(35f, 127f, Projectile.scale * Projectile.scale); Projectile.Center = fireFrom; } // Die of the owner is invalid in some way. else { Projectile.Kill(); return; } // Die if the owner is not performing Ares' deathray attack. if (Main.npc[OwnerIndex].Calamity().newAI[0] != (float)AresBody.Phase.Deathrays && Main.npc[OwnerIndex].type != ModContent.NPCType()) { Projectile.Kill(); return; } // Difficulty modes bool bossRush = BossRushEvent.BossRushActive; bool death = CalamityWorld.death || bossRush; bool revenge = CalamityWorld.revenge || bossRush; bool expertMode = Main.expertMode || bossRush; // Telegraph duration for deathray spiral float deathrayTelegraphDuration = bossRush ? AresBody.deathrayTelegraphDuration_BossRush : death ? AresBody.deathrayTelegraphDuration_Death : revenge ? AresBody.deathrayTelegraphDuration_Rev : expertMode ? AresBody.deathrayTelegraphDuration_Expert : AresBody.deathrayTelegraphDuration_Normal; Time = Main.npc[OwnerIndex].Calamity().newAI[2] - deathrayTelegraphDuration; } public override void UpdateLaserMotion() { // Declare difficulty modes. bool bossRush = BossRushEvent.BossRushActive; bool death = CalamityWorld.death || bossRush; bool revenge = CalamityWorld.revenge || bossRush; bool expertMode = Main.expertMode || bossRush; float angularSlowdownDivisor = bossRush ? 300f : death ? 320f : revenge ? 330f : expertMode ? 340f : 360f; float angularVelocity = MathHelper.TwoPi * Time / Lifetime / angularSlowdownDivisor; if (Main.npc[OwnerIndex].ai[3] % 2f == 0f) angularVelocity *= -1f; // Update the direction and rotation of the laser. Projectile.velocity = Projectile.velocity.RotatedBy(angularVelocity); Projectile.rotation = Projectile.velocity.ToRotation() - MathHelper.PiOver2; } public override float DetermineLaserLength() { float[] sampledLengths = new float[10]; Collision.LaserScan(Projectile.Center, Projectile.velocity, Projectile.width * Projectile.scale, MaxLaserLength, sampledLengths); float newLaserLength = sampledLengths.Average(); // Fire laser through walls at max length if target is behind tiles. if (!Collision.CanHitLine(Main.npc[OwnerIndex].Center, 1, 1, Main.player[Main.npc[OwnerIndex].target].Center, 1, 1)) newLaserLength = MaxLaserLength; return newLaserLength; } public override void PostAI() { // Spawn dust at the end of the beam. int dustType = 107; Vector2 dustCreationPosition = Projectile.Center + Projectile.velocity * (LaserLength - 14f); for (int i = 0; i < 2; i++) { float dustDirection = Projectile.velocity.ToRotation() + Main.rand.NextBool().ToDirectionInt() * MathHelper.PiOver2; Vector2 dustVelocity = dustDirection.ToRotationVector2() * Main.rand.NextFloat(2f, 4f); Dust exoEnergy = Dust.NewDustDirect(dustCreationPosition, 0, 0, dustType, dustVelocity.X, dustVelocity.Y, 0, new Color(0, 255, 255), 1f); exoEnergy.noGravity = true; exoEnergy.scale = 1.7f; } if (Main.rand.NextBool(5)) { Vector2 dustSpawnOffset = Projectile.velocity.RotatedBy(MathHelper.PiOver2) * Main.rand.NextFloatDirection() * Projectile.width * 0.5f; Dust exoEnergy = Dust.NewDustDirect(dustCreationPosition + dustSpawnOffset - Vector2.One * 4f, 8, 8, dustType, 0f, 0f, 100, new Color(0, 255, 255), 1.5f); exoEnergy.velocity *= 0.5f; // Ensure that the dust always moves up. exoEnergy.velocity.Y = -Math.Abs(exoEnergy.velocity.Y); } // Determine frames. Projectile.frameCounter++; if (Projectile.frameCounter % 5f == 0f) Projectile.frame = (Projectile.frame + 1) % Main.projFrames[Projectile.type]; } public override bool PreDraw(ref Color lightColor) { // This should never happen, but just in case- if (Projectile.velocity == Vector2.Zero) return false; // Don't draw the laser if its scale is too low, as that could lead to an infinite loop and out of memory crash. // This has happened in multiplayer historically, so this check is important. if (Projectile.scale < 0.001f) return false; Color beamColor = LaserOverlayColor; Rectangle startFrameArea = LaserBeginTexture.Frame(1, Main.projFrames[Projectile.type], 0, Projectile.frame); Rectangle middleFrameArea = LaserMiddleTexture.Frame(1, Main.projFrames[Projectile.type], 0, Projectile.frame); Rectangle endFrameArea = LaserEndTexture.Frame(1, Main.projFrames[Projectile.type], 0, Projectile.frame); // Start texture drawing. Main.EntitySpriteDraw(LaserBeginTexture, Projectile.Center - Main.screenPosition, startFrameArea, beamColor, Projectile.rotation, LaserBeginTexture.Size() / 2f, Projectile.scale, SpriteEffects.None, 0); // Prepare things for body drawing. float laserBodyLength = LaserLength + middleFrameArea.Height; Vector2 centerOnLaser = Projectile.Center; // Body drawing. if (laserBodyLength > 0f) { float laserOffset = middleFrameArea.Height * Projectile.scale; float incrementalBodyLength = 0f; while (incrementalBodyLength + 1f < laserBodyLength) { Main.EntitySpriteDraw(LaserMiddleTexture, centerOnLaser - Main.screenPosition, middleFrameArea, beamColor, Projectile.rotation, LaserMiddleTexture.Size() * 0.5f, Projectile.scale, SpriteEffects.None, 0); incrementalBodyLength += laserOffset; centerOnLaser += Projectile.velocity * laserOffset; middleFrameArea.Y += LaserMiddleTexture.Height / Main.projFrames[Projectile.type]; if (middleFrameArea.Y + middleFrameArea.Height > LaserMiddleTexture.Height) middleFrameArea.Y = 0; } } Vector2 laserEndCenter = centerOnLaser - Main.screenPosition; Main.EntitySpriteDraw(LaserEndTexture, laserEndCenter, endFrameArea, beamColor, Projectile.rotation, LaserEndTexture.Size() * 0.5f, Projectile.scale, SpriteEffects.None, 0); return false; } public override void OnHitPlayer(Player target, Player.HurtInfo info) { if (info.Damage <= 0) return; target.AddBuff(ModContent.BuffType(), 300); } public override bool CanHitPlayer(Player target) => Projectile.scale >= 0.5f; } }