Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
spotlight.c
Go to the documentation of this file.
1 class Spotlight extends ItemBase
2 {
3  private bool m_IsFolded;
4  private bool m_EvaluateDeployment;
5  SpotlightLight m_Light;
6 
7  static vector m_LightLocalPosition;
8  static vector m_LightLocalOrientation = "0 0 0";
9 
10  // Spotlight can be extended and compressed
11  static const string SEL_REFLECTOR_COMP_U = "reflector";
12  static const string SEL_CORD_FOLDED_U = "cord_folded";
13  static const string SEL_CORD_PLUGGED_U = "cord_plugged";
14  static const string SEL_CORD_PLUGGED_F = "spotlight_folded_cord_plugged";
15  static const string SEL_CORD_FOLDED_F = "spotlight_folded_cord_folded";
16 
17  static const string SEL_INVENTORY = "inventory";
18  static const string SEL_PLACING = "placing";
19  static const string SEL_GLASS_F = "glass_folded";
20  static const string SEL_GLASS_U = "glass_unfolded";
21  static const string SEL_REFLECTOR_F = "reflector_folded";
22  static const string SEL_REFLECTOR_U = "reflector_unfolded";
23 
24  static const int ID_GLASS_UNFOLDED = 3;
25  static const int ID_REFLECTOR_UNFOLDED = 4;
26  static const int ID_GLASS_FOLDED = 5;
27  static const int ID_REFLECTOR_FOLDED = 6;
28 
29  static string LIGHT_OFF_GLASS = "dz\\gear\\camping\\Data\\spotlight_glass.rvmat";
30  static string LIGHT_OFF_REFLECTOR = "dz\\gear\\camping\\Data\\spotlight.rvmat";
31  static string LIGHT_ON_GLASS = "dz\\gear\\camping\\Data\\spotlight_glass_on.rvmat";
32  static string LIGHT_ON_REFLECTOR = "dz\\gear\\camping\\Data\\spotlight_glass_on.rvmat";
33 
34  //sound
35  const string SOUND_TURN_ON = "spotlight_turn_on_SoundSet";
36  const string SOUND_TURN_OFF = "spotlight_turn_off_SoundSet";
37 
38  protected EffectSound m_SoundTurnOn;
39  protected EffectSound m_SoundTurnOff;
41 
42  //Spotlight, folded and unfolded
43  void Spotlight()
44  {
45  m_EvaluateDeployment = false;
46 
47  RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
48  RegisterNetSyncVariableBool("m_IsDeploySound");
49  RegisterNetSyncVariableBool("m_IsFolded");
50  }
51 
52  void ~Spotlight()
53  {
55  SEffectManager.DestroyEffect(m_DeployLoopSound);
56  }
57 
58  override bool IsElectricAppliance()
59  {
60  return true;
61  }
62 
63  override void OnInitEnergy()
64  {
65  if (GetCompEM().IsPlugged())
66  {
67  if (!GetHierarchyRoot())
68  Unfold();
69  else
70  Fold(true);
71  }
72  else
73  Fold();
74 
75  UpdateAllSelections();
76  }
77 
78  override void EOnInit(IEntity other, int extra)
79  {
80  if (!IsHologram())
81  {
82  UpdateAllSelections();
83  HideSelection(SEL_CORD_FOLDED_F);
84  }
85  else
86  {
87  Unfold();
88  }
89  }
90 
91  //--- POWER EVENTS
92  override void OnSwitchOn()
93  {
94  super.OnSwitchOn();
95 
96  SoundTurnOn();
97  }
98 
99  override void OnSwitchOff()
100  {
101  super.OnSwitchOff();
102 
103  SoundTurnOff();
104 
105  if (m_Light)
106  {
107  m_Light.FadeOut(0.05);
108  m_Light = null;
109  }
110  }
111 
112  override void OnWorkStart()
113  {
114  #ifndef SERVER
115  m_Light = SpotlightLight.Cast(ScriptedLightBase.CreateLight(SpotlightLight, "0 0 0"));
116  m_Light.AttachOnObject(this, m_LightLocalPosition, m_LightLocalOrientation);
117  #endif
118 
119  UpdateAllSelections();
120  }
121 
122  override void OnWorkStop()
123  {
124  #ifndef SERVER
125  if (m_Light)
126  m_Light.FadeOut();
127  #endif
128 
129  UpdateAllSelections();
130  }
131 
132  // Called when this device is picked up
133  override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
134  {
135  super.OnItemLocationChanged(old_owner, new_owner);
136 
137  //skips folding of currently deployed spotlight
138  if (m_EvaluateDeployment || IsBeingPlaced())
139  {
140  m_EvaluateDeployment = false;
141  if (m_Light)
142  {
143  m_Light.AttachOnObject(this, m_LightLocalPosition, m_LightLocalOrientation);
144  }
145 
146  Unfold();
147 
148  return;
149  }
150 
151  // When the item is picked up by a player
152  PlayerBase player = PlayerBase.Cast(new_owner);
153  if (player && player.GetItemInHands() == this)
154  {
155  Fold(GetCompEM().IsPlugged());
156  }
157  }
158 
159  override void OnIsPlugged(EntityAI source_device)
160  {
161  Unfold();
162  UpdateAllSelections();
163  }
164 
165  protected void UpdateAllSelections()
166  {
167  bool isPlugged = GetCompEM().IsPlugged();
168  PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
169  bool isInHands = player && player.GetItemInHands() == this;
170  HideAllSelections();
171 
172  m_LightLocalPosition = GetSelectionPositionLS("beamStart");
173 
174  if (IsFolded() || isInHands)
175  {
176  HideSelection(SEL_REFLECTOR_COMP_U);
177 
178  ShowSelection(SEL_INVENTORY);
179  ShowSelection(SEL_GLASS_F);
180  ShowSelection(SEL_REFLECTOR_F);
181 
182  if (isPlugged)
183  {
184  ShowSelection(SEL_CORD_PLUGGED_F);
185  }
186  else
187  {
188  ShowSelection(SEL_CORD_FOLDED_F);
189  }
190 
191  if (!IsHologram())
192  {
193  if (GetCompEM().IsWorking())
194  {
195  SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_ON_GLASS);
196  SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_ON_REFLECTOR);
197  SetObjectMaterial(ID_GLASS_FOLDED, LIGHT_ON_GLASS);
198  SetObjectMaterial(ID_REFLECTOR_FOLDED, LIGHT_ON_REFLECTOR);
199  }
200  else
201  {
202  SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_OFF_GLASS);
203  SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_OFF_REFLECTOR);
204  SetObjectMaterial(ID_GLASS_FOLDED, LIGHT_OFF_GLASS);
205  SetObjectMaterial(ID_REFLECTOR_FOLDED, LIGHT_OFF_REFLECTOR);
206  }
207  }
208  }
209  else
210  {
211  ShowSelection(SEL_PLACING);
212  ShowSelection(SEL_REFLECTOR_U);
213  ShowSelection(SEL_GLASS_U);
214  ShowSelection(SEL_REFLECTOR_COMP_U);
215 
216  if (isPlugged)
217  {
218  ShowSelection(SEL_CORD_PLUGGED_U);
219  }
220  else
221  {
222  ShowSelection(SEL_CORD_FOLDED_U);
223  }
224 
225  if (!IsHologram())
226  {
227  if (GetCompEM().IsWorking())
228  {
229  SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_ON_GLASS);
230  SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_ON_REFLECTOR);
231  }
232  else
233  {
234  SetObjectMaterial(ID_GLASS_UNFOLDED, LIGHT_OFF_GLASS);
235  SetObjectMaterial(ID_REFLECTOR_UNFOLDED, LIGHT_OFF_REFLECTOR);
236  }
237  }
238  }
239  }
240 
241  override void OnIsUnplugged(EntityAI last_energy_source)
242  {
243  Fold();
244  UpdateAllSelections();
245  }
246 
247  override void OnVariablesSynchronized()
248  {
249  super.OnVariablesSynchronized();
250 
251  if (IsDeploySound())
252  {
253  PlayDeploySound();
254  }
255 
257  {
259  }
260 
262  {
264  }
265  }
266 
267  void Fold(bool keep_connected = false)
268  {
269  m_IsFolded = true;
270  if (!keep_connected)
271  {
272  GetCompEM().SwitchOff();
273  GetCompEM().UnplugThis();
274  }
275 
276  UpdateAllSelections();
277  SetSynchDirty();
278  }
279 
280  void Unfold()
281  {
282  m_IsFolded = false;
283  UpdateAllSelections();
284  SetSynchDirty();
285  }
286 
287  override void OnStoreSave(ParamsWriteContext ctx)
288  {
289  super.OnStoreSave(ctx);
290 
291  // Save folded/unfolded state
292  ctx.Write(m_IsFolded);
293  }
294 
295  override bool OnStoreLoad(ParamsReadContext ctx, int version)
296  {
297  if (!super.OnStoreLoad(ctx, version))
298  return false;
299 
300  // Load folded/unfolded state
301  bool b_is_folded = false;
302  if (!ctx.Read(b_is_folded))
303  b_is_folded = true;
304 
305  if (b_is_folded)
306  {
307  Fold();
308  }
309  else
310  {
311  Unfold();
312  }
313 
314  return true;
315  }
316 
317  override bool CanPutInCargo(EntityAI parent)
318  {
319  if (!super.CanPutInCargo(parent))
320  return false;
321 
322  return !GetCompEM().IsPlugged();
323  }
324 
325  override bool CanPutIntoHands(EntityAI player)
326  {
327  if (!super.CanPutIntoHands(parent))
328  {
329  return false;
330  }
331  // Commented out so Reposition action is possible to execute
332  return true;
333  }
334 
335  bool IsFolded()
336  {
337  return m_IsFolded;
338  }
339 
340  //================================================================
341  // SOUNDS
342  //================================================================
343  protected void SoundTurnOn()
344  {
345  PlaySoundSet(m_SoundTurnOn, SOUND_TURN_ON, 0, 0);
346  }
347 
348  protected void SoundTurnOff()
349  {
350  PlaySoundSet(m_SoundTurnOff, SOUND_TURN_OFF, 0, 0);
351  }
352 
353  //================================================================
354  // ADVANCED PLACEMENT
355  //================================================================
356  override void OnPlacementStarted(Man player)
357  {
358  super.OnPlacementStarted(player);
359 
360  array<string> selections = {
361  SEL_CORD_PLUGGED_F,
362  SEL_CORD_FOLDED_F
363  };
364 
365  PlayerBase playerPB = PlayerBase.Cast(player);
366  foreach (string selection : selections)
367  {
368  if (GetGame().IsMultiplayer() && GetGame().IsServer())
369  playerPB.GetHologramServer().SetSelectionToRefresh(selection);
370  else
371  playerPB.GetHologramLocal().SetSelectionToRefresh(selection);
372  }
373  }
374 
375  override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
376  {
377  super.OnPlacementComplete(player, position, orientation);
378 
379  if (GetGame().IsServer())
380  {
381  SetIsDeploySound(true);
382  }
383 
384  Unfold();
385  m_EvaluateDeployment = true;
386  }
387 
388  override void OnPlacementCancelled(Man player)
389  {
390  super.OnPlacementCancelled(player);
391 
392  Fold(true);
393  }
394 
395  override bool IsDeployable()
396  {
397  return true;
398  }
399 
400  override string GetDeploySoundset()
401  {
402  return "placeSpotlight_SoundSet";
403  }
404 
405  override string GetLoopDeploySoundset()
406  {
407  return "spotlight_deploy_SoundSet";
408  }
409 
410  void PlayDeployLoopSound()
411  {
412  if (!GetGame().IsDedicatedServer() && !m_DeployLoopSound)
413  {
415  m_DeployLoopSound.SetAutodestroy(true);
416  }
417  }
418 
419  void StopDeployLoopSound()
420  {
421  if (!GetGame().IsDedicatedServer())
422  {
423  m_DeployLoopSound.SetSoundFadeOut(0.5);
424  m_DeployLoopSound.SoundStop();
425  }
426  }
427 
428  override int GetViewIndex()
429  {
430  if (MemoryPointExists("invView2") && !m_IsFolded)
431  {
432  return 1;
433  }
434 
435  return 0;
436  }
437 
438  override void SetActions()
439  {
440  super.SetActions();
441 
443 
453  }
454 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
OnPlacementStarted
override void OnPlacementStarted(Man player)
Definition: itembase.c:3804
ActionPlugIn
Definition: actionplugin.c:1
ActionTakeItemToHands
Definition: actiontakeitemtohands.c:1
ActionTurnOffSpotlight
Definition: actionturnoffspotlight.c:1
ActionDeployObject
PlaceObjectActionReciveData ActionReciveData ActionDeployObject()
Definition: actiondeployobject.c:9
OnWorkStop
override void OnWorkStop()
Definition: m18smokegrenade_colorbase.c:2
SetIsDeploySound
void SetIsDeploySound(bool is_deploy_sound)
Definition: itembase.c:4293
RemoveAction
void RemoveAction(typename actionName)
Definition: advancedcommunication.c:118
OnVariablesSynchronized
override void OnVariablesSynchronized()
Definition: anniversarymusicsource.c:42
m_Light
protected ExplosiveLight m_Light
light
Definition: explosivesbase.c:31
PlayDeploySound
void PlayDeploySound()
Definition: itembase.c:4324
PlayDeployLoopSound
void PlayDeployLoopSound()
EOnInit
protected override void EOnInit(IEntity other, int extra)
Definition: testframework.c:235
OnPlacementComplete
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
Definition: explosivesbase.c:133
ActionClapBearTrapWithThisItem
Definition: actionclapbeartrapwiththisitem.c:1
GetLoopDeploySoundset
override string GetLoopDeploySoundset()
Definition: largetent.c:151
IEntity
Definition: enentity.c:164
ActionTogglePlaceObject
Definition: actiontoggleplaceobject.c:1
IsDeploySound
bool IsDeploySound()
Definition: itembase.c:4298
ActionTurnOnSpotlight
Definition: actionturnonspotlight.c:1
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
GetPosition
class JsonUndergroundAreaTriggerData GetPosition
Definition: undergroundarealoader.c:9
EffectSound
Wrapper class for managing sound through SEffectManager.
Definition: effectsound.c:4
PlayerBase
Definition: playerbaseclient.c:1
CanPutIntoHands
override bool CanPutIntoHands(EntityAI parent)
Definition: explosivesbase.c:257
vector
Definition: enconvert.c:105
CanPutInCargo
override bool CanPutInCargo(EntityAI parent)
Definition: explosivesbase.c:247
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
ScriptedLightBase
Definition: pointlightbase.c:1
SetActions
void SetActions()
Definition: advancedcommunication.c:79
ActionUnplugThisByCord
Definition: actionunplugthisbycord.c:1
ActionRepositionPluggedItem
Definition: actionrepositionpluggeditem.c:1
array< string >
IsDeployable
override bool IsDeployable()
Definition: basebuildingbase.c:339
m_DeployLoopSound
protected ref EffectSound m_DeployLoopSound
Definition: trapbase.c:47
OnStoreSave
void OnStoreSave(ParamsWriteContext ctx)
Definition: modifierbase.c:229
IsBeingPlaced
override bool IsBeingPlaced()
Definition: itembase.c:946
IsHologram
override bool IsHologram()
Definition: itembase.c:962
StopDeployLoopSound
void StopDeployLoopSound()
CanPlayDeployLoopSound
bool CanPlayDeployLoopSound()
Definition: itembase.c:4360
OnItemLocationChanged
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition: combinationlock.c:75
SEffectManager
Manager class for managing Effect (EffectParticle, EffectSound)
Definition: effectmanager.c:5
OnStoreLoad
bool OnStoreLoad(ParamsReadContext ctx, int version)
Definition: modifiersmanager.c:270
GetDeploySoundset
override string GetDeploySoundset()
Definition: largetent.c:146
EntityAI
Definition: building.c:5
OnPlacementCancelled
override void OnPlacementCancelled(Man player)
Definition: trap_tripwire.c:361
OnWorkStart
override void OnWorkStart()
Definition: smokegrenadebase.c:175