universallight.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. class UniversalLight extends Switchable_Base
  2. {
  3. UniversallightLight m_Light;
  4. static int REFLECTOR_ID = 1;
  5. static int GLASS_ID = 2;
  6. static string LIGHT_OFF_GLASS = "dz\\gear\\tools\\data\\flashlight_glass.rvmat";
  7. static string LIGHT_OFF_REFLECTOR = "dz\\weapons\\attachments\\data\\m4_flashlight.rvmat";
  8. static string LIGHT_ON_GLASS = "dz\\gear\\tools\\data\\flashlight_glass_on.rvmat";
  9. static string LIGHT_ON_REFLECTOR = "dz\\weapons\\attachments\\data\\m4_flashlight_on.rvmat";
  10. ref array<int> m_AttachmentSlotsCheck;
  11. void UniversalLight()
  12. {
  13. InitAttachmentsSlotsToCheck(m_AttachmentSlotsCheck);
  14. }
  15. override ScriptedLightBase GetLight()
  16. {
  17. return m_Light;
  18. }
  19. override bool CanPutAsAttachment( EntityAI parent )
  20. {
  21. if ( !super.CanPutAsAttachment(parent) ) {return false;}
  22. bool req_attachment = false;
  23. bool rail_attachment_found = false;
  24. int slot_id;
  25. ItemBase attachment;
  26. for ( int i = 0; i < parent.GetInventory().GetAttachmentSlotsCount(); i++ )
  27. {
  28. slot_id = parent.GetInventory().GetAttachmentSlotId(i);
  29. if ( m_AttachmentSlotsCheck.Find(slot_id) != -1 )
  30. {
  31. req_attachment = true;
  32. attachment = ItemBase.Cast(parent.GetInventory().FindAttachment(slot_id));
  33. if ( attachment && attachment.ConfigIsExisting("hasRailFunctionality") && attachment.ConfigGetBool("hasRailFunctionality") )
  34. rail_attachment_found = true;
  35. }
  36. }
  37. return !req_attachment || (req_attachment && rail_attachment_found);
  38. }
  39. override void OnWorkStart()
  40. {
  41. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // Client side
  42. {
  43. m_Light = UniversallightLight.Cast( ScriptedLightBase.CreateLight(UniversallightLight, "0 0 0", 0.08) ); // Position is zero because light is attached on parent immediately.
  44. m_Light.AttachOnMemoryPoint(this, "beamStart", "beamEnd");
  45. SetObjectMaterial(GLASS_ID, LIGHT_ON_GLASS);
  46. SetObjectMaterial(REFLECTOR_ID, LIGHT_ON_REFLECTOR);
  47. }
  48. }
  49. override void OnWork( float consumed_energy )
  50. {
  51. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // Client side
  52. {
  53. Battery9V battery = Battery9V.Cast( GetCompEM().GetEnergySource() );
  54. if (battery && m_Light)
  55. {
  56. float efficiency = battery.GetEfficiency0To1();
  57. if ( efficiency < 1 )
  58. {
  59. m_Light.SetIntensity( efficiency, GetCompEM().GetUpdateInterval() );
  60. }
  61. else
  62. {
  63. m_Light.SetIntensity( 1, 0 );
  64. }
  65. }
  66. }
  67. }
  68. override void OnWorkStop()
  69. {
  70. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // Client side
  71. {
  72. if (m_Light)
  73. m_Light.FadeOut();
  74. m_Light = NULL;
  75. SetObjectMaterial(GLASS_ID, LIGHT_OFF_GLASS);
  76. SetObjectMaterial(REFLECTOR_ID, LIGHT_OFF_REFLECTOR);
  77. }
  78. }
  79. // Inventory manipulation
  80. override void OnInventoryExit(Man player)
  81. {
  82. super.OnInventoryExit(player);
  83. if ( GetCompEM().IsWorking() )
  84. {
  85. if (player)
  86. {
  87. vector ori_rotate = player.GetOrientation();
  88. ori_rotate = ori_rotate + Vector(270,0,0);
  89. SetOrientation(ori_rotate);
  90. }
  91. }
  92. }
  93. override void SetActions()
  94. {
  95. super.SetActions();
  96. AddAction(ActionTurnOnWhileInHands);
  97. AddAction(ActionTurnOffWhileInHands);
  98. }
  99. override bool IsLightSource()
  100. {
  101. return true;
  102. }
  103. //! Enter att slot types to check on attach
  104. void InitAttachmentsSlotsToCheck(out array<int> AttSlots)
  105. {
  106. if (!AttSlots)
  107. {
  108. AttSlots = new array<int>;
  109. AttSlots.Insert(InventorySlots.GetSlotIdFromString("weaponHandguardM4"));
  110. AttSlots.Insert(InventorySlots.GetSlotIdFromString("weaponHandguardAK"));
  111. AttSlots.Insert(InventorySlots.GetSlotIdFromString("weaponHandguardMP5"));
  112. }
  113. }
  114. }