Skip to content

Commit

Permalink
Humans convert into zombies
Browse files Browse the repository at this point in the history
  • Loading branch information
jweimann committed Apr 19, 2018
1 parent 2079ebf commit f5e1e35
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
29 changes: 2 additions & 27 deletions Assets/GameCode/HumanInfectionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@
using Unity.Transforms2D;
using UnityEngine;

class HumanToZombieSystem : ComponentSystem
{
struct HumansToConvertData
{
public int Length;
public ComponentDataArray<Human> Humans;
public EntityArray Entities;
}

[Inject] private HumansToConvertData humanData;

protected override void OnUpdate()
{
for (int i = 0; i < humanData.Length; i++)
{
Human human = humanData.Humans[i];
if (human.IsInfected == 1)
{
PostUpdateCommands.DestroyEntity(humanData.Entities[i]);
}
}
}
}

class HumanInfectionSystem : JobComponentSystem
{
[Inject] private HumanInfectionData humanData;
Expand Down Expand Up @@ -69,10 +45,9 @@ public void Execute(int index)

if (distSquared < infectionDistance)
{
var human = humanData.Humans[i];
var human = humanData.Humans[index];
human.IsInfected = 1;
humanData.Humans[i] = human;

humanData.Humans[index] = human;
}

}
Expand Down
39 changes: 39 additions & 0 deletions Assets/GameCode/HumanToZombieSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Transforms2D;

class HumanToZombieSystem : ComponentSystem
{
struct HumansToConvertData
{
public int Length;
public ComponentDataArray<Human> Humans;
[ReadOnly] public ComponentDataArray<Position2D> Positions;
public EntityArray Entities;
}

[Inject] private HumansToConvertData humanData;

protected override void OnUpdate()
{
for (int i = 0; i < humanData.Length; i++)
{
Human human = humanData.Humans[i];
if (human.IsInfected == 1)
{
PostUpdateCommands.DestroyEntity(humanData.Entities[i]);

PostUpdateCommands.CreateEntity(ZombieSimulatorBootstrap.ZombieArchetype);
PostUpdateCommands.SetComponent(new Position2D { Value = humanData.Positions[i].Value });
PostUpdateCommands.SetComponent(new Heading2D { Value = new float2(1.0f, 0.0f) });
PostUpdateCommands.SetComponent(new MoveSpeed { speed = ZombieSettings.Instance.HumanSpeed });

// Finally we add a shared component which dictates the rendered look
PostUpdateCommands.AddSharedComponent(ZombieSimulatorBootstrap.ZombieLook);

}
}
}
}
11 changes: 11 additions & 0 deletions Assets/GameCode/HumanToZombieSystem.cs.meta

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

17 changes: 11 additions & 6 deletions Assets/GameCode/ZombieSimulatorBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@ private static void CreateZombies(EntityManager entityManager)
var randomSpawnLocation = ComputeSpawnLocation();

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

// Finally we add a shared component which dictates the rendered look
entityManager.AddSharedComponentData(zombie, ZombieLook);
InitializeZombie(entityManager, zombie, randomSpawnLocation);
}
}

public static void InitializeZombie(EntityManager entityManager, Entity zombie, float2 position)
{
entityManager.SetComponentData(zombie, new Position2D { Value = position });
entityManager.SetComponentData(zombie, new Heading2D { Value = new float2(1.0f, 0.0f) });
entityManager.SetComponentData(zombie, new MoveSpeed { speed = Settings.HumanSpeed });

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

private static void CreateHumans(EntityManager entityManager)
{
NativeArray<Entity> humans = new NativeArray<Entity>(Settings.HumanCount, Allocator.Persistent);
Expand Down
5 changes: 3 additions & 2 deletions Assets/Scenes/ECS Zombie Simulator.unity
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
HumanSpeed: 3
HumanCount: 100
ZombieCount: 10
HumanCount: 10000
ZombieCount: 100
InfectionDistance: 1
Playfield:
serializedVersion: 2
x: -30
Expand Down

0 comments on commit f5e1e35

Please sign in to comment.