RunnerSpawnerBehaviour.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using GPUECSAnimationBaker.Engine.AnimatorSystem;
  3. using Unity.Entities;
  4. using UnityEngine;
  5. using Random = Unity.Mathematics.Random;
  6. namespace GPUECSAnimationBaker.Samples.SampleScenes.Marathon.RunnerSystems
  7. {
  8. public class RunnerSpawnerBehaviour : MonoBehaviour
  9. {
  10. public GameObject[] gpuEcsAnimatorPrefabs;
  11. public float walkCycleDistance;
  12. public AnimationClip walkAnimation;
  13. public float runCycleDistance;
  14. public AnimationClip runAnimation;
  15. public float sprintCycleDistance;
  16. public AnimationClip sprintAnimation;
  17. public float minSpeed;
  18. public float maxSpeed;
  19. }
  20. public class RunnerSpawnerBaker : Baker<RunnerSpawnerBehaviour>
  21. {
  22. public override void Bake(RunnerSpawnerBehaviour authoring)
  23. {
  24. float speedWalking = authoring.walkCycleDistance / authoring.walkAnimation.length;
  25. float speedRunning = authoring.runCycleDistance / authoring.runAnimation.length;
  26. float speedSprinting = authoring.sprintCycleDistance / authoring.sprintAnimation.length;
  27. float minSpeed = Math.Min(authoring.minSpeed, speedWalking);
  28. float maxSpeed = Math.Max(authoring.maxSpeed, speedSprinting);
  29. Entity entity = GetEntity(TransformUsageFlags.None);
  30. AddComponent(entity, new RunnerSpawnerComponent()
  31. {
  32. fieldSizeZ = 0,
  33. fieldSizeX = 0,
  34. nbrOfRunners = 0,
  35. speedWalking = speedWalking,
  36. speedRunning = speedRunning,
  37. speedSprinting = speedSprinting,
  38. minSpeed = minSpeed,
  39. maxSpeed = maxSpeed
  40. });
  41. AddComponent(entity, new RunnerSpawnerUpdateComponent()
  42. {
  43. fieldSizeZ = 0,
  44. fieldSizeX = 0,
  45. nbrOfRunners = 0,
  46. updateTime = 0,
  47. random = Random.CreateFromIndex((uint)Mathf.RoundToInt(Time.time))
  48. });
  49. DynamicBuffer<RunnerSpawnerAnimatorPrefabBufferElement> crowdSpawnerAnimatorPrefabs
  50. = AddBuffer<RunnerSpawnerAnimatorPrefabBufferElement>(entity);
  51. foreach(GameObject gpuEcsAnimatorPrefab in authoring.gpuEcsAnimatorPrefabs)
  52. {
  53. crowdSpawnerAnimatorPrefabs.Add(new RunnerSpawnerAnimatorPrefabBufferElement()
  54. {
  55. gpuEcsAnimatorPrefab = GetEntity(gpuEcsAnimatorPrefab,
  56. gpuEcsAnimatorPrefab.GetComponent<GpuEcsAnimatorBehaviour>().transformUsageFlags)
  57. });
  58. }
  59. AddBuffer<RunnerSpawnerAnimatorBufferElement>(entity);
  60. }
  61. }
  62. }