-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
TurretShootingSystem.cs
47 lines (42 loc) · 1.54 KB
/
TurretShootingSystem.cs
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
using Tutorials.Tanks.Step2;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
namespace Tutorials.Tanks.Step4
{
[UpdateInGroup(typeof(LateSimulationSystemGroup))]
public partial struct TurretShootingSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Execute.TurretShooting>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (turret, localToWorld) in
SystemAPI.Query<TurretAspect, RefRO<LocalToWorld>>()
.WithAll<Shooting>())
{
Entity instance = state.EntityManager.Instantiate(turret.CannonBallPrefab);
state.EntityManager.SetComponentData(instance, new LocalTransform
{
Position = SystemAPI.GetComponent<LocalToWorld>(turret.CannonBallSpawn).Position,
Rotation = quaternion.identity,
Scale = SystemAPI.GetComponent<LocalTransform>(turret.CannonBallPrefab).Scale
});
state.EntityManager.SetComponentData(instance, new CannonBall
{
Velocity = localToWorld.ValueRO.Up * 20.0f
});
state.EntityManager.SetComponentData(instance, new URPMaterialPropertyBaseColor
{
Value = turret.Color
});
}
}
}
}