forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SafeZoneSystem.cs
executable file
·60 lines (54 loc) · 1.94 KB
/
SafeZoneSystem.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
48
49
50
51
52
53
54
55
56
57
58
59
60
using Tutorials.Tanks.Step2;
using Tutorials.Tanks.Step4;
using Tutorials.Tanks.Step6;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Tutorials.Tanks.Step7
{
[UpdateBefore(typeof(TurretShootingSystem))]
[UpdateInGroup(typeof(LateSimulationSystemGroup))]
public partial struct SafeZoneSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Execute.SafeZone>();
state.RequireForUpdate<Config>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
float radius = SystemAPI.GetSingleton<Config>().SafeZoneRadius;
// Debug rendering (the white circle).
const float debugRenderStepInDegrees = 20;
for (float angle = 0; angle < 360; angle += debugRenderStepInDegrees)
{
var a = float3.zero;
var b = float3.zero;
math.sincos(math.radians(angle), out a.x, out a.z);
math.sincos(math.radians(angle + debugRenderStepInDegrees), out b.x, out b.z);
Debug.DrawLine(a * radius, b * radius);
}
var safeZoneJob = new SafeZoneJob
{
SquaredRadius = radius * radius
};
safeZoneJob.ScheduleParallel();
}
}
[WithAll(typeof(Turret))]
[WithOptions(EntityQueryOptions.IgnoreComponentEnabledState)]
[BurstCompile]
public partial struct SafeZoneJob : IJobEntity
{
public float SquaredRadius;
// Because we want the global position of a child entity, we read LocalToWorld instead of LocalTransform.
void Execute(in LocalToWorld transformMatrix, EnabledRefRW<Shooting> shootingState)
{
shootingState.ValueRW = math.lengthsq(transformMatrix.Position) > SquaredRadius;
}
}
}