public SoundInstance PlayAttachedSound(string url, Entity parent, float pitch = 1f, float volume = 1f, float distanceScale = 1f, bool looped = false)
        {
            Vector3 pos     = parent.Transform.WorldPosition();
            float   sqrDist = (pos - AudioEngine.DefaultListener.Position).LengthSquared();

            if (MaxSoundDistance > 0f && sqrDist >= MaxSoundDistance * MaxSoundDistance)
            {
                return(null);
            }
            SoundInstance s = getFreeInstance(url, true);

            if (s == null)
            {
                return(null);
            }
            s.Pitch     = pitch < 0f ? RandomPitch() : pitch;
            s.Volume    = volume * MasterVolume;
            s.IsLooping = looped;
            s.Pan       = 0f;
            s.Apply3D(pos, null, null, distanceScale);
            s.Play();
            var posSnd = new PositionalSound()
            {
                pos            = pos,
                soundInstance  = s,
                entity         = parent,
                distance_scale = distanceScale
            };

            lock (currentAttached) {
                currentAttached.Add(posSnd);
            }
            return(s);
        }
Exemple #2
0
        /**
         * <summary>
         * Plays a sound
         * </summary>
         */
        private void PlaySound(int id, SoundBank sb)
        {
            SoundInfo sound = sb.SoundCollection[id];

            si = WaveServices.SoundPlayer.Play(sound);

            //Only repeat if it is not moving or something similar
            si.Loop = !wo.IsActionBlocking();

            //3D effect o.O
            si.Apply3D(emitter, listener);
        }
Exemple #3
0
        public SoundInstance PlayCentralSound(string url, float pitch = 1f, float volume = 1f, float pan = 0f, bool looped = false)
        {
            SoundInstance s = getFreeInstance(url, false);

            if (s != null)
            {
                s.Pitch     = pitch < 0f ? RandomPitch() : pitch;
                s.Volume    = volume * MasterVolume;
                s.IsLooping = looped;
                s.Pan       = pan;
                if (s.IsSpatialized)
                {
                    s.Apply3D(AudioEngine.DefaultListener.Position);
                }
                s.Play();
            }
            return(s);
        }
        public SoundInstance PlayPositionSound(string url, Vector3 position, float pitch = 1f, float volume = 1f, float distanceScale = 1f, bool looped = false)
        {
            float sqrDist = (position - AudioEngine.DefaultListener.Position).LengthSquared();

            if (MaxSoundDistance > 0f && sqrDist >= MaxSoundDistance * MaxSoundDistance)
            {
                return(null);
            }
            SoundInstance s = getFreeInstance(url, true);

            if (s == null)
            {
                return(null);
            }
            s.Pitch     = pitch < 0f ? RandomPitch() : pitch;
            s.Volume    = volume * MasterVolume;
            s.IsLooping = looped;
            s.Pan       = 0f;
            s.Apply3D(position, null, null, distanceScale);
            s.Play();
            return(s);
        }
Exemple #5
0
        /**
         * <summary>
         * Starts or stops a sound
         * </summary>
         */
        private void HandleSounds()
        {
            emitter.WorldTransform  = wo.transform.WorldTransform;
            listener.WorldTransform = cam.WorldTransform;
            if (si != null)
            {
                //3D effect continuously o.O
                si.Apply3D(emitter, listener);
            }
            if (lastActionSound != wo.GetAction())
            {
                if (si != null && si.Loop)
                {
                    StopSound();
                }

                if (FogOfWar.fog.IsVisible(Map.map.GetTileByWorldPosition(wo.GetCenteredPosition())))
                {
                    SoundHandler sh = UIBehavior.ui.Owner.FindComponent <SoundHandler>();
                    if (sh.sounds.ContainsKey(wo.GetAction()))
                    {
                        List <int> sl = sh.sounds[wo.GetAction()];
                        if (sl.Count > 0)
                        {
                            //Play a random sound from available
                            int s = sl[new System.Random().Next(0, sl.Count)];
                            PlaySound(s, sh.soundBank);
                        }
                    }
                }
                else
                {
                    StopSound();
                }

                lastActionSound = wo.GetAction();
            }
        }