Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
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
22
25
28 protected static ref array<int> m_FreeEffecterIDs;
29
30 protected static int m_HighestFreeEffecterID = 1;
31
37
47 static int PlayInWorld(notnull Effect eff, vector pos)
48 {
49 // Stop the effect first, just in case
50 eff.Stop();
51
52 int id = EffectRegister(eff);
53
54 eff.SetPosition( pos );
55 eff.Start();
56
57 return id;
58 }
59
70 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)
71 {
72 // Stop the effect first, just in case
73 eff.Stop();
74
75 int id = EffectRegister(eff);
76
77 if (!obj)
78 {
79 ErrorEx("Parent object is null.", ErrorExSeverity.WARNING);
80 eff.SetPosition(local_pos);
81 }
82 else
83 {
84 eff.SetPosition(obj.GetPosition());
85 }
86
87 eff.SetParent(obj);
88 eff.SetLocalPosition(local_pos);
89 eff.SetAttachedLocalOri(local_ori);
90
91 if (force_rotation_relative_to_world)
92 {
93 EffectParticle eff_particle = EffectParticle.Cast(eff);
94
95 if (eff_particle)
96 {
97 eff_particle.ForceParticleRotationRelativeToWorld(force_rotation_relative_to_world);
98 }
99 }
100
101 eff.Start();
102
103 return id;
104 }
105
110 static void Stop(int effect_id)
111 {
112 Effect eff = m_EffectsMap.Get(effect_id);
113
114 if (eff)
115 {
116 eff.Stop();
117 }
118 else
119 {
120 ErrorEx(string.Format("Failed to stop Effect with ID %1. The ID is not registered in m_EffectsMap!", effect_id));
121 }
122 }
123
125
126
127
132
144 static EffectSound CreateSound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false, bool enviroment = false)
145 {
146 EffectSound effect_sound = new EffectSound();
147 effect_sound.SetSoundSet(sound_set);
148 effect_sound.SetPosition(position);
149 effect_sound.SetSoundFadeIn(play_fade_in);
150 effect_sound.SetSoundFadeOut(stop_fade_out);
151 effect_sound.SetSoundLoop(loop);
152 effect_sound.SetEnviromentVariables(enviroment);
153
154 EffectRegister( effect_sound );
155
156 return effect_sound;
157 }
158
169 static EffectSound PlaySound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
170 {
171 EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, false);
172
173 effect_sound.SoundPlay();
174
175 return effect_sound;
176 }
177
188 static EffectSound PlaySoundParams(notnull SoundParams params, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
189 {
190 EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
191
192 effect_sound.SoundPlayEx(params);
193
194 return effect_sound;
195 }
196
207 static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
208 {
209 SoundParams params = GetCachedSoundParam(sound_set);
210
211 EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
212
213 effect_sound.SoundPlayEx(params);
214
215 return effect_sound;
216 }
217
228 static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
229 {
230 EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, true);
231
232 effect_sound.SoundPlay();
233
234 return effect_sound;
235 }
236
247 static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
248 {
249 EffectSound effect_sound = CreateSound(sound_set, parent_object.GetPosition(), play_fade_in, stop_fade_out, loop);
250
251 effect_sound.SetParent( parent_object );
252 effect_sound.SetLocalPosition( vector.Zero );
253 effect_sound.SoundPlay();
254
255 return effect_sound;
256 }
257
259
260
261
266
271 static void DestroyEffect(Effect effect)
272 {
273 if (effect)
274 {
275 if (effect.CanDestroy())
276 {
277 // Functionality already happens in dtor of Effect to be safe
278 delete effect;
279 }
280 else
281 {
282 // Make it clean up itself when done
283 effect.SetAutodestroy(true);
284 effect.Stop();
285 }
286 }
287 }
288
294 static bool IsEffectExist( int effect_id )
295 {
296 if (!m_IsCleanup)
297 return m_EffectsMap[effect_id] != null;
298 else
299 return false;
300 }
301
307 static Effect GetEffectByID(int effect_id)
308 {
309 if (!m_IsCleanup)
310 return m_EffectsMap[effect_id];
311 else
312 return null;
313 }
314
322 static int EffectRegister(Effect effect)
323 {
324 if (effect.IsRegistered())
325 {
326 ErrorEx(string.Format("Attempted to register Effect '%1' which was already registered.", effect.GetDebugName()), ErrorExSeverity.INFO);
327 return effect.GetID();
328 }
329
330 int id;
331
332 if (!m_IsCleanup)
333 {
334 id = GetFreeEffectID();
335 m_EffectsMap.Insert(id, effect);
336 effect.Event_OnRegistered(id);
337 }
338 else
339 ErrorEx("Attempted to register Effect while SEffectManager is cleaning up, request ignored.", ErrorExSeverity.WARNING);
340
341 return id;
342 }
343
344 protected static int GetFreeEffecterID()
345 {
346 int return_id;
347
348 if (m_FreeEffecterIDs.Count() > 0)
349 {
350 return_id = m_FreeEffecterIDs.Get(0);
351 m_FreeEffecterIDs.Remove(0);
352 }
353 else
354 {
355 return_id = m_HighestFreeEffecterID;
357 }
358
359 return return_id;
360 }
361
369 static void EffectUnregister(int id)
370 {
371 if (m_IsCleanup)
372 return; // No error needed, since it will have been unregistered anyways after cleanup is finished
373
374 Effect effect;
375 if ( m_EffectsMap.Find(id, effect) )
376 {
377 effect.Event_OnUnregistered();
378 m_EffectsMap.Remove(id);
379 }
380
381 if ( m_FreeEffectIDs.Find(id) == -1 )
382 {
383 m_FreeEffectIDs.Insert(id);
384 }
385 }
386
391 static void EffectUnregisterEx(Effect effect)
392 {
393 EffectUnregister(effect.GetID());
394 }
395
400 protected static int GetFreeEffectID()
401 {
402 int return_id;
403
404 if (m_FreeEffectIDs.Count() > 0)
405 {
406 return_id = m_FreeEffectIDs.Get(0);
407 m_FreeEffectIDs.Remove(0);
408 }
409 else
410 {
411 return_id = m_HighestFreeEffectID;
413 }
414
415 return return_id;
416 }
417
419
420
421
426
432 static bool DestroySound(EffectSound sound_effect)
433 {
434 DestroyEffect(sound_effect);
435 return true;
436 }
437
443 static SoundParams GetCachedSoundParam(string soundset)
444 {
445 SoundParams params;
446 if (!m_ParamsMap.Find(soundset, params))
447 {
448 params = new SoundParams(soundset);
449 m_ParamsMap.Insert(soundset, params);
450 }
451 return params;
452 }
453
455
456
457
462
468 static void Event_OnSoundWaveEnded(EffectSound effect_sound)
469 {
470
471 }
472
480 static void Event_OnFrameUpdate(float time_delta)
481 {
482 Event_OnFrameUpdate.Invoke(time_delta);
483 }
484
486
487
488
493
507
508 static void InitServer()
509 {
512 }
513
518 static void Cleanup()
519 {
520 // Nothing to clean
521 if (!m_IsInitialized)
522 return;
523
524 m_IsCleanup = true;
525
526 // There should not be anything in here on server
527 if (GetGame() && GetGame().IsDedicatedServer())
528 {
529 if (m_ParamsMap.Count() > 0)
530 ErrorEx(string.Format("SEffectManager containing SoundParams on server."), ErrorExSeverity.WARNING);
531
532 if (m_EffectsMap.Count() > 0)
533 ErrorEx(string.Format("SEffectManager containing Effect on server."), ErrorExSeverity.WARNING);
534 }
535
536 // These are intentionally cached, just clear them
537 m_ParamsMap.Clear();
538
539 // These might not be intentionally still here, so log how many there are
540 #ifdef DEVELOPER
541 Print("--- SEffectManager Cleanup dump - Begin ------------------------");
542 Print(string.Format("Effect count: %1", m_EffectsMap.Count()));
543 #endif
544
545 // Best to call the unregister event before clearing the map
546 // In case some ref is still being held elsewhere and will still be kept alive
547 foreach (int id, Effect eff : m_EffectsMap)
548 {
549 eff.Event_OnUnregistered();
550 #ifdef SFXM_DUMP
551 Print(string.Format( "%1 :: %2 :: %3", eff, typename.EnumToString(EffectType, eff.GetEffectType()), eff.GetDebugName() ));
552 #endif
553 }
554
555 foreach (int i, EffecterBase effecter : m_EffectersMap)
556 {
557 effecter.Delete();
558 }
559
560 #ifdef DEVELOPER
561 Print("--- SEffectManager Cleanup dump - End --------------------------");
562 #endif
563
564 // Now we can clear it
565 m_EffectsMap.Clear();
566 m_EffectersMap.Clear();
567
568 // Reset the state
570 Event_OnFrameUpdate.Clear();
571 m_IsCleanup = false;
572 }
573
575
577 static int CreateParticleServer(vector pos, EffecterParameters parameters)
578 {
579 EffecterBase eff;
580 eff = EffecterBase.Cast(GetGame().CreateObjectEx(parameters.m_EffecterType, pos, ECE_PLACE_ON_SURFACE));
581
582 if (eff)
583 {
584 int id = GetFreeEffecterID();
585 m_EffectersMap.Insert(id, eff);
586 }
587
588 eff.Init(id, parameters);
589 return id;
590 }
591
593 static void ReinitParticleServer(int effecterID, EffecterParameters parameters)
594 {
595 EffecterBase eff = m_EffectersMap.Get(effecterID);
596 if (eff)
597 {
598 eff.Init(effecterID,parameters);
599 }
600 }
601
602 static void ReactivateParticleServer(int effecterID)
603 {
604 EffecterBase eff = m_EffectersMap.Get(effecterID);
605 if (eff)
606 {
607 eff.Reactivate();
608 }
609 }
610
611 static void StartParticleServer(int effecterID)
612 {
613 EffecterBase eff = m_EffectersMap.Get(effecterID);
614 if (eff)
615 {
616 eff.Start();
617 }
618
619 }
620
621 static void StopParticleServer(int effecterID)
622 {
623 EffecterBase eff = m_EffectersMap.Get(effecterID);
624 if (eff)
625 {
626 eff.Stop();
627 }
628 }
629
630 static void DestroyEffecterParticleServer(int effecterID)
631 {
632 EffecterBase eff = m_EffectersMap.Get(effecterID);
633 if (eff)
634 {
635 m_EffectersMap.Remove(effecterID);
636 eff.DeleteSafe();
637 }
638 }
639
640 static void OnUpdate(float timeslice)
641 {
642 if (m_EffectersMap)
643 {
644 foreach (int i, EffecterBase effecter : m_EffectersMap)
645 {
646 effecter.DecreaseLifespan(timeslice);
647 }
648 }
649 }
650}
651
652enum EffecterCommands
653{
654 NONE = -1,
658 REACTIVATE1
659}
660
661class EffecterParameters
662{
665 void EffecterParameters(string type, float lifespan)
666 {
667 m_EffecterType = type;
668 m_Lifespan = lifespan;
669 }
670}
671
672class ParticleEffecterParameters : EffecterParameters
673{
674 int m_ParticleID;
675 void ParticleEffecterParameters(string type, float lifespan, int particleID)
676 {
677 m_ParticleID = particleID;
678 }
679}
680
681
682class EffecterBase : EntityAI
683{
684 const float NOT_DEFINED_LIFESPAN = -1;
685 protected float m_Lifespan;
686 protected int m_ID;
687 protected int m_Command = EffecterCommands.NONE;
688 protected int m_CommandSync = EffecterCommands.NONE;
689
691 {
692 RegisterNetSyncVariableInt("m_CommandSync");
693 }
694
695 void Init(int id, EffecterParameters parameters)
696 {
697 m_ID = id;
698 SetLifespan(parameters.m_Lifespan);
699 }
700
701 void DecreaseLifespan(float timeSlice)
702 {
704 return;
705
706 m_Lifespan -= timeSlice;
707 if (m_Lifespan < 0)
708 {
710 }
711 }
712
713 void SetLifespan(float lifespan)
714 {
715 m_Lifespan = lifespan;
716 }
717
718 void Start()
719 {
720 m_CommandSync = EffecterCommands.START;
721 Process();
722 }
723
724 void Stop()
725 {
726 m_CommandSync = EffecterCommands.STOP;
727 Process();
728 }
729
731 {
732 if (m_CommandSync == EffecterCommands.REACTIVATE0)
733 {
734 m_CommandSync = EffecterCommands.REACTIVATE1;
735 }
736 else
737 {
738 m_CommandSync = EffecterCommands.REACTIVATE0;
739 }
740 Process();
741 }
742
743 void Process()
744 {
745 if (GetGame().IsMultiplayer())
746 {
747 SetSynchDirty();
748 }
749 else
750 {
752 }
753 }
754
755 override int GetHideIconMask()
756 {
757 return EInventoryIconVisibility.HIDE_VICINITY;
758 }
759}
760
761class ParticleEffecter : EffecterBase
762{
763 protected int m_ParticleEffectID = -1;
764 protected int m_ParticleEffectIDSync = -1;
765 protected ref EffectParticleGeneral m_Effect = null;
766 //protected int m_EffectID = -1;
767
768 void ParticleEffecter(int lifespan)
769 {
770 RegisterNetSyncVariableInt("m_ParticleEffectIDSync");
771 }
772
773 override void Init(int id, EffecterParameters parameters)
774 {
775 super.Init(id, parameters);
776
778 SetParticle(par.m_ParticleID);
779 }
780
781 void SetParticle(int particleID)
782 {
783 m_ParticleEffectIDSync = particleID;
784 Process();
785 }
786
788 {
790 {
791 if (m_Effect)
792 {
793 m_Effect.SetParticle(m_ParticleEffectIDSync);
794 }
795 else
796 {
798 m_Effect.SetParticle(m_ParticleEffectIDSync);
799 SEffectManager.PlayInWorld(m_Effect, GetWorldPosition());
800 }
802 }
803
805 {
806 switch (m_CommandSync)
807 {
808 case EffecterCommands.START:
809 if (m_Effect && !m_Effect.IsPlaying())
810 {
811 m_Effect.SetParticle(m_ParticleEffectID);
812 m_Effect.Start();
813 }
814 break;
815
816 case EffecterCommands.STOP:
817 if (m_Effect && m_Effect.IsPlaying())
818 {
819 m_Effect.Stop();
820 }
821 break;
822
823 case EffecterCommands.REACTIVATE0:
824 case EffecterCommands.REACTIVATE1:
825 if (m_Effect)
826 {
827 m_Effect.SetParticle(m_ParticleEffectID);
828 }
829 if (!m_Effect.IsPlaying())
830 {
831 m_Effect.Start();
832 }
833 break;
834
835 default:
836 break;
837 }
838
840 }
841 }
842
847}
848
850{
853 {
854 }
855
856 void SetParticle( int particleID )
857 {
858 bool was_playing = IsPlaying();
859
860 Stop();
861
862 SetParticleID(particleID);
863
864 if (was_playing)
865 {
866 Start();
867 }
868 }
869
870 override void SetParticleID( int id )
871 {
872 super.SetParticleID(id);
873 m_LastParticleID = id;
874 }
875}
const int ECE_PLACE_ON_SURFACE
Wrapper class for managing particles through SEffectManager.
override void Stop()
Stops all elements this effect consists of.
override void Start()
Plays all elements this effect consists of.
void SetParticleID(int id)
Sets the id of the particle to be used.
void SetParticle(Particle p)
Sets the main particle which this Effect will manage.
void ForceParticleRotationRelativeToWorld(bool state)
Set orientation setting to be used by the effect when the Effect starts.
Wrapper class for managing sound through SEffectManager.
Definition effectsound.c:5
void SetSoundSet(string snd)
Set soundset for the sound.
bool SoundPlayEx(out SoundParams params)
Plays sound.
void SetSoundLoop(bool loop)
Set if the sound loops.
void SetSoundFadeIn(float fade_in)
Set the sound fade in duration.
void SetSoundFadeOut(float fade_out)
Set the sound fade out duration.
void SetEnviromentVariables(bool setEnvVariables)
Sets whether AddEnvSoundVariables needs to be called during Loading.
override void SetParent(Object parent_obj, int pivot)
Set parent for the sound to follow.
bool SoundPlay()
Plays sound.
override void Init(int id, EffecterParameters parameters)
ref EffectParticleGeneral m_Effect
void ParticleEffecter(int lifespan)
void SetParticle(int particleID)
override void OnVariablesSynchronized()
Manager class for managing Effect (EffectParticle, EffectSound)
static void Event_OnSoundWaveEnded(EffectSound effect_sound)
Event called from EffectSound.Event_OnSoundWaveEnded.
static int GetFreeEffecterID()
static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound, updating environment variables.
static void EffectUnregister(int id)
Unregisters Effect in SEffectManager.
static void Cleanup()
Cleanup method to properly clean up the static data.
static ref map< int, EffecterBase > m_EffectersMap
static EffectSound PlaySoundParams(notnull SoundParams params, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
static ref array< int > m_FreeEffecterIDs
Static array of IDs that were previously used, but freed up by unregistering Effecters.
static void DestroyEffecterParticleServer(int effecterID)
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 void InitServer()
static int GetFreeEffectID()
Helper function for EffectRegister to decide an Effect ID.
static ref array< int > m_FreeEffectIDs
Static array of IDs that were previously used, but freed up by unregistering.
static const int INVALID_ID
As the counter starts at 1, Effect ID can never be 0.
static int PlayInWorld(notnull Effect eff, vector pos)
Play an Effect.
static ref ScriptInvoker Event_OnFrameUpdate
Static invoker for the SEffectManager.Event_OnFrameUpdate called form MissionGameplay....
static int m_HighestFreeEffecterID
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)
Play an Effect.
static int CreateParticleServer(vector pos, EffecterParameters parameters)
returns unique effecter ID
static Effect GetEffectByID(int effect_id)
Gets the Effect with the given registered Effect ID.
static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
static ref map< int, ref Effect > m_EffectsMap
Static map of all registered effects <id, Effect>
static int m_HighestFreeEffectID
Counter for quickly getting the next ID if FreeEffectIDs array is empty.
static bool m_IsCleanup
Bool to check whether Cleanup is happening, which means that the maps should no longer be accessed.
static void OnUpdate(float timeslice)
static void Init()
Initialize the containers.
static void StartParticleServer(int effecterID)
static void Stop(int effect_id)
Stops the Effect.
static SoundParams GetCachedSoundParam(string soundset)
Get or create a cached SoundParams object.
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.
static bool m_IsInitialized
Bool to check whether Init was called.
static bool IsEffectExist(int effect_id)
Checks whether an Effect ID is registered in SEffectManager.
static bool DestroySound(EffectSound sound_effect)
Legacy, backwards compatibility.
static void EffectUnregisterEx(Effect effect)
Unregisters Effect in SEffectManager.
static EffectSound CreateSound(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false, bool enviroment=false)
Create an EffectSound.
static ref map< string, ref SoundParams > m_ParamsMap
Static map of cached sound params, to prevent having to recreate them.
static void ReinitParticleServer(int effecterID, EffecterParameters parameters)
allows re-initializing existing effecter with new parameters (extept m_EffecterType,...
static int EffectRegister(Effect effect)
Registers Effect in SEffectManager.
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
static void StopParticleServer(int effecterID)
static void Event_OnFrameUpdate(float time_delta)
Event called on frame.
static void ReactivateParticleServer(int effecterID)
ScriptInvoker Class provide list of callbacks usage:
Definition tools.c:116
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
override void OnVariablesSynchronized()
override Widget Init()
Definition dayzgame.c:127
void Start()
Plays all elements this effects consists of.
Definition effect.c:157
EffectType
Enum to determine what type of effect the Effect is.
Definition effect.c:3
int m_ID
ID of effect, given by SEffectManager when registered (automatically done when playing through it)
Definition effect.c:51
bool IsPlaying()
Returns true when the Effect is playing, false otherwise.
Definition effect.c:197
void DecreaseLifespan(float timeSlice)
ParticleEffecter m_LastParticleID
class SEffectManager m_EffecterType
void EffecterParameters(string type, float lifespan)
float m_Lifespan
class SEffectManager REACTIVATE0
void EffectParticleGeneral()
class SEffectManager STOP
class SEffectManager NONE
class SEffectManager START
void SetLifespan(float lifespan)
int m_Command
override int GetHideIconMask()
ParticleEffecterParameters NOT_DEFINED_LIFESPAN
void Reactivate()
void EffecterBase()
void Process()
int m_CommandSync
proto native CGame GetGame()
ErrorExSeverity
Definition endebug.c:62
proto void Print(void var)
Prints content of variable to console/log.
enum ShapeType ErrorEx
class SoundObject SoundParams(string name)
void Stop()
Stops all elements this effect consists of.
Definition effect.c:183