DirectorUtil.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { director } from "cc";
  2. import { TweenSystem } from "cc";
  3. import { PhysicsSystem } from "cc";
  4. import { PhysicsSystem2D } from "cc";
  5. import { AnimationManager } from "cc";
  6. import { sp } from "cc";
  7. import { Animation, AnimationState, Node } from "cc";
  8. export default class DirectorUtil {
  9. //没有用到龙骨 dragon_bones_as:dragonBones.ArmatureDisplay[]
  10. private static pause_data: { state_b: boolean, physics_2d_b: boolean, physics_3d_b: boolean, scheduler_as: any[], anim_as: AnimationState[], tween_target_as: any[], spine_as: sp.Skeleton[] } = {
  11. /**暂停状态 */
  12. state_b: false,
  13. /**2d物理系统状态 */
  14. physics_2d_b: false,
  15. /**3d物理系统状态 */
  16. physics_3d_b: false,
  17. /**定时器对象列表 */
  18. scheduler_as: [],
  19. /**动画列表 */
  20. anim_as: [],
  21. /**缓动对象列表 */
  22. tween_target_as: [],
  23. /**龙骨组件列表 */
  24. //dragon_bones_as:[],
  25. spine_as: []
  26. };
  27. /**暂停游戏 */
  28. static pause(config_?: {/**排除节点 */exclude_as?: Node[];/**递归排除节点 */recu_exclude_as?: Node[] }): void {
  29. if (this.pause_data.state_b) return;
  30. // 暂停定时器
  31. this.pause_data.scheduler_as = director.getScheduler().pauseAllTargets();
  32. // 暂停当前动画
  33. {
  34. let anim_system = director.getSystem(AnimationManager.ID);
  35. this.pause_data.anim_as.splice(0, this.pause_data.anim_as.length, ...anim_system["_anims"].array);
  36. this.pause_data.anim_as.forEach(v1 => { v1.pause(); });
  37. }
  38. // 暂停spine动画
  39. {
  40. this.pause_data.spine_as = director.getScene().getComponentsInChildren(sp.Skeleton);
  41. this.pause_data.spine_as.forEach(v1 => { v1.timeScale = 0; });
  42. }
  43. // 暂停龙骨动画
  44. // {
  45. //this.pause_data.dragon_bones_as = director.getScene().getComponentsInChildren(dragonBones.ArmatureDisplay);
  46. //this.pause_data.dragon_bones_as.forEach(v1 => {v1.timeScale = 0;});
  47. //}
  48. // 暂停当前缓动
  49. this.pause_data.tween_target_as = TweenSystem.instance.ActionManager.pauseAllRunningActions();
  50. // 暂停物理系统
  51. {
  52. if (PhysicsSystem2D && PhysicsSystem2D.instance.enable) {
  53. this.pause_data.physics_2d_b = PhysicsSystem2D.instance.enable;
  54. PhysicsSystem2D.instance.enable = false;
  55. }
  56. if (PhysicsSystem && PhysicsSystem.instance.enable) {
  57. this.pause_data.physics_3d_b = PhysicsSystem.instance.enable;
  58. PhysicsSystem.instance.enable = false;
  59. }
  60. }
  61. // 恢复排除节点
  62. if (config_) {
  63. let exclude_as: Node[] = [];
  64. exclude_as.push(...config_.exclude_as);
  65. config_.recu_exclude_as?.forEach(v1 => {
  66. exclude_as.push(...this.recu_node_list(v1));
  67. });
  68. exclude_as.forEach(v1 => {
  69. this.resume_node(v1);
  70. });
  71. }
  72. this.pause_data.state_b = true;
  73. }
  74. private static recu_node_list(node_: Node, result_as: Node[] = []): Node[] {
  75. if (!node_) {
  76. return result_as;
  77. }
  78. result_as.push(node_);
  79. node_.children.forEach(v1 => {
  80. result_as.push(v1);
  81. this.recu_node_list(v1);
  82. });
  83. return result_as;
  84. }
  85. /**恢复游戏 */
  86. public static resume(): void {
  87. // 恢复定时器
  88. director.getScheduler().resumeTargets(this.pause_data.scheduler_as);
  89. // 恢复动画
  90. this.pause_data.anim_as.forEach(v1 => {
  91. if (v1.isPlaying && v1.isPaused) {
  92. v1.play();
  93. }
  94. });
  95. // 恢复龙骨动画
  96. //this.pause_data.dragon_bones_as.forEach(v1 => {
  97. //v1.timeScale = 1;
  98. //});
  99. this.pause_data.spine_as.forEach(v1 => {
  100. v1.timeScale = 1;
  101. });
  102. // 恢复缓动
  103. TweenSystem.instance.ActionManager.resumeTargets(this.pause_data.tween_target_as);
  104. // 恢复物理系统
  105. {
  106. if (this.pause_data.physics_2d_b) {
  107. PhysicsSystem2D.instance.enable = this.pause_data.physics_2d_b;
  108. }
  109. if (this.pause_data.physics_3d_b) {
  110. PhysicsSystem.instance.enable = this.pause_data.physics_3d_b;
  111. }
  112. }
  113. this.pause_data.state_b = false;
  114. }
  115. /**暂停节点
  116. * -物理系统需手动启用/禁用
  117. */
  118. static pause_node(node_: Node): void;
  119. static pause_node(node_as_: Node[]): void;
  120. static pause_node(args1_: Node | Node[]): void {
  121. let node_as: Node[];
  122. if (Array.isArray(args1_)) {
  123. node_as = args1_;
  124. } else {
  125. node_as = [args1_];
  126. }
  127. node_as.forEach(v1 => {
  128. // 暂停定时器
  129. director.getScheduler().pauseTarget(v1);
  130. // 暂停动画
  131. v1.getComponent(Animation)?.pause();
  132. //暂停spine动画
  133. if (v1.getComponent(sp.Skeleton)) {
  134. v1.getComponent(sp.Skeleton).timeScale = 0;
  135. }
  136. // 暂停龙骨
  137. //if (v1.getComponent(dragonBones.ArmatureDisplay)) {
  138. //v1.getComponent(dragonBones.ArmatureDisplay).timeScale = 0;
  139. //}
  140. // 暂停缓动
  141. TweenSystem.instance.ActionManager.pauseTarget(v1);
  142. });
  143. }
  144. /**恢复节点 */
  145. static resume_node(node_: Node): void;
  146. static resume_node(node_as_: Node[]): void;
  147. static resume_node(args1_: Node | Node[]): void {
  148. let node_as: Node[];
  149. if (Array.isArray(args1_)) {
  150. node_as = args1_;
  151. } else {
  152. node_as = [args1_];
  153. }
  154. node_as.forEach(v1 => {
  155. // 恢复定时器
  156. director.getScheduler().resumeTarget(v1);
  157. // 恢复动画
  158. v1.getComponent(Animation)?.resume();
  159. //恢复spine动画
  160. if (v1.getComponent(sp.Skeleton)) {
  161. v1.getComponent(sp.Skeleton).timeScale = 1;
  162. }
  163. //恢复龙骨
  164. //if (v1.getComponent(dragonBones.ArmatureDisplay)) {
  165. //v1.getComponent(dragonBones.ArmatureDisplay).timeScale = 1;
  166. //}
  167. // 恢复缓动
  168. TweenSystem.instance.ActionManager.resumeTarget(v1);
  169. });
  170. }
  171. //
  172. }