forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallSpawnerSystem.cs
More file actions
49 lines (44 loc) · 1.63 KB
/
Copy pathBallSpawnerSystem.cs
File metadata and controls
49 lines (44 loc) · 1.63 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
using Unity.Burst;
using Unity.Entities;
using Unity.NetCode;
using Unity.Transforms;
namespace KickBall
{
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
public partial struct BallSpawnerSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EntityPrefabs>();
state.RequireForUpdate<BallConfig>();
state.RequireForUpdate<NetworkTime>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var prefabs = SystemAPI.GetSingleton<EntityPrefabs>();
var ballConfig = SystemAPI.GetSingleton<BallConfig>();
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
// avoid repeating spawns
if (!networkTime.IsFirstTimeFullyPredictingTick)
{
return;
}
foreach (var (input, playerTransform, color) in
SystemAPI.Query<RefRO<PlayerInput>, RefRO<LocalTransform>, RefRO<Color>>()
.WithAll<Player, Simulate>())
{
if (!input.ValueRO.SpawnBall.IsSet)
{
continue;
}
var ball = state.EntityManager.Instantiate(prefabs.Ball);
var ballTransform = playerTransform.ValueRO;
ballTransform.Position.y += ballConfig.SpawnHeight;
state.EntityManager.SetComponentData(ball, ballTransform);
state.EntityManager.SetComponentData(ball, color.ValueRO);
}
}
}
}