forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurretShootingSystem.cs
More file actions
80 lines (71 loc) · 3.23 KB
/
Copy pathTurretShootingSystem.cs
File metadata and controls
80 lines (71 loc) · 3.23 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
using Tutorials.Tanks.Execute;
using Tutorials.Tanks.Step2;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
namespace Tutorials.Tanks.Step4
{
[UpdateAfter(typeof(TurretRotationSystem))]
[BurstCompile]
public partial struct TurretShootingSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<TurretShooting>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
// To make structural changes, the job needs an EntityCommandBuffer.
// An EntityCommandBuffer created from an EntityCommandBufferSystem will
// be played back and disposed by that system.
var ecbSystemSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSystemSingleton.CreateCommandBuffer(state.WorldUnmanaged);
// Instantiate the job.
var turretShootJob = new TurretShoot
{
LocalToWorldLookup = SystemAPI.GetComponentLookup<LocalToWorld>(true),
LocalTransformLookup = SystemAPI.GetComponentLookup<LocalTransform>(true),
ECB = ecb
};
// Schedule the job to run as a single-threaded job.
turretShootJob.Schedule();
}
}
// IJobEntity relies on source generation to implicitly define a query from the signature of the Execute function.
[WithAll(typeof(Shooting))] // Used in Step 8.
[BurstCompile]
public partial struct TurretShoot : IJobEntity
{
[ReadOnly] public ComponentLookup<LocalToWorld> LocalToWorldLookup;
[ReadOnly] public ComponentLookup<LocalTransform> LocalTransformLookup;
public EntityCommandBuffer ECB;
// We need global transforms, so we access the LocalToWorld matrix of the turret and of the spawn position.
// Note that the parameters use "in", which makes them readonly.
// You should mark data in jobs as readonly wherever possible because the safety checks
// allow jobs that access the same readonly data to run concurrently. If the parameters here
// were "ref" instead, the safety checks would not allow this job to run concurrently
// with any other jobs that also read the same component types.
public void Execute(in TurretAspect turret, in LocalToWorld localToWorld)
{
var instance = ECB.Instantiate(turret.CannonBallPrefab);
var spawnLocalToWorld = LocalToWorldLookup[turret.CannonBallSpawn];
ECB.SetComponent(instance, new LocalTransform
{
Position = spawnLocalToWorld.Position,
Rotation = quaternion.identity,
Scale = LocalTransformLookup[turret.CannonBallPrefab].Scale
});
ECB.SetComponent(instance, new CannonBall
{
Velocity = localToWorld.Up * 20.0f
});
// The line below propagates the color from the turret to the cannon ball.
ECB.SetComponent(instance, new URPMaterialPropertyBaseColor { Value = turret.Color });
}
}
}