Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. public unsafe class TestSystem : JobComponentSystem
  2. {
  3. private EntityQuery _query;
  4. private IReactionBuilder _reactions;
  5. private IReactionBuilder _burstReactions;
  6. private NativeObject<OnSelected> _onSelected;
  7.  
  8. protected override void OnCreate()
  9. {
  10. SetupMethods();
  11. SetupBurstMethods();
  12.  
  13. _query = GetEntityQuery(_reactions.QueryDesc, _burstReactions.QueryDesc);
  14.  
  15. RequireForUpdate(_query);
  16. }
  17.  
  18. private void SetupMethods()
  19. {
  20. _reactions = new ReactionBuilder<
  21. Delegate<OnMatchedEvent, MatchedEvent>,
  22. Delegate<OnPointerPressedEvent, PointerPressedEvent>,
  23. Delegate<DoSomethingElse, PointerPressedEvent>>();
  24.  
  25. _reactions.Add<MatchedEvent, OnMatchedEvent>();
  26.  
  27. // Test multiple methods triggered off the same component.
  28. _reactions.Add<PointerPressedEvent, OnPointerPressedEvent>();
  29. _reactions.Add<PointerPressedEvent, DoSomethingElse>();
  30. }
  31.  
  32. private void SetupBurstMethods()
  33. {
  34. // Test burst and components passed to method being different from trigger component.
  35. _burstReactions = new ReactionBuilder<
  36. Delegate<OnSelected, Translation, BoardPosition, Rotation>>();
  37.  
  38. _onSelected = _burstReactions.Add<SelectedElement, OnSelected>(new OnSelected
  39. {
  40. Count = 1000 // verify starting values passed in on creation make it through to the job.
  41. });
  42. }
  43.  
  44. protected override JobHandle OnUpdate(JobHandle inputDeps)
  45. {
  46. // Verify burst jobs are working
  47. Debug.Log($"{_onSelected.Value.Count}");
  48.  
  49. if (_onSelected.Value.Count > 1050)
  50. {
  51. // Test directly editing data jobs are using.
  52. _onSelected.Value.Count = 1000;
  53. }
  54.  
  55. return JobHandle.CombineDependencies(
  56. _reactions.Job.Schedule(_query, inputDeps),
  57. _burstReactions.Job.Schedule(_query, inputDeps));
  58. }
  59.  
  60. [BurstCompile]
  61. public struct OnSelected : IEntityHandler<Translation, BoardPosition, Rotation>
  62. {
  63. public void Invoke(Entity entity, ref Translation translation, ref BoardPosition boardPosition, ref Rotation rotation)
  64. {
  65. Count++;
  66. }
  67. public int Count;
  68. }
  69.  
  70. public struct OnMatchedEvent : IEntityHandler<MatchedEvent>
  71. {
  72. public void Invoke(Entity entity, ref MatchedEvent component)
  73. {
  74. Debug.Log($"OnMatchedEvent Invoked {entity} Count={component.Count}");
  75. Count++;
  76. }
  77.  
  78. public int Count;
  79. }
  80.  
  81. public struct OnPointerPressedEvent : IEntityHandler<PointerPressedEvent>
  82. {
  83. public void Invoke(Entity entity, ref PointerPressedEvent component)
  84. {
  85. Debug.Log($"OnPointerPressedEvent Invoked {entity} Position={component.Position}");
  86. Count++;
  87. }
  88. public int Count;
  89. }
  90.  
  91. public struct DoSomethingElse : IEntityHandler<PointerPressedEvent>
  92. {
  93. public void Invoke(Entity entity, ref PointerPressedEvent component)
  94. {
  95. Debug.Log($"DoSomethingElse Invoked {entity} Position={component.Position}");
  96. Count++;
  97. }
  98. public int Count;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement