forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnChainSystem.cs
More file actions
117 lines (96 loc) · 4.18 KB
/
Copy pathSpawnChainSystem.cs
File metadata and controls
117 lines (96 loc) · 4.18 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Samples.Common;
using Unity.Transforms;
namespace Samples.Common
{
public class SpawnChainSystem : ComponentSystem
{
struct SpawnChainInstance
{
public int spawnerIndex;
public Entity sourceEntity;
public float3 position;
}
ComponentGroup m_MainGroup;
protected override void OnCreateManager(int capacity)
{
m_MainGroup = GetComponentGroup(typeof(SpawnChain),typeof(Position));
}
protected override void OnUpdate()
{
var uniqueTypes = new List<SpawnChain>(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<SpawnChainInstance>(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 SpawnChainInstance();
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 minDistance = spawner.minDistance;
float maxDistance = spawner.maxDistance;
float3 center = spawnInstances[spawnIndex].position;
var sourceEntity = spawnInstances[spawnIndex].sourceEntity;
EntityManager.Instantiate(prefab, entities);
{
PositionConstraint contraint = new PositionConstraint();
contraint.parentEntity = sourceEntity;
contraint.maxDistance = maxDistance;
EntityManager.AddComponentData(entities[0],contraint);
}
for (int i = 1; i < count; i++ )
{
PositionConstraint contraint = new PositionConstraint();
contraint.parentEntity = entities[i - 1];
contraint.maxDistance = maxDistance;
EntityManager.AddComponentData(entities[i],contraint);
}
float3 dv = new float3( 0.0f, minDistance, 0.0f );
for (int i = 0; i < count; i++)
{
var position = new Position
{
Value = center - (dv * (float) i)
};
EntityManager.SetComponentData(entities[i],position);
}
EntityManager.RemoveComponent<SpawnChain>(sourceEntity);
entities.Dispose();
}
spawnInstances.Dispose();
}
}
}