ammoeffects.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. \brief Static data holder for certain ammo config values
  3. \note Kept the names similar to what is in config, but it might be a little deceiving as this is mainly used for explosives
  4. */
  5. class AmmoEffects
  6. {
  7. //! Key: Ammo class name; Data: ParticleList ID
  8. static ref map<string, int> m_AmmoParticles;
  9. //! Key: Ammo class name; Data: ParticleList ID
  10. static ref map<string, typename> m_AmmoEffects;
  11. /** \name Ammo particles
  12. Methods regarding getting/playing ammo particle
  13. */
  14. //@{
  15. //! Get the ParticleList ID for the particle for this ammoType
  16. static int GetAmmoParticleID(string ammoType)
  17. {
  18. int particleID;
  19. // Search for it in the static map
  20. if ( !m_AmmoParticles.Find(ammoType, particleID) )
  21. {
  22. // Load it in when we can't find it
  23. string particleFileName;
  24. GetGame().ConfigGetText(string.Format("cfgAmmo %1 particle", ammoType), particleFileName);
  25. // If we found a valid entry, try looking for it in ParticleList
  26. if ( particleFileName != "" )
  27. {
  28. particleID = ParticleList.GetParticleIDByName(particleFileName);
  29. }
  30. // Store it for next search
  31. m_AmmoParticles.Insert(ammoType, particleID);
  32. }
  33. return particleID;
  34. }
  35. //! Attempt to play the ammo particle at pos if found, returns true on success
  36. static bool PlayAmmoParticle(string ammoType, vector pos)
  37. {
  38. int particleID = GetAmmoParticleID(ammoType);
  39. if (ParticleList.IsValidId(particleID))
  40. {
  41. return ParticleManager.GetInstance().PlayInWorld(particleID, pos) != null;
  42. }
  43. return false;
  44. }
  45. //@}
  46. /** \name Ammo effects
  47. Methods regarding getting/playing ammo effect
  48. */
  49. //@{
  50. //! Get the typename for the effect for this ammoType
  51. static typename GetAmmoEffectTypename(string ammoType)
  52. {
  53. typename typeName;
  54. // Search for it in the static map
  55. if ( !m_AmmoEffects.Find(ammoType, typeName) )
  56. {
  57. // Load it in when we can't find it
  58. string effectName;
  59. GetGame().ConfigGetText(string.Format("cfgAmmo %1 effect", ammoType), effectName);
  60. // If we found a valid entry, try looking for it in ParticleList
  61. if ( effectName != "" )
  62. {
  63. typeName = effectName.ToType();
  64. }
  65. // Store it for next search
  66. m_AmmoEffects.Insert(ammoType, typeName);
  67. }
  68. return typeName;
  69. }
  70. //! Attempt to play the ammo effect at pos if found, returns true on success
  71. static bool PlayAmmoEffect(string ammoType, vector pos)
  72. {
  73. typename typeName = GetAmmoEffectTypename(ammoType);
  74. if ( typeName )
  75. {
  76. Effect eff = Effect.Cast(typeName.Spawn());
  77. if ( eff )
  78. {
  79. eff.SetAutodestroy(true);
  80. return SEffectManager.PlayInWorld( eff, pos );
  81. }
  82. }
  83. return false;
  84. }
  85. //@}
  86. /** \name Lifetime
  87. Creation and cleanup
  88. */
  89. //@{
  90. //! Initialize the containers: this is done this way, to have these not exist on server
  91. static void Init()
  92. {
  93. m_AmmoParticles = new map<string, int>();
  94. m_AmmoEffects = new map<string, typename>();
  95. }
  96. //! Clean up the data
  97. static void Cleanup()
  98. {
  99. /* These ain't containing no refs, so whatever
  100. m_AmmoParticles.Clear();
  101. m_AmmoEffects.Clear();
  102. */
  103. }
  104. //@}
  105. }