itemsoundhandler.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. class SoundParameters
  2. {
  3. float m_FadeIn = 0;
  4. float m_FadeOut = 0;
  5. bool m_Loop = false;
  6. }
  7. /*
  8. Provides functionality to start item related sound from server and synchronize it to clients
  9. Useful in situations where unique animation can't be used to bind the sound
  10. Steps to add new item sound:
  11. 1) Add new sound ID to SoundConstants class
  12. 2) Override InitItemSounds() method on subject item to link the new sound ID to a soundset
  13. 2.5) Generally soundset is retreived by adding override to the subject item class, but retreiving from config or using other means could also be used here
  14. 3) Start sound using ItemBase::StartItemSoundServer(int id) called from server side
  15. 3.5) For looped sounds which do not end on their own, use ItemBase::StopItemSoundServer(int id)
  16. Limitations:
  17. Avoid starting two sounds at the same time using this handler as just single variable is used for synchronization (can be solved by secondary synch var if need arises)
  18. */
  19. class ItemSoundHandler : Managed
  20. {
  21. protected ItemBase m_Parent;
  22. protected ref map<int, ref EffectSound> m_PlayingSounds;
  23. protected ref map<int, string> m_AvailableSoundsets;
  24. protected ref map<int, ref SoundParameters> m_SoundParamsMap;
  25. void ItemSoundHandler(ItemBase parent)
  26. {
  27. m_Parent = parent;
  28. Init();
  29. }
  30. void ~ItemSoundHandler()
  31. {
  32. if (!m_PlayingSounds)
  33. return;
  34. foreach (EffectSound sound : m_PlayingSounds)
  35. {
  36. if (sound)
  37. sound.SoundStop();
  38. }
  39. }
  40. void Init()
  41. {
  42. m_PlayingSounds = new map<int, ref EffectSound>();
  43. m_AvailableSoundsets = new map<int, string>();
  44. m_SoundParamsMap = new map<int, ref SoundParameters>();
  45. }
  46. void PlayItemSoundClient(int id)
  47. {
  48. string soundSet = m_AvailableSoundsets.Get(id);
  49. if (soundSet == string.Empty)
  50. {
  51. Debug.Log("Attempting to play soundID " + id + " without defined soundset. Item: " + m_Parent.GetType());
  52. return;
  53. }
  54. if (m_PlayingSounds.Get(id)) // already playing
  55. return;
  56. EffectSound sound;
  57. SoundParameters params = m_SoundParamsMap.Get(id);
  58. if (params)
  59. sound = SEffectManager.PlaySoundCachedParams(soundSet, m_Parent.GetPosition(), params.m_FadeIn, params.m_FadeOut, params.m_Loop);
  60. else
  61. sound = SEffectManager.PlaySound(soundSet, m_Parent.GetPosition());
  62. if (sound)
  63. sound.SetAutodestroy(true);
  64. else
  65. Debug.Log("Null when creating sound from set: " + soundSet + " Item: " + m_Parent.GetType() );
  66. m_PlayingSounds.Insert(id, sound);
  67. }
  68. void StopItemSoundClient(int id)
  69. {
  70. if (GetGame().IsDedicatedServer())
  71. return;
  72. EffectSound sound = m_PlayingSounds.Get(id);
  73. if (sound)
  74. {
  75. sound.SetSoundFadeOut(0.1);
  76. sound.SoundStop();
  77. }
  78. m_PlayingSounds.Remove(id);
  79. }
  80. void AddSound(int sound, string soundset, SoundParameters params = null)
  81. {
  82. m_AvailableSoundsets.Insert(sound, soundset);
  83. if (params)
  84. m_SoundParamsMap.Insert(sound, params);
  85. }
  86. }