Mirror of Svelto.ECS because we're a fan of it
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

167 linhas
6.8KB

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