Advertisement
Guest User

Untitled

a guest
Apr 6th, 2018
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. using Unity.Entities;
  2. using Unity.Mathematics;
  3. using Unity.Transforms2D;
  4. using UnityEngine;
  5. using Unity.Jobs;
  6.  
  7. //Just gives the animals a little movement as an example
  8. public class EntityMovementSystem : JobComponentSystem
  9. {
  10. [Inject] private Data data;
  11.  
  12. [ComputeJobOptimization]
  13. struct PositionJob : IJobProcessComponentData<Position2D>
  14. {
  15. public float dt;
  16.  
  17. public void Execute(ref Position2D position)
  18. {
  19. float wobbleX = Mathf.PerlinNoise(position.Value.x, position.Value.y) - 0.5f;
  20. float wobbleY = Mathf.PerlinNoise(position.Value.y, position.Value.x) - 0.5f;
  21. position.Value += dt * new float2(wobbleX, wobbleY);
  22. }
  23. }
  24.  
  25. // protected override void OnUpdate()
  26. // {
  27. // var dt = Time.deltaTime;
  28.  
  29. // for (var index = 0; index < data.Length; ++index)
  30. // {
  31. // var position = data.Position[index].Value;
  32. // var heading = data.Heading[index].Value;
  33.  
  34. // float wobbleX = Mathf.PerlinNoise(position.x, position.y) - 0.5f;
  35. // float wobbleY = Mathf.PerlinNoise(position.y, position.x) - 0.5f;
  36. // position += dt * new float2(wobbleX, wobbleY);
  37.  
  38. // data.Position[index] = new Position2D {Value = position};
  39. // data.Heading[index] = new Heading2D {Value = heading};
  40. // }
  41. // }
  42.  
  43. protected override JobHandle OnUpdate(JobHandle inputDeps)
  44. {
  45. var job = new PositionJob() { dt = Time.deltaTime };
  46. return job.Schedule(this, 64, inputDeps);
  47. }
  48.  
  49. public struct Data
  50. {
  51. public int Length;
  52. public ComponentDataArray<Position2D> Position;
  53. public ComponentDataArray<Heading2D> Heading;
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement