Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTA;
- using GTA.Native;
- using GTA.Math;
- using System;
- using System.Windows.Forms;
- using System.Collections.Generic;
- using IrrKlang;
- public class StaticEmitterHandler : Script
- {
- public static List<StaticEmitter> emitters = new List<StaticEmitter>();
- public StaticEmitterHandler()
- {
- Tick += OnTick;
- Aborted += OnAborted;
- }
- private void OnAborted(object sender, EventArgs e)
- {
- StaticEmitterAudio.SoundEngine.StopAllSounds();
- StaticEmitterAudio.SoundEngine.Dispose();
- foreach (var emitter in emitters.ToArray())
- {
- if (emitter != null)
- {
- emitter.Dispose(true);
- }
- }
- }
- private void OnTick(object sender, EventArgs e)
- {
- foreach (var emitter in emitters.ToArray())
- {
- emitter.Tick();
- }
- StaticEmitterAudio.ManageSoundEngine();
- }
- }
- public class StaticEmitter
- {
- public Vector3 Position;
- public StaticEmitterAudio Audio;
- public float Range = 1f;
- public StaticEmitter(Vector3 pos, string filePath)
- {
- Audio = new StaticEmitterAudio(filePath);
- Position = pos;
- Audio.Play3DSound(Position, true, false, true);
- StaticEmitterHandler.emitters.Add(this);
- }
- public void Tick()
- {
- Audio.SetDistances(0f, Range);
- Audio.ProcessSound(Position);
- }
- public void Dispose(bool delete = false)
- {
- Audio.StopSound();
- StaticEmitterHandler.emitters.Remove(this);
- }
- }
- public class StaticEmitterAudio
- {
- public string FilePath;
- public ISound Sound;
- public ISoundEffectControl SoundEffect;
- public float MaximumDistance = 20f;
- public float MinimumDistance = 1f;
- public StaticEmitterAudio(string filepath)
- {
- FilePath = filepath;
- }
- public void Play3DSound(Vector3 sourcePosition, bool playLooped, bool allowMultipleInstances = false, bool allowSoundEffects = false)
- {
- if (allowMultipleInstances || (!allowMultipleInstances && (Sound == null || Sound != null && Sound.Finished)))
- {
- Vector3D sourcePos = SoundHelperIK.Vector3ToVector3D(GameplayCamera.GetOffsetFromWorldCoords(sourcePosition));
- Sound = SoundEngine.Play3D(FilePath, sourcePos.X, sourcePos.Y, sourcePos.Z, playLooped, false, StreamMode.AutoDetect, allowSoundEffects);
- if (allowSoundEffects)
- {
- SoundEffect = Sound.SoundEffectControl;
- }
- }
- }
- public void StopSound()
- {
- if (Sound == null || Sound.Finished) return;
- Sound.Stop();
- }
- public bool IsPlaying()
- {
- return Sound != null && !Sound.Finished;
- }
- public void ProcessSound(Vector3 sourcePosition)
- {
- if (Sound != null && !Sound.Finished)
- {
- Sound.MaxDistance = MaximumDistance;
- Sound.MinDistance = MinimumDistance;
- Vector3D sourcePos = SoundHelperIK.Vector3ToVector3D(GameplayCamera.GetOffsetFromWorldCoords(sourcePosition));
- Sound.Position = sourcePos;
- }
- }
- public void SetDistances(float max, float min)
- {
- MaximumDistance = max;
- MinimumDistance = min;
- }
- public void Dispose()
- {
- Sound.Stop();
- Sound.Dispose();
- }
- public static ISoundEngine SoundEngine = new ISoundEngine();
- public static void ManageSoundEngine()
- {
- SoundEngine.SetListenerPosition(new Vector3D(0, 0, 0), new Vector3D(0, 0, 1), new Vector3D(0, 0, 0), new Vector3D(0, 1, 0));
- SoundEngine.Update();
- }
- }
- static class SoundHelperIK
- {
- public static Vector3D Vector3ToVector3D(Vector3 vec)
- {
- return new Vector3D(vec.X, vec.Z, vec.Y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement