Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
headtorch_colorbase.c
Go to the documentation of this file.
1 class Headtorch_ColorBase extends Clothing
2 {
3  HeadtorchLight m_Light;
4 
5  static int REFLECTOR_ID = 4;
6  static int GLASS_ID = 5;
7 
8  static string LIGHT_OFF_GLASS = "dz\\characters\\headgear\\data\\HeadTorchGlass.rvmat";
9  static string LIGHT_OFF_REFLECTOR = "dz\\characters\\headgear\\data\\HeadTorch.rvmat";
10  static string LIGHT_ON_GLASS = "dz\\characters\\headgear\\data\\HeadTorchGlass_on.rvmat";
11  static string LIGHT_ON_GLASS_RED = "dz\\characters\\headgear\\data\\HeadTorchGlass_on_red.rvmat";
12  static string LIGHT_ON_REFLECTOR = "dz\\characters\\headgear\\data\\HeadTorch_ON.rvmat";
13  static string LIGHT_ON_REFLECTOR_RED = "dz\\characters\\headgear\\data\\HeadTorch_ON_red.rvmat";
14 
15  static vector m_OnHeadLocalPos = Vector(0.12,0.15,0);
16  static vector m_OnHeadLocalOri = Vector(0,90,0);
17  static string m_OffHeadLightPoint = "beamStart";
18  static string m_OffHeadLightTarget = "beamEnd";
19 
20  ref Timer m_Timer;
21 
22  void Headtorch_ColorBase()
23  {
24  if (GetGame().IsServer())
25  {
26  if (!m_Timer)
28 
29  m_Timer.Run(1 , this, "CheckParent", NULL, false);
30  }
31  }
32 
33  override protected void InitGlobalExclusionValues()
34  {
35  super.InitGlobalExclusionValues();
36  AddSingleExclusionValueGlobal(EAttExclusions.EXCLUSION_HEADSTRAP_0);
37  }
38 
39  void CheckParent()
40  {
41  EntityAI owner = GetHierarchyParent();
42 
43  // Spawn a battery in the headtorch if it's attached on a zombie and switch it on
44  if (owner && owner.IsZombie())
45  {
46  GetInventory().CreateAttachment("Battery9V");
47  GetCompEM().SwitchOn();
48  }
49 
50  if (m_Timer)
51  m_Timer.Stop();
52 
53  m_Timer = null;
54  }
55 
56  override void OnWorkStart()
57  {
58  super.OnWorkStart();
59 
60  if (!GetGame().IsServer() || !GetGame().IsMultiplayer()) // Client side
61  {
62  CreateHeadtorchLight();
63  }
64 
65  if (IsInherited(Headtorch_Black))
66  {
67  SetObjectMaterial(GLASS_ID, LIGHT_ON_GLASS_RED);
68  SetObjectMaterial(REFLECTOR_ID, LIGHT_ON_REFLECTOR_RED);
69  }
70  else
71  {
72  SetObjectMaterial(GLASS_ID, LIGHT_ON_GLASS);
73  SetObjectMaterial(REFLECTOR_ID, LIGHT_ON_REFLECTOR);
74  }
75  }
76 
77  override void OnWorkStop()
78  {
79  super.OnWorkStop();
80 
81  if (!GetGame().IsServer() || !GetGame().IsMultiplayer()) // Client side
82  {
83  if (m_Light)
84  m_Light.FadeOut();
85 
86  m_Light = NULL;
87  }
88 
89  SetObjectMaterial(GLASS_ID, LIGHT_OFF_GLASS);
90  SetObjectMaterial(REFLECTOR_ID, LIGHT_OFF_REFLECTOR);
91  }
92 
93  override void OnWork(float consumed_energy)
94  {
95  super.OnWork(consumed_energy);
96 
97  if (!GetGame().IsServer() || !GetGame().IsMultiplayer()) // Client side
98  {
99  Battery9V battery = Battery9V.Cast(GetCompEM().GetEnergySource());
100 
101  if (battery && m_Light)
102  {
103  float efficiency = battery.GetEfficiency0To1();
104 
105  if (efficiency < 1)
106  {
107  m_Light.SetIntensity(efficiency, GetCompEM().GetUpdateInterval());
108  }
109  else
110  {
111  m_Light.SetIntensity(1, 0);
112  }
113  }
114  }
115  }
116 
117  void OnLightCreated()
118  {
119  m_Light.SetColorToWhite();
120  }
121 
122  void CreateHeadtorchLight()
123  {
124  if (!m_Light)
125  m_Light = HeadtorchLight.Cast(ScriptedLightBase.CreateLight( HeadtorchLight, "0 0 0"));
126 
127  OnLightCreated();
128 
129  m_Light.PerformVisibilityCheck(this);
130 
131  EntityAI owner = GetHierarchyParent();
132 
133  if (owner)
134  {
135  if (owner.IsPlayer())
136  {
137  PlayerBase owner_PB = PlayerBase.Cast(owner);
138  ItemBase att_item = owner_PB.GetItemOnSlot("Eyewear");
139 
140  if (att_item == this)
141  {
142  AttachLightOnHead(owner_PB);
143  }
144  else
145  {
146  m_Light.AttachOnMemoryPoint(this, m_OffHeadLightPoint, m_OffHeadLightTarget);
147  }
148  }
149  else if (owner.IsZombie())
150  {
151  int slot_id = InventorySlots.GetSlotIdFromString("Eyewear");
152  EntityAI item_EAI = owner.GetInventory().FindAttachment(slot_id);
153  ItemBase item_IB = ItemBase.Cast(item_EAI);
154 
155  if (item_IB == this)
156  {
157  AttachLightOnHead(owner);
158  }
159  }
160  }
161  else
162  {
163  m_Light.AttachOnMemoryPoint(this, m_OffHeadLightPoint, m_OffHeadLightTarget);
164  }
165  }
166 
167  void AttachLightOnHead(EntityAI person)
168  {
169  if (person.IsPlayer())
170  {
171  PlayerBase player = PlayerBase.Cast(person);
172  int boneIdx = player.GetBoneIndexByName("Head");
173 
174  if (boneIdx != -1)
175  {
176  if (m_Light.GetParent())
177  m_Light.DetachFromParent();
178 
179  m_Light.SetPosition(m_OnHeadLocalPos);
180  m_Light.SetOrientation(m_OnHeadLocalOri);
181 
182  player.AddChild(m_Light, boneIdx);
183  }
184  }
185  else if (person.IsZombie())
186  {
187  if (m_Light.GetParent())
188  m_Light.DetachFromParent();
189 
190  m_Light.SetPosition(m_OnHeadLocalPos);
191  m_Light.SetOrientation(m_OnHeadLocalOri);
192 
193  person.AddChild(m_Light, 17);
194  }
195  }
196 
197  override void OnWasAttached(EntityAI parent, int slot_id)
198  {
199  super.OnWasAttached(parent, slot_id);
200 
201  PlayerBase player = PlayerBase.Cast(parent);
202 
203  if (player)
204  {
205  if (m_Light)
206  {
207  AttachLightOnHead(player);
208  }
209  }
210  }
211 
212  override void OnWasDetached(EntityAI parent, int slot_id)
213  {
214  super.OnWasDetached(parent, slot_id);
215 
216  if (parent && (parent.IsPlayer() || parent.IsZombie()))
217  {
218  if (m_Light)
219  {
220  m_Light.DetachFromParent();
221  m_Light.AttachOnMemoryPoint(this, m_OffHeadLightPoint, m_OffHeadLightTarget);
222  }
223  }
224  }
225 
226  override void SetActions()
227  {
228  super.SetActions();
233  }
234 
235  override bool IsLightSource()
236  {
237  return true;
238  }
239 
240  override ItemBase GetLightSourceItem()
241  {
242  return this;
243  }
244 
245  override void OnInvisibleSet(bool invisible)
246  {
247  if (m_Light)
248  {
249  m_Light.SetEnabled(!invisible);
250  }
251  }
252 };
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
CALL_CATEGORY_SYSTEM
const int CALL_CATEGORY_SYSTEM
Definition: tools.c:8
OnWasAttached
override void OnWasAttached(EntityAI parent, int slot_id)
Definition: torch.c:945
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
m_Timer
ref Timer m_Timer
Definition: dayzgame.c:690
ActionTurnOnWhileInHands
Definition: actionturnonwhileinhands.c:1
Battery9V
Definition: battery9v.c:1
GetLightSourceItem
ItemBase GetLightSourceItem()
Definition: itembase.c:4612
OnWorkStop
override void OnWorkStop()
Definition: m18smokegrenade_colorbase.c:2
m_Light
protected ExplosiveLight m_Light
light
Definition: explosivesbase.c:31
ActionTurnOnHeadtorch
Definition: actionturnonheadtorch.c:1
PlayerBase
Definition: playerbaseclient.c:1
vector
Definition: enconvert.c:105
Clothing
Definition: armband_colorbase.c:1
OnWork
override void OnWork(float consumed_energy)
Definition: smokegrenadebase.c:195
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
ScriptedLightBase
Definition: pointlightbase.c:1
SetActions
void SetActions()
Definition: advancedcommunication.c:79
OnWasDetached
override void OnWasDetached(EntityAI parent, int slot_id)
Definition: remotedetonator.c:237
IsLightSource
bool IsLightSource()
Definition: itembase.c:918
Headtorch_Black
Definition: headtorch_black.c:1
ActionTurnOffWhileInHands
Definition: actionturnoffwhileinhands.c:1
Timer
Definition: dayzplayerimplement.c:62
Vector
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
EntityAI
Definition: building.c:5
OnWorkStart
override void OnWorkStart()
Definition: smokegrenadebase.c:175
ActionTurnOffHeadtorch
Definition: actionturnoffheadtorch.c:1