3 [
EntityEditorProps(
category:
"GameScripted/Utility", description:
"Entity used for spawning grids filled with models", color:
"0 0 255 255")]
23 [
Attribute(
"100", UIWidgets.Slider,
"The amount of objects to spawn",
"0 100000 1")]
24 private int m_iObjectsCount;
27 [
Attribute(
"0", UIWidgets.CheckBox,
"If enabled, bounding boxes will not be taken into account, spacing will be absolute",
"")]
28 private bool m_bUseAbsoluteSpacing;
31 [
Attribute(
"0.1", UIWidgets.Slider,
"Spacing between each entity (counting with BBOX)",
"-5 50 0.05")]
32 private float m_fSpacing;
35 [
Attribute(
"0", UIWidgets.Slider,
"Seed used for randomized rotations. Leave 0 for no randomization.",
"0 1337 1")]
36 private int m_iRandomizationSeed;
40 [
Attribute(
"", UIWidgets.EditBox,
"List of entities in text file. Overrides the default array if not empty.",
"")]
41 private string m_sObjectsListPath;
44 [
Attribute(
"", UIWidgets.ResourceNamePicker,
"Assets to be spawned. Models (xob) or prefabs (et).",
"xob et")]
45 private ref array<ResourceName> m_aObjects;
48 private ref array<IEntity> m_aSpawnedEntities =
null;
51 sealed array<IEntity> GetSpawnedEntities()
57 protected int GetObjectCount()
60 return m_aObjects.Count();
66 protected bool GetObjectName(
int index, out
string name)
72 name = m_aObjects[
index];
81 private int GetObjectType(
string object)
83 if (
object ==
string.Empty)
86 int lastDotIndex =
object.LastIndexOf(
".");
87 int count =
object.Length();
88 string ext =
object.Substring(lastDotIndex, count-lastDotIndex);
108 bool Spawn(
int index)
111 return Spawn(
index, v);
118 protected bool Spawn(
int index, out vector centerPoint)
121 BaseWorld baseWorld =
GetGame().GetWorld();
129 Print(
"GridSpawner: Error, no objects specified!", LogLevel.ERROR);
135 int objectsCount = m_aObjects.Count();
136 if (index < 0 || index >= objectsCount)
139 Print(
"GridSpawner: Error, index " +
index +
" is out of bounds!", LogLevel.ERROR);
145 ResourceName
object = m_aObjects[
index];
148 if (
object ==
string.Empty)
152 int type = GetObjectType(
object);
156 Print(
"GridSpawnerEntity: Trying to spawn an invalid object. You must specify a valid template (.et) or mesh (.xob).", LogLevel.ERROR);
170 int countPerAxis = Math.Floor(Math.Sqrt(m_iObjectsCount));
171 Resource resource = Resource.Load(
object);
172 IEntity spawnedObject =
null;
175 float spacing = m_fSpacing;
178 if (m_iRandomizationSeed != 0)
180 Math.Randomize(m_iRandomizationSeed);
185 if (!m_bUseAbsoluteSpacing)
188 vector dummyMatrix[4];
189 Math3D.MatrixIdentity4(dummyMatrix);
193 const float dummyOffset = -999;
194 for (
int i = 0; i < 3; i++)
195 dummyMatrix[3][i] = dummyOffset;
197 autoptr EntitySpawnParams spawnParams =
new EntitySpawnParams();
198 spawnParams.TransformMode = ETransformMode.WORLD;
199 for (
int comp = 0; comp < 4; comp++)
201 spawnParams.Transform[comp] = dummyMatrix[comp];
209 dummyObject =
GetGame().SpawnEntityPrefab(resource, baseWorld, spawnParams);
213 VObject obj = resource.GetResource().ToVObject();
215 dummyObject.SetObject(obj,
"");
223 dummyObject.GetBounds(min, max);
227 float boundsSpan = vector.Distance(min, max);
228 spacing += boundsSpan;
239 for (
int i = 0; i < countPerAxis; i++)
241 for (
int j = 0; j < countPerAxis; j++)
245 Math3D.MatrixIdentity4(mat);
248 if (m_iRandomizationSeed != 0)
250 vector yawPitchRoll = Vector(Math.RandomFloat(0, 360), 0.0, 0.0);
251 Math3D.AnglesToMatrix(yawPitchRoll, mat);
255 vector newPos = currentPosition;
256 newPos[0] = newPos[0] + i * spacing;
257 newPos[2] = newPos[2] + j * spacing;
263 autoptr EntitySpawnParams spawnParams =
new EntitySpawnParams();
264 spawnParams.TransformMode = ETransformMode.WORLD;
265 for (
int comp = 0; comp < 4; comp++)
267 spawnParams.Transform[comp] = mat[comp];
275 spawnedObject =
GetGame().SpawnEntityPrefab(resource, baseWorld, spawnParams);
279 VObject obj = resource.GetResource().ToVObject();
281 spawnedObject.SetObject(obj,
"");
282 spawnedObject.SetFlags(EntityFlags.VISIBLE,
true);
292 centerPoint[0] = 0.5 * (float)countPerAxis * spacing;
293 centerPoint[2] = 0.5 * (float)countPerAxis * spacing;
298 protected void DeleteEntities()
305 for (
int i = count-1; i >= 0; i--)
322 protected override void EOnFrame(IEntity owner,
float timeSlice)
327 protected override void EOnInit(IEntity owner)
330 if (!
GetGame().GetWorldEntity())
334 if (m_sObjectsListPath !=
string.Empty)
337 FileHandle file = FileIO.OpenFile(m_sObjectsListPath, FileMode.READ);
342 string line =
string.Empty;
343 while (file.ReadLine(line) > 0)
345 if (line !=
string.Empty)
346 m_aObjects.Insert(line);
357 SetEventMask(EntityEvent.POSTFRAME | EntityEvent.FRAME | EntityEvent.INIT);