diff --git a/Items/Weapons/Magic/AnahitasArpeggio.cs b/Items/Weapons/Magic/AnahitasArpeggio.cs index cf278064be..23a16ea650 100644 --- a/Items/Weapons/Magic/AnahitasArpeggio.cs +++ b/Items/Weapons/Magic/AnahitasArpeggio.cs @@ -17,6 +17,10 @@ namespace CalamityMod.Items.Weapons.Magic public float RotationOffset; public static int MusicNoteAmt = 0; + public static readonly SoundStyle CapSound = new("CalamityMod/Sounds/Item/HarpLV6"); + public static readonly SoundStyle EndSound = new("CalamityMod/Sounds/Item/HarpEnd"); + public static readonly SoundStyle HitSound = new("CalamityMod/Sounds/Item/HarpNoteHit"); + public override void SetDefaults() { Item.width = 56; @@ -47,7 +51,7 @@ namespace CalamityMod.Items.Weapons.Magic if (MusicNoteAmt >= musicNoteCap) { Main.musicPitch = -0.5f; - SoundEngine.PlaySound(SoundID.Item26 with { Volume = 0.8f }, player.Center); + SoundEngine.PlaySound(CapSound with { Volume = 0.8f }, player.Center); return false; } else @@ -62,6 +66,8 @@ namespace CalamityMod.Items.Weapons.Magic } } + public override void HoldItem(Player player) => player.Calamity().mouseWorldListener = true; + public override void UseStyle(Player player, Rectangle heldItemFrame) { player.itemLocation.X -= 15f * player.direction; diff --git a/Projectiles/Magic/AnahitasArpeggioNote.cs b/Projectiles/Magic/AnahitasArpeggioNote.cs index 9f7f0b6a4d..6269d9feae 100644 --- a/Projectiles/Magic/AnahitasArpeggioNote.cs +++ b/Projectiles/Magic/AnahitasArpeggioNote.cs @@ -1,4 +1,5 @@ using CalamityMod.Items.Weapons.Magic; +using CalamityMod.Systems; using Microsoft.Xna.Framework; using System; using Terraria; @@ -14,10 +15,13 @@ namespace CalamityMod.Projectiles.Magic public ref float Timer => ref Projectile.ai[0]; public ref float AIState => ref Projectile.ai[1]; + public ref float NoteSequence => ref Projectile.localAI[1]; public int LingeringTime = 300; public int FadeOutTime = 20; public bool HasSetFadeOutVelocity = false; + public Player Owner => Main.player[Projectile.owner]; + public override void SetStaticDefaults() { ProjectileID.Sets.TrailCacheLength[Projectile.type] = 4; @@ -39,7 +43,6 @@ namespace CalamityMod.Projectiles.Magic public override void AI() { - Player Owner = Main.player[Projectile.owner]; Timer++; // Slight size oscillation @@ -57,10 +60,7 @@ namespace CalamityMod.Projectiles.Magic } if (Timer == 1f) - { - Main.musicPitch = (0.1f * AnahitasArpeggio.MusicNoteAmt) - 0.1f; - SoundEngine.PlaySound(SoundID.Item26, Owner.Center); - } + SoundEngine.PlaySound(new("CalamityMod/Sounds/Item/HarpLV" + Math.Clamp((int)NoteSequence + 1, 1, 6)), Owner.Center); if (Main.zenithWorld) Lighting.AddLight(Projectile.Center, 1.25f, 1.25f, 1.25f); @@ -107,16 +107,15 @@ namespace CalamityMod.Projectiles.Magic Projectile.alpha = 255; float degreesAmt = Main.zenithWorld ? 51.428f : 60f; - Vector2 musicNoteRotationOffset = Vector2.UnitY.RotatedBy(MathHelper.ToRadians(degreesAmt * Projectile.localAI[1]) + Projectile.ai[2]); + Vector2 musicNoteRotationOffset = Vector2.UnitY.RotatedBy(MathHelper.ToRadians(degreesAmt * NoteSequence) + Projectile.ai[2]); - Projectile.Center = Main.MouseWorld + musicNoteRotationOffset * 220f; - playerDirection = Projectile.Center - Main.MouseWorld; + Projectile.Center = Owner.Calamity().mouseWorld + musicNoteRotationOffset * 220f; + playerDirection = Projectile.Center - Owner.Calamity().mouseWorld; playerDirection.Normalize(); playerDirection *= -9.2f; Projectile.velocity = playerDirection; - Main.musicPitch = 0f; - SoundEngine.PlaySound(SoundID.Item26, Projectile.Center); + SoundEngine.PlaySound(AnahitasArpeggio.EndSound, Projectile.Center); AIState = 2f; } } @@ -150,7 +149,7 @@ namespace CalamityMod.Projectiles.Magic if (Main.zenithWorld) { Color stupidEasterEggColor = default; - switch (Projectile.localAI[1]) + switch (NoteSequence) { case 0: stupidEasterEggColor = new Color(255, 0, 0); @@ -194,6 +193,8 @@ namespace CalamityMod.Projectiles.Magic { target.AddBuff(BuffID.Wet, 900); target.AddBuff(BuffID.Confused, 300); + if (!SoundEngine.TryGetActiveSound(SingularSoundInstanceSystem.SoundSlot, out var activeSound)) + SingularSoundInstanceSystem.PlaySingleInstance(AnahitasArpeggio.HitSound, 60, 60, Owner); } } } diff --git a/Sounds/Item/HarpEnd.ogg b/Sounds/Item/HarpEnd.ogg new file mode 100644 index 0000000000..c48231b2c6 Binary files /dev/null and b/Sounds/Item/HarpEnd.ogg differ diff --git a/Sounds/Item/HarpLV1.ogg b/Sounds/Item/HarpLV1.ogg new file mode 100644 index 0000000000..44dd26b1e0 Binary files /dev/null and b/Sounds/Item/HarpLV1.ogg differ diff --git a/Sounds/Item/HarpLV2.ogg b/Sounds/Item/HarpLV2.ogg new file mode 100644 index 0000000000..d9b9ca3a78 Binary files /dev/null and b/Sounds/Item/HarpLV2.ogg differ diff --git a/Sounds/Item/HarpLV3.ogg b/Sounds/Item/HarpLV3.ogg new file mode 100644 index 0000000000..b4d9c55c83 Binary files /dev/null and b/Sounds/Item/HarpLV3.ogg differ diff --git a/Sounds/Item/HarpLV4.ogg b/Sounds/Item/HarpLV4.ogg new file mode 100644 index 0000000000..10af3f4fa2 Binary files /dev/null and b/Sounds/Item/HarpLV4.ogg differ diff --git a/Sounds/Item/HarpLV5.ogg b/Sounds/Item/HarpLV5.ogg new file mode 100644 index 0000000000..34ef0721bd Binary files /dev/null and b/Sounds/Item/HarpLV5.ogg differ diff --git a/Sounds/Item/HarpLV6.ogg b/Sounds/Item/HarpLV6.ogg new file mode 100644 index 0000000000..88a4fc797f Binary files /dev/null and b/Sounds/Item/HarpLV6.ogg differ diff --git a/Sounds/Item/HarpNoteHit.ogg b/Sounds/Item/HarpNoteHit.ogg new file mode 100644 index 0000000000..3bd3dbb437 Binary files /dev/null and b/Sounds/Item/HarpNoteHit.ogg differ diff --git a/Systems/SingularSoundInstanceSystem.cs b/Systems/SingularSoundInstanceSystem.cs new file mode 100644 index 0000000000..8fc518ee36 --- /dev/null +++ b/Systems/SingularSoundInstanceSystem.cs @@ -0,0 +1,79 @@ +using Microsoft.Xna.Framework; +using ReLogic.Utilities; +using Terraria; +using Terraria.Audio; +using Terraria.ModLoader; + +namespace CalamityMod.Systems +{ + public class SingularSoundInstanceSystem : ModSystem + { + public static SlotId SoundSlot; + + internal static int maxPlaytime = 0; + internal static int remainingPlaytime = 0; + internal static int timeUntilReset = 0; + internal static Entity AttachedEntity; + + public static bool currentlyPlaying = false; + + public override void UpdateUI(GameTime gameTime) + { + // Decrement timers + if (remainingPlaytime > 0) + --remainingPlaytime; + if (timeUntilReset > 0) + --timeUntilReset; + + if (currentlyPlaying) + { + // If the reset timer has run out, stop the active sound instance entirely. + // The next time the sound is played, it will start from the beginning. + if (timeUntilReset <= 0) + { + currentlyPlaying = false; + maxPlaytime = 0; + AttachedEntity = null; + if (SoundEngine.TryGetActiveSound(SoundSlot, out var activeSound)) + activeSound.Stop(); + SoundSlot = SlotId.Invalid; + } + + // Otherwise, set the volume of the active sound instance appropriately. + else + { + bool foundSound = SoundEngine.TryGetActiveSound(SoundSlot, out var activeSound); + // If an attached entity stops existing, so does the sound. + if (!foundSound || (AttachedEntity != null && !AttachedEntity.active)) + { + currentlyPlaying = false; + maxPlaytime = 0; + AttachedEntity = null; + return; + } + // Otherwise, attach to the entity. + else if (AttachedEntity != null && AttachedEntity.active) + activeSound.Position = AttachedEntity.Center; + + float newVolume = MathHelper.Clamp(remainingPlaytime / (float)maxPlaytime, 0f, 1f); + activeSound.Volume = newVolume; + activeSound.Update(); + } + } + } + + public static void PlaySingleInstance(SoundStyle Sound, int Playtime, int ResetTime, Entity Attachment = null) + { + if (!currentlyPlaying) + SoundSlot = SoundEngine.PlaySound(Sound); + + currentlyPlaying = true; + remainingPlaytime = Playtime; + maxPlaytime = Playtime; + timeUntilReset = ResetTime; + + if (Attachment != null) + AttachedEntity = Attachment; + } + } +}