1[
EntityEditorProps(
category:
"GameScripted/Debris", description:
"Entity used to represent small chunks of debris. Automatically managed.", dynamicBox:
true)]
12#ifdef ENABLE_BASE_DESTRUCTION
14 private static int s_iDebrisMaximumCount = 1000;
17 private static ref array<SCR_DebrisSmallEntity> s_aDebrisSmallList = null;
23 private float m_fMaxDistance;
26 protected vector m_vSoundPositionLast;
29 protected float m_fSoundThreshold;
35 protected SCR_EMaterialSoundTypeDebris m_eMaterialSoundType;
38 protected bool m_bExteriorSource;
41 static private int s_iSpawnedThisFrame;
44 static private int s_iDebrisPerFrameLimit = 128;
48 protected void RegisterDebris()
54 if (!s_aDebrisSmallList)
55 s_aDebrisSmallList = {};
58 if (s_aDebrisSmallList)
60 s_aDebrisSmallList.Insert(
this);
66 private void UnregisterDebris()
72 if (s_aDebrisSmallList)
76 int idx = s_aDebrisSmallList.Find(
this);
77 int count = s_aDebrisSmallList.Count();
80 s_aDebrisSmallList.Remove(
idx);
81 s_aDebrisSmallList.Resize(count-1);
83 if (s_aDebrisSmallList.Count() <= 0)
85 s_aDebrisSmallList = null;
97 float spdDiff = contact.GetRelativeNormalVelocityAfter() - contact.GetRelativeNormalVelocityBefore();
100 if (spdDiff > m_fSoundThreshold && vector.DistanceSq(m_vSoundPositionLast, contact.Position) >= MINIMAL_DISTANCE_SQ && m_eMaterialSoundType != 0 &&
m_fAgeTime > MINIMAL_AGE)
105 m_fdVelocity = spdDiff;
115 void PlaySound(vector pos,
float dVelocity)
117 SCR_SoundManagerModule soundManager = SCR_SoundManagerModule.GetInstance(GetWorld());
121 SCR_MPDestructionManager destructionManager = SCR_MPDestructionManager.GetInstance();
122 if (!destructionManager)
126 if (!audioSourceConfiguration)
130 if (m_bExteriorSource)
136 audioSourceConfiguration.m_sSoundEventName = SCR_SoundEvent.SOUND_MPD_ +
typename.EnumToString(SCR_EMaterialSoundTypeDebris, m_eMaterialSoundType);
138 SCR_AudioSource audioSource = soundManager.CreateAudioSource(
this, audioSourceConfiguration, pos);
146 audioSource.SetSignalValue(
SCR_AudioSource.COLLISION_D_V_SIGNAL_NAME, dVelocity - m_fSoundThreshold);
147 audioSource.SetSignalValue(
SCR_AudioSource.ENTITY_SIZE_SIGNAL_NAME, GetPhysics().GetMass());
150 soundManager.PlayAudioSource(audioSource);
154 m_vSoundPositionLast = pos;
158 SoundDebugPlaySound(m_fSoundThreshold, dVelocity, pos);
165 super.EOnFrame(owner, timeSlice);
177 SoundDebugOnFrame(m_fSoundThreshold);
191 static SCR_DebrisSmallEntity SpawnDebris(BaseWorld world, vector mat[4], ResourceName model,
float mass = 10,
float lifeTime = 10.0,
float maxDistance = 256.0,
int priority = 1, vector linearVelocity =
"0 0 0", vector angularVelocity =
"0 0 0",
string remap =
"",
bool isStatic =
false, SCR_EMaterialSoundTypeDebris materialSoundType = 0,
bool exteriorSource =
false)
193 if (s_iSpawnedThisFrame >= s_iDebrisPerFrameLimit)
197 if (model ==
string.Empty)
201 if (GetDistanceToCamera(world, mat[3]) > maxDistance)
204 SCR_DebrisSmallEntity entity = null;
207 if (!s_aDebrisSmallList)
208 s_aDebrisSmallList = {};
211 if (s_aDebrisSmallList)
213 int count = s_aDebrisSmallList.Count();
215 if (count >= s_iDebrisMaximumCount)
217 foreach(SCR_DebrisSmallEntity debrisSmallEntity : s_aDebrisSmallList)
219 if (debrisSmallEntity)
221 if (debrisSmallEntity.m_iPriority < priority)
223 entity = debrisSmallEntity;
231 Physics entityPhysics = entity.GetPhysics();
233 entityPhysics.Destroy();
247 s_iSpawnedThisFrame++;
249 entity.SetTransform(mat);
250 Resource resource = Resource.Load(model);
254 BaseResourceObject baseRes = resource.GetResource();
258 VObject obj = baseRes.ToVObject();
259 entity.SetObject(obj, remap);
261 entity.m_fLifeTime = lifeTime;
262 entity.m_iPriority = priority;
263 entity.m_fMaxDistance = maxDistance;
264 entity.m_eMaterialSoundType = materialSoundType;
267 entity.m_fSoundThreshold = Math.Sqrt(2 * KINETIC_ENERGY_THRESHOLD / mass);
270 entity.GetWorldBounds(mins, maxs);
271 entity.m_vSoundPositionLast = vector.Lerp(mins, maxs, 0.5);
273 entity.m_bExteriorSource = exteriorSource;
276 Physics entityPhysics = entity.GetPhysics();
281 entityPhysics = Physics.CreateStatic(entity, -1);
285 entityPhysics = Physics.CreateDynamic(entity, mass, -1);
289 vector entityOrigin = entity.GetOrigin();
290 float terrainYMins =
GetGame().GetWorld().GetSurfaceY(mins[0], mins[2]);
291 float terrainYMaxs =
GetGame().GetWorld().GetSurfaceY(maxs[0], maxs[2]);
293 if ((mins[1] < terrainYMins || mins[1] < terrainYMaxs) && (maxs[1] > terrainYMins || maxs[1] > terrainYMaxs))
295 float highestTerrainY;
296 if (terrainYMaxs > terrainYMins)
297 highestTerrainY = terrainYMaxs;
299 highestTerrainY = terrainYMins;
301 float newHeight = highestTerrainY - mins[1] + entityOrigin[1] + 0.1;
302 entity.SetOrigin({entityOrigin[0], newHeight, entityOrigin[2]});
305 entityPhysics.SetVelocity(linearVelocity);
306 entityPhysics.SetAngularVelocity(angularVelocity * Math.DEG2RAD);
311 entityPhysics.SetInteractionLayer(EPhysicsLayerDefs.Debris);
320 static void DeleteRandomDebris(
int count = 1)
322 for (
int i = 0; i < count; i++)
324 if (s_aDebrisSmallList)
326 if (s_aDebrisSmallList.Count() > 0)
328 SCR_DebrisSmallEntity ent = s_aDebrisSmallList.GetRandomElement();
343 super.DeleteDebris();
347 override void EOnPostFrame(
IEntity owner,
float timeSlice)
349 s_iSpawnedThisFrame = 0;
360 void SCR_DebrisSmallEntity(IEntitySource src,
IEntity parent)
366 void ~SCR_DebrisSmallEntity()
SCR_BaseDebrisSmallEntityClass m_bDelete
Whether this debris has reached end of its lifetime and should be deleted.
void SCR_BaseDebrisSmallEntity(IEntitySource src, IEntity parent)
float m_fAgeTime
Entity age in seconds. After this time is bigger thatn m_fLifeTime, debris will despawn.
void DeleteDebris()
Delete debris - unregisters it from the list and makes it scale down and delete.
ArmaReforgerScripted GetGame()
enum EAudioSourceFlag m_AudioHandle
Stores valid Audio handle.
void SCR_AudioSource(SCR_AudioSourceConfiguration audioSourceConfiguration, vector mat[4])
EAudioSourceConfigurationFlag
IEntity SpawnEntity(ResourceName entityResourceName, notnull IEntity slotOwner)
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
void PlaySound(string soundName)
override void EOnContact(IEntity owner, IEntity other, Contact contact)
override void EOnFrame(IEntity owner, float timeSlice)
enum EVehicleType IEntity
proto external vector GetOrigin()
proto external BaseWorld GetWorld()
SCR_AudioSourceConfiguration GetAudioSourceConfiguration()
override void EOnInit(IEntity owner)
EntityEvent
Various entity events.