1 [
EntityEditorProps(
category:
"GameScripted/Debris", description:
"Entity used to represent a large piece of debris from a collapsing building.", dynamicBox:
true)]
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;
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;
34 ResourceName model = buildingSetup.GetDamagedRegionModel(buildingRegion);
37 if (model ==
string.Empty)
40 Resource resource = Resource.Load(model);
43 if (!resource.IsValid())
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);
59 entity.GetBounds(mins, maxs);
61 float regionVerticalSize = 3;
63 entity.m_vCenter = (maxs - mins) * 0.5 + mins;
64 regionVerticalSize = maxs[1] - mins[1];
67 vector center = entity.CoordToParent(entity.m_vCenter);
68 autoptr TraceParam param =
new TraceParam;
69 param.Exclude = ignoreEnt;
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;
76 float traced = entity.GetWorld().TraceMove(param,
null);
79 entity.m_fDebrisPosY = (param.End[1] - param.Start[1]) * traced + param.Start[1];
80 entity.m_fImpactPosY = entity.m_fDebrisPosY - regionVerticalSize;
89 m_ParentBuildingSetup = buildingSetup;
93 vector GetLinearVelocity()
95 return m_vLinearVelocity;
99 vector GetAngularVelocity()
101 return m_vAngularVelocity;
105 void SetLinearVelocity(vector vel)
107 m_vLinearVelocity = vel;
111 void SetAngularVelocity(vector vel)
113 m_vAngularVelocity = vel;
117 protected void SetBuildingRegion(
int buildingRegion)
119 m_iBuildingRegion = buildingRegion;
123 override protected void EOnFrame(IEntity owner,
float timeSlice)
125 #ifdef BUILDING_DESTRUCTION_DEBUG
126 if (Debug.KeyState(KeyCode.KC_SUBTRACT))
131 GetTransform(curMat);
134 Math3D.AnglesToMatrix(m_vAngularVelocity * timeSlice, rotMat);
137 Math3D.MatrixMultiply3(curMat, rotMat, newMat);
138 newMat[3] = curMat[3] + m_vLinearVelocity * timeSlice;
141 vector center = CoordToParent(m_vCenter);
143 float height = center[1];
144 float heightAboveImpact = height - m_fImpactPosY;
146 float gravityMult = 1;
147 if (heightAboveImpact < RANDOM_BUMP_HEIGHT_MAX)
151 m_vLinearVelocity += PhysicsWorld.GetGravity(
GetGame().GetWorldEntity()) * timeSlice * gravityMult;
154 if (m_vAngularVelocity.LengthSq() < Math.Pow(ANGULAR_VELOCITY_MAX, 2))
155 m_vAngularVelocity += m_vAngularVelocity * timeSlice;
158 m_fRandomBumpDelay -= timeSlice;
159 if (m_fRandomBumpDelay <= 0 && heightAboveImpact > RANDOM_BUMP_HEIGHT_MAX)
161 m_fRandomBumpDelay = Math.RandomFloat(RANDOM_BUMP_DELAY_MIN, RANDOM_BUMP_DELAY_MAX);
163 float rndPosYPctMin = 0.3;
164 float rndPosYPctMax = 0.9;
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));
170 if (!m_bSpawnedDebris && height < m_fDebrisPosY)
172 m_bSpawnedDebris =
true;
173 if (m_ParentBuildingSetup)
174 m_ParentBuildingSetup.SpawnDebrisForRegion(
this, m_iBuildingRegion, m_vLinearVelocity, m_vAngularVelocity);
177 if (height > m_fImpactPosY)
186 SetEventMask(EntityEvent.INIT | EntityEvent.FRAME);
187 SetFlags(EntityFlags.ACTIVE,
true);
189 m_fRandomBumpDelay = Math.RandomFloat(RANDOM_BUMP_DELAY_MIN, RANDOM_BUMP_DELAY_MAX);