forked from Unity-Technologies/Megacity-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundsSystem.cs
57 lines (53 loc) · 2.12 KB
/
BoundsSystem.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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using static Unity.Entities.SystemAPI;
namespace Unity.Megacity.Gameplay
{
/// <summary>
/// System that updates the player location bounds.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.LocalSimulation)]
[UpdateInGroup(typeof(TransformSystemGroup))]
public partial struct UpdateBoundsSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<LevelBounds>();
state.RequireForUpdate<PlayerLocationBounds>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var levelBounds = GetSingleton<LevelBounds>();
var evaluateIsInsideBoundsJob = new EvaluateIsInsideBoundsJob { LevelBounds = levelBounds };
evaluateIsInsideBoundsJob.ScheduleParallel();
}
}
[BurstCompile]
public partial struct EvaluateIsInsideBoundsJob : IJobEntity
{
[ReadOnly] public LevelBounds LevelBounds;
public void Execute(in LocalToWorld localToWorld, ref PlayerLocationBounds locationBounds)
{
var position = localToWorld.Position;
// evaluate top cylinder radius
if (position.y > LevelBounds.Top.y)
{
locationBounds.IsInside = math.distancesq(position, LevelBounds.Top) < LevelBounds.SafeAreaSq;
}
// evaluate center cylinder radius
else if (position.y > LevelBounds.Bottom.y && position.y < LevelBounds.Top.y)
{
var point = new float3(LevelBounds.Center.x, position.y, LevelBounds.Center.z);
locationBounds.IsInside = math.distancesq(position, point) < LevelBounds.SafeAreaSq;
}
// evaluate bottom cylinder radius
else if (position.y < LevelBounds.Bottom.y)
locationBounds.IsInside = math.distancesq(position, LevelBounds.Bottom) > LevelBounds.SafeAreaSq;
}
}
}