-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHumanNavigationSystem.cs
More file actions
80 lines (66 loc) · 2.35 KB
/
HumanNavigationSystem.cs
File metadata and controls
80 lines (66 loc) · 2.35 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 Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Transforms2D;
using UnityEngine;
[UpdateAfter(typeof(HumanToZombieSystem))]
class HumanNavigationSystem : JobComponentSystem
{
[Inject] private HumanData humanDatum;
NativeArray<float> randomFloats = new NativeArray<float>(1000000, Allocator.Persistent);
NativeArray<float> randomFloats2 = new NativeArray<float>(1000000, Allocator.Persistent);
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
for (int i = 0; i < humanDatum.Length; i++)
{
randomFloats[i] = Random.value;
randomFloats2[i] = Random.value;
}
var humanNavigationJob = new HumanNavigationJob
{
humanDatum = humanDatum,
dt = Time.deltaTime,
randomFloats = randomFloats,
randomFloats2 = randomFloats2
};
return humanNavigationJob.Schedule(humanDatum.Length, 64, inputDeps);
}
}
[ComputeJobOptimization]
struct HumanNavigationJob : IJobParallelFor
{
public HumanData humanDatum;
public float dt;
public NativeArray<float> randomFloats;
public NativeArray<float> randomFloats2;
public void Execute(int index)
{
var human = humanDatum.Human[index];
if (human.IsInfected == 1)
return;
human.TimeTillNextDirectionChange -= dt;
if (human.TimeTillNextDirectionChange <= 0)
{
human.TimeTillNextDirectionChange = 5;
float randomX = (randomFloats[index] - 0.5f) * 2f;
float randomY = (randomFloats2[index] - 0.5f) * 2f;
Heading2D heading2D = humanDatum.Heading[index];
heading2D.Value = new float2(randomX, randomY);
humanDatum.Heading[index] = heading2D;
//MoveSpeed moveSpeed = humanDatum.MoveSpeed[index];
//moveSpeed.speed = index;
//humanDatum.MoveSpeed[index] = moveSpeed;
}
humanDatum.Human[index] = human;
}
}
public struct HumanData
{
public int Length;
public ComponentDataArray<Position2D> Positions;
public ComponentDataArray<Heading2D> Heading;
public ComponentDataArray<MoveSpeed> MoveSpeed;
public ComponentDataArray<Human> Human;
}