forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeriodicallySpawnRandomShapesAuthoring.cs
More file actions
105 lines (87 loc) · 3.75 KB
/
Copy pathPeriodicallySpawnRandomShapesAuthoring.cs
File metadata and controls
105 lines (87 loc) · 3.75 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
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics.Systems;
using Unity.Transforms;
public interface IPeriodicSpawnSettings
{
int SpawnRate { get; set; }
int DeathRate { get; set; }
}
class PeriodicallySpawnRandomShapesAuthoring : SpawnRandomObjectsAuthoringBase<PeriodicSpawnSettings>
{
public int SpawnRate = 50;
public int DeathRate = 50;
internal override void Configure(ref PeriodicSpawnSettings spawnSettings)
{
spawnSettings.SpawnRate = SpawnRate;
spawnSettings.DeathRate = DeathRate;
}
}
struct PeriodicSpawnSettings : IComponentData, ISpawnSettings, IPeriodicSpawnSettings
{
public Entity Prefab { get; set; }
public float3 Position { get; set; }
public quaternion Rotation { get; set; }
public float3 Range { get; set; }
public int Count { get; set; }
public int SpawnRate { get; set; }
public int DeathRate { get; set; }
}
abstract class PeriodicalySpawnRandomObjectsSystem<T> : SpawnRandomObjectsSystemBase<T> where T : struct, ISpawnSettings, IPeriodicSpawnSettings, IComponentData
{
public int FrameCount = 0;
internal override int GetRandomSeed(T spawnSettings)
{
int seed = base.GetRandomSeed(spawnSettings);
seed = (seed * 397) ^ spawnSettings.Prefab.GetHashCode();
seed = (seed * 397) ^ spawnSettings.DeathRate;
seed = (seed * 397) ^ spawnSettings.SpawnRate;
seed = (seed * 397) ^ FrameCount;
return seed;
}
internal override void OnBeforeInstantiatePrefab(ref T spawnSettings)
{
if (!EntityManager.HasComponent<LifeTime>(spawnSettings.Prefab))
{
EntityManager.AddComponent<LifeTime>(spawnSettings.Prefab);
}
EntityManager.SetComponentData(spawnSettings.Prefab, new LifeTime { Value = spawnSettings.DeathRate });
}
protected override void OnUpdate()
{
var lFrameCount = FrameCount;
// Entities.ForEach in generic system types are not supported
using (var entities = GetEntityQuery(new ComponentType[] { typeof(T) }).ToEntityArray(Allocator.TempJob))
{
for (int j = 0; j < entities.Length; j++)
{
var entity = entities[j];
var spawnSettings = EntityManager.GetComponentData<T>(entity);
if (lFrameCount % spawnSettings.SpawnRate == 0)
{
var count = spawnSettings.Count;
OnBeforeInstantiatePrefab(ref spawnSettings);
var instances = new NativeArray<Entity>(count, Allocator.Temp);
EntityManager.Instantiate(spawnSettings.Prefab, instances);
var positions = new NativeArray<float3>(count, Allocator.Temp);
var rotations = new NativeArray<quaternion>(count, Allocator.Temp);
RandomPointsInRange(spawnSettings.Position, spawnSettings.Rotation, spawnSettings.Range, ref positions, ref rotations, GetRandomSeed(spawnSettings));
for (int i = 0; i < count; i++)
{
var instance = instances[i];
EntityManager.SetComponentData(instance, new Translation { Value = positions[i] });
EntityManager.SetComponentData(instance, new Rotation { Value = rotations[i] });
ConfigureInstance(instance, ref spawnSettings);
}
}
EntityManager.SetComponentData<T>(entity, spawnSettings);
}
}
FrameCount++;
}
}
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(BuildPhysicsWorld))]
class PeriodicallySpawnRandomShapeSystem : PeriodicalySpawnRandomObjectsSystem<PeriodicSpawnSettings>
{}