using CalamityMod.Particles; using CalamityMod.Particles.Metaballs; using Microsoft.Xna.Framework; using System; using Terraria; using Terraria.ModLoader; namespace CalamityMod.Projectiles.Magic { public class SmallSpirit : ModProjectile { public Player Owner => Main.player[Projectile.owner]; public Projectile ProjectileOwner { get { int spiritType = ModContent.ProjectileType(); for (int i = 0; i < Main.maxProjectiles; i++) { if (Main.projectile[i].type != spiritType || Main.projectile[i].identity != Projectile.ai[0] || Main.projectile[i].owner != Projectile.owner) { continue; } return Main.projectile[i]; } return null; } } public override string Texture => "CalamityMod/Projectiles/InvisibleProj"; public override void SetStaticDefaults() { DisplayName.SetDefault("Small Angry Spirit"); Main.projFrames[Projectile.type] = 3; } public override void SetDefaults() { Projectile.width = Projectile.height = 28; Projectile.alpha = 255; Projectile.penetrate = -1; Projectile.tileCollide = false; Projectile.ignoreWater = true; Projectile.timeLeft = 360; } public override void AI() { float radius = MathHelper.SmoothStep(84f, 44f, (float)Math.Sqrt(1f - Projectile.timeLeft / 360f)); // Handle frames. Projectile.frameCounter++; Projectile.frame = Projectile.frameCounter / 5 % Main.projFrames[Projectile.type]; float maxOpacity = 1f; // Fly around towards a target. Entity target = Owner; float flySpeed = 8f; float flyInertia = 54f; Projectile.hostile = true; Projectile.friendly = false; if (ProjectileOwner != null && (ProjectileOwner.ModProjectile().CurrentPower > 0.97f || Projectile.timeLeft < 95)) { Projectile.hostile = false; radius += 54f; target = ProjectileOwner; flySpeed = 29f; flyInertia = 5f; // Die if close to the owner projectile, effectively being absorbed. Projectile.Center = Projectile.Center.MoveTowards(target.Center, 8.5f); if (Projectile.WithinRange(target.Center, 80f)) Projectile.Kill(); // Become translucent when returning to the projectile owner. maxOpacity = 0.4f; } // Die if no valid target exists or the projectile owner is absent. if (target is null || ProjectileOwner is null || !ProjectileOwner.active) { Projectile.Kill(); return; } // If not close to the target, redirect towards them. if (!Projectile.WithinRange(target.Center, 260f)) { Vector2 idealVelocity = Projectile.SafeDirectionTo(target.Center) * flySpeed; Projectile.velocity = (Projectile.velocity * (flyInertia - 1f) + idealVelocity) / flyInertia; } // Otherwise accelerate. else Projectile.velocity *= 1.01f; // Rapidly fade in. Projectile.Opacity = MathHelper.Clamp(Projectile.Opacity + 0.075f, 0f, maxOpacity); // Decide rotation. Projectile.rotation = Projectile.velocity.ToRotation() - MathHelper.PiOver2; // Emit particles. Vector2 spawnPosition = Projectile.Center + Main.rand.NextVector2Circular(5f, 5f) * radius / 130f; FusableParticleManager.GetParticleSetByType()?.SpawnParticle(spawnPosition, radius); } public override Color? GetAlpha(Color lightColor) => Color.White * Projectile.Opacity; public override void Kill(int timeLeft) { for (int i = 0; i < 6; i++) { Dust phantoplasm = Dust.NewDustPerfect(Projectile.Center + Main.rand.NextVector2Circular(12f, 12f), 261); phantoplasm.color = Color.Lerp(Color.LightPink, Color.Red, Main.rand.NextFloat(0.67f)); phantoplasm.scale = 1.2f; phantoplasm.fadeIn = 0.55f; phantoplasm.noGravity = true; } } } }