Dayz Explorer 1.29.162510
Loading...
Searching...
No Matches
itemsoundhandler.c
Go to the documentation of this file.
2{
3 float m_FadeIn = 0;
4 float m_FadeOut = 0;
5 bool m_Loop = false;
6}
7
8/*
9Provides functionality to start item related sound from server and synchronize it to clients
10Useful in situations where unique animation can't be used to bind the sound
11
12Steps to add new item sound:
13 1) Add new sound ID to SoundConstants class
14 2) Override InitItemSounds() method on subject item to link the new sound ID to a soundset
15 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
16 3) Start sound using ItemBase::StartItemSoundServer(int id) called from server side
17 3.5) For looped sounds which do not end on their own, use ItemBase::StopItemSoundServer(int id)
18
19Limitations:
20 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)
21
22*/
24{
25 protected ItemBase m_Parent;
26
30
32 {
33 m_Parent = parent;
34 Init();
35 }
36
38 {
39 if (!m_PlayingSounds)
40 return;
41
42 foreach (EffectSound sound : m_PlayingSounds)
43 {
44 if (sound)
45 sound.SoundStop();
46 }
47 }
48
55
56 void PlayItemSoundClient(int id, int slotId = InventorySlots.INVALID)
57 {
58 InventoryItemType itemType = m_Parent.GetInventoryItemType();
59
60 string soundSet = GetSoundSetForEvent(id, slotId);
61 if (soundSet == string.Empty)
62 {
63 Debug.Log("No soundset defined for sound event id " + typename.EnumToString(SoundConstants, id) + ". Item: " + m_Parent.GetType() + " | Slot ID: " + slotId);
64 return;
65 }
66
67 if (m_PlayingSounds.Get(id)) // already playing
68 return;
69
70 EffectSound sound;
71 SoundParameters params = m_SoundParamsMap.Get(id);
72 vector position = m_Parent.GetPosition();
73
74 if (params)
75 sound = SEffectManager.PlaySoundCachedParams(soundSet, position, params.m_FadeIn, params.m_FadeOut, params.m_Loop);
76 else
77 sound = SEffectManager.PlaySound(soundSet, position);
78
79 if (sound)
80 sound.SetAutodestroy(true);
81 else
82 Debug.Log("Null when creating sound from set: " + soundSet + " Item: " + m_Parent.GetType() );
83
84 m_PlayingSounds.Insert(id, sound);
85 }
86
87 string GetSoundSetForEvent(int id, int slotId = InventorySlots.INVALID)
88 {
89 string soundSet;
90 InventoryItemType itemType = m_Parent.GetInventoryItemType();
91
92 switch (id)
93 {
94 case SoundConstants.ITEM_ATTACH:
95 {
96 if (itemType)
97 soundSet = itemType.GetSlotAttachSoundSet(slotId);
98 break;
99 }
100 case SoundConstants.ITEM_DETACH:
101 {
102 if (itemType)
103 soundSet = itemType.GetSlotDetachSoundSet(slotId);
104 break;
105 }
106 default:
107 {
108 soundSet = m_AvailableSoundsets[id];
109 break;
110 }
111 }
112 return soundSet;
113 }
114
116 {
117 if (g_Game.IsDedicatedServer())
118 return;
119
120 EffectSound sound = m_PlayingSounds.Get(id);
121 if (sound)
122 {
123 sound.SetSoundFadeOut(0.1);
124 sound.SoundStop();
125 }
126
127 m_PlayingSounds.Remove(id);
128 }
129
130 void AddSound(int sound, string soundset, SoundParameters params = null)
131 {
132 m_AvailableSoundsets.Insert(sound, soundset);
133
134 if (params)
135 m_SoundParamsMap.Insert(sound, params);
136 }
137}
Entity m_Parent
Definition debug.c:2
Wrapper class for managing sound through SEffectManager.
Definition effectsound.c:5
override void SetAutodestroy(bool auto_destroy)
Sets whether Effect automatically cleans up when it stops.
void SetSoundFadeOut(float fade_out)
Set the sound fade out duration.
void SoundStop()
Stops sound.
provides access to slot configuration
TODO doc.
Definition enscript.c:118
void Init()
Manager class for managing Effect (EffectParticle, EffectSound).
static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound, using or creating cached SoundParams.
static EffectSound PlaySound(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
DayZGame g_Game
Definition dayzgame.c:3942
Empty
Definition hand_states.c:14
ref map< int, string > m_AvailableSoundsets
string GetSoundSetForEvent(int id, int slotId=InventorySlots.INVALID)
void PlayItemSoundClient(int id, int slotId=InventorySlots.INVALID)
void ~ItemSoundHandler()
void AddSound(int sound, string soundset, SoundParameters params=null)
void ItemSoundHandler(ItemBase parent)
void StopItemSoundClient(int id)
ref map< int, ref EffectSound > m_PlayingSounds
ref map< int, ref SoundParameters > m_SoundParamsMap