Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
effectmanager.c
Go to the documentation of this file.
1 
6 {
8  protected static ref map<int, ref Effect> m_EffectsMap;
10  protected static ref array<int> m_FreeEffectIDs;
12  protected static int m_HighestFreeEffectID = 1;
14  static const int INVALID_ID = 0;
16  protected static bool m_IsCleanup;
18  protected static bool m_IsInitialized;
19 
21  protected static ref map<string, ref SoundParams> m_ParamsMap;
22 
24  static ref ScriptInvoker Event_OnFrameUpdate;
25 
26 
27 
33 
43  static int PlayInWorld(notnull Effect eff, vector pos)
44  {
45  // Stop the effect first, just in case
46  eff.Stop();
47 
48  int id = EffectRegister(eff);
49 
50  eff.SetPosition( pos );
51  eff.Start();
52 
53  return id;
54  }
55 
66  static int PlayOnObject(notnull Effect eff, Object obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_rotation_relative_to_world = false)
67  {
68  // Stop the effect first, just in case
69  eff.Stop();
70 
71  int id = EffectRegister(eff);
72 
73  if (!obj)
74  {
75  ErrorEx("Parent object is null.", ErrorExSeverity.WARNING);
76  eff.SetPosition(local_pos);
77  }
78  else
79  {
80  eff.SetPosition(obj.GetPosition());
81  }
82 
83  eff.SetParent(obj);
84  eff.SetLocalPosition(local_pos);
85  eff.SetAttachedLocalOri(local_ori);
86 
87  if (force_rotation_relative_to_world)
88  {
89  EffectParticle eff_particle = EffectParticle.Cast(eff);
90 
91  if (eff_particle)
92  {
93  eff_particle.ForceParticleRotationRelativeToWorld(force_rotation_relative_to_world);
94  }
95  }
96 
97  eff.Start();
98 
99  return id;
100  }
101 
106  static void Stop(int effect_id)
107  {
108  Effect eff = m_EffectsMap.Get(effect_id);
109 
110  if (eff)
111  {
112  eff.Stop();
113  }
114  else
115  {
116  ErrorEx(string.Format("Failed to stop Effect with ID %1. The ID is not registered in m_EffectsMap!", effect_id));
117  }
118  }
119 
121 
122 
123 
128 
140  static EffectSound CreateSound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false, bool enviroment = false)
141  {
142  EffectSound effect_sound = new EffectSound();
143  effect_sound.SetSoundSet(sound_set);
144  effect_sound.SetPosition(position);
145  effect_sound.SetSoundFadeIn(play_fade_in);
146  effect_sound.SetSoundFadeOut(stop_fade_out);
147  effect_sound.SetSoundLoop(loop);
148  effect_sound.SetEnviromentVariables(enviroment);
149 
150  EffectRegister( effect_sound );
151 
152  return effect_sound;
153  }
154 
165  static EffectSound PlaySound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
166  {
167  EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, false);
168 
169  effect_sound.SoundPlay();
170 
171  return effect_sound;
172  }
173 
184  static EffectSound PlaySoundParams(notnull SoundParams params, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
185  {
186  EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
187 
188  effect_sound.SoundPlayEx(params);
189 
190  return effect_sound;
191  }
192 
203  static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
204  {
205  SoundParams params = GetCachedSoundParam(sound_set);
206 
207  EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
208 
209  effect_sound.SoundPlayEx(params);
210 
211  return effect_sound;
212  }
213 
224  static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
225  {
226  EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, true);
227 
228  effect_sound.SoundPlay();
229 
230  return effect_sound;
231  }
232 
243  static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
244  {
245  EffectSound effect_sound = CreateSound(sound_set, parent_object.GetPosition(), play_fade_in, stop_fade_out, loop);
246 
247  effect_sound.SetParent( parent_object );
248  effect_sound.SetLocalPosition( vector.Zero );
249  effect_sound.SoundPlay();
250 
251  return effect_sound;
252  }
253 
255 
256 
257 
262 
267  static void DestroyEffect(Effect effect)
268  {
269  if (effect)
270  {
271  if (effect.CanDestroy())
272  {
273  // Functionality already happens in dtor of Effect to be safe
274  delete effect;
275  }
276  else
277  {
278  // Make it clean up itself when done
279  effect.SetAutodestroy(true);
280  effect.Stop();
281  }
282  }
283  }
284 
290  static bool IsEffectExist( int effect_id )
291  {
292  if (!m_IsCleanup)
293  return m_EffectsMap[effect_id] != null;
294  else
295  return false;
296  }
297 
303  static Effect GetEffectByID(int effect_id)
304  {
305  if (!m_IsCleanup)
306  return m_EffectsMap[effect_id];
307  else
308  return null;
309  }
310 
318  static int EffectRegister(Effect effect)
319  {
320  if (effect.IsRegistered())
321  {
322  ErrorEx(string.Format("Attempted to register Effect '%1' which was already registered.", effect.GetDebugName()), ErrorExSeverity.INFO);
323  return effect.GetID();
324  }
325 
326  int id;
327 
328  if (!m_IsCleanup)
329  {
330  id = GetFreeEffectID();
331  m_EffectsMap.Insert(id, effect);
332  effect.Event_OnRegistered(id);
333  }
334  else
335  ErrorEx("Attempted to register Effect while SEffectManager is cleaning up, request ignored.", ErrorExSeverity.WARNING);
336 
337  return id;
338  }
339 
347  static void EffectUnregister(int id)
348  {
349  if (m_IsCleanup)
350  return; // No error needed, since it will have been unregistered anyways after cleanup is finished
351 
352  Effect effect;
353  if ( m_EffectsMap.Find(id, effect) )
354  {
355  effect.Event_OnUnregistered();
356  m_EffectsMap.Remove(id);
357  }
358 
359  if ( m_FreeEffectIDs.Find(id) == -1 )
360  {
361  m_FreeEffectIDs.Insert(id);
362  }
363  }
364 
369  static void EffectUnregisterEx(Effect effect)
370  {
371  EffectUnregister(effect.GetID());
372  }
373 
378  protected static int GetFreeEffectID()
379  {
380  int return_id;
381 
382  if (m_FreeEffectIDs.Count() > 0)
383  {
384  return_id = m_FreeEffectIDs.Get(0);
385  m_FreeEffectIDs.Remove(0);
386  }
387  else
388  {
389  return_id = m_HighestFreeEffectID;
390  ++m_HighestFreeEffectID;
391  }
392 
393  return return_id;
394  }
395 
397 
398 
399 
404 
410  static bool DestroySound(EffectSound sound_effect)
411  {
412  DestroyEffect(sound_effect);
413  return true;
414  }
415 
421  static SoundParams GetCachedSoundParam(string soundset)
422  {
423  SoundParams params;
424  if (!m_ParamsMap.Find(soundset, params))
425  {
426  params = new SoundParams(soundset);
427  m_ParamsMap.Insert(soundset, params);
428  }
429  return params;
430  }
431 
433 
434 
435 
440 
446  static void Event_OnSoundWaveEnded(EffectSound effect_sound)
447  {
448 
449  }
450 
458  static void Event_OnFrameUpdate(float time_delta)
459  {
460  Event_OnFrameUpdate.Invoke(time_delta);
461  }
462 
464 
465 
466 
471 
476  static void Init()
477  {
478  m_EffectsMap = new map<int, ref Effect>;
479  m_FreeEffectIDs = new array<int>;
480  m_ParamsMap = new map<string, ref SoundParams>;
482 
483  m_IsInitialized = true;
484  }
485 
490  static void Cleanup()
491  {
492  // Nothing to clean
493  if (!m_IsInitialized)
494  return;
495 
496  m_IsCleanup = true;
497 
498  // There should not be anything in here on server
499  if (GetGame() && GetGame().IsDedicatedServer())
500  {
501  if (m_ParamsMap.Count() > 0)
502  ErrorEx(string.Format("SEffectManager containing SoundParams on server."), ErrorExSeverity.WARNING);
503 
504  if (m_EffectsMap.Count() > 0)
505  ErrorEx(string.Format("SEffectManager containing Effect on server."), ErrorExSeverity.WARNING);
506  }
507 
508  // These are intentionally cached, just clear them
509  m_ParamsMap.Clear();
510 
511  // These might not be intentionally still here, so log how many there are
512  #ifdef DEVELOPER
513  Print("--- SEffectManager Cleanup dump - Begin ------------------------");
514  Print(string.Format("Effect count: %1", m_EffectsMap.Count()));
515  #endif
516 
517  // Best to call the unregister event before clearing the map
518  // In case some ref is still being held elsewhere and will still be kept alive
519  foreach (int id, Effect eff : m_EffectsMap)
520  {
521  eff.Event_OnUnregistered();
522  #ifdef SFXM_DUMP
523  Print(string.Format( "%1 :: %2 :: %3", eff, typename.EnumToString(EffectType, eff.GetEffectType()), eff.GetDebugName() ));
524  #endif
525  }
526 
527  #ifdef DEVELOPER
528  Print("--- SEffectManager Cleanup dump - End --------------------------");
529  #endif
530 
531  // Now we can clear it
532  m_EffectsMap.Clear();
533 
534  // Reset the state
535  m_HighestFreeEffectID = 1;
536  Event_OnFrameUpdate.Clear();
537  m_IsCleanup = false;
538  }
539 
541 }
GetGame
proto native CGame GetGame()
EffectType
EffectType
Enum to determine what type of effect the Effect is.
Definition: effect.c:2
Event_OnFrameUpdate
void Event_OnFrameUpdate(float time_delta)
Event called on frame when enabled by SetEnableEventFrame(true)
Definition: effect.c:343
m_IsInitialized
protected bool m_IsInitialized
Definition: combinationlock.c:22
Print
proto void Print(void var)
Prints content of variable to console/log.
EffectParticle
Wrapper class for managing particles through SEffectManager.
Definition: effectparticle.c:4
ErrorEx
enum ShapeType ErrorEx
SoundParams
Definition: sound.c:100
EffectSound
Wrapper class for managing sound through SEffectManager.
Definition: effectsound.c:4
map
map
Definition: controlsxboxnew.c:3
vector
Definition: enconvert.c:105
Effect
void Effect()
ctor
Definition: effect.c:70
ErrorExSeverity
ErrorExSeverity
Definition: endebug.c:61
Object
Definition: objecttyped.c:1
array
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
Definition: isboxcollidinggeometryproxyclasses.c:27
SEffectManager
Manager class for managing Effect (EffectParticle, EffectSound)
Definition: effectmanager.c:5
ScriptInvoker
ScriptInvoker Class provide list of callbacks usage:
Definition: tools.c:115