Beispiel #1
0
        public static SoundEffectInstance PlayPitched(string path, float volume, float pitch, Vector2 position = default)
        {
            if (Main.netMode == NetmodeID.Server)
            {
                return(null);
            }

            for (int i = 0; i < instances.Count; i++)
            {
                var instance = instances[i];
                if (instance == null)
                {
                    instances.RemoveAt(i);
                    i--;
                }
                if (instance.State == SoundState.Stopped)
                {
                    instances[i].Dispose();
                    instances.RemoveAt(i);
                    i--;
                }
            }

            var soundEffect = ModContent.GetSound("StarlightRiver/Sounds/" + path).CreateInstance();

            float distFactor = 1;

            if (position != default)
            {
                distFactor = 1 - MathHelper.Clamp(Vector2.Distance(Main.LocalPlayer.Center, position) / 2000f, 0, 1);
            }

            soundEffect.Volume = MathHelper.Clamp(volume * Main.soundVolume * distFactor, 0, 1);
            soundEffect.Pitch  = pitch;

            instances.Add(soundEffect);
            soundEffect.Play();
            return(soundEffect);
        }