forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnRandomInSphereSystem.cs
More file actions
102 lines (85 loc) · 3.69 KB
/
Copy pathSpawnRandomInSphereSystem.cs
File metadata and controls
102 lines (85 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Samples.Common;
using Unity.Transforms;
namespace Samples.Common
{
public class SpawnRandomInSphereSystem : ComponentSystem
{
struct SpawnRandomInSphereInstance
{
public int spawnerIndex;
public Entity sourceEntity;
public float3 position;
public float radius;
}
ComponentGroup m_MainGroup;
protected override void OnCreateManager(int capacity)
{
m_MainGroup = GetComponentGroup(typeof(SpawnRandomInSphere), typeof(Position));
}
protected override void OnUpdate()
{
var uniqueTypes = new List<SpawnRandomInSphere>(10);
EntityManager.GetAllUniqueSharedComponentDatas(uniqueTypes);
int spawnInstanceCount = 0;
for (int sharedIndex = 0; sharedIndex != uniqueTypes.Count; sharedIndex++)
{
var spawner = uniqueTypes[sharedIndex];
m_MainGroup.SetFilter(spawner);
var entities = m_MainGroup.GetEntityArray();
spawnInstanceCount += entities.Length;
}
if (spawnInstanceCount == 0)
return;
var spawnInstances = new NativeArray<SpawnRandomInSphereInstance>(spawnInstanceCount, Allocator.Temp);
{
int spawnIndex = 0;
for (int sharedIndex = 0; sharedIndex != uniqueTypes.Count; sharedIndex++)
{
var spawner = uniqueTypes[sharedIndex];
m_MainGroup.SetFilter(spawner);
var entities = m_MainGroup.GetEntityArray();
var positions = m_MainGroup.GetComponentDataArray<Position>();
for (int entityIndex = 0; entityIndex < entities.Length; entityIndex++)
{
var spawnInstance = new SpawnRandomInSphereInstance();
spawnInstance.sourceEntity = entities[entityIndex];
spawnInstance.spawnerIndex = sharedIndex;
spawnInstance.position = positions[entityIndex].Value;
spawnInstances[spawnIndex] = spawnInstance;
spawnIndex++;
}
}
}
for (int spawnIndex = 0; spawnIndex < spawnInstances.Length; spawnIndex++)
{
int spawnerIndex = spawnInstances[spawnIndex].spawnerIndex;
var spawner = uniqueTypes[spawnerIndex];
int count = spawner.count;
var entities = new NativeArray<Entity>(count,Allocator.Temp);
var prefab = spawner.prefab;
float radius = spawner.radius;
var spawnPositions = new NativeArray<float3>(count, Allocator.Temp);
float3 center = spawnInstances[spawnIndex].position;
var sourceEntity = spawnInstances[spawnIndex].sourceEntity;
GeneratePoints.RandomPointsInSphere(center,radius,ref spawnPositions);
EntityManager.Instantiate(prefab, entities);
for (int i = 0; i < count; i++)
{
var position = new Position
{
Value = spawnPositions[i]
};
EntityManager.SetComponentData(entities[i],position);
}
EntityManager.RemoveComponent<SpawnRandomInSphere>(sourceEntity);
spawnPositions.Dispose();
entities.Dispose();
}
spawnInstances.Dispose();
}
}
}