Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
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")]
5
6class SCR_UnitDisplayComponent : ScriptComponent
7{
8 private FactionAffiliationComponent m_FactionAffiliatonComponent;
9 private DamageManagerComponent m_DamageManagerComponent;
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
27 if (!m_FactionAffiliatonComponent || !m_DamageManagerComponent)
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 }
39 if (m_ShapeHealthBackground)
40 {
41 delete m_ShapeHealthBackground;
42 m_ShapeHealthBackground = null;
43 }
44 return;
45 }
46 // If unit is dead (destroyed) delete shape, return
47 if (m_DamageManagerComponent)
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 }
62 if (m_ShapeHealthBackground)
63 {
64 delete m_ShapeHealthBackground;
65 m_ShapeHealthBackground = null;
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;
164 if (m_ShapeHealthBackground)
165 delete m_ShapeHealthBackground;
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));
188 m_DamageManagerComponent = DamageManagerComponent.Cast(genEnt.FindComponent(DamageManagerComponent));
189 }
190
191 m_Entity = ent;
192 }
193}
SCR_EAIThreatSectorFlags flags
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)
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
enum EAITargetInfoCategory m_Entity
SCR_UnitDisplayComponentClass m_FactionAffiliatonComponent
enum EVehicleType IEntity
proto external int SetEventMask(notnull IEntity owner, int mask)
proto external Managed FindComponent(typename typeName)
proto external vector GetOrigin()
proto external void GetTransform(out vector mat[])
proto external string GetName()
Definition Math.c:13
void EOnFrame(IEntity owner, float timeSlice)
Instance of created debug visualizer.
Definition Shape.c:14
ShapeType
Definition ShapeType.c:13
ShapeFlags
Definition ShapeFlags.c:13
EntityEvent
Various entity events.
Definition EntityEvent.c:14
EDamageState
proto native vector Vector(float x, float y, float z)
vector m_Position
Only in XY, Z is ignored.
Definition EnWidgets.c:81
proto int ARGB(int a, int r, int g, int b)