From 423132c5679a731f8bcbd95f8afec007e32058fa Mon Sep 17 00:00:00 2001 From: zxg0622 <247274249@qq.com> Date: Tue, 18 Sep 2018 14:03:44 +0800 Subject: [PATCH] migrate app to director (#3259) --- cocos/3d/framework/animation-component.js | 9 +- cocos/3d/framework/animation-system.js | 4 +- .../3d/framework/skinning-model-component.js | 6 +- cocos/3d/framework/skinning-model-system.js | 4 +- cocos/core/CCDirector.js | 145 +++++++----------- cocos/scene-graph/base-node.js | 4 +- 6 files changed, 68 insertions(+), 104 deletions(-) diff --git a/cocos/3d/framework/animation-component.js b/cocos/3d/framework/animation-component.js index dcacb93465..e2c5fdaa69 100644 --- a/cocos/3d/framework/animation-component.js +++ b/cocos/3d/framework/animation-component.js @@ -99,6 +99,9 @@ class AnimationCtrl { } } + +@executionOrder(200) +@ccclass('AnimationComponent') export default class AnimationComponent extends Component { onInit() { this._name2states = {}; @@ -110,7 +113,7 @@ export default class AnimationComponent extends Component { */ this.clips = this._clips; - this._system.add(this); + AnimationComponent.system.add(this); if (this._playAutomatically != undefined && this._playAutomatically && @@ -121,7 +124,7 @@ export default class AnimationComponent extends Component { } onDestroy() { - this._system.remove(this); + AnimationComponent.system.remove(this); } get skeleton () { @@ -194,4 +197,4 @@ AnimationComponent.schema = { this._playAutomatically = val; } } -}; \ No newline at end of file +}; diff --git a/cocos/3d/framework/animation-system.js b/cocos/3d/framework/animation-system.js index a3de8a16e2..7ac2954a63 100644 --- a/cocos/3d/framework/animation-system.js +++ b/cocos/3d/framework/animation-system.js @@ -17,7 +17,7 @@ export default class AnimationSystem extends System { this._anims.fastRemove(this._anims.indexOf(comp)); } - tick() { + update() { for (let i = 0; i < this._anims.length; ++i) { let anim = this._anims.data[i]; if (!anim.enabled) { @@ -33,4 +33,4 @@ export default class AnimationSystem extends System { } } } -} \ No newline at end of file +} diff --git a/cocos/3d/framework/skinning-model-component.js b/cocos/3d/framework/skinning-model-component.js index 4e0f1dd966..649f72448d 100644 --- a/cocos/3d/framework/skinning-model-component.js +++ b/cocos/3d/framework/skinning-model-component.js @@ -5,6 +5,8 @@ import utils from '../misc/utils'; let _m4_tmp = mat4.create(); +@executionOrder(100) +@ccclass('SkinningModelComponent') export default class SkinningModelComponent extends ModelComponent { onInit() { this._models = []; @@ -17,13 +19,13 @@ export default class SkinningModelComponent extends ModelComponent { this._updateCastShadow(); this._updateReceiveShadow(); - this._system.add(this); + SkinningModelComponent.system.add(this); this.joints = this._joints; } onDestroy() { - this._system.remove(this); + SkinningModelComponent.system.remove(this); } _updateMatrices() { diff --git a/cocos/3d/framework/skinning-model-system.js b/cocos/3d/framework/skinning-model-system.js index da5caa6336..4011198e51 100644 --- a/cocos/3d/framework/skinning-model-system.js +++ b/cocos/3d/framework/skinning-model-system.js @@ -16,7 +16,7 @@ export default class SkinningModelSystem extends System { this._comps.fastRemove(this._comps.indexOf(comp)); } - tick() { + update() { for (let i = 0; i < this._comps.length; ++i) { let comp = this._comps.data[i]; if (comp.enabled === false) { @@ -26,4 +26,4 @@ export default class SkinningModelSystem extends System { comp._updateMatrices(); } } -} \ No newline at end of file +} diff --git a/cocos/core/CCDirector.js b/cocos/core/CCDirector.js index 529ab91295..6cca9918b9 100644 --- a/cocos/core/CCDirector.js +++ b/cocos/core/CCDirector.js @@ -31,6 +31,11 @@ import ccobject from './data/object'; import game from './CCGame'; import Scheduler from './CCScheduler'; import {autoRelease} from '../load-pipeline/auto-release-utils'; +import ComponentScheduler from '../scene-graph/component-scheduler'; +import NodeActivator from '../scene-graph/node-activator'; +import AnimationSystem from '../3d/framework/animation-system'; +import SkinningModelSystem from '../3d/framework/skinning-model-system'; +import { getClassByName } from './utils/js'; // const ComponentScheduler = require('./component-scheduler'); // const NodeActivator = require('./node-activator'); @@ -115,8 +120,6 @@ class Director extends EventTarget { // purge? this._purgeDirectorInNextLoop = false; - this._winSizeInPoints = null; - // scenes this._loadingScene = ''; this._scene = null; @@ -135,6 +138,8 @@ class Director extends EventTarget { // Action manager this._actionManager = null; + this._systems = []; + var self = this; game.on(game.EVENT_SHOW, function () { self._lastUpdate = performance.now(); @@ -148,7 +153,6 @@ class Director extends EventTarget { this._lastUpdate = performance.now(); this._paused = false; this._purgeDirectorInNextLoop = false; - this._winSizeInPoints = cc.size(0, 0); this._scheduler = new Scheduler(); if (cc.ActionManager) { @@ -170,42 +174,8 @@ class Director extends EventTarget { this._compScheduler = new ComponentScheduler(); this._nodeActivator = new NodeActivator(); - // Event manager - if (eventManager) { - eventManager.setEnabled(true); - } - - // Animation manager - if (cc.AnimationManager) { - this._animationManager = new cc.AnimationManager(); - this._scheduler.scheduleUpdate(this._animationManager, Scheduler.PRIORITY_SYSTEM, false); - } - else { - this._animationManager = null; - } - - // collision manager - if (cc.CollisionManager) { - this._collisionManager = new cc.CollisionManager(); - this._scheduler.scheduleUpdate(this._collisionManager, Scheduler.PRIORITY_SYSTEM, false); - } - else { - this._collisionManager = null; - } - - // physics manager - if (cc.PhysicsManager) { - this._physicsManager = new cc.PhysicsManager(); - this._scheduler.scheduleUpdate(this._physicsManager, Scheduler.PRIORITY_SYSTEM, false); - } - else { - this._physicsManager = null; - } - - // WidgetManager - if (cc._widgetManager) { - cc._widgetManager.init(this); - } + this.registerSystem('animation', AnimationSystem, ['AnimationComponent'], 200); + this.registerSystem('skinning-model', SkinningModelSystem, ['SkinningModelComponent'], 100); cc.loader.init(this); } @@ -315,7 +285,7 @@ class Director extends EventTarget { /** * !#en Pause the director's ticker, only involve the game logic execution. * It won't pause the rendering process nor the event manager. - * If you want to pause the entier game including rendering, audio and event, + * If you want to pause the entier game including rendering, audio and event, * please use {{#crossLink "Game.pause"}}cc.game.pause{{/crossLink}} * !#zh 暂停正在运行的场景,该暂停只会停止游戏逻辑执行,但是不会停止渲染和 UI 响应。 * 如果想要更彻底得暂停游戏,包含渲染,音频和事件,请使用 {{#crossLink "Game.pause"}}cc.game.pause{{/crossLink}}。 @@ -373,25 +343,9 @@ class Director extends EventTarget { if (eventManager) eventManager.setEnabled(true); - // Action manager - if (this._actionManager){ - this._scheduler.scheduleUpdate(this._actionManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } - - // Animation manager - if (this._animationManager) { - this._scheduler.scheduleUpdate(this._animationManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } - - // Collider manager - if (this._collisionManager) { - this._scheduler.scheduleUpdate(this._collisionManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } - - // Physics manager - if (this._physicsManager) { - this._scheduler.scheduleUpdate(this._physicsManager, cc.Scheduler.PRIORITY_SYSTEM, false); - } + this._systems.forEach((sys) => { + this._scheduler.scheduleUpdate(sys, sys._priority, false); + }); this.startAnimation(); } @@ -588,8 +542,8 @@ class Director extends EventTarget { var info = this._getSceneUuid(sceneName); if (info) { this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName); - cc.loader.load({ uuid: info.uuid, type: 'uuid' }, - onProgress, + cc.loader.load({ uuid: info.uuid, type: 'uuid' }, + onProgress, function (error, asset) { if (error) { cc.errorID(1210, sceneName, error.message); @@ -597,7 +551,7 @@ class Director extends EventTarget { if (onLoaded) { onLoaded(error, asset); } - }); + }); } else { var error = 'Can not preload the scene "' + sceneName + '" because it is not in the build settings.'; @@ -821,6 +775,41 @@ class Director extends EventTarget { } } + /** + * !#en register a system. + * !#zh 注册一个system。 + * @method registerSystem + * @param {string} name + * @param {function} cls + * @param {Array} compClsNames + * @param {number} priority + * @return {System} + */ + registerSystem(name, cls, compClsNames, priority) { + let sys = new cls(); + sys._id = name; + sys._priority = priority; + compClsNames.forEach((compName) => { + getClassByName(compName).system = sys; + }); + this._scheduler.scheduleUpdate(sys, priority, false); + sys.init(); + this._systems.push(sys); + return sys; + } + + /** + * !#en get a system. + * !#zh 获取一个system。 + * @param {string} name + * @return {System} + */ + getSystem(name) { + return this._systems.find((sys) => { + return sys._id === name; + }); + } + /** * !#en Returns the cc.ActionManager associated with this director. * !#zh 获取和 director 相关联的 cc.ActionManager(动作管理器)。 @@ -846,36 +835,6 @@ class Director extends EventTarget { } } - /* - * !#en Returns the cc.AnimationManager associated with this director. - * !#zh 获取和 director 相关联的 cc.AnimationManager(动画管理器)。 - * @method getAnimationManager - * @return {AnimationManager} - */ - getAnimationManager () { - return this._animationManager; - } - - /** - * !#en Returns the cc.CollisionManager associated with this director. - * !#zh 获取和 director 相关联的 cc.CollisionManager (碰撞管理器)。 - * @method getCollisionManager - * @return {CollisionManager} - */ - getCollisionManager () { - return this._collisionManager; - } - - /** - * !#en Returns the cc.PhysicsManager associated with this director. - * !#zh 返回与 director 相关联的 cc.PhysicsManager (物理管理器)。 - * @method getPhysicsManager - * @return {PhysicsManager} - */ - getPhysicsManager () { - return this._physicsManager; - } - // Loop management /* * Starts Animation @@ -1056,4 +1015,4 @@ Director.EVENT_AFTER_DRAW = "director_after_draw"; let director = cc.director = new Director(); cc.Director = Director; cc.director = director; -export default director; \ No newline at end of file +export default director; diff --git a/cocos/scene-graph/base-node.js b/cocos/scene-graph/base-node.js index 411fb566d8..7ca586163d 100644 --- a/cocos/scene-graph/base-node.js +++ b/cocos/scene-graph/base-node.js @@ -32,7 +32,7 @@ const { ccclass, property } = _decorator; const Destroying = CCObject.Flags.Destroying; const DontDestroy = CCObject.Flags.DontDestroy; -const Deactivating = CCObject.Flags.Deactivating; +const Deactivating = CCObject.Flags.Deactivating; const CHILD_ADDED = 'child-added'; const CHILD_REMOVED = 'child-removed'; @@ -591,7 +591,7 @@ export default class BaseNode extends CCObject { // post call postfunc(curr); } - + // Avoid memory leak stack[index] = null; // Do not repeatly visit child tree, just do post call and continue walk