Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_VehicleDustComponent.c
Go to the documentation of this file.
1 // Spawns dust particle effect that drags with the vehicle during ride
2 
3 [ComponentEditorProps(category: "GameScripted/Test", description:"SCR_VehicleDust")]
4 class SCR_VehicleDustClass: ScriptComponentClass
5 {
6 };
7 
9 {
10  // LIST OF PARTICLE SOURCES
11  static string particle_attribute_descriptions = "Select particle effect for driving on this surface type.";
12  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
13  ResourceName m_Grass;
14  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
15  ResourceName m_Gravel;
16  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
17  ResourceName m_Dirt;
18  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
19  ResourceName m_Default;
20  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
21  ResourceName m_Wood;
22  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
23  ResourceName m_Asphalt;
24  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
25  ResourceName m_Snow;
26  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
27  ResourceName m_Sand;
28  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
29  ResourceName m_Metal;
30  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
31  ResourceName m_Concrete;
32  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
33  ResourceName m_Soil;
34  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
35  ResourceName m_Stone;
36  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
37  ResourceName m_Rubber;
38  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
39  ResourceName m_Flesh;
40  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
41  ResourceName m_Glass;
42  [Attribute( "", UIWidgets.ResourceNamePicker, particle_attribute_descriptions )]
43  ResourceName m_Plastic;
44 
45  [Attribute( "", UIWidgets.Coords, "Orientation of the effect relative to the vehicle" )]
46  vector m_vLocalOrientation; // Local rotation of the particle
47 
48  [Attribute( "", UIWidgets.Coords, "Position of the effect relative to the object" )]
49  vector m_vLocalOffset; // Local offset of the particle
50 
51  [Attribute( "", UIWidgets.EditBox, "Minimal speed (km/h) which triggers the dust effect and its interpolation from 0%" )]
52  float m_fDustStartSpeed;
53 
54  [Attribute( "", UIWidgets.EditBox, "Speed (km/h) which tops the effect's interpolation at 100% values" )]
55  float m_fDustTopSpeed;
56  VehicleWheeledSimulation m_VehicleWheeledSimulation;
57  VehicleBodyEffectBase m_Effect;
58  Physics m_Physics;
59  SignalsManagerComponent m_SignalManagerComp;
60  ref map<int, ResourceName> surface_particles = new map<int, ResourceName>();
61  int m_iCurrentSurfaceID;
62  int m_iSurfaceSignalID = -1;
63 
64  // Optimization timers
65  float m_fUpdateDelay; // Desired delay between each particle calculations in seconds. 0 means per frame.
66  float m_fCurrentUpdateDelay; // The time of current delay
67  const float TICK_TIME_DELAY_MAX = 2; // Max acceptable delay in seconds
68  const float TICK_DELAY_RANGE_START = 15; // Starting range at which the delay begins to increase (from 0 to TICK_TIME_DELAY_MAX)
69  const float TICK_DELAY_RANGE_END = 100; // End range at which delay reaches its maximum (TICK_TIME_DELAY_MAX)
70 
71  override void OnPostInit(IEntity owner)
72  {
73  super.OnPostInit(owner);
74 
75  // Don't run this component on console app
76  if (System.IsConsoleApp())
77  {
78  Deactivate(owner);
79  return;
80  }
81  SetEventMask(owner, EntityEvent.INIT | EntityEvent.FRAME);
82  }
83 
84  //------------------------------------------------------------------------------------------------
85  override void EOnInit(IEntity owner)
86  {
87  super.EOnInit(owner);
88 
89  // Initialize the list of surface types, but only once.
90  if (surface_particles.Count() == 0)
91  RegisterSurfaceTypes();
92 
93  // Get data
94  GenericEntity generic_entity = GenericEntity.Cast(owner);
95 
96  if (generic_entity)
97  {
98  m_SignalManagerComp = SignalsManagerComponent.Cast(generic_entity.FindComponent(SignalsManagerComponent));
99  m_Physics = owner.GetPhysics();
100  m_VehicleWheeledSimulation = VehicleWheeledSimulation.Cast(generic_entity.FindComponent(VehicleWheeledSimulation));
101  }
102 
103  // Prep eff
104  if (!m_Effect)
105  {
106  m_Effect = VehicleBodyEffectBase.Cast(GetGame().SpawnEntity(VehicleBodyEffectBase, owner.GetWorld()));
107  if (m_Effect)
108  {
109  vector mat[4];
110  owner.GetTransform(mat);
111  m_Effect.SetTransform(mat);
112  owner.AddChild(m_Effect, 0);
113  }
114 
115  }
116  }
117 
118  // Assigns particle files to surface IDs. This should be called only once
119  void RegisterSurfaceTypes()
120  {
121  // DO NOT CHANGE ORDER OF THESE SURFACE TYPES! THEY ARE DEFINED IN THE ENGINE!
122  surface_particles.Insert(0, m_Default);
123  surface_particles.Insert(31, m_Grass);
124  surface_particles.Insert(21, m_Dirt);
125  surface_particles.Insert(25, m_Gravel);
126  surface_particles.Insert(51, m_Wood);
127  surface_particles.Insert(11, m_Asphalt);
128  surface_particles.Insert(41, m_Snow);
129  surface_particles.Insert(28, m_Sand);
130  surface_particles.Insert(61, m_Metal);
131  surface_particles.Insert(12, m_Concrete);
132  surface_particles.Insert(23, m_Soil);
133  surface_particles.Insert(15, m_Stone);
134  surface_particles.Insert(27, m_Stone);
135  surface_particles.Insert(72, m_Rubber);
136  surface_particles.Insert(91, m_Flesh);
137  surface_particles.Insert(71, m_Glass);
138  surface_particles.Insert(73, m_Plastic);
139  }
140 
141  override void EOnFrame(IEntity owner, float timeSlice)
142  {
143  m_fCurrentUpdateDelay += timeSlice;
144 
145  // Calculate distance between owner and camera
146  vector camera_transform[4];
147  GetGame().GetWorld().GetCurrentCamera(camera_transform);
148  vector camera_world_pos = camera_transform[3];
149  vector relative_pos = camera_world_pos - owner.GetOrigin();
150  float distance = relative_pos.Length();
151 
152  // Prolong update interval based on the distance between camera and effect
154  {
156  }
157  else
158  {
159  m_fUpdateDelay = 0;
160  }
161 
162  // Check if delay treshold was crossed. This prevents calling intense calculations per frame for vehicles which are far away from camera as they have low priority
164  {
166  UpdateEffect(owner);
167  }
168  }
169 
170  // Called when the effects needs to be updated. This happens per frame if the camera is close to it, but much slower if further away fr optimization purposes.
171  void UpdateEffect(IEntity owner)
172  {
173  if (owner && m_Physics && m_SignalManagerComp) // Makre sure that data exists
174  {
175  float speed = m_Physics.GetVelocity().Length();
176 
177  // Activate particle effect only within the desired speed
178  if (speed >= m_fDustStartSpeed && m_fDustStartSpeed > 0 && m_fDustTopSpeed >= m_fDustStartSpeed)
179  {
180  int previousSurfaceID = m_iCurrentSurfaceID;
181 
182  // Only create the signal when it's necessary, otherwise use the ID
183  if (m_iSurfaceSignalID < 0)
184  m_iSurfaceSignalID = m_SignalManagerComp.AddOrFindSignal("surface");
185 
186  m_iCurrentSurfaceID = m_SignalManagerComp.GetSignalValue(m_iSurfaceSignalID);
187 
188  if (!surface_particles)
189  RegisterSurfaceTypes();
190 
191 
192  // get and set surface material
193  ResourceName surface_type = m_Default;
194  m_iCurrentSurfaceID = m_iCurrentSurfaceID/100;
195  if (m_iCurrentSurfaceID >= 0 && surface_particles.Contains(m_iCurrentSurfaceID))
196  surface_type = surface_particles.Get(m_iCurrentSurfaceID);
197 
198 
199  // Vehicle is moving fast enough to generate dust
200  if (m_Effect && previousSurfaceID == m_iCurrentSurfaceID)
201  {
202  m_Effect.Play();
203  m_Effect.UpdateVehicleDustEffect(speed, m_fDustStartSpeed, m_fDustTopSpeed);
204  }
205  else
206  {
207  if (m_Effect)
208  m_Effect.StopEmission();
209 
210  if (surface_type.Length() > 0)
211  {
212  ParticleEffectEntitySpawnParams spawnParams();
213  spawnParams.Type = VehicleBodyEffectBase;
214  Math3D.AnglesToMatrix(m_vLocalOrientation, spawnParams.Transform);
215  spawnParams.Transform[3] = m_vLocalOffset;
216  spawnParams.Parent = owner;
217  spawnParams.UseFrameEvent = true;
218 
219  m_Effect = VehicleBodyEffectBase.Cast(ParticleEffectEntity.SpawnParticleEffect(surface_type, spawnParams));
220  m_Effect.UpdateVehicleDustEffect(speed, m_fDustStartSpeed*3.6, m_fDustTopSpeed);
221  }
222  }
223  }
224  // Vehicle is NOT moving fast enough to generate dust
225  else if (m_Effect)
226  {
227  m_Effect.StopEmission(); // Stop dust effect
228  }
229  }
230  }
231 
232  //------------------------------------------------------------------------------------------------
233  void ~SCR_VehicleDust()
234  {
235  if (m_Effect)
236  m_Effect.Stop(); // Clean up
237  }
238 };
SCR_VehicleDustClass
Definition: SCR_VehicleDustComponent.c:4
m_fUpdateDelay
protected float m_fUpdateDelay
Definition: SCR_MotorExhaustEffectGeneralComponent.c:25
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SpawnEntity
protected IEntity SpawnEntity(ResourceName entityResourceName, notnull IEntity slotOwner)
Definition: SCR_CatalogEntitySpawnerComponent.c:1008
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
TICK_TIME_DELAY_MAX
const protected float TICK_TIME_DELAY_MAX
Definition: SCR_MotorExhaustEffectGeneralComponent.c:27
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_Physics
private Physics m_Physics
Definition: InteractableBoxComponent.c:13
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
TICK_DELAY_RANGE_START
const protected float TICK_DELAY_RANGE_START
Definition: SCR_MotorExhaustEffectGeneralComponent.c:28
Attribute
typedef Attribute
Post-process effect of scripted camera.
distance
float distance
Definition: SCR_DestructibleTreeV2.c:29
TICK_DELAY_RANGE_END
const protected float TICK_DELAY_RANGE_END
Definition: SCR_MotorExhaustEffectGeneralComponent.c:29
Deactivate
protected void Deactivate()
Definition: SCR_BaseHintCondition.c:27
SCR_VehicleDust
Definition: SCR_VehicleDustComponent.c:8
VehicleBodyEffectBase
Definition: VehicleBody.c:5
m_fCurrentUpdateDelay
protected float m_fCurrentUpdateDelay
Definition: SCR_MotorExhaustEffectGeneralComponent.c:26
m_SignalManagerComp
protected SignalsManagerComponent m_SignalManagerComp
Definition: SCR_FuelNode.c:62
m_Default
ref SCR_ButtonEffectImage_ImageOrImageSet m_Default
Definition: SCR_ButtonEffectImage.c:6
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180