Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
UnitDisplayComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Spectating", description: "Displays a colored sphere over an entity - if FactionAffiliationComponent is present")]
2 class SCR_UnitDisplayComponentClass : ScriptComponentClass
3 {
4 }
5 
7 {
8  private FactionAffiliationComponent m_FactionAffiliatonComponent;
10 
11  private ref Shape m_Shape = null;
12  private ref Shape m_ShapeHealth = null;
13  private ref Shape m_ShapeHealthBackground = null;
14  private float m_DistanceToCamera = 0.0;
15 
16  // Holds offset from the entity attached to
17  private vector m_Position = vector.Zero;
18  private IEntity m_Entity = null;
19 
20  //------------------------------------------------------------------------------------------------
24  override void EOnFrame(IEntity owner, float timeSlice)
25  {
26  // No faction affiliation component, delete shape if it exists, return
28  {
29  if (m_Shape)
30  {
31  delete m_Shape;
32  m_Shape = null;
33  }
34  if (m_ShapeHealth)
35  {
36  delete m_ShapeHealth;
37  m_ShapeHealth = null;
38  }
40  {
43  }
44  return;
45  }
46  // If unit is dead (destroyed) delete shape, return
48  {
49  EDamageState state = m_DamageManagerComponent.GetState();
50  if (state == EDamageState.DESTROYED)
51  {
52  if (m_Shape)
53  {
54  delete m_Shape;
55  m_Shape = null;
56  }
57  if (m_ShapeHealth)
58  {
59  delete m_ShapeHealth;
60  m_ShapeHealth = null;
61  }
63  {
66  }
67  return;
68  }
69  }
70 
71  // Get position
72  m_Position = owner.GetOrigin();
73  m_Position[1] = m_Position[1] + SCR_UnitDisplaySettings.s_fShapeHeightOffset;
74 
75  // Get camera matrix
76  vector camMat[4];
77  m_Entity.GetWorld().GetCurrentCamera(camMat);
78  m_DistanceToCamera = vector.Distance(m_Position, camMat[3]);
79 
80  float scaleMult;
81  if (SCR_UnitDisplaySettings.s_bShapeUseScaling)
82  {
83  // Calculate distance
84  if (m_DistanceToCamera == 0) // to prevent division by 0
85  scaleMult = 1.0;
86  else if (m_DistanceToCamera > 0)
87  scaleMult = 1 / (SCR_UnitDisplaySettings.s_fShapeSizeGoal / m_DistanceToCamera);
88 
89  // Get scale multiplier, color from faction and update shape
90  scaleMult = Math.Clamp(scaleMult, SCR_UnitDisplaySettings.s_fShapeSizeMinimum, SCR_UnitDisplaySettings.s_fShapeSizeMaximum);
91  }
92  else
93  {
94  scaleMult = 1.0;
95  }
96 
97  // Get color from faction affiliation, get proper flags, create shape
98  int color = 0;
99  Faction affiliatedFaction = m_FactionAffiliatonComponent.GetAffiliatedFaction();
100  if (affiliatedFaction)
101  color = affiliatedFaction.GetFactionColor().PackToInt();
102  if (color != 0)
103  {
104  int flags = ShapeFlags.NOOUTLINE;
105  if (SCR_UnitDisplaySettings.s_bShapeUseAlpha)
106  flags |= ShapeFlags.TRANSP;
107  m_Shape = Shape.CreateSphere(color, flags, m_Position, SCR_UnitDisplaySettings.s_fShapeRadius * scaleMult);
108  }
109 
110  // Check if text should be drawn and draw it if so
111  if (SCR_UnitDisplaySettings.s_bShapeUseText)
112  {
113  // If entity is in text drawing distance, get cam matrix, use negative forward and draw text above
114  if (m_DistanceToCamera <= SCR_UnitDisplaySettings.s_fShapeTextDistance)
115  {
116  vector mat[4];
117  owner.GetTransform(mat);
118  mat[3][1] = mat[3][1] + SCR_UnitDisplaySettings.s_fShapeTextHeightOffset;
119  mat[0] = camMat[0];
120  mat[1] = camMat[1];
121  mat[2] = -camMat[2];
122 
123  string name = owner.GetName();
124  int textcolor = color;
125  CreateSimpleText(name, mat, SCR_UnitDisplaySettings.s_fShapeTextSize, textcolor, ShapeFlags.ONCE, null, 1, false);
126  }
127  }
128 
129  if (SCR_UnitDisplaySettings.s_bShapeUseHealthbar)
130  {
131  // If entity is in text drawing distance, get cam matrix, use negative forward and draw text above
132  if (m_DistanceToCamera <= SCR_UnitDisplaySettings.s_fShapeTextDistance)
133  {
134  string name = owner.GetName();
135  int hpcolor = color;
136  float health = m_DamageManagerComponent.GetHealthScaled();
137 
138  vector mat[4];
139  owner.GetTransform(mat);
140  mat[0] = camMat[0];
141  mat[1] = camMat[1];
142  mat[2] = -camMat[2];
143  mat[3][1] = mat[3][1] + SCR_UnitDisplaySettings.s_fShapeTextHeightOffset + SCR_UnitDisplaySettings.s_fShapeHealthbarHeight + 0.05;
144 
145  if (SCR_UnitDisplaySettings.s_bShapeUseText)
146  mat[3][1] = mat[3][1] + SCR_UnitDisplaySettings.s_fShapeTextSize;
147 
148  vector min, max;
149  min = Vector(- SCR_UnitDisplaySettings.s_fShapeHealthbarWidth * 0.5, -SCR_UnitDisplaySettings.s_fShapeHealthbarHeight, 0.01);
150  max = Vector( (health*SCR_UnitDisplaySettings.s_fShapeHealthbarWidth)-SCR_UnitDisplaySettings.s_fShapeHealthbarWidth * 0.5, SCR_UnitDisplaySettings.s_fShapeHealthbarHeight, 0.01);
151  vector minAbsolute = min;
152  minAbsolute[2] = 0.001;
153  vector maxAbsolute = Vector(SCR_UnitDisplaySettings.s_fShapeHealthbarWidth * 0.5, SCR_UnitDisplaySettings.s_fShapeHealthbarHeight, 0.001);
154 
155  m_ShapeHealthBackground = Shape.Create(ShapeType.BBOX, ARGB(255,255,255,255), 0, minAbsolute, maxAbsolute);
156  m_ShapeHealthBackground.SetMatrix(mat);
157  m_ShapeHealth = Shape.Create(ShapeType.BBOX, hpcolor, 0, min, max);
158  m_ShapeHealth.SetMatrix(mat);
159  }
160  else
161  {
162  if (m_ShapeHealth)
163  delete m_ShapeHealth;
166  }
167  }
168  }
169 
170  //------------------------------------------------------------------------------------------------
171  override void OnPostInit(IEntity owner)
172  {
173  super.OnPostInit(owner);
174  SetEventMask(owner, EntityEvent.FRAME);
175  }
176 
177  //------------------------------------------------------------------------------------------------
178  // constructor
182  void SCR_UnitDisplayComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
183  {
184  GenericEntity genEnt = GenericEntity.Cast(ent);
185  if (genEnt)
186  {
187  m_FactionAffiliatonComponent = FactionAffiliationComponent.Cast(genEnt.FindComponent(FactionAffiliationComponent));
189  }
190 
191  m_Entity = ent;
192  }
193 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
m_ShapeHealthBackground
private ref Shape m_ShapeHealthBackground
Definition: UnitDisplayComponent.c:13
m_Position
private vector m_Position
Definition: UnitDisplayComponent.c:17
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
SCR_UnitDisplayComponentClass
Definition: UnitDisplayComponent.c:2
EOnFrame
override void EOnFrame(IEntity owner, float timeSlice)
Definition: UnitDisplayComponent.c:24
EDamageState
EDamageState
Definition: EDamageState.c:12
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
m_FactionAffiliatonComponent
SCR_UnitDisplayComponentClass m_FactionAffiliatonComponent
m_ShapeHealth
private ref Shape m_ShapeHealth
Definition: UnitDisplayComponent.c:12
CreateSimpleText
void CreateSimpleText(string text, vector mat[4], float size, int color, ShapeFlags flags, array< ref Shape > output=null, float scaleWidth=1, bool doBackground=false, int backgroundColor=0x80000000)
Definition: DebugShapes.c:236
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: UnitDisplayComponent.c:171
Faction
Definition: Faction.c:12
m_DamageManagerComponent
private DamageManagerComponent m_DamageManagerComponent
Definition: UnitDisplayComponent.c:9
m_DistanceToCamera
private float m_DistanceToCamera
Definition: UnitDisplayComponent.c:14
SCR_UnitDisplayComponent
void SCR_UnitDisplayComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: UnitDisplayComponent.c:182
m_Shape
private ref Shape m_Shape
Definition: UnitDisplayComponent.c:11
m_Entity
private IEntity m_Entity
Definition: UnitDisplayComponent.c:18
SCR_UnitDisplaySettings
Definition: UnitDisplayManager.c:7
DamageManagerComponent
Definition: DamageManagerComponent.c:12
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180