Skip to content

Commit

Permalink
10k Humans can run around
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlyApps committed Apr 16, 2018
1 parent 5fc4b7c commit 6442e6b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Assets/GameCode/ComponentTypes/Human.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public struct Human : IComponentData
{

public float TimeTillNextDirectionChange;
}
76 changes: 51 additions & 25 deletions Assets/GameCode/HumanNavigationSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Unity.Entities;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
Expand All @@ -7,43 +8,68 @@

class HumanNavigationSystem : JobComponentSystem
{
public struct HumanData
{
public int Length;
public ComponentDataArray<Position2D> Position;
public ComponentDataArray<Heading2D> Heading;
public ComponentDataArray<MoveSpeed> MoveSpeed;
public ComponentDataArray<Human> Human;
}

[Inject] private HumanData humanDatum;

NativeArray<float> randomFloats = new NativeArray<float>(1000000, Allocator.Persistent);
NativeArray<float> randomFloats2 = new NativeArray<float>(1000000, Allocator.Persistent);

struct HumanNavigationJob : IJobParallelFor
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
public HumanData humanDatum;
public void Execute(int index)
for (int i = 0; i < humanDatum.Length; i++)
{
Heading2D heading2D = humanDatum.Heading[index];
heading2D.Value = new Unity.Mathematics.float2(1f, 0f);
humanDatum.Heading[index] = heading2D;

MoveSpeed moveSpeed = humanDatum.MoveSpeed[index];
moveSpeed.speed = index;
humanDatum.MoveSpeed[index] = moveSpeed;

randomFloats[i] = Random.value;
randomFloats2[i] = Random.value;
}
}

protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var humanNavigationJob = new HumanNavigationJob
{
humanDatum = humanDatum
humanDatum = humanDatum,
dt = Time.deltaTime,
randomFloats = randomFloats,
randomFloats2 = randomFloats2
};

return humanNavigationJob.Schedule(humanDatum.Length, 64, inputDeps);
}
}

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];
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> Position;
public ComponentDataArray<Heading2D> Heading;
public ComponentDataArray<MoveSpeed> MoveSpeed;
public ComponentDataArray<Human> Human;
}
2 changes: 1 addition & 1 deletion Assets/GameCode/ZombieSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
internal class ZombieSettings : MonoBehaviour
{
public float HumanSpeed = 10;
internal int HumanCount = 1000;
public int HumanCount = 1000;


public Rect Playfield = new Rect { x = -30.0f, y = -30.0f, width = 60.0f, height = 60.0f };
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scenes/ECS Zombie Simulator.unity
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5a21cd627d80c4b4a9b41c1c1b4a3165, type: 3}
m_Name:
m_EditorClassIdentifier:
HumanSpeed: 3
HumanCount: 10000
Playfield:
serializedVersion: 2
x: -30
y: -30
width: 60
height: 60
--- !u!4 &657261337
Transform:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 6442e6b

Please sign in to comment.