Skip to content

Commit

Permalink
Human infection system
Browse files Browse the repository at this point in the history
  • Loading branch information
jweimann committed Apr 19, 2018
1 parent 2933683 commit 9397630
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Assets/GameCode/ComponentTypes/Human.cs
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;
}
69 changes: 69 additions & 0 deletions Assets/GameCode/HumanInfectionSystem.cs
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;
}
11 changes: 11 additions & 0 deletions Assets/GameCode/HumanInfectionSystem.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Assets/GameCode/ZombieNavigationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ protected override JobHandle OnUpdate(JobHandle inputDeps)

public struct ZombieNavigationJob : IJobParallelFor
{

public ZombieData zombieDatum;

[NativeDisableParallelForRestriction]
Expand Down Expand Up @@ -74,5 +73,5 @@ public struct ZombieTargetData
{
public int Length;
[ReadOnly] public ComponentDataArray<Position2D> Positions;
[ReadOnly] public ComponentDataArray<Human> Human;
public ComponentDataArray<Human> Humans;
}
3 changes: 2 additions & 1 deletion Assets/GameCode/ZombieSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ internal class ZombieSettings : MonoBehaviour
public float HumanSpeed = 10;
public int HumanCount = 1000;
public int ZombieCount = 10;
public float InfectionDistance = 1f;

public Rect Playfield = new Rect { x = -30.0f, y = -30.0f, width = 60.0f, height = 60.0f };


public static ZombieSettings Instance { get; private set; }

Expand Down

0 comments on commit 9397630

Please sign in to comment.