File size: 16,015 Bytes
24b81cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
/**
\brief Manager class for managing Effect (EffectParticle, EffectSound)
\warning Keeps a ref to any Effect registered (Created/Played), make sure to perform the necessary cleanup
*/
class SEffectManager
{
//! Static map of all registered effects <id, Effect>
protected static ref map<int, ref Effect> m_EffectsMap;
//! Static array of IDs that were previously used, but freed up by unregistering
protected static ref array<int> m_FreeEffectIDs;
//! Counter for quickly getting the next ID if FreeEffectIDs array is empty
protected static int m_HighestFreeEffectID = 1;
//! As the counter starts at 1, Effect ID can never be 0
static const int INVALID_ID = 0;
//! Bool to check whether Cleanup is happening, which means that the maps should no longer be accessed
protected static bool m_IsCleanup;
//! Bool to check whether Init was called
protected static bool m_IsInitialized;
//! Static map of cached sound params, to prevent having to recreate them
protected static ref map<string, ref SoundParams> m_ParamsMap;
//! Static invoker for the SEffectManager.Event_OnFrameUpdate called form MissionGameplay.OnUpdate
static ref ScriptInvoker Event_OnFrameUpdate;
/** \name Generic playback
Methods for playing Effect
\note Since 1.15, these should work on EffectSound as well
*/
//@{
/**
*\brief Play an Effect
* \warning As the Effect is automatically registered, it will not be freed automatically (because of the ref)
* Unless 'SetAutodestroy(true)' is called on the created 'Effect', which will clean it up when the sound stop
* Alternatively, SEffectManager.DestroyEffect can be called manually, which will also unregister and cleanup
* \param eff \p Effect The Effect to play
* \param pos \p vector The position to play the Effect
* \return \p int The registered ID of the Effect
*/
static int PlayInWorld(notnull Effect eff, vector pos)
{
// Stop the effect first, just in case
eff.Stop();
int id = EffectRegister(eff);
eff.SetPosition( pos );
eff.Start();
return id;
}
/**
*\brief Play an Effect
* \warning Read PlayInWorld warning
* \param eff \p Effect The Effect to play
* \param obj \p Object The parent of the Effect
* \param local_pos \p vector The local position to play the Effect in relation to the parent (Optional)
* \param local_ori \p vector The local orientation to play the Effect in relation to the parent (Optional)
* \param force_rotation_relative_to_world \p bool Whether to force the orientation to stay in WS (Optional)
* \return \p int The registered ID of the Effect
*/
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)
{
// Stop the effect first, just in case
eff.Stop();
int id = EffectRegister(eff);
if (!obj)
{
ErrorEx("Parent object is null.", ErrorExSeverity.WARNING);
eff.SetPosition(local_pos);
}
else
{
eff.SetPosition(obj.GetPosition());
}
eff.SetParent(obj);
eff.SetLocalPosition(local_pos);
eff.SetAttachedLocalOri(local_ori);
if (force_rotation_relative_to_world)
{
EffectParticle eff_particle = EffectParticle.Cast(eff);
if (eff_particle)
{
eff_particle.ForceParticleRotationRelativeToWorld(force_rotation_relative_to_world);
}
}
eff.Start();
return id;
}
/**
\brief Stops the Effect
\param effect_id \p int The ID of the Effect to Stop
*/
static void Stop(int effect_id)
{
Effect eff = m_EffectsMap.Get(effect_id);
if (eff)
{
eff.Stop();
}
else
{
ErrorEx(string.Format("Failed to stop Effect with ID %1. The ID is not registered in m_EffectsMap!", effect_id));
}
}
//@}
/** \name Create/Play sound
Methods for playing/creating sound
*/
//@{
/**
*\brief Create an EffectSound
* \warning Read PlayInWorld warning
* \param sound_set \p string The sound set name of the sound
* \param position \p vector The position to play the sound
* \param play_fade_in \p float The fade in duration of the sound (Optional)
* \param stop_fade_out \p float The fade out duration of the sound (Optional)
* \param loop \p bool Whether the sound should loop (Optional)
* \param enviroment \p bool Whether to set environment variables (Optional)
* \return \p EffectSound The created EffectSound
*/
static EffectSound CreateSound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false, bool enviroment = false)
{
EffectSound effect_sound = new EffectSound();
effect_sound.SetSoundSet(sound_set);
effect_sound.SetPosition(position);
effect_sound.SetSoundFadeIn(play_fade_in);
effect_sound.SetSoundFadeOut(stop_fade_out);
effect_sound.SetSoundLoop(loop);
effect_sound.SetEnviromentVariables(enviroment);
EffectRegister( effect_sound );
return effect_sound;
}
/**
*\brief Create and play an EffectSound
* \warning Calls CreateSound, read CreateSound warning
* \param sound_set \p string The sound set name of the sound
* \param position \p vector The position to play the sound
* \param play_fade_in \p float The fade in duration of the sound (Optional)
* \param stop_fade_out \p float The fade out duration of the sound (Optional)
* \param loop \p bool Whether the sound should loop (Optional)
* \return \p EffectSound The created EffectSound
*/
static EffectSound PlaySound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
{
EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, false);
effect_sound.SoundPlay();
return effect_sound;
}
/**
*\brief Create and play an EffectSound
* \warning Calls CreateSound, read CreateSound warning
* \param params \p SoundParams Params to create the sound with
* \param position \p vector The position to play the sound
* \param play_fade_in \p float The fade in duration of the sound (Optional)
* \param stop_fade_out \p float The fade out duration of the sound (Optional)
* \param loop \p bool Whether the sound should loop (Optional)
* \return \p EffectSound The created EffectSound
*/
static EffectSound PlaySoundParams(notnull SoundParams params, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
{
EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
effect_sound.SoundPlayEx(params);
return effect_sound;
}
/**
*\brief Create and play an EffectSound, using or creating cached SoundParams
* \warning Calls CreateSound, read CreateSound warning
* \param sound_set \p string The sound set name of the sound
* \param position \p vector The position to play the sound
* \param play_fade_in \p float The fade in duration of the sound (Optional)
* \param stop_fade_out \p float The fade out duration of the sound (Optional)
* \param loop \p bool Whether the sound should loop (Optional)
* \return \p EffectSound The created EffectSound
*/
static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
{
SoundParams params = GetCachedSoundParam(sound_set);
EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
effect_sound.SoundPlayEx(params);
return effect_sound;
}
/**
*\brief Create and play an EffectSound, updating environment variables
* \warning Calls CreateSound, read CreateSound warning
* \param sound_set \p string The sound set name of the sound
* \param position \p vector The position to play the sound
* \param play_fade_in \p float The fade in duration of the sound (Optional)
* \param stop_fade_out \p float The fade out duration of the sound (Optional)
* \param loop \p bool Whether the sound should loop (Optional)
* \return \p EffectSound The created EffectSound
*/
static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
{
EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, true);
effect_sound.SoundPlay();
return effect_sound;
}
/**
*\brief Create and play an EffectSound
* \warning Calls CreateSound, read CreateSound warning
* \param sound_set \p string The sound set name of the sound
* \param parent_object \p Object The parent Object for the sound to follow
* \param play_fade_in \p float The fade in duration of the sound (Optional)
* \param stop_fade_out \p float The fade out duration of the sound (Optional)
* \param loop \p bool Whether the sound should loop (Optional)
* \return \p EffectSound The created EffectSound
*/
static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
{
EffectSound effect_sound = CreateSound(sound_set, parent_object.GetPosition(), play_fade_in, stop_fade_out, loop);
effect_sound.SetParent( parent_object );
effect_sound.SetLocalPosition( vector.Zero );
effect_sound.SoundPlay();
return effect_sound;
}
//@}
/** \name Generic API
General methods used for SEffectManager
*/
//@{
/**
\brief Unregisters, stops and frees the Effect
\param effect_sound \p EffectSound The EffectSound to free
*/
static void DestroyEffect(Effect effect)
{
if (effect)
{
if (effect.CanDestroy())
{
// Functionality already happens in dtor of Effect to be safe
delete effect;
}
else
{
// Make it clean up itself when done
effect.SetAutodestroy(true);
effect.Stop();
}
}
}
/**
\brief Checks whether an Effect ID is registered in SEffectManager
\param effect_id \p int The Effect ID to check
\return \p bool Whether there is an Effect registered for this ID
*/
static bool IsEffectExist( int effect_id )
{
if (!m_IsCleanup)
return m_EffectsMap[effect_id] != null;
else
return false;
}
/**
\brief Gets the Effect with the given registered Effect ID
\param effect_id \p int The Effect ID
\return \p Effect The Effect registered to the ID or null
*/
static Effect GetEffectByID(int effect_id)
{
if (!m_IsCleanup)
return m_EffectsMap[effect_id];
else
return null;
}
/**
\brief Registers Effect in SEffectManager
\note Already handled in SEffectManager Create/Play methods
\note This will make SEffectManager hold a strong ref for the Effect
\param effect \p Effect The Effect to register
\return \p int The Effect ID
*/
static int EffectRegister(Effect effect)
{
if (effect.IsRegistered())
{
ErrorEx(string.Format("Attempted to register Effect '%1' which was already registered.", effect.GetDebugName()), ErrorExSeverity.INFO);
return effect.GetID();
}
int id;
if (!m_IsCleanup)
{
id = GetFreeEffectID();
m_EffectsMap.Insert(id, effect);
effect.Event_OnRegistered(id);
}
else
ErrorEx("Attempted to register Effect while SEffectManager is cleaning up, request ignored.", ErrorExSeverity.WARNING);
return id;
}
/**
\brief Unregisters Effect in SEffectManager
\note Will automatically occur on stop when the Effect is AutoDestroy
\note ID can be gotten from the Effect by calling Effect.GetID
\note Generic Play methods will also return the ID
\param id \p int The ID of the Effect to unregister
*/
static void EffectUnregister(int id)
{
if (m_IsCleanup)
return; // No error needed, since it will have been unregistered anyways after cleanup is finished
Effect effect;
if ( m_EffectsMap.Find(id, effect) )
{
effect.Event_OnUnregistered();
m_EffectsMap.Remove(id);
}
if ( m_FreeEffectIDs.Find(id) == -1 )
{
m_FreeEffectIDs.Insert(id);
}
}
/**
\brief Unregisters Effect in SEffectManager
\param effect \p Effect The Effect to unregister
*/
static void EffectUnregisterEx(Effect effect)
{
EffectUnregister(effect.GetID());
}
/**
\brief Helper function for EffectRegister to decide an Effect ID
\return \p int A currently unused Effect ID
*/
protected static int GetFreeEffectID()
{
int return_id;
if (m_FreeEffectIDs.Count() > 0)
{
return_id = m_FreeEffectIDs.Get(0);
m_FreeEffectIDs.Remove(0);
}
else
{
return_id = m_HighestFreeEffectID;
++m_HighestFreeEffectID;
}
return return_id;
}
//@}
/** \name Sound helpers
Sound specific helper methods
*/
//@{
/**
\brief Legacy, backwards compatibility
\param sound_effect \p EffectSound The EffectSound to free
\return \p bool A bool which is always true
*/
static bool DestroySound(EffectSound sound_effect)
{
DestroyEffect(sound_effect);
return true;
}
/**
\brief Get or create a cached SoundParams object
\param soundset \p string The sound set name of the sound
\return \p SoundParams The cached SoundParams for the given soundset
*/
static SoundParams GetCachedSoundParam(string soundset)
{
SoundParams params;
if (!m_ParamsMap.Find(soundset, params))
{
params = new SoundParams(soundset);
m_ParamsMap.Insert(soundset, params);
}
return params;
}
//@}
/** \name Events
Various events that can be overriden for custom behaviour
*/
//@{
/**
\brief Event called from EffectSound.Event_OnSoundWaveEnded
\note Every registered sound is registered to call this
\param effect_sound \p EffectSound The EffectSound calling the event
*/
static void Event_OnSoundWaveEnded(EffectSound effect_sound)
{
}
/**
\brief Event called on frame
\note Called from MissionGameplay.OnUpdate
\note Effects register themselves by Effect.SetEnableEventFrame(true)
\note EffectSound is automatically registered
\param time_delta \p float Time passed since the previous frame
*/
static void Event_OnFrameUpdate(float time_delta)
{
Event_OnFrameUpdate.Invoke(time_delta);
}
//@}
/** \name Lifetime
Creation and cleanup
*/
//@{
/**
\brief Initialize the containers
\note This is done this way, to have these not exist on server
*/
static void Init()
{
m_EffectsMap = new map<int, ref Effect>;
m_FreeEffectIDs = new array<int>;
m_ParamsMap = new map<string, ref SoundParams>;
Event_OnFrameUpdate = new ScriptInvoker();
m_IsInitialized = true;
}
/**
\brief Cleanup method to properly clean up the static data
\note Will be called when MissionBase is destroyed
*/
static void Cleanup()
{
// Nothing to clean
if (!m_IsInitialized)
return;
m_IsCleanup = true;
// There should not be anything in here on server
if (GetGame() && GetGame().IsDedicatedServer())
{
if (m_ParamsMap.Count() > 0)
ErrorEx(string.Format("SEffectManager containing SoundParams on server."), ErrorExSeverity.WARNING);
if (m_EffectsMap.Count() > 0)
ErrorEx(string.Format("SEffectManager containing Effect on server."), ErrorExSeverity.WARNING);
}
// These are intentionally cached, just clear them
m_ParamsMap.Clear();
// These might not be intentionally still here, so log how many there are
#ifdef DEVELOPER
Print("--- SEffectManager Cleanup dump - Begin ------------------------");
Print(string.Format("Effect count: %1", m_EffectsMap.Count()));
#endif
// Best to call the unregister event before clearing the map
// In case some ref is still being held elsewhere and will still be kept alive
foreach (int id, Effect eff : m_EffectsMap)
{
eff.Event_OnUnregistered();
#ifdef SFXM_DUMP
Print(string.Format( "%1 :: %2 :: %3", eff, typename.EnumToString(EffectType, eff.GetEffectType()), eff.GetDebugName() ));
#endif
}
#ifdef DEVELOPER
Print("--- SEffectManager Cleanup dump - End --------------------------");
#endif
// Now we can clear it
m_EffectsMap.Clear();
// Reset the state
m_HighestFreeEffectID = 1;
Event_OnFrameUpdate.Clear();
m_IsCleanup = false;
}
//@}
} |