Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PrefabSpawnable.c
Go to the documentation of this file.
3{
4 [Attribute(ResourceName.Empty, UIWidgets.ResourcePickerThumbnail, "Prefabs to spawn (spawns ALL of them)", "et")]
5 ref array<ResourceName> m_Prefabs;
6 [Attribute("0.1", UIWidgets.Slider, "Damage received to physics impulse (speed / mass) multiplier", "0 10000 0.01")]
7 float m_fDamageToImpulse;
8 [Attribute("0.25", UIWidgets.Slider, "Random linear velocity multiplier (m/s)", "0 200 0.1")]
9 float m_fRandomVelocityLinear;
10 [Attribute("45", UIWidgets.Slider, "Random angular velocity multiplier (deg/s)", "0 3600 0.1")]
11 float m_fRandomVelocityAngular;
12 [Attribute("0", UIWidgets.CheckBox, "Whether the spawned prefabs should be set as children (sets auto-transform)")]
13 bool m_bSpawnAsChildren;
14
15#ifdef WORKBENCH
16 //------------------------------------------------------------------------------------------------
19 override bool CompareAttributes(SCR_BaseSpawnable other)
20 {
21 SCR_PrefabSpawnable otherPrefab = SCR_PrefabSpawnable.Cast(other);
22
23 if (!super.CompareAttributes(other))
24 return false;
25
26 int count = m_Prefabs.Count();
27 if (otherPrefab.m_Prefabs.Count() != count)
28 return false;
29
30 for (int i = count - 1; i >= 0; i--)
31 {
32 if (otherPrefab.m_Prefabs[i] != m_Prefabs[i])
33 return false;
34 }
35
36 if (otherPrefab.m_fDamageToImpulse != m_fDamageToImpulse)
37 return false;
38
39 if (otherPrefab.m_fRandomVelocityLinear != m_fRandomVelocityLinear)
40 return false;
41
42 if (otherPrefab.m_fRandomVelocityAngular != m_fRandomVelocityAngular)
43 return false;
44
45 if (otherPrefab.m_bSpawnAsChildren != m_bSpawnAsChildren)
46 return false;
47
48 return true;
49 }
50
51 //------------------------------------------------------------------------------------------------
52 override void SetVariables(WorldEditorAPI api, IEntitySource source, array<ref ContainerIdPathEntry> path, int index)
53 {
54 super.SetVariables(api, source, path, index);
55
56 string prefabsArray = "";
57 // Set all variables of the spawn object
58 for (int i = 0, count = m_Prefabs.Count(); i < count; i++)
59 {
60 prefabsArray += m_Prefabs[i];
61
62 if (i != count - 1) // Not last item
63 prefabsArray += ",";
64 }
65
66 api.SetVariableValue(source, path, "m_Prefabs", prefabsArray);
67
68 api.SetVariableValue(source, path, "m_fDamageToImpulse", m_fDamageToImpulse.ToString());
69 api.SetVariableValue(source, path, "m_fRandomVelocityLinear", m_fRandomVelocityLinear.ToString());
70 api.SetVariableValue(source, path, "m_fRandomVelocityAngular", m_fRandomVelocityAngular.ToString());
71 api.SetVariableValue(source, path, "m_bSpawnAsChildren", m_bSpawnAsChildren.ToString(true));
72 }
73
74 //------------------------------------------------------------------------------------------------
75 override bool CreateObject(WorldEditorAPI api, IEntitySource source, array<ref ContainerIdPathEntry> path, int index)
76 {
77 if (!AlreadyExists(api, source, index))
78 {
79 api.CreateObjectArrayVariableMember(source, path, "m_aPhaseDestroySpawnObjects", "SCR_PrefabSpawnable", index);
80 return true;
81 }
82
83 return false;
84 }
85#endif
86
87 //------------------------------------------------------------------------------------------------
89 override IEntity Spawn(IEntity owner, Physics parentPhysics, SCR_HitInfo hitInfo, bool snapToTerrain = false)
90 {
91 if (!hitInfo)
92 return null;
93
94 int numModelPrefabs = 0;
95 if (m_Prefabs)
96 numModelPrefabs = m_Prefabs.Count();
97
98 for (int i = 0; i < numModelPrefabs; i++)
99 {
100 ResourceName prefabPath = m_Prefabs[i];
101
102 bool isPrefab;
103 if (SCR_Global.GetResourceContainsComponent(prefabPath, "RplComponent", isPrefab) && RplSession.Mode() == RplMode.Client)
104 continue;
105
106 if (!isPrefab)
107 continue;
108
109 vector spawnMat[4];
110 GetSpawnTransform(owner, spawnMat, m_bSpawnAsChildren);
111
112 // Divide the matrix by owner scale to remove scale from transformation
113 float ownerScale = owner.GetScale();
114 if (ownerScale > 0) // Avoid division by zero
115 {
116 spawnMat[0] = spawnMat[0] / ownerScale; // X axis
117 spawnMat[1] = spawnMat[1] / ownerScale; // Y axis
118 spawnMat[2] = spawnMat[2] / ownerScale; // Z axis
119 // Position (spawnMat[3]) is NOT divided - it should remain in world space
120 }
121
122 EntitySpawnParams prefabSpawnParams = EntitySpawnParams();
123 prefabSpawnParams.Transform = spawnMat;
124 // Use Scale parameter to set scale instead of using transformation
125 prefabSpawnParams.Scale = ownerScale;
126
127 IEntity spawnedPrefab = GetGame().SpawnEntityPrefab(Resource.Load(prefabPath), null, prefabSpawnParams);
128 if (!spawnedPrefab)
129 continue;
130
131 if (m_bSpawnAsChildren)
132 {
133 owner.AddChild(spawnedPrefab, -1, EAddChildFlags.AUTO_TRANSFORM);
134 continue;
135 }
136
137 Physics prefabPhysics = spawnedPrefab.GetPhysics();
138 if (!prefabPhysics || !prefabPhysics.IsDynamic())
139 continue;
140
141 //hotfix for spawned prefabs getting stuck in terrain causing low fps
142 vector mins, maxs;
143 spawnedPrefab.GetWorldBounds(mins, maxs);
144
145 vector entityOrigin = spawnedPrefab.GetOrigin();
146 float terrainYMins = GetGame().GetWorld().GetSurfaceY(mins[0], mins[2]);
147 float terrainYMaxs = GetGame().GetWorld().GetSurfaceY(maxs[0], maxs[2]);
148
149 if ((mins[1] < terrainYMins || mins[1] < terrainYMaxs) && (maxs[1] > terrainYMins || maxs[1] > terrainYMaxs))
150 {
151 float highestTerrainY;
152 if (terrainYMaxs > terrainYMins)
153 highestTerrainY = terrainYMaxs;
154 else
155 highestTerrainY = terrainYMins;
156
157 float newHeight = highestTerrainY - mins[1] + entityOrigin[1] + 0.1;
158 spawnedPrefab.SetOrigin({entityOrigin[0], newHeight, entityOrigin[2]});
159 }
160
161 float dmgSpeed = hitInfo.m_HitDamage * m_fDamageToImpulse / prefabPhysics.GetMass();
162
163 vector linearVelocity = hitInfo.m_HitDirection * Math.RandomFloat(0, 1);
164 linearVelocity += Vector(Math.RandomFloat(-1, 1), Math.RandomFloat(-1, 1), Math.RandomFloat(-1, 1)) * m_fRandomVelocityLinear;
165 linearVelocity *= dmgSpeed;
166 vector angularVelocity = Vector(Math.RandomFloat(-1, 1), Math.RandomFloat(-1, 1), Math.RandomFloat(-1, 1)) * Math.RandomFloat(0.25, 4) * m_fRandomVelocityAngular;
167 angularVelocity *= dmgSpeed;
168
169 if (parentPhysics)
170 {
171 linearVelocity += parentPhysics.GetVelocity();
172 angularVelocity += parentPhysics.GetAngularVelocity();
173 }
174
175 prefabPhysics.SetVelocity(linearVelocity);
176 prefabPhysics.SetAngularVelocity(angularVelocity * Math.DEG2RAD);
177 }
178
179 return null; // We spawned multiple entities
180 }
181}
string path
ArmaReforgerScripted GetGame()
Definition game.c:1398
RplMode
Mode of replication.
Definition RplMode.c:9
SCR_AIAnimation_Loitering BaseContainerProps
Commanding menu commanding element class.
SCR_AIGroupClass snapToTerrain
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
proto external float GetScale()
proto external void SetOrigin(vector orig)
proto external vector GetOrigin()
proto external Physics GetPhysics()
proto external int AddChild(notnull IEntity child, TNodeId pivot, EAddChildFlags flags=EAddChildFlags.AUTO_TRANSFORM)
Add Entity to hierarchy. Pivot is pivot index, or -1 for center of parent.
proto external void GetWorldBounds(out vector mins, out vector maxs)
Definition Math.c:13
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
void GetSpawnTransform(IEntity owner, out vector outMat[4], bool localCoords=false)
Calculates the spawn tranformation matrix for the object.
static bool GetResourceContainsComponent(ResourceName resourcePath, string componentClassName, out bool isPrefab)
Read the input resource and returns whether it contains a component on input name,...
Definition Functions.c:1522
Class to temporarily store information about the last hit that dealt damage.
Definition SCR_HitInfo.c:3
void EntitySpawnParams()
Definition gameLib.c:130
AISpawnerGroupClass AIGroupClass Spawn()
Spawns a new group entity, sets its transformation and then calls OnSpawn.
SCR_FieldOfViewSettings Attribute
EAddChildFlags
proto native vector Vector(float x, float y, float z)