forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTankSpawningSystem.cs
More file actions
executable file
·65 lines (55 loc) · 2.62 KB
/
Copy pathTankSpawningSystem.cs
File metadata and controls
executable file
·65 lines (55 loc) · 2.62 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
using Tutorials.Tanks.Execute;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Rendering;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace Tutorials.Tanks.Step6
{
[BurstCompile]
partial struct TankSpawningSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<TankSpawning>();
state.RequireForUpdate<Config>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var config = SystemAPI.GetSingleton<Config>();
// This system will only run once, so the random seed can be hard-coded.
// Using an arbitrary constant seed makes the behavior deterministic.
var random = Random.CreateFromIndex(1234);
var hue = random.NextFloat();
// Helper to create any amount of colors as distinct from each other as possible.
// The logic behind this approach is detailed at the following address:
// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
URPMaterialPropertyBaseColor RandomColor()
{
// Note: if you are not familiar with this concept, this is a "local function".
// You can search for that term on the internet for more information.
// 0.618034005f == 2 / (math.sqrt(5) + 1) == inverse of the golden ratio
hue = (hue + 0.618034005f) % 1;
var color = Color.HSVToRGB(hue, 1.0f, 1.0f);
return new URPMaterialPropertyBaseColor { Value = (Vector4)color };
}
var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
var vehicles = CollectionHelper.CreateNativeArray<Entity>(config.TankCount, Allocator.Temp);
ecb.Instantiate(config.TankPrefab, vehicles);
var query = SystemAPI.QueryBuilder().WithAll<URPMaterialPropertyBaseColor>().Build();
// An EntityQueryMask provides an efficient test of whether a specific entity would
// be selected by an EntityQuery.
var queryMask = query.GetEntityQueryMask();
foreach (var vehicle in vehicles)
{
// Every prefab root contains a LinkedEntityGroup, a list of all its entities.
ecb.SetComponentForLinkedEntityGroup(vehicle, queryMask, RandomColor());
}
state.Enabled = false;
}
}
}