nvgoggles.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. class NVGoggles extends PoweredOptic_Base
  2. {
  3. bool m_IsLowered;
  4. Clothing m_Strap;
  5. void NVGoggles()
  6. {
  7. RotateGoggles(true);
  8. }
  9. override void SetActions()
  10. {
  11. super.SetActions();
  12. RemoveAction(ActionViewOptics);
  13. AddAction(ActionViewBinoculars);
  14. }
  15. override void EEItemAttached(EntityAI item, string slot_name)
  16. {
  17. super.EEItemAttached(item, slot_name);
  18. if (GetCompEM().CanWork() && m_IsLowered)
  19. GetCompEM().SwitchOn();
  20. }
  21. override void EEItemDetached(EntityAI item, string slot_name)
  22. {
  23. super.EEItemDetached(item, slot_name);
  24. RotateGoggles(true);
  25. PlayerBase player;
  26. if (m_Strap && PlayerBase.CastTo(player, GetHierarchyRootPlayer()))
  27. m_Strap.UpdateNVGStatus(player, false, true);
  28. }
  29. override void OnWasAttached(EntityAI parent, int slot_id)
  30. {
  31. super.OnWasAttached(parent, slot_id);
  32. RotateGoggles(true);
  33. m_Strap = Clothing.Cast(parent);
  34. }
  35. override void OnWasDetached(EntityAI parent, int slot_id)
  36. {
  37. super.OnWasDetached(parent, slot_id);
  38. RotateGoggles(true);
  39. SetPlayer(null);
  40. PlayerBase player;
  41. if (m_Strap == parent && PlayerBase.CastTo(player, parent.GetHierarchyRootPlayer()))
  42. {
  43. if (parent && Clothing.Cast(parent))
  44. Clothing.Cast(parent).UpdateNVGStatus(player, false, true);
  45. }
  46. m_Strap = null;
  47. }
  48. override void OnWorkStart()
  49. {
  50. super.OnWorkStart();
  51. PlayerBase player;
  52. EntityAI headgear;
  53. EntityAI glasses;
  54. if (m_Strap && PlayerBase.CastTo(player, m_Strap.GetHierarchyParent()))
  55. {
  56. headgear = player.FindAttachmentBySlotName("Headgear");
  57. glasses = player.FindAttachmentBySlotName("Eyewear");
  58. //adjust on load - ComponentEnergyManager stores the 'working' state independently
  59. if (!m_IsLowered)
  60. RotateGoggles(false);
  61. if (!GetGame().IsServer() || !GetGame().IsMultiplayer())
  62. {
  63. if ((headgear == m_Strap || glasses == m_Strap))
  64. m_Strap.UpdateNVGStatus(player);
  65. }
  66. }
  67. }
  68. override void OnWorkStop()
  69. {
  70. super.OnWorkStop();
  71. PlayerBase player;
  72. EntityAI headgear;
  73. EntityAI glasses;
  74. if (m_Strap && PlayerBase.CastTo(player, m_Strap.GetHierarchyParent()))
  75. {
  76. headgear = player.FindAttachmentBySlotName("Headgear");
  77. glasses = player.FindAttachmentBySlotName("Eyewear");
  78. if (!GetGame().IsServer() || !GetGame().IsMultiplayer())
  79. {
  80. if ((headgear == m_Strap || glasses == m_Strap))
  81. m_Strap.UpdateNVGStatus(player);
  82. }
  83. }
  84. }
  85. override void OnWork(float consumed_energy)
  86. {
  87. //adjust on load - ComponentEnergyManager stores the 'working' state independently
  88. if (!m_IsLowered)
  89. RotateGoggles(false);
  90. PlayerBase player;
  91. EntityAI headgear;
  92. EntityAI glasses;
  93. if (m_Strap && PlayerBase.CastTo(player, m_Strap.GetHierarchyParent()))
  94. {
  95. headgear = player.FindAttachmentBySlotName("Headgear");
  96. glasses = player.FindAttachmentBySlotName("Eyewear");
  97. if (!GetGame().IsServer() || !GetGame().IsMultiplayer()) // Client side
  98. {
  99. if ((headgear == m_Strap || glasses == m_Strap) && player.IsControlledPlayer())
  100. player.AddActiveNV(GetCurrentNVType());
  101. }
  102. }
  103. }
  104. void RotateGoggles(bool state)
  105. {
  106. if (!m_Strap && !state) // disable non default rotation while not strapped
  107. return;
  108. SetAnimationPhase("rotate", !state);
  109. m_IsLowered = !state;
  110. PlayerBase player;
  111. int slotId;
  112. string slotName;
  113. if (m_Strap && m_Strap.GetInventory().GetCurrentAttachmentSlotInfo(slotId, slotName) && PlayerBase.CastTo(player, m_Strap.GetHierarchyParent()))
  114. m_Strap.UpdateNVGStatus(player);
  115. if (GetCompEM())
  116. {
  117. if (!state && GetCompEM().CanWork())
  118. GetCompEM().SwitchOn();
  119. else
  120. GetCompEM().SwitchOff();
  121. }
  122. }
  123. override int GetCurrentNVType()
  124. {
  125. if (IsWorking())
  126. {
  127. if (IsUsingOptics2DModel())
  128. return NVTypes.NV_GOGGLES_2D;
  129. return NVTypes.NV_GOGGLES; //omits the widget occluder
  130. }
  131. else
  132. {
  133. return NVTypes.NV_GOGGLES_OFF;
  134. }
  135. }
  136. override bool IsNVG()
  137. {
  138. return true;
  139. }
  140. //!
  141. //! DEPRECATED
  142. //!
  143. ref Timer m_WorkCheckTimer = new Timer();
  144. //! check for animation state, if another player lowered them first (or solve by synced variable)
  145. void LoweredCheck()
  146. {
  147. if (GetAnimationPhase("rotate") != m_IsLowered)
  148. {
  149. m_IsLowered = GetAnimationPhase("rotate");
  150. }
  151. }
  152. void SwitchOnNVGCheck()
  153. {
  154. GetCompEM().SwitchOn();
  155. if (GetCompEM().IsSwitchedOn())
  156. m_WorkCheckTimer.Stop();
  157. }
  158. }