Skip to content

Commit

Permalink
1000 humans spawn in random locations
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlyApps committed Apr 16, 2018
1 parent 00aa3cc commit 5fc4b7c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Assets/GameCode/HumanNavigationSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Transforms2D;
using UnityEngine;

class HumanNavigationSystem : JobComponentSystem
{
Expand All @@ -9,6 +12,7 @@ public struct HumanData
public int Length;
public ComponentDataArray<Position2D> Position;
public ComponentDataArray<Heading2D> Heading;
public ComponentDataArray<MoveSpeed> MoveSpeed;
public ComponentDataArray<Human> Human;
}

Expand All @@ -23,6 +27,11 @@ public void Execute(int index)
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;

}
}

Expand All @@ -35,4 +44,6 @@ protected override JobHandle OnUpdate(JobHandle inputDeps)

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


}
22 changes: 22 additions & 0 deletions Assets/GameCode/ZombieSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,26 @@
internal class ZombieSettings : MonoBehaviour
{
public float HumanSpeed = 10;
internal int HumanCount = 1000;


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


public static ZombieSettings Instance { get; private set; }

void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
}


}
30 changes: 28 additions & 2 deletions Assets/GameCode/ZombieSimulatorBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,44 @@ public static void InitializeWithScene()

private static void NewGame()
{

var entityManager = World.Active.GetOrCreateManager<EntityManager>();

for (int i = 0; i < Settings.HumanCount; i++)
{
CreateHuman(entityManager);
}
}

private static void CreateHuman(EntityManager entityManager)
{
Entity firstHuman = entityManager.CreateEntity(HumanArchetype);

var randomSpawnLocation = ComputeSpawnLocation();

// We can tweak a few components to make more sense like this.
entityManager.SetComponentData(firstHuman, new Position2D { Value = new float2(0.0f, 0.0f) });
entityManager.SetComponentData(firstHuman, new Position2D { Value = randomSpawnLocation });
entityManager.SetComponentData(firstHuman, new Heading2D { Value = new float2(0.0f, 1.0f) });
entityManager.SetComponentData(firstHuman, new MoveSpeed { speed = Settings.HumanSpeed });

// Finally we add a shared component which dictates the rendered look
entityManager.AddSharedComponentData(firstHuman, HumanLook);
}

private static float2 ComputeSpawnLocation()
{
var settings = ZombieSettings.Instance;

float r = Random.value;
float x0 = settings.Playfield.xMin;
float x1 = settings.Playfield.xMax;
float x = x0 + (x1 - x0) * r;

float r2 = Random.value;
float y0 = settings.Playfield.yMin;
float y1 = settings.Playfield.yMax;
float y = y0 + (y1 - y0) * r2;

return new float2(x, y);
}

private static void DefineArchetypes(EntityManager entityManager)
Expand Down

0 comments on commit 5fc4b7c

Please sign in to comment.