Example #1
0
        public static SoundEffectInstance Play(SfxEntry entry, Vector2 position, int variant, PlaySoundEvent onPlay)
        {
            if (Main.dedServ || WorldGen.gen || Main.netMode == 2)
                return null;

            var kvp = new KeyValuePair<SfxEntry, int>(entry, entry.Variants == 1 ? 0 : variant);

            var t = CalcParams(entry, position, variant, onPlay);

            if (t == null || t.Item2 <= 0f)
                return null;

            SoundEffectInstance inst;
            var b = entry.PlayBehaviour(variant);
            switch (b)
            {
                case SfxPlayBehaviour.MultipleInstances:
                    inst = entry.GetInstance(variant);

                    if (inst == null)
                        return null;

                    instancePool.Add(inst); // don't GC
                    break;
                case SfxPlayBehaviour.PlayIfStopped:
                case SfxPlayBehaviour.PlayIfStoppedUpdateParams:
                    if (instanceMap.ContainsKey(kvp))
                    {
                        var inst_ = instanceMap[kvp];

                        if (inst_.State == SoundState.Stopped)
                            inst = inst_;
                        else
                        {
                            if (b == SfxPlayBehaviour.PlayIfStoppedUpdateParams)
                                ApplyParams(inst_, t);

                            return null;
                        }
                    }
                    else
                    {
                        inst = entry.GetInstance(variant);

                        if (inst == null)
                            return null;

                        instanceMap.Add(kvp, inst);
                    }
                    break;
                case SfxPlayBehaviour.Singleton:
                    if (instanceMap.ContainsKey(kvp))
                    {
                        var inst_ = instanceMap[kvp];

                        inst_.Stop();

                        instanceMap.Remove(kvp);
                    }

                    inst = entry.GetInstance(variant);

                    if (inst == null)
                        return null;

                    instanceMap.Add(kvp, inst);
                    break;
                // required, compiler will complain about 'inst' not being assigned to otherwise
                // and this is more foolproof than setting 'inst' to null.
                default:
                    return null;
            }

            ApplyParams(inst, t);

            inst.Play(); // !

            CleanupLingeringInstances();

            return inst;
        }
Example #2
0
        static Tuple<int, float, float, float> CalcParams(SfxEntry e, Vector2 pos, int v, PlaySoundEvent onPlay)
        {
            var volSetting = e.IsAmbient ? (Main.gameInactive ? 0f : Main.ambientVolume) : Main.soundVolume;

            if (volSetting <= 0f)
                return null;

            bool shouldPlay = true;
            float
                vol = 1f, pitch = 0f, pan = 0f;

            if (pos.X <= 0f || pos.Y <= 0f || Single.IsNaN(pos.X) || Single.IsNaN(pos.Y))
                shouldPlay = true;
            else
            {
            #pragma warning disable RECS0091 // Use 'var' keyword when possible
                                 // to the code analyser: learn to C#
                Rectangle
            #pragma warning restore RECS0091
                    screen = new Rectangle((int)(Main.screenPosition.X - Main.screenWidth * 2), (int)(Main.screenPosition.Y - Main.screenHeight * 2), Main.screenWidth * 5, Main.screenHeight * 5),
                    sound  = new Rectangle((int)pos.X, (int)pos.Y, 1, 1);

                shouldPlay |= sound.Intersects(screen);

                if (shouldPlay)
                {
                    var screenCentre = new Vector2(Main.screenPosition.X + Main.screenWidth * 0.5f, Main.screenPosition.Y + Main.screenHeight * 0.5f);

                    pan = (pos.X - screenCentre.X) / (Main.screenWidth * 0.5f);
                    vol = 1f - Vector2.Distance(pos, screenCentre) / (Main.screenWidth * 1.5f);
                }
            }
            pan = MathHelper.Clamp(pan, -1f, 1f);
            vol = Math.Min(vol * volSetting, 1f);

            if (vol <= 0f)
                return null;

            if (onPlay != null && !onPlay(e, pos, ref v, ref vol, ref pitch, ref pan))
                return null;

            vol   = MathHelper.Clamp(vol  ,  0f, 1f);
            pitch = MathHelper.Clamp(pitch, -1f, 1f);
            pan   = MathHelper.Clamp(pan  , -1f, 1f);

            return Tuple.Create(v, vol, pitch, pan);
        }
Example #3
0
 public static SoundEffectInstance Play(SfxEntry entry, Point tilePos, int variant, PlaySoundEvent onPlay)
 {
     return Play(entry, tilePos.ToVector2() * 16f, variant, onPlay);
 }