underobjectdecalspawncomponent.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. class UnderObjectDecalSpawnSettings
  2. {
  3. bool m_RandomizeRotation = true;
  4. float m_ScaleMin = 0.1;
  5. float m_ScaleMax = 1.0;
  6. vector m_PositionOffset = vector.Zero;
  7. }
  8. class UnderObjectDecalSpawnComponent
  9. {
  10. private const string SURFACE_PARAM_DECAL_NAME = "underObjectDecal";
  11. protected ref UnderObjectDecalSpawnSettings m_Settings;
  12. protected Object m_Parent;
  13. protected Object m_Decal;
  14. private float m_LastScaleValue;
  15. void UnderObjectDecalSpawnComponent(notnull UnderObjectDecalSpawnSettings pSettings, notnull Object pParent)
  16. {
  17. m_Settings = pSettings;
  18. m_Parent = pParent;
  19. }
  20. private string GetObjectNameFromSurfaceConfig(string surfaceParamName = SURFACE_PARAM_DECAL_NAME)
  21. {
  22. string surfaceType
  23. int liquidType;
  24. g_Game.SurfaceUnderObjectCorrectedLiquid(m_Parent, surfaceType, liquidType);
  25. return Surface.GetParamText(surfaceType, surfaceParamName);
  26. }
  27. void SpawnDecal()
  28. {
  29. if (m_Decal)
  30. return;
  31. #ifndef SERVER
  32. m_Decal = g_Game.CreateObjectEx(
  33. GetObjectNameFromSurfaceConfig(),
  34. m_Parent.GetPosition() + m_Settings.m_PositionOffset,
  35. ECE_LOCAL|ECE_PLACE_ON_SURFACE,
  36. );
  37. if (m_Decal)
  38. {
  39. if (m_Settings.m_RandomizeRotation)
  40. {
  41. vector v;
  42. v[0] = Math.RandomFloat(-Math.PI, Math.PI);
  43. m_Decal.SetOrientation(v * Math.RAD2DEG);
  44. }
  45. m_Decal.SetScale(m_Settings.m_ScaleMin);
  46. }
  47. #endif
  48. }
  49. void RemoveDecal()
  50. {
  51. #ifndef SERVER
  52. if (m_Decal)
  53. m_Decal.Delete();
  54. #endif
  55. }
  56. void UpdateSize(float pScaleValue)
  57. {
  58. #ifndef SERVER
  59. if (m_Decal)
  60. {
  61. if (pScaleValue != m_LastScaleValue)
  62. {
  63. m_LastScaleValue = m_Decal.GetScale();
  64. m_Decal.SetScale(Math.Clamp(pScaleValue, m_Settings.m_ScaleMin, m_Settings.m_ScaleMax));
  65. }
  66. }
  67. #endif
  68. }
  69. }