public void PlaySound(float startPitch, float endPitch, float volume)
        {
            startPitch *= pitchModifier;
            endPitch   *= pitchModifier;
            volume     *= volumneModifier;

            SoundParams param = new SoundParams()
            {
                Location        = soundName,
                DisposeOnFinish = true,
                Pitch           = startPitch,
                Volume          = volume,
                Position        = entity.Pos.XYZ.ToVec3f().Add(0, (float)entity.EyeHeight, 0),
                ShouldLoop      = false,
                Range           = 8,
            };

            ILoadedSound sound = capi.World.LoadSound(param);

            if (startPitch != endPitch)
            {
                slidingPitchSounds.Add(new SlidingPitchSound()
                {
                    startPitch = startPitch,
                    endPitch   = endPitch,
                    sound      = sound,
                    startMs    = capi.World.ElapsedMilliseconds
                });
            }


            sound.Start();
        }
    public void PlayMusic(AudioClip soundToPlay, SoundParams parameters = null)
    {
        for (int i = 0; i < soundsPlaying.Count; i++)
        {
            if (soundsPlaying[i].parameters.isMusic)
            {
                soundsPlaying[i].Destroy();
                soundsPlaying.RemoveAt(i);
            }
        }

        if (parameters == null)
        {
            parameters = new SoundParams();
        }

        parameters.loop    = true;
        parameters.isMusic = true;

        Sound sound = new Sound()
        {
            soundPlaying = soundToPlay,
            startTime    = Time.time,
        };

        GameObject soundObject = new GameObject(parameters.subtitle != "" ? parameters.subtitle : soundToPlay.name);

        soundObject.transform.position = Vector3.zero;
        AudioSource source = soundObject.AddComponent <AudioSource>();

        sound.Play(source, soundObject, parameters);
        soundsPlaying.Add(sound);
    }
Exemple #3
0
    public void PlaySound(string key, Vector3 position, SoundParams settings = null)
    {
        AudioSource source = sourcePool.Activate(position, Quaternion.identity, ResetAudioSource).GetComponent <AudioSource>();

        AudioClip[] clips;
        bool        hasClip = sounds.TryGetValue(key, out clips);

        if (hasClip)
        {
            if (settings != null)
            {
                source.volume = settings.volume;
                source.pitch  = settings.pitch;
            }
            //Debug.Log("Playing sound on " + source.gameObject.name);

            AudioClip clip = clips[Random.Range(0, clips.Length)];

            //Debug.Log(source);

            source.clip = clip;
            source.Play();
        }
        else
        {
            Debug.LogWarning("Non-existant sound event \"" + key + "\"");
        }
    }
        protected void CreateSubParams()
        {
            SubParams.Clear();
            var health = MainElement.GetChildElement("health");

            if (health != null)
            {
                Health = new HealthParams(health, this);
                SubParams.Add(Health);
            }
            // TODO: support for multiple ai elements?
            var ai = MainElement.GetChildElement("ai");

            if (ai != null)
            {
                AI = new AIParams(ai, this);
                SubParams.Add(AI);
            }
            foreach (var element in MainElement.GetChildElements("bloodemitter"))
            {
                var emitter = new ParticleParams(element, this);
                BloodEmitters.Add(emitter);
                SubParams.Add(emitter);
            }
            foreach (var element in MainElement.GetChildElements("gibemitter"))
            {
                var emitter = new ParticleParams(element, this);
                GibEmitters.Add(emitter);
                SubParams.Add(emitter);
            }
            foreach (var element in MainElement.GetChildElements("damageemitter"))
            {
                var emitter = new ParticleParams(element, this);
                GibEmitters.Add(emitter);
                SubParams.Add(emitter);
            }
            foreach (var soundElement in MainElement.GetChildElements("sound"))
            {
                var sound = new SoundParams(soundElement, this);
                Sounds.Add(sound);
                SubParams.Add(sound);
            }
            foreach (var inventoryElement in MainElement.GetChildElements("inventory"))
            {
                var inventory = new InventoryParams(inventoryElement, this);
                Inventories.Add(inventory);
                SubParams.Add(inventory);
            }
        }
 public void Play(AudioSource source, GameObject obj, SoundParams parameters)
 {
     this.parameters          = parameters;
     this.source              = source;
     this.source.clip         = soundPlaying;
     this.source.volume       = parameters.volume;
     this.source.loop         = parameters.loop;
     this.source.spatialBlend = parameters.isMusic ? 0 : 1f;
     this.source.maxDistance  = 500f;
     this.source.minDistance  = 1f;
     this.source.pitch        = parameters.pitch;
     this.source.Play();
     this.obj = obj;
     endTime  = Time.time + soundPlaying.length;
 }
Exemple #6
0
    // Start is called before the first frame update
    void Awake()
    {
        if (instance == null)
        {
            if (events != null)
            {
                Debug.Log("Initilizing Oneshot System...");
                instance = this;

                sourcePool = new GameObjectPool(prefab, initialSize, gameObject.transform);

                foreach (SoundEvent e in events.events)
                {
                    sounds.Add(e.key, e.clips);
                }

                if (sounds.Count > 0)
                {
                    Debug.Log(sounds.Count + " sound events loaded");
                }
                else
                {
                    Debug.LogWarning("No sound events loaded");
                }
                DontDestroyOnLoad(instance.gameObject);
            }
            else
            {
                Debug.LogError("No sound event library provided!");
            }
        }
        else
        {
            Debug.Log("Removing duplicate sound manager");
            Destroy(this.gameObject);
        }

        defaultSoundSettings = new SoundParams
        {
            volume = this.volume,
            pitch  = this.pitch
        };
    }
    public void PlaySound(AudioClip soundToPlay, Vector3 location, SoundParams parameters = null)
    {
        if (parameters == null)
        {
            parameters = new SoundParams();
        }

        Sound sound = new Sound()
        {
            soundPlaying = soundToPlay,
            startTime    = Time.time,
        };

        GameObject soundObject = new GameObject(parameters.subtitle != "" ? parameters.subtitle : soundToPlay.name);

        soundObject.transform.position = location;
        AudioSource source = soundObject.AddComponent <AudioSource>();

        sound.Play(source, soundObject, parameters);
        soundsPlaying.Add(sound);
    }
        public void PlaySound(float startPitch, float endPitch, float volume)
        {
            SoundParams param = new SoundParams()
            {
                Location        = soundName,
                DisposeOnFinish = true,
                Pitch           = startPitch,
                Volume          = volume,
                Position        = entity.Pos.XYZ.ToVec3f().Add(0, (float)entity.EyeHeight, 0),
                ShouldLoop      = false,
                Range           = 8,
            };

            if (startPitch != endPitch)
            {
                //slidingPitchSounds.Add(new KeyValuePair<ILoadedSound, float>());
            }


            ILoadedSound sound = capi.World.LoadSound(param);

            sound.Start();
        }
 public bool RemoveSound(SoundParams soundParams) => RemoveSubParam(soundParams);
Exemple #10
0
        public static void Prefix(
            SoundParams sound,
            AudioData data
            )
        {
            if (sound.SoundType == EnumSoundType.Music)
            {
                return;
            }


            var locationPath = sound.Location.Path;
            var soundId      = locationPath.StartsWith("sounds/") && locationPath.EndsWith(".ogg") ? locationPath.Substring(7, locationPath.Length - 7 - 4) : locationPath;

            // strip last character if it's a number (e.g. "block/dirt1", "block/dirt2", "block/dirt3", "block/dirt4")
            var lastChar = soundId.ToCharArray(soundId.Length - 1, 1)[0];

            if (lastChar >= '0' && lastChar <= '9')
            {
                soundId = soundId.Substring(0, soundId.Length - 1);
            }

            var name = Lang.GetIfExists("subtitles:" + soundId);

            if (name == "")
            {
                return;
            }
            if (name == null)
            {
                name = soundId;
                capi.Logger.Debug($"Subtitles: missing lang entry --> \"subtitles:{soundId}\": \"TODO\",");
            }

            var player = capi.World.Player;

            if (player == null)
            {
                return;
            }
            var playerPos = player.Entity.Pos.AsBlockPos;

            if (sound.Position == null || (sound.Position.X == 0 && sound.Position.Y == 0 && sound.Position.Z == 0))
            {
                subtitlesMod.OnNewSound(name, Double.NaN, sound.Volume);
                return;
            }

            var dx = sound.Position.X - playerPos.X;
            var dy = sound.Position.Y - playerPos.Y;
            var dz = sound.Position.Z - playerPos.Z;

            // if the source of the sound is very close, do not treat it as directional
            if (Math.Abs(dx) < 2 && Math.Abs(dy) < 2 && Math.Abs(dz) < 2)
            {
                subtitlesMod.OnNewSound(name, Double.NaN, sound.Volume);
                return;
            }

            var yaw  = Math.Atan2(dz, dx);
            var dist = Math.Sqrt(dx * dx + dy * dy + dz * dz);

            // if it's out of "range", don't display it
            if (dist <= sound.Range * AUDIBILITY_FACTOR)
            {
            }

            subtitlesMod.OnNewSound(name, yaw, sound.Volume);
        }