Created
October 22, 2021 12:48
-
-
Save JohnnyTurbo/332fa2f8b397c3188e774c4a1c47ebcd to your computer and use it in GitHub Desktop.
Code used in Entity Queries tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/7GDpzTnFe90
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
namespace TMG.EntityQueries | |
{ | |
[GenerateAuthoringComponent] | |
public struct CommanderTag : IComponentData {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
namespace TMG.EntityQueries | |
{ | |
[GenerateAuthoringComponent] | |
public struct JumpData : IComponentData | |
{ | |
public float Value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Transforms; | |
using UnityEngine; | |
namespace TMG.EntityQueries | |
{ | |
[DisableAutoCreation] | |
public class SoldierMarchForEachSystem : SystemBase | |
{ | |
private EntityQuery _soldierQuery; | |
protected override void OnStartRunning() | |
{ | |
Debug.Log($"There are {_soldierQuery.CalculateEntityCount()} soldiers in the parade!"); | |
} | |
protected override void OnUpdate() | |
{ | |
var deltaTime = Time.DeltaTime; | |
Entities | |
.WithStoreEntityQueryInField(ref _soldierQuery) | |
.ForEach((Entity e, ref Translation position, in SoldierMoveData soldierMoveData) => | |
{ | |
position.Value.x += soldierMoveData.Value * deltaTime; | |
}).ScheduleParallel(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Entities; | |
using Unity.Transforms; | |
namespace TMG.EntityQueries | |
{ | |
//[DisableAutoCreation] | |
public class SoldierMarchJobSystem : SystemBase | |
{ | |
protected override void OnUpdate() | |
{ | |
var soldierQuery = GetEntityQuery(typeof(Translation), ComponentType.ReadOnly<SoldierMoveData>()); | |
var soldierMarchJob = new SoldierMarchJob | |
{ | |
TranslationTypeHandle = GetComponentTypeHandle<Translation>(false), | |
SoldierMoveTypeHandle = GetComponentTypeHandle<SoldierMoveData>(true), | |
DeltaTime = Time.DeltaTime | |
}; | |
Dependency = soldierMarchJob.ScheduleParallel(soldierQuery, 1, Dependency); | |
RequireForUpdate(soldierQuery); | |
} | |
} | |
public struct SoldierMarchJob : IJobEntityBatch | |
{ | |
public ComponentTypeHandle<Translation> TranslationTypeHandle; | |
[ReadOnly] public ComponentTypeHandle<SoldierMoveData> SoldierMoveTypeHandle; | |
public float DeltaTime; | |
[BurstCompile] | |
public void Execute(ArchetypeChunk batchInChunk, int batchIndex) | |
{ | |
var translationArray = batchInChunk.GetNativeArray(TranslationTypeHandle); | |
var soldierMoveDataArray = batchInChunk.GetNativeArray(SoldierMoveTypeHandle); | |
for (var i = 0; i < batchInChunk.Count; i++) | |
{ | |
var translation = translationArray[i]; | |
var soldierMoveData = soldierMoveDataArray[i]; | |
translation.Value.x += soldierMoveData.Value * DeltaTime; | |
translationArray[i] = translation; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
namespace TMG.EntityQueries | |
{ | |
[GenerateAuthoringComponent] | |
public struct SoldierMoveData : IComponentData | |
{ | |
public float Value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Transforms; | |
namespace TMG.EntityQueries | |
{ | |
//[DisableAutoCreation] | |
public class SpecOpsMarchJobSystem : SystemBase | |
{ | |
private EntityQueryDesc _entityQueryDesc; | |
protected override void OnCreate() | |
{ | |
_entityQueryDesc = new EntityQueryDesc | |
{ | |
All = new[] {ComponentType.ReadOnly<SoldierMoveData>()}, | |
Any = new []{ComponentType.ReadOnly<SpinSpeedData>(), ComponentType.ReadOnly<JumpData>()}, | |
None = new[] {ComponentType.ReadOnly<CommanderTag>()} | |
}; | |
RequireForUpdate(GetEntityQuery(_entityQueryDesc)); | |
} | |
protected override void OnUpdate() | |
{ | |
var soldierQuery = GetEntityQuery(_entityQueryDesc); | |
var soldierMarchJob = new ComplexSoldierMarchJob() | |
{ | |
TranslationTypeHandle = GetComponentTypeHandle<Translation>(false), | |
RotationTypeHandle = GetComponentTypeHandle<Rotation>(false), | |
SpinSpeedTypeHandle = GetComponentTypeHandle<SpinSpeedData>(true), | |
JumpDataTypeHandle = GetComponentTypeHandle<JumpData>(true), | |
DeltaTime = Time.DeltaTime, | |
CurrentTime = (float) Time.ElapsedTime | |
}; | |
Dependency = soldierMarchJob.ScheduleParallel(soldierQuery, 1, Dependency); | |
} | |
} | |
public struct ComplexSoldierMarchJob : IJobEntityBatch | |
{ | |
public ComponentTypeHandle<Translation> TranslationTypeHandle; | |
public ComponentTypeHandle<Rotation> RotationTypeHandle; | |
[ReadOnly] public ComponentTypeHandle<SpinSpeedData> SpinSpeedTypeHandle; | |
[ReadOnly] public ComponentTypeHandle<JumpData> JumpDataTypeHandle; | |
public float DeltaTime; | |
public float CurrentTime; | |
[BurstCompile] | |
public void Execute(ArchetypeChunk batchInChunk, int batchIndex) | |
{ | |
if (batchInChunk.Has(SpinSpeedTypeHandle)) | |
{ | |
var spinSpeedDataArray = batchInChunk.GetNativeArray(SpinSpeedTypeHandle); | |
var rotationDataArray = batchInChunk.GetNativeArray(RotationTypeHandle); | |
for (var i = 0; i < batchInChunk.Count; i++) | |
{ | |
var rotation = rotationDataArray[i]; | |
rotation.Value = math.mul(rotation.Value, | |
quaternion.RotateZ(math.radians(spinSpeedDataArray[i].Value * DeltaTime))); | |
rotationDataArray[i] = rotation; | |
} | |
} | |
else if(batchInChunk.Has(JumpDataTypeHandle)) | |
{ | |
var translationArray = batchInChunk.GetNativeArray(TranslationTypeHandle); | |
var jumpDataArray = batchInChunk.GetNativeArray(JumpDataTypeHandle); | |
for (var i = 0; i < batchInChunk.Count; i++) | |
{ | |
var translation = translationArray[i]; | |
var magnitude = jumpDataArray[i].Value; | |
translation.Value.y = magnitude * math.cos(CurrentTime * 4 + 1f) + 2f; | |
translationArray[i] = translation; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
namespace TMG.EntityQueries | |
{ | |
[GenerateAuthoringComponent] | |
public struct SpinSpeedData : IComponentData | |
{ | |
public float Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment