forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObstacleSpawnerSystem.cs
More file actions
52 lines (44 loc) · 1.8 KB
/
Copy pathObstacleSpawnerSystem.cs
File metadata and controls
52 lines (44 loc) · 1.8 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
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
namespace KickBall
{
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct ObstacleSpawnerSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EntityPrefabs>();
state.RequireForUpdate<ObstacleConfig>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
state.Enabled = false;
var prefabs = SystemAPI.GetSingleton<EntityPrefabs>();
var obstacleConfig = SystemAPI.GetSingleton<ObstacleConfig>();
// For simplicity and consistency, we'll use a fixed random seed value.
var rand = new Random(123);
var prefabTransform = state.EntityManager.GetComponentData<LocalTransform>(prefabs.Obstacle);
// Spawn the obstacles in a grid.
for (int column = 0; column < obstacleConfig.NumColumns; column++)
{
for (int row = 0; row < obstacleConfig.NumRows; row++)
{
var obstacle = state.EntityManager.Instantiate(prefabs.Obstacle);
prefabTransform.Position = new float3
{
x = (column * obstacleConfig.GridCellSize) + rand.NextFloat(obstacleConfig.Offset),
y = 0,
z = (row * obstacleConfig.GridCellSize) + rand.NextFloat(obstacleConfig.Offset)
};
state.EntityManager.SetComponentData(obstacle, prefabTransform);
}
}
}
}
}