replacesoundeventhandler.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. enum ESoundEventType
  2. {
  3. SOUND_COMMON = 0 // "Sound"
  4. SOUND_WEAPON, // "SoundWeapon"
  5. SOUND_ATTACHMENT, // "SoundAttachment"
  6. SOUND_VOICE // "SoundVoice"
  7. }
  8. enum EReplaceSoundEventID
  9. {
  10. DIG_PRIMARY = 1,
  11. DIG_SECONDARY,
  12. CLEANHANDS_PRIMARY,
  13. CLEANHANDS_SECONDARY
  14. }
  15. // Main purpose is to intercept animation system based sound event calls and replace them with different ones based on context
  16. class ReplaceSoundEventHandler
  17. {
  18. protected static ref array< ref map<int, ref ReplaceSoundEventBase> > m_SoundEventReplaceMaps = {};
  19. protected PlayerBase m_Player;
  20. protected ref Timer m_UpdateTimer;
  21. void ReplaceSoundEventHandler(PlayerBase player)
  22. {
  23. m_Player = player;
  24. RegisterEvent(new DigPrimarySoundEvent());
  25. RegisterEvent(new DigSecondarySoundEvent());
  26. RegisterEvent(new CleanHandsPrimarySoundEvent());
  27. RegisterEvent(new CleanHandsSecondarySoundEvent());
  28. }
  29. // Inserts sound replace event to array of event maps, creates new event map if first sound of an event type is being registered
  30. protected void RegisterEvent(ReplaceSoundEventBase soundEvent)
  31. {
  32. int sType = soundEvent.GetSoundEventType();
  33. if (!m_SoundEventReplaceMaps.IsValidIndex(sType))
  34. {
  35. ref map<int, ref ReplaceSoundEventBase> replaceMap = new map<int, ref ReplaceSoundEventBase>();
  36. m_SoundEventReplaceMaps.InsertAt(replaceMap, sType);
  37. }
  38. m_SoundEventReplaceMaps[sType].Insert(soundEvent.GetSoundAnimEventClassID(), soundEvent);
  39. }
  40. int GetSoundEventID(int anim_id, ESoundEventType soundType)
  41. {
  42. if (!m_SoundEventReplaceMaps.IsValidIndex(soundType))
  43. return 0;
  44. ReplaceSoundEventBase soundEvent = m_SoundEventReplaceMaps[soundType].Get(anim_id);
  45. if (!soundEvent)
  46. return 0;
  47. return soundEvent.GetSoundEventID();
  48. }
  49. ReplaceSoundEventBase GetSoundEventByID(int id, ESoundEventType soundType)
  50. {
  51. if (!m_SoundEventReplaceMaps.IsValidIndex(soundType))
  52. return null;
  53. foreach (int animID, ReplaceSoundEventBase soundEvent : m_SoundEventReplaceMaps[soundType])
  54. {
  55. if (soundEvent.GetSoundEventID() == id)
  56. return soundEvent;
  57. }
  58. return null;
  59. }
  60. bool PlayReplaceSound(int soundEventID, ESoundEventType soundType, int flags)
  61. {
  62. ReplaceSoundEventBase soundEvent = GetSoundEventByID(soundEventID, soundType);
  63. if (!soundEvent)
  64. return false;
  65. ReplaceSoundEventBase soundEventObj = ReplaceSoundEventBase.Cast(soundEvent.ClassName().ToType().Spawn());
  66. soundEventObj.Init(m_Player);
  67. if (soundEventObj.Play())
  68. return true;
  69. return false;
  70. }
  71. }