Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
DebrisBuildingEntity.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Debris", description: "Entity used to represent a large piece of debris from a collapsing building.", dynamicBox: true)]
2 class SCR_DebrisBuildingEntityClass: GenericEntityClass
3 {
4 };
5 
6 //------------------------------------------------------------------------------------------------
8 {
9  const static float RANDOM_INITIAL_LINEAR_VELOCITY_XZ = 0.5;
10  const static float RANDOM_INITIAL_LINEAR_VELOCITY_Y = 1;
11  const static float RANDOM_INITIAL_ANGULAR_VELOCITY = 3;
12  const static float RANDOM_BUMP_DELAY_MIN = 0.5;
13  const static float RANDOM_BUMP_DELAY_MAX = 2;
14  const static float RANDOM_BUMP_HEIGHT_MAX = 15;
15  const static float ANGULAR_VELOCITY_MAX = 30;
16 
17  protected SCR_BuildingSetup m_ParentBuildingSetup;
18  protected int m_iBuildingRegion = 0;
19  protected vector m_vLinearVelocity = vector.Zero;
20  protected vector m_vAngularVelocity = vector.Zero;
21  protected vector m_vCenter = vector.Zero;
22  protected float m_fImpactPosY = 0;
23  protected float m_fDebrisPosY = 0;
24  protected float m_fDelayDeleteTime = 0;
25  protected float m_fRandomBumpDelay = 0;
26  protected bool m_bSpawnedDebris = false;
27 
28  //------------------------------------------------------------------------------------------------
29  static SCR_DebrisBuildingEntity SpawnBuildingDebris(vector mat[4], SCR_BuildingSetup buildingSetup, int buildingRegion, IEntity ignoreEnt, string remap = "")
30  {
31  if (!buildingSetup)
32  return null;
33 
34  ResourceName model = buildingSetup.GetDamagedRegionModel(buildingRegion);
35 
36  // Check if model is valid
37  if (model == string.Empty)
38  return null;
39 
40  Resource resource = Resource.Load(model);
41 
42  // Check if resource is valid
43  if (!resource.IsValid())
44  return null;
45 
47  if (!entity)
48  return null;
49 
50  entity.SetParentBuilding(buildingSetup);
51  entity.SetTransform(mat);
52  VObject obj = resource.GetResource().ToVObject();
53  entity.SetObject(obj, remap);
54  entity.m_vLinearVelocity = Vector(Math.RandomFloat(-RANDOM_INITIAL_LINEAR_VELOCITY_XZ, RANDOM_INITIAL_LINEAR_VELOCITY_XZ), Math.RandomFloat(-RANDOM_INITIAL_LINEAR_VELOCITY_Y, RANDOM_INITIAL_LINEAR_VELOCITY_Y), Math.RandomFloat(-RANDOM_INITIAL_LINEAR_VELOCITY_XZ, RANDOM_INITIAL_LINEAR_VELOCITY_XZ));
55  entity.m_vAngularVelocity = Vector(Math.RandomFloat(-1, 1), Math.RandomFloat(-1, 1), Math.RandomFloat(-1, 1)) * RANDOM_INITIAL_ANGULAR_VELOCITY;
56  entity.SetBuildingRegion(buildingRegion);
57 
58  vector mins, maxs;
59  entity.GetBounds(mins, maxs);
60 
61  float regionVerticalSize = 3;
62 
63  entity.m_vCenter = (maxs - mins) * 0.5 + mins;
64  regionVerticalSize = maxs[1] - mins[1];
65 
66  // Trace downward and find the impact pos
67  vector center = entity.CoordToParent(entity.m_vCenter);
68  autoptr TraceParam param = new TraceParam;
69  param.Exclude = ignoreEnt;
70  param.Start = center;
71  param.End = param.Start - Vector(0, buildingSetup.m_fLargeDebrisDropMax, 0);
72  param.Flags = TraceFlags.ENTS | TraceFlags.WORLD;
73  entity.m_fImpactPosY = param.End[1] - regionVerticalSize;
74  entity.m_fDebrisPosY = entity.m_fImpactPosY;
75 
76  float traced = entity.GetWorld().TraceMove(param, null);
77  if (traced < 1)
78  {
79  entity.m_fDebrisPosY = (param.End[1] - param.Start[1]) * traced + param.Start[1];
80  entity.m_fImpactPosY = entity.m_fDebrisPosY - regionVerticalSize;
81  }
82 
83  return entity;
84  }
85 
86  //------------------------------------------------------------------------------------------------
87  void SetParentBuilding(SCR_BuildingSetup buildingSetup)
88  {
89  m_ParentBuildingSetup = buildingSetup;
90  }
91 
92  //------------------------------------------------------------------------------------------------
93  vector GetLinearVelocity()
94  {
95  return m_vLinearVelocity;
96  }
97 
98  //------------------------------------------------------------------------------------------------
99  vector GetAngularVelocity()
100  {
101  return m_vAngularVelocity;
102  }
103 
104  //------------------------------------------------------------------------------------------------
105  void SetLinearVelocity(vector vel)
106  {
107  m_vLinearVelocity = vel;
108  }
109 
110  //------------------------------------------------------------------------------------------------
111  void SetAngularVelocity(vector vel)
112  {
113  m_vAngularVelocity = vel;
114  }
115 
116  //------------------------------------------------------------------------------------------------
117  protected void SetBuildingRegion(int buildingRegion)
118  {
119  m_iBuildingRegion = buildingRegion;
120  }
121 
122  //------------------------------------------------------------------------------------------------
123  override protected void EOnFrame(IEntity owner, float timeSlice) //EntityEvent.FRAME
124  {
125  #ifdef BUILDING_DESTRUCTION_DEBUG
126  if (Debug.KeyState(KeyCode.KC_SUBTRACT))
127  timeSlice *= 0.1;
128  #endif
129 
130  vector curMat[4];
131  GetTransform(curMat);
132 
133  vector rotMat[3];
134  Math3D.AnglesToMatrix(m_vAngularVelocity * timeSlice, rotMat);
135 
136  vector newMat[4];
137  Math3D.MatrixMultiply3(curMat, rotMat, newMat);
138  newMat[3] = curMat[3] + m_vLinearVelocity * timeSlice;
139 
140  SetTransform(newMat);
141  vector center = CoordToParent(m_vCenter);
142 
143  float height = center[1];
144  float heightAboveImpact = height - m_fImpactPosY;
145 
146  float gravityMult = 1;
147  if (heightAboveImpact < RANDOM_BUMP_HEIGHT_MAX)
148  gravityMult = 0.5;
149 
150  // Add gravity to fall
151  m_vLinearVelocity += PhysicsWorld.GetGravity(GetGame().GetWorldEntity()) * timeSlice * gravityMult;
152 
153  // Add angular velocity, but don't increase if at maximum
154  if (m_vAngularVelocity.LengthSq() < Math.Pow(ANGULAR_VELOCITY_MAX, 2))
155  m_vAngularVelocity += m_vAngularVelocity * timeSlice;
156 
157  // Handle random "bumping", so simulate bouncing off surroundings
158  m_fRandomBumpDelay -= timeSlice;
159  if (m_fRandomBumpDelay <= 0 && heightAboveImpact > RANDOM_BUMP_HEIGHT_MAX)
160  {
161  m_fRandomBumpDelay = Math.RandomFloat(RANDOM_BUMP_DELAY_MIN, RANDOM_BUMP_DELAY_MAX);
162  float rndPosXZ = 1;
163  float rndPosYPctMin = 0.3;
164  float rndPosYPctMax = 0.9;
165  float rndAng = 2;
166  m_vLinearVelocity += Vector(Math.RandomFloat(-rndPosXZ, rndPosXZ), Math.RandomFloat(rndPosYPctMin, rndPosYPctMax) * -m_vLinearVelocity[1], Math.RandomFloat(-rndPosXZ, rndPosXZ));
167  m_vAngularVelocity += Vector(Math.RandomFloat(-rndAng, rndAng), Math.RandomFloat(-rndAng, rndAng), Math.RandomFloat(-rndAng, rndAng));
168  }
169 
170  if (!m_bSpawnedDebris && height < m_fDebrisPosY)
171  {
172  m_bSpawnedDebris = true;
173  if (m_ParentBuildingSetup)
174  m_ParentBuildingSetup.SpawnDebrisForRegion(this, m_iBuildingRegion, m_vLinearVelocity, m_vAngularVelocity);
175  }
176 
177  if (height > m_fImpactPosY)
178  return;
179 
180  delete this;
181  }
182 
183  //------------------------------------------------------------------------------------------------
184  void SCR_DebrisBuildingEntity(IEntitySource src, IEntity parent)
185  {
186  SetEventMask(EntityEvent.INIT | EntityEvent.FRAME);
187  SetFlags(EntityFlags.ACTIVE, true);
188 
189  m_fRandomBumpDelay = Math.RandomFloat(RANDOM_BUMP_DELAY_MIN, RANDOM_BUMP_DELAY_MAX);
190  }
191 
192  //------------------------------------------------------------------------------------------------
194  {
195  IEntity child = GetChildren();
196  while (child)
197  {
198  delete child;
199  child = GetChildren();
200  }
201  }
202 };
SpawnEntity
protected IEntity SpawnEntity(ResourceName entityResourceName, notnull IEntity slotOwner)
Definition: SCR_CatalogEntitySpawnerComponent.c:1008
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
SCR_DebrisBuildingEntityClass
Definition: DebrisBuildingEntity.c:2
SetTransform
override void SetTransform(vector transform[4], bool changedByUser=false)
Definition: SCR_EditableCharacterComponent.c:455
SCR_DebrisBuildingEntity
Definition: DebrisBuildingEntity.c:7
GetChildren
void GetChildren(out array< SCR_ScenarioFrameworkLayerBase > children)
Definition: SCR_ScenarioFrameworkLayerBase.c:359
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
SCR_BuildingSetup
Definition: SCR_BuildingSetup.c:4