spotlight.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. class Spotlight extends ItemBase
  2. {
  3. private bool m_IsFolded;
  4. private bool m_EvaluateDeployment;
  5. SpotlightLight m_Light;
  6. static vector m_LightLocalPosition;
  7. static vector m_LightLocalOrientation = "0 0 0";
  8. // Spotlight can be extended and compressed
  9. static const string SEL_REFLECTOR_COMP_U = "reflector";
  10. static const string SEL_CORD_FOLDED_U = "cord_folded";
  11. static const string SEL_CORD_PLUGGED_U = "cord_plugged";
  12. static const string SEL_CORD_PLUGGED_F = "spotlight_folded_cord_plugged";
  13. static const string SEL_CORD_FOLDED_F = "spotlight_folded_cord_folded";
  14. static const string SEL_INVENTORY = "inventory";
  15. static const string SEL_PLACING = "placing";
  16. static const string SEL_GLASS_F = "glass_folded";
  17. static const string SEL_GLASS_U = "glass_unfolded";
  18. static const string SEL_REFLECTOR_F = "reflector_folded";
  19. static const string SEL_REFLECTOR_U = "reflector_unfolded";
  20. static const int ID_GLASS_UNFOLDED = 3;
  21. static const int ID_REFLECTOR_UNFOLDED = 4;
  22. static const int ID_GLASS_FOLDED = 5;
  23. static const int ID_REFLECTOR_FOLDED = 6;
  24. static string LIGHT_OFF_GLASS = "dz\\gear\\camping\\Data\\spotlight_glass.rvmat";
  25. static string LIGHT_OFF_REFLECTOR = "dz\\gear\\camping\\Data\\spotlight.rvmat";
  26. static string LIGHT_ON_GLASS = "dz\\gear\\camping\\Data\\spotlight_glass_on.rvmat";
  27. static string LIGHT_ON_REFLECTOR = "dz\\gear\\camping\\Data\\spotlight_glass_on.rvmat";
  28. //sound
  29. const string SOUND_TURN_ON = "spotlight_turn_on_SoundSet";
  30. const string SOUND_TURN_OFF = "spotlight_turn_off_SoundSet";
  31. protected EffectSound m_SoundTurnOn;
  32. protected EffectSound m_SoundTurnOff;
  33. //Spotlight, folded and unfolded
  34. void Spotlight()
  35. {
  36. m_EvaluateDeployment = false;
  37. RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
  38. RegisterNetSyncVariableBool("m_IsDeploySound");
  39. RegisterNetSyncVariableBool("m_IsFolded");
  40. }
  41. override bool IsElectricAppliance()
  42. {
  43. return true;
  44. }
  45. override void OnInitEnergy()
  46. {
  47. if (GetCompEM().IsPlugged())
  48. {
  49. if (!GetHierarchyRoot())
  50. Unfold();
  51. else
  52. Fold(true);
  53. }
  54. else
  55. Fold();
  56. UpdateAllSelections();
  57. }
  58. override void EOnInit(IEntity other, int extra)
  59. {
  60. if (!IsHologram())
  61. {
  62. UpdateAllSelections();
  63. HideSelection(SEL_CORD_FOLDED_F);
  64. }
  65. else
  66. {
  67. Unfold();
  68. }
  69. }
  70. //--- POWER EVENTS
  71. override void OnSwitchOn()
  72. {
  73. super.OnSwitchOn();
  74. SoundTurnOn();
  75. }
  76. override void OnSwitchOff()
  77. {
  78. super.OnSwitchOff();
  79. SoundTurnOff();
  80. if (m_Light)
  81. {
  82. m_Light.FadeOut(0.05);
  83. m_Light = null;
  84. }
  85. }
  86. override void OnWorkStart()
  87. {
  88. #ifndef SERVER
  89. m_Light = SpotlightLight.Cast(ScriptedLightBase.CreateLight(SpotlightLight, "0 0 0"));
  90. m_Light.AttachOnObject(this, m_LightLocalPosition, m_LightLocalOrientation);
  91. #endif
  92. UpdateAllSelections();
  93. }
  94. override void OnWorkStop()
  95. {
  96. #ifndef SERVER
  97. if (m_Light)
  98. m_Light.FadeOut();
  99. #endif
  100. UpdateAllSelections();
  101. }
  102. // Called when this device is picked up
  103. override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
  104. {
  105. super.OnItemLocationChanged(old_owner, new_owner);
  106. //skips folding of currently deployed spotlight
  107. if (m_EvaluateDeployment || IsBeingPlaced())
  108. {
  109. m_EvaluateDeployment = false;
  110. if (m_Light)
  111. {
  112. m_Light.AttachOnObject(this, m_LightLocalPosition, m_LightLocalOrientation);
  113. }
  114. Unfold();
  115. return;
  116. }
  117. // When the item is picked up by a player
  118. PlayerBase player = PlayerBase.Cast(new_owner);
  119. if (player && player.GetItemInHands() == this)
  120. {
  121. Fold(GetCompEM().IsPlugged());
  122. }
  123. }
  124. override void OnIsPlugged(EntityAI source_device)
  125. {
  126. Unfold();
  127. UpdateAllSelections();
  128. }
  129. protected void UpdateAllSelections()
  130. {
  131. bool isPlugged = GetCompEM().IsPlugged();
  132. PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
  133. bool isInHands = player && player.GetItemInHands() == this;
  134. HideAllSelections();
  135. m_LightLocalPosition = GetSelectionPositionLS("beamStart");
  136. if (IsFolded() || isInHands)
  137. {
  138. HideSelection(SEL_REFLECTOR_COMP_U);
  139. ShowSelection(SEL_INVENTORY);
  140. ShowSelection(SEL_GLASS_F);
  141. ShowSelection(SEL_REFLECTOR_F);
  142. if (isPlugged)
  143. {
  144. ShowSelection(SEL_CORD_PLUGGED_F);
  145. }
  146. else
  147. {
  148. ShowSelection(SEL_CORD_FOLDED_F);
  149. }
  150. if (!IsHologram())
  151. {
  152. if (GetCompEM().IsWorking())
  153. {
  154. SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_ON_GLASS);
  155. SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_ON_REFLECTOR);
  156. SetObjectMaterial(ID_GLASS_FOLDED, LIGHT_ON_GLASS);
  157. SetObjectMaterial(ID_REFLECTOR_FOLDED, LIGHT_ON_REFLECTOR);
  158. }
  159. else
  160. {
  161. SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_OFF_GLASS);
  162. SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_OFF_REFLECTOR);
  163. SetObjectMaterial(ID_GLASS_FOLDED, LIGHT_OFF_GLASS);
  164. SetObjectMaterial(ID_REFLECTOR_FOLDED, LIGHT_OFF_REFLECTOR);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. ShowSelection(SEL_PLACING);
  171. ShowSelection(SEL_REFLECTOR_U);
  172. ShowSelection(SEL_GLASS_U);
  173. ShowSelection(SEL_REFLECTOR_COMP_U);
  174. if (isPlugged)
  175. {
  176. ShowSelection(SEL_CORD_PLUGGED_U);
  177. }
  178. else
  179. {
  180. ShowSelection(SEL_CORD_FOLDED_U);
  181. }
  182. if (!IsHologram())
  183. {
  184. if (GetCompEM().IsWorking())
  185. {
  186. SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_ON_GLASS);
  187. SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_ON_REFLECTOR);
  188. }
  189. else
  190. {
  191. SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_OFF_GLASS);
  192. SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_OFF_REFLECTOR);
  193. }
  194. }
  195. }
  196. }
  197. override void OnIsUnplugged(EntityAI last_energy_source)
  198. {
  199. Fold();
  200. UpdateAllSelections();
  201. }
  202. override void OnVariablesSynchronized()
  203. {
  204. super.OnVariablesSynchronized();
  205. if (IsDeploySound())
  206. {
  207. PlayDeploySound();
  208. }
  209. }
  210. void Fold(bool keep_connected = false)
  211. {
  212. m_IsFolded = true;
  213. if (!keep_connected)
  214. {
  215. GetCompEM().SwitchOff();
  216. GetCompEM().UnplugThis();
  217. }
  218. UpdateAllSelections();
  219. SetSynchDirty();
  220. }
  221. void Unfold()
  222. {
  223. m_IsFolded = false;
  224. UpdateAllSelections();
  225. SetSynchDirty();
  226. }
  227. override void OnStoreSave(ParamsWriteContext ctx)
  228. {
  229. super.OnStoreSave(ctx);
  230. // Save folded/unfolded state
  231. ctx.Write(m_IsFolded);
  232. }
  233. override bool OnStoreLoad(ParamsReadContext ctx, int version)
  234. {
  235. if (!super.OnStoreLoad(ctx, version))
  236. return false;
  237. // Load folded/unfolded state
  238. bool b_is_folded = false;
  239. if (!ctx.Read(b_is_folded))
  240. b_is_folded = true;
  241. if (b_is_folded)
  242. {
  243. Fold();
  244. }
  245. else
  246. {
  247. Unfold();
  248. }
  249. return true;
  250. }
  251. override bool CanPutInCargo(EntityAI parent)
  252. {
  253. if (!super.CanPutInCargo(parent))
  254. return false;
  255. return !GetCompEM().IsPlugged();
  256. }
  257. override bool CanPutIntoHands(EntityAI player)
  258. {
  259. if (!super.CanPutIntoHands(parent))
  260. {
  261. return false;
  262. }
  263. // Commented out so Reposition action is possible to execute
  264. return true;
  265. }
  266. bool IsFolded()
  267. {
  268. return m_IsFolded;
  269. }
  270. //================================================================
  271. // SOUNDS
  272. //================================================================
  273. protected void SoundTurnOn()
  274. {
  275. PlaySoundSet(m_SoundTurnOn, SOUND_TURN_ON, 0, 0);
  276. }
  277. protected void SoundTurnOff()
  278. {
  279. PlaySoundSet(m_SoundTurnOff, SOUND_TURN_OFF, 0, 0);
  280. }
  281. //================================================================
  282. // ADVANCED PLACEMENT
  283. //================================================================
  284. override void OnPlacementStarted(Man player)
  285. {
  286. super.OnPlacementStarted(player);
  287. array<string> selections = {
  288. SEL_CORD_PLUGGED_F,
  289. SEL_CORD_FOLDED_F
  290. };
  291. PlayerBase playerPB = PlayerBase.Cast(player);
  292. foreach (string selection : selections)
  293. {
  294. if (GetGame().IsMultiplayer() && GetGame().IsServer())
  295. playerPB.GetHologramServer().SetSelectionToRefresh(selection);
  296. else
  297. playerPB.GetHologramLocal().SetSelectionToRefresh(selection);
  298. }
  299. }
  300. override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
  301. {
  302. super.OnPlacementComplete(player, position, orientation);
  303. if (GetGame().IsServer())
  304. {
  305. SetIsDeploySound(true);
  306. }
  307. Unfold();
  308. m_EvaluateDeployment = true;
  309. }
  310. override void OnPlacementCancelled(Man player)
  311. {
  312. super.OnPlacementCancelled(player);
  313. Fold(true);
  314. }
  315. override bool IsDeployable()
  316. {
  317. return true;
  318. }
  319. override string GetDeploySoundset()
  320. {
  321. return "placeSpotlight_SoundSet";
  322. }
  323. override string GetLoopDeploySoundset()
  324. {
  325. return "spotlight_deploy_SoundSet";
  326. }
  327. override int GetViewIndex()
  328. {
  329. if (MemoryPointExists("invView2") && !m_IsFolded)
  330. {
  331. return 1;
  332. }
  333. return 0;
  334. }
  335. override void SetActions()
  336. {
  337. super.SetActions();
  338. RemoveAction(ActionTakeItemToHands);
  339. AddAction(ActionClapBearTrapWithThisItem);
  340. AddAction(ActionPlugIn);
  341. AddAction(ActionTogglePlaceObject);
  342. AddAction(ActionUnplugThisByCord);
  343. AddAction(ActionTurnOnSpotlight);
  344. AddAction(ActionTurnOffSpotlight);
  345. AddAction(ActionRepositionPluggedItem);
  346. AddAction(ActionDeployObject);
  347. AddAction(ActionTakeItemToHands);
  348. }
  349. //!DEPRECATED
  350. protected ref EffectSound m_DeployLoopSound; //DEPRECATED in favor of m_DeployLoopSoundEx
  351. void PlayDeployLoopSound(); //!DEPRECATED
  352. void StopDeployLoopSound(); //!DEPRECATED
  353. }