forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFallAndDestroySystem.cs
More file actions
57 lines (52 loc) · 2.33 KB
/
Copy pathFallAndDestroySystem.cs
File metadata and controls
57 lines (52 loc) · 2.33 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
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace HelloCube.Prefabs
{
public partial struct FallAndDestroySystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ExecutePrefabs>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
// rotation
float deltaTime = SystemAPI.Time.DeltaTime;
foreach (var (transform, speed) in
SystemAPI.Query<RefRW<LocalTransform>, RefRO<RotationSpeed>>())
{
// ValueRW and ValueRO both return a ref to the actual component value.
// The difference is that ValueRW does a safety check for read-write access while
// ValueRO does a safety check for read-only access.
transform.ValueRW = transform.ValueRO.RotateY(
speed.ValueRO.RadiansPerSecond * deltaTime);
}
// An EntityCommandBuffer created from EntityCommandBufferSystem.Singleton will be
// played back and disposed by the EntityCommandBufferSystem when it next updates.
var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
// Downward vector
var movement = new float3(0, -SystemAPI.Time.DeltaTime * 5f, 0);
// WithAll() includes RotationSpeed in the query, but
// the RotationSpeed component values will not be accessed.
// WithEntityAccess() includes the Entity ID as the last element of the tuple.
foreach (var (transform, entity) in
SystemAPI.Query<RefRW<LocalTransform>>()
.WithAll<RotationSpeed>()
.WithEntityAccess())
{
transform.ValueRW.Position += movement;
if (transform.ValueRO.Position.y < 0)
{
// Making a structural change would invalidate the query we are iterating through,
// so instead we record a command to destroy the entity later.
ecb.DestroyEntity(entity);
}
}
}
}
}