Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
6.0KB

  1. #if UNITY_ECS
  2. using Svelto.Common;
  3. using Svelto.ECS.Schedulers;
  4. using Unity.Entities;
  5. using Unity.Jobs;
  6. namespace Svelto.ECS.SveltoOnDOTS
  7. {
  8. /// <summary>
  9. /// This is a high level class to abstract the complexity of creating a Svelto ECS application that interacts
  10. /// with DOTS ECS.
  11. /// This is a JobifiedEngine and as such it expect to be ticked. Normally it must be executed in a
  12. /// SortedEnginesGroup as step that happens after the Svelto jobified engines run.
  13. ///
  14. /// The flow should be:
  15. /// Svelto (GameLogic) Engines Run first
  16. /// Then this Engine runs, which causes:
  17. /// Jobs to be completed (it's a sync point)
  18. /// Synchronizations engines to be executed (Svelto to DOTS ECS)
  19. /// Submission of Entities to be executed
  20. /// Svelto Add/Remove callbacks to be called
  21. /// ISveltoOnDOTSStructuralEngine to be executed
  22. /// DOTS ECS engines to executed
  23. /// Synchronizations engines to be executed (DOTS ECS To Svelto)
  24. /// </summary>
  25. [Sequenced(nameof(JobifiedSveltoEngines.SveltoOnDOTS))]
  26. public class SveltoOnDOTSEnginesGroup: IJobifiedEngine
  27. {
  28. public SveltoOnDOTSEnginesGroup(EnginesRoot enginesRoot)
  29. {
  30. DBC.ECS.Check.Require(
  31. enginesRoot.scheduler is SimpleEntitiesSubmissionScheduler
  32. , "The Engines root must use a EntitiesSubmissionScheduler scheduler implementation");
  33. CreateUnityECSWorldForSvelto(enginesRoot.scheduler as SimpleEntitiesSubmissionScheduler, enginesRoot);
  34. }
  35. /// <summary>
  36. /// for the user to add pure DOTS ECS SystemBase/ISystem systems to the DOTS ECS world
  37. /// </summary>
  38. public World world { get; private set; }
  39. public ISveltoOnDOTSSubmission submitter => _sveltoDotsEntitiesSubmissionGroup;
  40. public JobHandle Execute(JobHandle inputDeps)
  41. {
  42. //this is a sync point, there won't be pending jobs after this
  43. _sveltoDotsEntitiesSubmissionGroup.SubmitEntities(inputDeps);
  44. //Mixed explicit job dependency and internal automatic ECS dependency system
  45. //Write in to DOTS ECS entities so the DOTS ECS dependencies react on the components touched
  46. var handle = _syncSveltoToDotsGroup.Execute(default);
  47. //As long as pure DOTS ECS systems do not use external containers (like native arrays and so) the Unity
  48. //automatic dependencies system will guarantee that there won't be race conditions
  49. world.Update();
  50. //this svelto group of DOTS ECS SystemBase systems
  51. return _syncDotsToSveltoGroup.Execute(handle);
  52. }
  53. public string name => nameof(SveltoOnDOTSEnginesGroup);
  54. public void AddSveltoToDOTSSyncEngine(SyncSveltoToDOTSEngine engine)
  55. {
  56. //it's a Svelto Engine/DOTS ECS SystemBase so it must be added in the DOTS ECS world AND svelto enginesRoot
  57. #if UNITY_ECS_100
  58. world.AddSystemManaged(engine);
  59. #else
  60. world.AddSystem(engine);
  61. #endif
  62. _enginesRoot.AddEngine(engine);
  63. _syncSveltoToDotsGroup.Add(engine);
  64. }
  65. public void AddDOTSToSveltoSyncEngine(SyncDOTSToSveltoEngine engine)
  66. {
  67. //it's a Svelto Engine/DOTS ECS SystemBase so it must be added in the DOTS ECS world AND svelto enginesRoot
  68. #if UNITY_ECS_100
  69. world.AddSystemManaged(engine);
  70. #else
  71. world.AddSystem(engine);
  72. #endif
  73. _enginesRoot.AddEngine(engine);
  74. _syncDotsToSveltoGroup.Add(engine);
  75. }
  76. public void AddSveltoOnDOTSSubmissionEngine(ISveltoOnDOTSStructuralEngine submissionEngine)
  77. {
  78. _sveltoDotsEntitiesSubmissionGroup.Add(submissionEngine);
  79. if (submissionEngine is IEngine enginesRootEngine)
  80. _enginesRoot.AddEngine(enginesRootEngine);
  81. }
  82. public void Dispose()
  83. {
  84. world.Dispose();
  85. }
  86. void CreateUnityECSWorldForSvelto(SimpleEntitiesSubmissionScheduler scheduler, EnginesRoot enginesRoot)
  87. {
  88. world = new World("Svelto<>DOTS world");
  89. var systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default);
  90. DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
  91. World.DefaultGameObjectInjectionWorld = world;
  92. //This is the DOTS ECS group that takes care of all the DOTS ECS systems that creates entities
  93. //it also submits Svelto entities through the scheduler
  94. var defaultSveltoOnDotsHandleLifeTimeEngine = new SveltoOnDOTSHandleLifeTimeEngine<DOTSEntityComponent>();
  95. _sveltoDotsEntitiesSubmissionGroup = new SveltoOnDOTSEntitiesSubmissionGroup(scheduler);
  96. enginesRoot.AddEngine(defaultSveltoOnDotsHandleLifeTimeEngine);
  97. _sveltoDotsEntitiesSubmissionGroup.Add(defaultSveltoOnDotsHandleLifeTimeEngine);
  98. enginesRoot.AddEngine(_sveltoDotsEntitiesSubmissionGroup);
  99. #if UNITY_ECS_100
  100. world.AddSystemManaged(_sveltoDotsEntitiesSubmissionGroup);
  101. #else
  102. world.AddSystem(_sveltoDotsEntitiesSubmissionGroup);
  103. #endif
  104. //This is the group that handles the DOTS ECS sync systems that copy the svelto entities values to DOTS ECS entities
  105. _syncSveltoToDotsGroup = new SyncSveltoToDOTSGroup();
  106. enginesRoot.AddEngine(_syncSveltoToDotsGroup);
  107. //This is the group that handles the DOTS ECS sync systems that copy the DOTS ECS entities values to svelto entities
  108. _syncDotsToSveltoGroup = new SyncDOTSToSveltoGroup();
  109. enginesRoot.AddEngine(_syncDotsToSveltoGroup);
  110. enginesRoot.AddEngine(this);
  111. _enginesRoot = enginesRoot;
  112. }
  113. EnginesRoot _enginesRoot;
  114. SveltoOnDOTSEntitiesSubmissionGroup _sveltoDotsEntitiesSubmissionGroup;
  115. SyncSveltoToDOTSGroup _syncSveltoToDotsGroup;
  116. SyncDOTSToSveltoGroup _syncDotsToSveltoGroup;
  117. }
  118. }
  119. #endif