forked from DeadlyApps/Unity-ECS-ZombieSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
using Unity.Entities; | ||
using Unity.Mathematics; | ||
|
||
public struct Human : IComponentData | ||
{ | ||
public float TimeTillNextDirectionChange; | ||
public int IsInfected; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Unity.Collections; | ||
using Unity.Entities; | ||
using Unity.Jobs; | ||
using Unity.Mathematics; | ||
using Unity.Transforms2D; | ||
using UnityEngine; | ||
|
||
class HumanInfectionSystem : JobComponentSystem | ||
{ | ||
[Inject] private HumanInfectionData humanData; | ||
[Inject] private ZombiePositionData zombieTargetData; | ||
|
||
protected override JobHandle OnUpdate(JobHandle inputDeps) | ||
{ | ||
var job = new HumanInfectionJob | ||
{ | ||
zombieTargetData = zombieTargetData, | ||
humanData = humanData, | ||
infectionDistance = ZombieSettings.Instance.InfectionDistance | ||
}; | ||
|
||
return job.Schedule(humanData.Length, 64, inputDeps); | ||
} | ||
} | ||
|
||
|
||
struct HumanInfectionJob : IJobParallelFor | ||
{ | ||
public HumanInfectionData humanData; | ||
|
||
[NativeDisableParallelForRestriction] | ||
public ZombiePositionData zombieTargetData; | ||
public float infectionDistance; | ||
|
||
public void Execute(int index) | ||
{ | ||
float2 humanPosition = humanData.Positions[index].Value; | ||
|
||
for (int i = 0; i < zombieTargetData.Length; i++) | ||
{ | ||
float2 zombiePosition = zombieTargetData.Positions[i].Value; | ||
|
||
float2 delta = zombiePosition - humanPosition; | ||
float distSquared = math.dot(delta, delta); | ||
|
||
if (distSquared < infectionDistance) | ||
{ | ||
var human = humanData.Humans[i]; | ||
human.IsInfected = 1; | ||
humanData.Humans[i] = human; | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
public struct HumanInfectionData | ||
{ | ||
public int Length; | ||
[ReadOnly] public ComponentDataArray<Position2D> Positions; | ||
public ComponentDataArray<Human> Humans; | ||
} | ||
|
||
public struct ZombiePositionData | ||
{ | ||
public int Length; | ||
[ReadOnly] public ComponentDataArray<Position2D> Positions; | ||
[ReadOnly] public ComponentDataArray<Zombie> Zombies; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters