EntityFactory.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. namespace ETModel
  3. {
  4. public static class EntityFactory
  5. {
  6. public static Entity CreateWithParent(Type type, Entity parent)
  7. {
  8. Entity component = Game.ObjectPool.Fetch(type);
  9. component.Domain = parent.Domain;
  10. component.Id = component.InstanceId;
  11. component.Parent = parent;
  12. Game.EventSystem.Awake(component);
  13. return component;
  14. }
  15. public static T CreateWithParent<T>(Entity parent) where T : Entity
  16. {
  17. Type type = typeof (T);
  18. T component = (T)Game.ObjectPool.Fetch(type);
  19. component.Domain = parent.Domain;
  20. component.Id = component.InstanceId;
  21. component.Parent = parent;
  22. Game.EventSystem.Awake(component);
  23. return component;
  24. }
  25. public static T CreateWithParent<T, A>(Entity parent, A a) where T : Entity
  26. {
  27. Type type = typeof (T);
  28. T component = (T)Game.ObjectPool.Fetch(type);
  29. component.Domain = parent.Domain;
  30. component.Id = component.InstanceId;
  31. component.Parent = parent;
  32. Game.EventSystem.Awake(component, a);
  33. return component;
  34. }
  35. public static T CreateWithParent<T, A, B>(Entity parent, A a, B b) where T : Entity
  36. {
  37. Type type = typeof (T);
  38. T component = (T)Game.ObjectPool.Fetch(type);
  39. component.Domain = parent.Domain;
  40. component.Id = component.InstanceId;
  41. component.Parent = parent;
  42. Game.EventSystem.Awake(component, a, b);
  43. return component;
  44. }
  45. public static T CreateWithParent<T, A, B, C>(Entity parent, A a, B b, C c, bool fromPool = true) where T : Entity
  46. {
  47. Type type = typeof (T);
  48. T component = (T)Game.ObjectPool.Fetch(type);
  49. component.Domain = parent.Domain;
  50. component.Id = component.InstanceId;
  51. component.Parent = parent;
  52. Game.EventSystem.Awake(component, a, b, c);
  53. return component;
  54. }
  55. public static T CreateWithParent<T, A, B, C, D>(Entity parent, A a, B b, C c, D d, bool fromPool = true) where T : Entity
  56. {
  57. Type type = typeof(T);
  58. T component = (T)Game.ObjectPool.Fetch(type);
  59. component.Domain = parent.Domain;
  60. component.Id = component.InstanceId;
  61. component.Parent = parent;
  62. Game.EventSystem.Awake(component, a, b, c, d);
  63. return component;
  64. }
  65. public static Entity Create(Entity domain, Type type)
  66. {
  67. Entity component = Game.ObjectPool.Fetch(type);
  68. component.Domain = domain ?? component;
  69. component.Id = component.InstanceId;
  70. Game.EventSystem.Awake(component);
  71. return component;
  72. }
  73. public static T Create<T>(Entity domain) where T : Entity
  74. {
  75. Type type = typeof (T);
  76. T component = (T)Game.ObjectPool.Fetch(type);
  77. component.Domain = domain ?? component;
  78. component.Id = component.InstanceId;
  79. Game.EventSystem.Awake(component);
  80. return component;
  81. }
  82. public static T Create<T, A>(Entity domain, A a) where T : Entity
  83. {
  84. Type type = typeof (T);
  85. T component = (T)Game.ObjectPool.Fetch(type);
  86. component.Domain = domain ?? component;
  87. component.Id = component.InstanceId;
  88. Game.EventSystem.Awake(component, a);
  89. return component;
  90. }
  91. public static T Create<T, A, B>(Entity domain, A a, B b) where T : Entity
  92. {
  93. Type type = typeof (T);
  94. T component = (T)Game.ObjectPool.Fetch(type);
  95. component.Domain = domain ?? component;
  96. component.Id = component.InstanceId;
  97. Game.EventSystem.Awake(component, a, b);
  98. return component;
  99. }
  100. public static T Create<T, A, B, C>(Entity domain, A a, B b, C c) where T : Entity
  101. {
  102. Type type = typeof (T);
  103. T component = (T) Game.ObjectPool.Fetch(type);
  104. component.Domain = domain ?? component;
  105. component.Id = component.InstanceId;
  106. Game.EventSystem.Awake(component, a, b, c);
  107. return component;
  108. }
  109. public static T CreateWithId<T>(Entity domain, long id) where T : Entity
  110. {
  111. Type type = typeof (T);
  112. T component = (T)Game.ObjectPool.Fetch(type);
  113. component.Domain = domain ?? component;
  114. component.Id = id;
  115. Game.EventSystem.Awake(component);
  116. return component;
  117. }
  118. public static T CreateWithId<T, A>(Entity domain, long id, A a) where T : Entity
  119. {
  120. Type type = typeof (T);
  121. T component = (T)Game.ObjectPool.Fetch(type);
  122. component.Domain = domain ?? component;
  123. component.Id = id;
  124. Game.EventSystem.Awake(component, a);
  125. return component;
  126. }
  127. public static T CreateWithId<T, A, B>(Entity domain, long id, A a, B b) where T : Entity
  128. {
  129. Type type = typeof (T);
  130. T component = (T)Game.ObjectPool.Fetch(type);
  131. component.Domain = domain ?? component;
  132. component.Id = id;
  133. Game.EventSystem.Awake(component, a, b);
  134. return component;
  135. }
  136. public static T CreateWithId<T, A, B, C>(Entity domain, long id, A a, B b, C c) where T : Entity
  137. {
  138. Type type = typeof (T);
  139. T component = (T)Game.ObjectPool.Fetch(type);
  140. component.Domain = domain ?? component;
  141. component.Id = id;
  142. Game.EventSystem.Awake(component, a, b, c);
  143. return component;
  144. }
  145. #if SERVER
  146. public static Scene CreateScene(long id, string name, SceneType sceneType, Scene parent = null)
  147. {
  148. Scene scene = (Scene)Game.ObjectPool.Fetch(typeof(Scene));
  149. scene.Id = id;
  150. Game.EventSystem.Remove(scene.InstanceId);
  151. // 高14位是进程id
  152. scene.InstanceId = IdGenerater.GenerateSceneInstanceId(id);
  153. Game.EventSystem.RegisterSystem(scene);
  154. if (parent != null)
  155. {
  156. scene.Parent = parent;
  157. }
  158. scene.Domain = scene;
  159. scene.Name = name;
  160. scene.SceneType = sceneType;
  161. return scene;
  162. }
  163. #endif
  164. public static Scene CreateScene(SceneType sceneType, string name, Scene parent = null,long id = 0)
  165. {
  166. Scene scene = (Scene)Game.ObjectPool.Fetch(typeof(Scene));
  167. scene.Id = id != 0 ? id : IdGenerater.GenerateId();
  168. scene.Name = name;
  169. scene.SceneType = sceneType;
  170. if (parent != null)
  171. {
  172. scene.Parent = parent;
  173. }
  174. scene.Domain = scene;
  175. return scene;
  176. }
  177. }
  178. }