import { director } from "cc"; import { TweenSystem } from "cc"; import { PhysicsSystem } from "cc"; import { PhysicsSystem2D } from "cc"; import { AnimationManager } from "cc"; import { sp } from "cc"; import { Animation, AnimationState, Node } from "cc"; export default class DirectorUtil { //没有用到龙骨 dragon_bones_as:dragonBones.ArmatureDisplay[] 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[] } = { /**暂停状态 */ state_b: false, /**2d物理系统状态 */ physics_2d_b: false, /**3d物理系统状态 */ physics_3d_b: false, /**定时器对象列表 */ scheduler_as: [], /**动画列表 */ anim_as: [], /**缓动对象列表 */ tween_target_as: [], /**龙骨组件列表 */ //dragon_bones_as:[], spine_as: [] }; /**暂停游戏 */ static pause(config_?: {/**排除节点 */exclude_as?: Node[];/**递归排除节点 */recu_exclude_as?: Node[] }): void { if (this.pause_data.state_b) return; // 暂停定时器 this.pause_data.scheduler_as = director.getScheduler().pauseAllTargets(); // 暂停当前动画 { let anim_system = director.getSystem(AnimationManager.ID); this.pause_data.anim_as.splice(0, this.pause_data.anim_as.length, ...anim_system["_anims"].array); this.pause_data.anim_as.forEach(v1 => { v1.pause(); }); } // 暂停spine动画 { this.pause_data.spine_as = director.getScene().getComponentsInChildren(sp.Skeleton); this.pause_data.spine_as.forEach(v1 => { v1.timeScale = 0; }); } // 暂停龙骨动画 // { //this.pause_data.dragon_bones_as = director.getScene().getComponentsInChildren(dragonBones.ArmatureDisplay); //this.pause_data.dragon_bones_as.forEach(v1 => {v1.timeScale = 0;}); //} // 暂停当前缓动 this.pause_data.tween_target_as = TweenSystem.instance.ActionManager.pauseAllRunningActions(); // 暂停物理系统 { if (PhysicsSystem2D && PhysicsSystem2D.instance.enable) { this.pause_data.physics_2d_b = PhysicsSystem2D.instance.enable; PhysicsSystem2D.instance.enable = false; } if (PhysicsSystem && PhysicsSystem.instance.enable) { this.pause_data.physics_3d_b = PhysicsSystem.instance.enable; PhysicsSystem.instance.enable = false; } } // 恢复排除节点 if (config_) { let exclude_as: Node[] = []; exclude_as.push(...config_.exclude_as); config_.recu_exclude_as?.forEach(v1 => { exclude_as.push(...this.recu_node_list(v1)); }); exclude_as.forEach(v1 => { this.resume_node(v1); }); } this.pause_data.state_b = true; } private static recu_node_list(node_: Node, result_as: Node[] = []): Node[] { if (!node_) { return result_as; } result_as.push(node_); node_.children.forEach(v1 => { result_as.push(v1); this.recu_node_list(v1); }); return result_as; } /**恢复游戏 */ public static resume(): void { // 恢复定时器 director.getScheduler().resumeTargets(this.pause_data.scheduler_as); // 恢复动画 this.pause_data.anim_as.forEach(v1 => { if (v1.isPlaying && v1.isPaused) { v1.play(); } }); // 恢复龙骨动画 //this.pause_data.dragon_bones_as.forEach(v1 => { //v1.timeScale = 1; //}); this.pause_data.spine_as.forEach(v1 => { v1.timeScale = 1; }); // 恢复缓动 TweenSystem.instance.ActionManager.resumeTargets(this.pause_data.tween_target_as); // 恢复物理系统 { if (this.pause_data.physics_2d_b) { PhysicsSystem2D.instance.enable = this.pause_data.physics_2d_b; } if (this.pause_data.physics_3d_b) { PhysicsSystem.instance.enable = this.pause_data.physics_3d_b; } } this.pause_data.state_b = false; } /**暂停节点 * -物理系统需手动启用/禁用 */ static pause_node(node_: Node): void; static pause_node(node_as_: Node[]): void; static pause_node(args1_: Node | Node[]): void { let node_as: Node[]; if (Array.isArray(args1_)) { node_as = args1_; } else { node_as = [args1_]; } node_as.forEach(v1 => { // 暂停定时器 director.getScheduler().pauseTarget(v1); // 暂停动画 v1.getComponent(Animation)?.pause(); //暂停spine动画 if (v1.getComponent(sp.Skeleton)) { v1.getComponent(sp.Skeleton).timeScale = 0; } // 暂停龙骨 //if (v1.getComponent(dragonBones.ArmatureDisplay)) { //v1.getComponent(dragonBones.ArmatureDisplay).timeScale = 0; //} // 暂停缓动 TweenSystem.instance.ActionManager.pauseTarget(v1); }); } /**恢复节点 */ static resume_node(node_: Node): void; static resume_node(node_as_: Node[]): void; static resume_node(args1_: Node | Node[]): void { let node_as: Node[]; if (Array.isArray(args1_)) { node_as = args1_; } else { node_as = [args1_]; } node_as.forEach(v1 => { // 恢复定时器 director.getScheduler().resumeTarget(v1); // 恢复动画 v1.getComponent(Animation)?.resume(); //恢复spine动画 if (v1.getComponent(sp.Skeleton)) { v1.getComponent(sp.Skeleton).timeScale = 1; } //恢复龙骨 //if (v1.getComponent(dragonBones.ArmatureDisplay)) { //v1.getComponent(dragonBones.ArmatureDisplay).timeScale = 1; //} // 恢复缓动 TweenSystem.instance.ActionManager.resumeTarget(v1); }); } // }