Skip to content

Commit

Permalink
Zombies have entered the field
Browse files Browse the repository at this point in the history
  • Loading branch information
jweimann committed Apr 19, 2018
1 parent 869ec12 commit 5bbf555
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 10 deletions.
76 changes: 76 additions & 0 deletions Assets/Art/ZombieSkin.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: ZombieSkin
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 1
m_DoubleSidedGI: 1
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 10305, guid: 0000000000000000f000000000000000, type: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.48742437, g: 0.94509804, b: 0.35294116, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
8 changes: 8 additions & 0 deletions Assets/Art/ZombieSkin.mat.meta

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

2 changes: 1 addition & 1 deletion Assets/GameCode/ComponentTypes/Human.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
public struct Human : IComponentData
{
public float TimeTillNextDirectionChange;
}
}
6 changes: 6 additions & 0 deletions Assets/GameCode/ComponentTypes/Zombie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Unity.Entities;

public struct Zombie : IComponentData
{

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

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

46 changes: 46 additions & 0 deletions Assets/GameCode/ZombieNavigationSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Transforms2D;
using UnityEngine;

class ZombieNavigationSystem : JobComponentSystem
{
[Inject] private ZombieData zombieDatum;

protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new ZombieNavigationJob
{
zombieDatum = zombieDatum,
dt = Time.deltaTime
};

return job.Schedule(zombieDatum.Length, 64, inputDeps);
}
}

public struct ZombieNavigationJob : IJobParallelFor
{
public ZombieData zombieDatum;
public float dt;

public void Execute(int index)
{
var zombie = zombieDatum.Zombie[index];

var heading = zombieDatum.Heading[index];
heading.Value = new float2(0f, 1f);
zombieDatum.Heading[index] = heading;
}
}

public struct ZombieData
{
public int Length;
public ComponentDataArray<Position2D> Position;
public ComponentDataArray<Heading2D> Heading;
public ComponentDataArray<MoveSpeed> MoveSpeed;
public ComponentDataArray<Zombie> Zombie;
}
11 changes: 11 additions & 0 deletions Assets/GameCode/ZombieNavigationSystem.cs.meta

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

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

public int ZombieCount = 10;

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
38 changes: 33 additions & 5 deletions Assets/GameCode/ZombieSimulatorBootstrap.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Unity.Collections;
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
Expand All @@ -12,8 +13,10 @@ public class ZombieSimulatorBootstrap
private static ZombieSettings Settings;

public static MeshInstanceRenderer HumanLook;
public static MeshInstanceRenderer ZombieLook;

public static EntityArchetype HumanArchetype { get; private set; }
public static EntityArchetype ZombieArchetype { get; private set; }

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Initialize()
Expand All @@ -25,13 +28,13 @@ public static void Initialize()
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
public static void InitializeWithScene()
{

var settingsGO = GameObject.Find("ZombieSettings");
Settings = settingsGO?.GetComponent<ZombieSettings>();
if (!Settings)
return;

HumanLook = GetLookFromPrototype("HumanRenderPrototype");
ZombieLook = GetLookFromPrototype("ZombieRenderPrototype");

NewGame();
}
Expand All @@ -40,6 +43,26 @@ private static void NewGame()
{
var entityManager = World.Active.GetOrCreateManager<EntityManager>();
CreateHumans(entityManager);
CreateZombies(entityManager);
}

private static void CreateZombies(EntityManager entityManager)
{
NativeArray<Entity> zombies = new NativeArray<Entity>(Settings.ZombieCount, Allocator.Persistent);
entityManager.CreateEntity(ZombieArchetype, zombies);

foreach (var zombie in zombies)
{
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);
}
}

private static void CreateHumans(EntityManager entityManager)
Expand All @@ -66,12 +89,12 @@ private static float2 ComputeSpawnLocation()
{
var settings = ZombieSettings.Instance;

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

float r2 = Random.value;
float r2 = UnityEngine.Random.value;
float y0 = settings.Playfield.yMin;
float y1 = settings.Playfield.yMax;
float y = y0 + (y1 - y0) * r2;
Expand All @@ -89,13 +112,18 @@ private static void DefineArchetypes(EntityManager entityManager)
typeof(Position2D),
typeof(TransformMatrix));

ZombieArchetype = entityManager.CreateArchetype(typeof(Zombie),
typeof(Heading2D),
typeof(MoveSpeed),
typeof(Position2D),
typeof(TransformMatrix));
}

private static MeshInstanceRenderer GetLookFromPrototype(string protoName)
{
var proto = GameObject.Find(protoName);
var result = proto.GetComponent<MeshInstanceRendererComponent>().Value;
Object.Destroy(proto);
UnityEngine.Object.Destroy(proto);
return result;
}

Expand Down
62 changes: 60 additions & 2 deletions Assets/Scenes/ECS Zombie Simulator.unity
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 282840810}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 30, z: 0}
m_LocalPosition: {x: 0, y: 90, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down Expand Up @@ -214,7 +214,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
HumanSpeed: 3
HumanCount: 10000
HumanCount: 100
ZombieCount: 10
Playfield:
serializedVersion: 2
x: -30
Expand All @@ -234,6 +235,63 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1414045372
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1414045375}
- component: {fileID: 1414045374}
- component: {fileID: 1414045373}
m_Layer: 0
m_Name: ZombieRenderPrototype
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1414045373
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1414045372}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b0fd4427893a4a16ba0c267dfd00217, type: 3}
m_Name:
m_EditorClassIdentifier:
m_SerializedData:
mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
material: {fileID: 2100000, guid: 3d6c2423d53289c4ca084d447b18c73d, type: 2}
castShadows: 1
receiveShadows: 1
--- !u!114 &1414045374
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1414045372}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5bf10cdea1344482e91a4f2b58506b77, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1414045375
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1414045372}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1507792993
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 5bbf555

Please sign in to comment.