diff --git a/Items/Weapons/Summon/Cosmilamp.cs b/Items/Weapons/Summon/Cosmilamp.cs index 972b111cfa..05629a6a1b 100644 --- a/Items/Weapons/Summon/Cosmilamp.cs +++ b/Items/Weapons/Summon/Cosmilamp.cs @@ -1,7 +1,6 @@ using CalamityMod.Projectiles.Summon; using CalamityMod.Rarities; using Microsoft.Xna.Framework; -using System; using Terraria; using Terraria.DataStructures; using Terraria.ID; @@ -11,11 +10,19 @@ namespace CalamityMod.Items.Weapons.Summon { public class Cosmilamp : ModItem { + public const int BeamShootRate = 105; + + public const float MaxTargetingDistance = 1360f; + + public const float BeamHomeSpeed = 17f; + + public const float LanternSummonCost = 2f; + public override void SetStaticDefaults() { DisplayName.SetDefault("Cosmilamp"); - Tooltip.SetDefault("Summons a cosmic lantern to fight for you\n" + - "Takes up 2 minion slots"); + Tooltip.SetDefault("Summons a cosmic lantern above your head to fight for you\n" + + $"Each lantern takes up {LanternSummonCost} minion slots"); SacrificeTotal = 1; } @@ -42,30 +49,26 @@ namespace CalamityMod.Items.Weapons.Summon public override bool Shoot(Player player, EntitySource_ItemUse_WithAmmo source, Vector2 position, Vector2 velocity, int type, int damage, float knockback) { - float num72 = Item.shootSpeed; - Vector2 vector2 = player.RotatedRelativePoint(player.MountedCenter, true); - float num78 = (float)Main.mouseX + Main.screenPosition.X - vector2.X; - float num79 = (float)Main.mouseY + Main.screenPosition.Y - vector2.Y; - if (player.gravDir == -1f) + if (player.altFunctionUse != 2) { - num79 = Main.screenPosition.Y + (float)Main.screenHeight - (float)Main.mouseY - vector2.Y; + // Reset the timer for all lamps, to re-align the formation. + for (int i = 0; i < Main.maxProjectiles; i++) + { + if (Main.projectile[i].type == type && Main.projectile[i].owner == player.whoAmI && Main.projectile[i].active) + { + Main.projectile[i].ModProjectile().Timer = 0f; + Main.projectile[i].netUpdate = true; + } + } + + int existingLamps = player.ownedProjectileCounts[type]; + int p = Projectile.NewProjectile(source, Main.MouseWorld, Vector2.Zero, type, damage, knockback, player.whoAmI); + if (Main.projectile.IndexInRange(p)) + { + Main.projectile[p].originalDamage = Item.damage; + Main.projectile[p].ai[0] = existingLamps; + } } - float num80 = (float)Math.Sqrt((double)(num78 * num78 + num79 * num79)); - if ((float.IsNaN(num78) && float.IsNaN(num79)) || (num78 == 0f && num79 == 0f)) - { - num78 = (float)player.direction; - } - else - { - num80 = num72 / num80; - } - num78 = 0f; - num79 = 0f; - vector2.X = (float)Main.mouseX + Main.screenPosition.X; - vector2.Y = (float)Main.mouseY + Main.screenPosition.Y; - int p = Projectile.NewProjectile(source, vector2.X, vector2.Y, num78, num79, ModContent.ProjectileType(), damage, knockback, player.whoAmI, 0f, 0f); - if (Main.projectile.IndexInRange(p)) - Main.projectile[p].originalDamage = Item.damage; return false; } } diff --git a/Projectiles/Summon/CosmilampBeam.cs b/Projectiles/Summon/CosmilampBeam.cs new file mode 100644 index 0000000000..457c9cb6b9 --- /dev/null +++ b/Projectiles/Summon/CosmilampBeam.cs @@ -0,0 +1,122 @@ +using CalamityMod.Items.Weapons.Summon; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using Terraria; +using Terraria.Graphics.Shaders; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Projectiles.Summon +{ + public class CosmilampBeam : ModProjectile + { + internal PrimitiveTrail TrailDrawer; + + public ref float Timer => ref Projectile.ai[0]; + + public const int SlowdownTime = 45; + + public const int Lifetime = 120; + + public override string Texture => "CalamityMod/Projectiles/InvisibleProj"; + + public override void SetStaticDefaults() + { + DisplayName.SetDefault("Cosmic Beam"); + ProjectileID.Sets.MinionShot[Projectile.type] = true; + ProjectileID.Sets.TrailingMode[Projectile.type] = 2; + ProjectileID.Sets.TrailCacheLength[Projectile.type] = 32; + } + + public override void SetDefaults() + { + Projectile.width = 16; + Projectile.height = 16; + Projectile.friendly = true; + Projectile.ignoreWater = true; + Projectile.tileCollide = false; + Projectile.DamageType = DamageClass.Summon; + Projectile.minionSlots = 0f; + Projectile.penetrate = -1; + Projectile.MaxUpdates = 3; + Projectile.timeLeft = Projectile.MaxUpdates * Lifetime; + Projectile.usesLocalNPCImmunity = true; + Projectile.localNPCHitCooldown = Projectile.MaxUpdates * 15; + } + + public override void AI() + { + Lighting.AddLight(Projectile.Center, 0.2f, 0.01f, 0.1f); + Projectile.Opacity = Utils.GetLerpValue(0f, Projectile.MaxUpdates * 10f, Projectile.timeLeft, true); + + // Emit light. + Lighting.AddLight(Projectile.Center, Vector3.One * Projectile.Opacity * 0.7f); + + NPC potentialTarget = Projectile.Center.MinionHoming(Cosmilamp.MaxTargetingDistance, Main.player[Projectile.owner]); + + // Slow down before doing anything else. + if (Timer < SlowdownTime) + Projectile.velocity *= 0.995f; + + // Afterwards, race towards nearby targets at incredible speeds. + else if (potentialTarget is not null) + { + Vector2 idealVelocity = Projectile.SafeDirectionTo(potentialTarget.Center) * Cosmilamp.BeamHomeSpeed; + if (!Projectile.WithinRange(potentialTarget.Center, 160f)) + { + Projectile.velocity = Vector2.Lerp(Projectile.velocity, idealVelocity, 0.036f); + Projectile.velocity = Projectile.velocity.ToRotation().AngleTowards(idealVelocity.ToRotation(), 0.12f).ToRotationVector2() * Projectile.velocity.Length(); + } + + // Spin around to give a slice motion if close to the target. To prevent overshooting, the amount of angular velocity varies based on how large the target is. + else + { + float angularTurnSpeed = MathHelper.Pi * potentialTarget.Size.Length() / 12000f; + if (angularTurnSpeed > MathHelper.Pi * 0.05f) + angularTurnSpeed = MathHelper.Pi * 0.05f; + + Projectile.velocity = Projectile.velocity.RotatedBy(angularTurnSpeed); + } + } + + if (Projectile.FinalExtraUpdate()) + Timer++; + } + + internal Color ColorFunction(float completionRatio) + { + float streakOpacity = Utils.GetLerpValue(0.8f, 0.54f, completionRatio, true) * Projectile.Opacity; + Color endColor = Color.Lerp(Color.Fuchsia, Color.DarkViolet, (float)Math.Sin(completionRatio * MathHelper.Pi * 1.6f - Main.GlobalTimeWrappedHourly * 4f) * 0.5f + 0.5f); + endColor = CalamityUtils.MulticolorLerp(completionRatio * completionRatio, endColor, Color.MediumPurple, Color.Red); + if (Projectile.localAI[0] == 1f) + endColor = Color.Lerp(endColor, Color.White, 0.5f) * streakOpacity; + + return Color.Lerp(endColor, Color.Black, 0.3f) * streakOpacity; + } + + internal float WidthFunction(float completionRatio) + { + float expansionCompletion = 1f - (float)Math.Pow(1f - Utils.GetLerpValue(0f, 0.3f, completionRatio, true), 2D); + float maxWidth = Projectile.Opacity * Projectile.width * 1.65f; + if (Projectile.localAI[0] == 1f) + maxWidth *= 0.4f; + + return MathHelper.Lerp(0f, maxWidth, expansionCompletion); + } + + public override bool PreDraw(ref Color lightColor) + { + // Initialize the trail drawer. + TrailDrawer ??= new(WidthFunction, ColorFunction, specialShader: GameShaders.Misc["CalamityMod:ImpFlameTrail"]); + + Projectile.localAI[0] = 0f; + GameShaders.Misc["CalamityMod:ImpFlameTrail"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/ScarletDevilStreak")); + TrailDrawer.Draw(Projectile.oldPos, Projectile.Size * 0.5f - Main.screenPosition, 42); + + Projectile.localAI[0] = 1f; + TrailDrawer.Draw(Projectile.oldPos, Projectile.Size * 0.5f - Main.screenPosition, 42); + return false; + } + } +} diff --git a/Projectiles/Summon/CosmilampMinion.cs b/Projectiles/Summon/CosmilampMinion.cs index 4f1226f3e3..45626d38ef 100644 --- a/Projectiles/Summon/CosmilampMinion.cs +++ b/Projectiles/Summon/CosmilampMinion.cs @@ -1,8 +1,10 @@ using CalamityMod.Buffs.Summon; -using CalamityMod.CalPlayer; +using CalamityMod.Items.Weapons.Summon; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; using System; using Terraria; +using Terraria.Audio; using Terraria.ID; using Terraria.ModLoader; @@ -10,14 +12,37 @@ namespace CalamityMod.Projectiles.Summon { public class CosmilampMinion : ModProjectile { + public Player Owner => Main.player[Projectile.owner]; + + public int HoverOffsetIndex => (int)Projectile.ai[0]; + + public float HoverOffsetInterpolant + { + get + { + float projectileCounts = Owner.ownedProjectileCounts[Type]; + + // Use a midway interpolant if the projectile count is one. This makes the fist use middle positions instead of + // sitting awkwardly to the left. + if (projectileCounts <= 1f) + return 0.5f; + + return HoverOffsetIndex / (projectileCounts - 1f); + } + } + + public ref float Timer => ref Projectile.ai[1]; + public override string Texture => "CalamityMod/NPCs/Signus/CosmicLantern"; public override void SetStaticDefaults() { DisplayName.SetDefault("Cosmilamp"); + Main.projFrames[Projectile.type] = 4; + ProjectileID.Sets.TrailingMode[Type] = 2; + ProjectileID.Sets.TrailCacheLength[Type] = 8; ProjectileID.Sets.MinionSacrificable[Projectile.type] = true; ProjectileID.Sets.MinionTargettingFeature[Projectile.type] = true; - Main.projFrames[Projectile.type] = 4; } public override void SetDefaults() @@ -26,216 +51,107 @@ namespace CalamityMod.Projectiles.Summon Projectile.height = 20; Projectile.netImportant = true; Projectile.friendly = true; - Projectile.minionSlots = 2f; - Projectile.timeLeft = 18000; - Projectile.penetrate = -1; - Projectile.timeLeft *= 5; - Projectile.minion = true; + Projectile.ignoreWater = false; Projectile.tileCollide = false; - Projectile.usesLocalNPCImmunity = true; - Projectile.localNPCHitCooldown = 24; - Projectile.extraUpdates = 1; + Projectile.minionSlots = Cosmilamp.LanternSummonCost; + Projectile.timeLeft = 90000; + Projectile.penetrate = -1; + Projectile.minion = true; + Projectile.MaxUpdates = 2; Projectile.DamageType = DamageClass.Summon; } public override void AI() { - Player player = Main.player[Projectile.owner]; - CalamityPlayer modPlayer = player.Calamity(); - if (Projectile.localAI[0] == 0f) - { - int num226 = 36; - for (int num227 = 0; num227 < num226; num227++) - { - Vector2 vector6 = Vector2.Normalize(Projectile.velocity) * new Vector2((float)Projectile.width / 2f, (float)Projectile.height) * 0.75f; - vector6 = vector6.RotatedBy((double)((float)(num227 - (num226 / 2 - 1)) * 6.28318548f / (float)num226), default) + Projectile.Center; - Vector2 vector7 = vector6 - Projectile.Center; - int num228 = Dust.NewDust(vector6 + vector7, 0, 0, 204, vector7.X * 1.5f, vector7.Y * 1.5f, 100, default, 1.4f); - Main.dust[num228].noGravity = true; - Main.dust[num228].noLight = true; - Main.dust[num228].velocity = vector7; - } - Projectile.localAI[0] += 1f; - } + // Decide whether the minion should still exist. + HandleMinionBools(); - Projectile.frameCounter++; - if (Projectile.frameCounter >= 4) - { - Projectile.frame++; - Projectile.frameCounter = 0; - } - if (Projectile.frame >= Main.projFrames[Projectile.type]) - { - Projectile.frame = 0; - } + // Decide frames. + DecideFrames(); - Lighting.AddLight(Projectile.Center, (255 - Projectile.alpha) * 0.75f / 255f, (255 - Projectile.alpha) * 0f / 255f, (255 - Projectile.alpha) * 0.75f / 255f); - float num395 = (float)Main.mouseTextColor / 200f - 0.35f; - num395 *= 0.2f; - Projectile.scale = num395 + 0.95f; - int num1262 = Dust.NewDust(Projectile.position, Projectile.width, Projectile.height, 204, 0f, 0f, 0, default, 1f); - Main.dust[num1262].velocity *= 0.1f; - Main.dust[num1262].scale = 0.7f; - Main.dust[num1262].noGravity = true; - bool flag64 = Projectile.type == ModContent.ProjectileType(); - player.AddBuff(ModContent.BuffType(), 3600); - if (flag64) + // Hover above the owner. + HoverInPlace(); + + // Increment the universal timer. + Timer++; + + // Shoot at nearby enemies. + NPC potentialTarget = Projectile.Center.MinionHoming(Cosmilamp.MaxTargetingDistance, Owner); + if (potentialTarget is not null) { - if (player.dead) + int wrappedAttackTimer = (int)(Timer % Cosmilamp.BeamShootRate); + if (wrappedAttackTimer == (int)(HoverOffsetInterpolant * (Cosmilamp.BeamShootRate - 18f))) { - modPlayer.cLamp = false; - } - if (modPlayer.cLamp) - { - Projectile.timeLeft = 2; - } - } - Projectile.MinionAntiClump(); - float num535 = Projectile.position.X; - float num536 = Projectile.position.Y; - float num537 = 3000f; - bool flag19 = false; - int num538 = 2500; - if (Projectile.ai[1] != 0f) - { - num538 = 4000; - } - if (Math.Abs(Projectile.Center.X - Main.player[Projectile.owner].Center.X) + Math.Abs(Projectile.Center.Y - Main.player[Projectile.owner].Center.Y) > (float)num538) - { - Projectile.ai[0] = 1f; - } - if (Projectile.ai[0] == 0f) - { - if (player.HasMinionAttackTargetNPC) - { - NPC npc = Main.npc[player.MinionAttackTargetNPC]; - if (npc.CanBeChasedBy(Projectile, false)) + SoundEngine.PlaySound(SoundID.Item158, Projectile.Center); + if (Main.myPlayer == Projectile.owner) { - float num539 = npc.position.X + (float)(npc.width / 2); - float num540 = npc.position.Y + (float)(npc.height / 2); - float num541 = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - num539) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - num540); - if (num541 < num537 && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, npc.position, npc.width, npc.height)) - { - num535 = num539; - num536 = num540; - flag19 = true; - } + Vector2 beamVelocity = Projectile.SafeDirectionTo(potentialTarget.Center).RotatedByRandom(0.32f) * Main.rand.NextFloat(9.4f, 11f); + int beam = Projectile.NewProjectile(Projectile.GetSource_FromThis(), Projectile.Center, beamVelocity, ModContent.ProjectileType(), Projectile.damage, Projectile.knockBack, Projectile.owner); + if (Main.projectile.IndexInRange(beam)) + Main.projectile[beam].originalDamage = Projectile.originalDamage; } } - if (!flag19) - { - for (int num542 = 0; num542 < Main.maxNPCs; num542++) - { - if (Main.npc[num542].CanBeChasedBy(Projectile, false)) - { - float num543 = Main.npc[num542].position.X + (float)(Main.npc[num542].width / 2); - float num544 = Main.npc[num542].position.Y + (float)(Main.npc[num542].height / 2); - float num545 = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - num543) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - num544); - if (num545 < num537) - { - num537 = num545; - num535 = num543; - num536 = num544; - flag19 = true; - } - } - } - } - } - if (!flag19) - { - float num546 = 12f; - if (Projectile.ai[0] == 1f) - { - num546 = 18f; - } - Vector2 vector42 = Projectile.Center; - float num547 = player.Center.X - vector42.X; - float num548 = player.Center.Y - vector42.Y - 60f; - float num549 = (float)Math.Sqrt((double)(num547 * num547 + num548 * num548)); - if (num549 < 400f && Projectile.ai[0] == 1f && !Collision.SolidCollision(Projectile.position, Projectile.width, Projectile.height)) - { - Projectile.ai[0] = 0f; - } - if (num549 > 2000f) - { - Projectile.position.X = Main.player[Projectile.owner].Center.X - (float)(Projectile.width / 2); - Projectile.position.Y = Main.player[Projectile.owner].Center.Y - (float)(Projectile.width / 2); - } - if (num549 > 70f) - { - num549 = num546 / num549; - num547 *= num549; - num548 *= num549; - Projectile.velocity.X = (Projectile.velocity.X * 20f + num547) / 21f; - Projectile.velocity.Y = (Projectile.velocity.Y * 20f + num548) / 21f; - } - else - { - if (Projectile.velocity.X == 0f && Projectile.velocity.Y == 0f) - { - Projectile.velocity.X = -0.15f; - Projectile.velocity.Y = -0.05f; - } - Projectile.velocity *= 1.01f; - } - Projectile.rotation = Projectile.velocity.X * 0.05f; - if ((double)Math.Abs(Projectile.velocity.X) > 0.2) - { - Projectile.spriteDirection = -Projectile.direction; - return; - } - } - else - { - if (Projectile.ai[1] == -1f) - { - Projectile.ai[1] = 17f; - } - if (Projectile.ai[1] > 0f) - { - Projectile.ai[1] -= 1f; - } - if (Projectile.ai[1] == 0f) - { - float num550 = 24f; //12 - Vector2 vector43 = new Vector2(Projectile.position.X + (float)Projectile.width * 0.5f, Projectile.position.Y + (float)Projectile.height * 0.5f); - float num551 = num535 - vector43.X; - float num552 = num536 - vector43.Y; - float num553 = (float)Math.Sqrt((double)(num551 * num551 + num552 * num552)); - if (num553 < 100f) - { - num550 = 28f; //14 - } - num553 = num550 / num553; - num551 *= num553; - num552 *= num553; - Projectile.velocity.X = (Projectile.velocity.X * 14f + num551) / 15f; - Projectile.velocity.Y = (Projectile.velocity.Y * 14f + num552) / 15f; - } - else - { - if (Math.Abs(Projectile.velocity.X) + Math.Abs(Projectile.velocity.Y) < 10f) - { - Projectile.velocity *= 1.05f; - } - } - Projectile.rotation = Projectile.velocity.X * 0.05f; - if (Math.Abs(Projectile.velocity.X) > 0.2f) - { - Projectile.spriteDirection = -Projectile.direction; - return; - } } } - public override void OnHitNPC(NPC target, int damage, float knockback, bool crit) + public void HandleMinionBools() { - if (Main.myPlayer == Projectile.owner) + Owner.AddBuff(ModContent.BuffType(), 3600); + if (Projectile.type == ModContent.ProjectileType()) { - Projectile.ai[1] = -1f; - Projectile.netUpdate = true; + if (Owner.dead) + Owner.Calamity().cLamp = false; + + if (Owner.Calamity().cLamp) + Projectile.timeLeft = 2; } } + + public void DecideFrames() + { + Projectile.frameCounter++; + Projectile.frame = Projectile.frameCounter / 8 % Main.projFrames[Projectile.type]; + } + + public void HoverInPlace() + { + Vector2 hoverDestination = Owner.Top + new Vector2(MathHelper.Lerp(-100f, 100f, HoverOffsetInterpolant), -80f); + hoverDestination.Y += ((float)Math.Sin(MathHelper.TwoPi * HoverOffsetInterpolant + Timer / 50f) * 0.5f + 0.5f) * 40f; + + // Zoom towards the hover destination. + Projectile.Center = Vector2.Lerp(Projectile.Center, hoverDestination, 0.02f).MoveTowards(hoverDestination, 8f); + } + + public override bool PreDraw(ref Color lightColor) + { + Texture2D texture = ModContent.Request(Texture).Value; + Rectangle frame = texture.Frame(1, Main.projFrames[Type], 0, Projectile.frame); + Vector2 origin = frame.Size() * 0.5f; + Vector2 drawPosition = Projectile.Center - Main.screenPosition; + SpriteEffects direction = Projectile.spriteDirection == 1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; + + // Draw afterimages. + for (int i = 0; i < Projectile.oldPos.Length; i++) + { + float afterimageFade = (1f - i / (float)Projectile.oldPos.Length); + Color afterimageDrawColor = Color.Lerp(Color.Fuchsia, Color.Cyan, afterimageFade) with { A = 25 } * Projectile.Opacity * afterimageFade * 0.6f; + Vector2 afterimageDrawPosition = Projectile.oldPos[i] + Projectile.Size * 0.5f - Main.screenPosition; + Main.EntitySpriteDraw(texture, afterimageDrawPosition, frame, afterimageDrawColor, Projectile.rotation, origin, Projectile.scale, direction, 0); + } + + // Draw a cyan backglow. + for (int i = 0; i < 8; i++) + { + Color afterimageDrawColor = Color.Cyan with { A = 25 } * Projectile.Opacity * 0.4f; + Vector2 afterimageDrawPosition = Projectile.Center - Main.screenPosition + (MathHelper.TwoPi * i / 8f).ToRotationVector2() * 3f; + Main.EntitySpriteDraw(texture, afterimageDrawPosition, frame, afterimageDrawColor, Projectile.rotation, origin, Projectile.scale, direction, 0); + } + + Main.EntitySpriteDraw(texture, drawPosition, frame, Projectile.GetAlpha(lightColor), Projectile.rotation, origin, Projectile.scale, direction, 0); + return false; + } + + // The lamps themselves do not do damage, but they do store damage for the sake of shooting projectiles. + public override bool? CanDamage() => false; } }