diff --git a/cocos/core/CCDirector.js b/cocos/core/CCDirector.js index 48619a4d43..1afff70933 100644 --- a/cocos/core/CCDirector.js +++ b/cocos/core/CCDirector.js @@ -25,15 +25,15 @@ THE SOFTWARE. ****************************************************************************/ -const EventTarget = require('./event/event-target'); -const AutoReleaseUtils = require('./load-pipeline/auto-release-utils'); -const ComponentScheduler = require('./component-scheduler'); -const NodeActivator = require('./node-activator'); -const Obj = require('./platform/CCObject'); -const game = require('./CCGame'); -const renderer = require('./renderer'); -const eventManager = require('./event-manager'); -const Scheduler = require('./CCScheduler'); +import EventTarget from './event/event-target'; +import eventManager from './platform/event-manager/CCEventManager'; +import ccobject from './data/object'; +import game from './CCGame'; +import Scheduler from './CCScheduler'; + +// const AutoReleaseUtils = require('./load-pipeline/auto-release-utils'); +// const ComponentScheduler = require('./component-scheduler'); +// const NodeActivator = require('./node-activator'); //---------------------------------------------------------------------------------------------------------------------- @@ -105,46 +105,45 @@ const Scheduler = require('./CCScheduler'); * @class Director * @extends EventTarget */ -cc.Director = function () { - EventTarget.call(this); +class Director extends EventTarget { + constructor () { + super(); - this.invalid = false; - // paused? - this._paused = false; - // purge? - this._purgeDirectorInNextLoop = false; + this.invalid = false; + // paused? + this._paused = false; + // purge? + this._purgeDirectorInNextLoop = false; - this._winSizeInPoints = null; + this._winSizeInPoints = null; - // scenes - this._loadingScene = ''; - this._scene = null; + // scenes + this._loadingScene = ''; + this._scene = null; - // FPS - this._totalFrames = 0; - this._lastUpdate = 0; - this._deltaTime = 0.0; + // FPS + this._totalFrames = 0; + this._lastUpdate = 0; + this._deltaTime = 0.0; - // Scheduler for user registration update - this._scheduler = null; - // Scheduler for life-cycle methods in component - this._compScheduler = null; - // Node activator - this._nodeActivator = null; - // Action manager - this._actionManager = null; + // Scheduler for user registration update + this._scheduler = null; + // Scheduler for life-cycle methods in component + this._compScheduler = null; + // Node activator + this._nodeActivator = null; + // Action manager + this._actionManager = null; - var self = this; - game.on(game.EVENT_SHOW, function () { - self._lastUpdate = performance.now(); - }); + var self = this; + game.on(game.EVENT_SHOW, function () { + self._lastUpdate = performance.now(); + }); - game.once(game.EVENT_ENGINE_INITED, this.init, this); -}; + game.once(game.EVENT_ENGINE_INITED, this.init, this); + } -cc.Director.prototype = { - constructor: cc.Director, - init: function () { + init () { this._totalFrames = 0; this._lastUpdate = performance.now(); this._paused = false; @@ -161,13 +160,13 @@ cc.Director.prototype = { this.sharedInit(); return true; - }, + } /* * Manage all init process shared between the web engine and jsb engine. * All platform independent init process should be occupied here. */ - sharedInit: function () { + sharedInit () { this._compScheduler = new ComponentScheduler(); this._nodeActivator = new NodeActivator(); @@ -209,12 +208,12 @@ cc.Director.prototype = { } cc.loader.init(this); - }, + } /** * calculates delta time since last time it was called */ - calculateDeltaTime: function () { + calculateDeltaTime () { var now = performance.now(); this._deltaTime = (now - this._lastUpdate) / 1000; @@ -222,7 +221,7 @@ cc.Director.prototype = { this._deltaTime = 1 / 60.0; this._lastUpdate = now; - }, + } /** * !#en @@ -235,7 +234,7 @@ cc.Director.prototype = { * @return {Vec2} * @deprecated since v2.0 */ - convertToGL: function (uiPoint) { + convertToGL (uiPoint) { var container = game.container; var view = cc.view; var box = container.getBoundingClientRect(); @@ -244,7 +243,7 @@ cc.Director.prototype = { var x = view._devicePixelRatio * (uiPoint.x - left); var y = view._devicePixelRatio * (top + box.height - uiPoint.y); return view._isRotated ? cc.v2(view._viewportRect.width - y, x) : cc.v2(x, y); - }, + } /** * !#en @@ -257,7 +256,7 @@ cc.Director.prototype = { * @return {Vec2} * @deprecated since v2.0 */ - convertToUI: function (glPoint) { + convertToUI (glPoint) { var container = game.container; var view = cc.view; var box = container.getBoundingClientRect(); @@ -273,15 +272,15 @@ cc.Director.prototype = { uiPoint.y = top + box.height - glPoint.y * view._devicePixelRatio; } return uiPoint; - }, + } /** * End the life of director in the next frame * @method end */ - end: function () { + end () { this._purgeDirectorInNextLoop = true; - }, + } /** * !#en @@ -292,9 +291,9 @@ cc.Director.prototype = { * @return {Size} * @deprecated since v2.0 */ - getWinSize: function () { + getWinSize () { return cc.size(cc.winSize); - }, + } /** * !#en @@ -309,9 +308,9 @@ cc.Director.prototype = { * @return {Size} * @deprecated since v2.0 */ - getWinSizeInPixels: function () { + getWinSizeInPixels () { return cc.size(cc.winSize); - }, + } /** * !#en Pause the director's ticker, only involve the game logic execution. @@ -322,24 +321,24 @@ cc.Director.prototype = { * 如果想要更彻底得暂停游戏,包含渲染,音频和事件,请使用 {{#crossLink "Game.pause"}}cc.game.pause{{/crossLink}}。 * @method pause */ - pause: function () { + pause () { if (this._paused) return; this._paused = true; - }, + } /** * Removes cached all cocos2d cached data. * @deprecated since v2.0 */ - purgeCachedData: function () { + purgeCachedData () { cc.loader.releaseAll(); - }, + } /** * Purge the cc.director itself, including unschedule all schedule, remove all event listeners, clean up and exit the running scene, stops all animations, clear cached data. */ - purgeDirector: function () { + purgeDirector () { //cleanup scheduler this._scheduler.unscheduleAll(); this._compScheduler.unscheduleAll(); @@ -363,12 +362,12 @@ cc.Director.prototype = { // Clear all caches cc.loader.releaseAll(); - }, + } /** * Reset the cc.director, can be used to restart the director after purge */ - reset: function () { + reset () { this.purgeDirector(); if (eventManager) @@ -395,7 +394,7 @@ cc.Director.prototype = { } this.startAnimation(); - }, + } /** * !#en @@ -407,7 +406,7 @@ cc.Director.prototype = { * @param {Function} [onBeforeLoadScene] - The function invoked at the scene before loading. * @param {Function} [onLaunched] - The function invoked at the scene after launch. */ - runSceneImmediate: function (scene, onBeforeLoadScene, onLaunched) { + runSceneImmediate (scene, onBeforeLoadScene, onLaunched) { cc.assertID(scene instanceof cc.Scene, 1216); CC_BUILD && CC_DEBUG && console.time('InitScene'); @@ -452,7 +451,7 @@ cc.Director.prototype = { this._scene = null; // purge destroyed nodes belongs to old scene - Obj._deferredDestroy(); + ccobject._deferredDestroy(); CC_BUILD && CC_DEBUG && console.timeEnd('Destroy'); if (onBeforeLoadScene) { @@ -474,7 +473,7 @@ cc.Director.prototype = { onLaunched(null, scene); } this.emit(cc.Director.EVENT_AFTER_SCENE_LAUNCH, scene); - }, + } /** * !#en @@ -487,7 +486,7 @@ cc.Director.prototype = { * @param {Function} [onLaunched] - The function invoked at the scene after launch. * @private */ - runScene: function (scene, onBeforeLoadScene, onLaunched) { + runScene (scene, onBeforeLoadScene, onLaunched) { cc.assertID(scene, 1205); cc.assertID(scene instanceof cc.Scene, 1216); @@ -498,11 +497,11 @@ cc.Director.prototype = { this.once(cc.Director.EVENT_AFTER_UPDATE, function () { this.runSceneImmediate(scene, onBeforeLoadScene, onLaunched); }, this); - }, + } // @Scene loading section - _getSceneUuid: function (key) { + _getSceneUuid (key) { var scenes = game._sceneInfos; if (typeof key === 'string') { if (!key.endsWith('.fire')) { @@ -531,7 +530,7 @@ cc.Director.prototype = { cc.errorID(1207, key); } return null; - }, + } /** * !#en Loads the scene by its name. @@ -542,7 +541,7 @@ cc.Director.prototype = { * @param {Function} [onLaunched] - callback, will be called after scene launched. * @return {Boolean} if error, return false */ - loadScene: function (sceneName, onLaunched, _onUnloaded) { + loadScene (sceneName, onLaunched, _onUnloaded) { if (this._loadingScene) { cc.errorID(1208, sceneName, this._loadingScene); return false; @@ -559,7 +558,7 @@ cc.Director.prototype = { cc.errorID(1209, sceneName); return false; } - }, + } /** * !#en @@ -580,7 +579,7 @@ cc.Director.prototype = { * @param {Function} [onLoaded] - callback, will be called after scene loaded. * @param {Error} onLoaded.error - null or the error object. */ - preloadScene: function (sceneName, onProgress, onLoaded) { + preloadScene (sceneName, onProgress, onLoaded) { if (onLoaded === undefined) { onLoaded = onProgress; onProgress = null; @@ -605,7 +604,7 @@ cc.Director.prototype = { onLoaded(new Error(error)); cc.error('preloadScene: ' + error); } - }, + } /** * Loads the scene by its uuid. @@ -617,7 +616,7 @@ cc.Director.prototype = { * only take effect in the Editor. * @private */ - _loadSceneByUuid: function (uuid, onLaunched, onUnloaded, dontRunScene) { + _loadSceneByUuid (uuid, onLaunched, onUnloaded, dontRunScene) { if (CC_EDITOR) { if (typeof onLaunched === 'boolean') { dontRunScene = onLaunched; @@ -668,14 +667,14 @@ cc.Director.prototype = { onLaunched(error); } }); - }, + } /** * !#en Resume game logic execution after pause, if the current scene is not paused, nothing will happen. * !#zh 恢复暂停场景的游戏逻辑,如果当前场景没有暂停将没任何事情发生。 * @method resume */ - resume: function () { + resume () { if (!this._paused) { return; } @@ -687,7 +686,7 @@ cc.Director.prototype = { this._paused = false; this._deltaTime = 0; - }, + } /** * !#en @@ -698,12 +697,12 @@ cc.Director.prototype = { * @param {Boolean} on * @deprecated since v2.0 */ - setDepthTest: function (value) { + setDepthTest (value) { if (!cc.Camera.main) { return; } cc.Camera.main.depth = !!value; - }, + } /** * !#en @@ -716,12 +715,12 @@ cc.Director.prototype = { * @param {Color} clearColor * @deprecated since v2.0 */ - setClearColor: function (clearColor) { + setClearColor (clearColor) { if (!cc.Camera.main) { return; } cc.Camera.main.backgroundColor = clearColor; - }, + } /** * !#en Returns current logic Scene. @@ -731,9 +730,9 @@ cc.Director.prototype = { * @return {Scene} * @deprecated since v2.0 */ - getRunningScene: function () { + getRunningScene () { return this._scene; - }, + } /** * !#en Returns current logic Scene. @@ -744,9 +743,9 @@ cc.Director.prototype = { * // This will help you to get the Canvas node in scene * cc.director.getScene().getChildByName('Canvas'); */ - getScene: function () { + getScene () { return this._scene; - }, + } /** * !#en Returns the FPS value. Please use {{#crossLink "Game.setFrameRate"}}cc.game.setFrameRate{{/crossLink}} to control animation interval. @@ -755,9 +754,9 @@ cc.Director.prototype = { * @deprecated since v2.0 * @return {Number} */ - getAnimationInterval: function () { + getAnimationInterval () { return 1000 / game.getFrameRate(); - }, + } /** * Sets animation interval, this doesn't control the main loop. @@ -766,9 +765,9 @@ cc.Director.prototype = { * @deprecated since v2.0 * @param {Number} value - The animation interval desired. */ - setAnimationInterval: function (value) { + setAnimationInterval (value) { game.setFrameRate(Math.round(1000 / value)); - }, + } /** * !#en Returns the delta time since last frame. @@ -776,9 +775,9 @@ cc.Director.prototype = { * @method getDeltaTime * @return {Number} */ - getDeltaTime: function () { + getDeltaTime () { return this._deltaTime; - }, + } /** * !#en Returns how many frames were called since the director started. @@ -786,9 +785,9 @@ cc.Director.prototype = { * @method getTotalFrames * @return {Number} */ - getTotalFrames: function () { + getTotalFrames () { return this._totalFrames; - }, + } /** * !#en Returns whether or not the Director is paused. @@ -796,9 +795,9 @@ cc.Director.prototype = { * @method isPaused * @return {Boolean} */ - isPaused: function () { + isPaused () { return this._paused; - }, + } /** * !#en Returns the cc.Scheduler associated with this director. @@ -806,9 +805,9 @@ cc.Director.prototype = { * @method getScheduler * @return {Scheduler} */ - getScheduler: function () { + getScheduler () { return this._scheduler; - }, + } /** * !#en Sets the cc.Scheduler associated with this director. @@ -816,11 +815,11 @@ cc.Director.prototype = { * @method setScheduler * @param {Scheduler} scheduler */ - setScheduler: function (scheduler) { + setScheduler (scheduler) { if (this._scheduler !== scheduler) { this._scheduler = scheduler; } - }, + } /** * !#en Returns the cc.ActionManager associated with this director. @@ -828,16 +827,16 @@ cc.Director.prototype = { * @method getActionManager * @return {ActionManager} */ - getActionManager: function () { + getActionManager () { return this._actionManager; - }, + } /** * !#en Sets the cc.ActionManager associated with this director. * !#zh 设置和 director 相关联的 cc.ActionManager(动作管理器)。 * @method setActionManager * @param {ActionManager} actionManager */ - setActionManager: function (actionManager) { + setActionManager (actionManager) { if (this._actionManager !== actionManager) { if (this._actionManager) { this._scheduler.unscheduleUpdate(this._actionManager); @@ -845,7 +844,7 @@ cc.Director.prototype = { this._actionManager = actionManager; this._scheduler.scheduleUpdate(this._actionManager, cc.Scheduler.PRIORITY_SYSTEM, false); } - }, + } /* * !#en Returns the cc.AnimationManager associated with this director. @@ -853,9 +852,9 @@ cc.Director.prototype = { * @method getAnimationManager * @return {AnimationManager} */ - getAnimationManager: function () { + getAnimationManager () { return this._animationManager; - }, + } /** * !#en Returns the cc.CollisionManager associated with this director. @@ -863,9 +862,9 @@ cc.Director.prototype = { * @method getCollisionManager * @return {CollisionManager} */ - getCollisionManager: function () { + getCollisionManager () { return this._collisionManager; - }, + } /** * !#en Returns the cc.PhysicsManager associated with this director. @@ -873,58 +872,30 @@ cc.Director.prototype = { * @method getPhysicsManager * @return {PhysicsManager} */ - getPhysicsManager: function () { + getPhysicsManager () { return this._physicsManager; - }, + } // Loop management /* * Starts Animation */ - startAnimation: function () { + startAnimation () { this.invalid = false; this._lastUpdate = performance.now(); - }, + } /* * Stops animation */ - stopAnimation: function () { + stopAnimation () { this.invalid = true; - }, + } /* * Run main loop of director */ - mainLoop: CC_EDITOR ? function (deltaTime, updateAnimate) { - this._deltaTime = deltaTime; - - // Update - if (!this._paused) { - this.emit(cc.Director.EVENT_BEFORE_UPDATE); - - this._compScheduler.startPhase(); - this._compScheduler.updatePhase(deltaTime); - - if (updateAnimate) { - this._scheduler.update(deltaTime); - } - - this._compScheduler.lateUpdatePhase(deltaTime); - - this.emit(cc.Director.EVENT_AFTER_UPDATE); - } - - // Render - this.emit(cc.Director.EVENT_BEFORE_DRAW); - renderer.render(this._scene); - - // After draw - this.emit(cc.Director.EVENT_AFTER_DRAW); - - this._totalFrames++; - - } : function () { + mainLoop () { if (this._purgeDirectorInNextLoop) { this._purgeDirectorInNextLoop = false; this.purgeDirector(); @@ -947,7 +918,7 @@ cc.Director.prototype = { // User can use this event to do things after update this.emit(cc.Director.EVENT_AFTER_UPDATE); // Destroy entities that have been removed recently - Obj._deferredDestroy(); + ccobject._deferredDestroy(); } // Render @@ -960,29 +931,16 @@ cc.Director.prototype = { eventManager.frameUpdateListeners(); this._totalFrames++; } - }, + } - __fastOn: function (type, callback, target) { + __fastOn (type, callback, target) { this.add(type, callback, target); - }, + } - __fastOff: function (type, callback, target) { + __fastOff (type, callback, target) { this.remove(type, callback, target); - }, -}; - -// Event target -cc.js.addon(cc.Director.prototype, EventTarget.prototype); - -/** - * !#en The event projection changed of cc.Director. This event will not get triggered since v2.0 - * !#zh cc.Director 投影变化的事件。从 v2.0 开始这个事件不会再被触发 - * @property {String} EVENT_PROJECTION_CHANGED - * @readonly - * @static - * @deprecated since v2.0 - */ -cc.Director.EVENT_PROJECTION_CHANGED = "director_projection_changed"; + } +} /** * !#en The event which will be triggered before loading a new scene. @@ -997,7 +955,7 @@ cc.Director.EVENT_PROJECTION_CHANGED = "director_projection_changed"; * @readonly * @static */ -cc.Director.EVENT_BEFORE_SCENE_LOADING = "director_before_scene_loading"; +Director.EVENT_BEFORE_SCENE_LOADING = "director_before_scene_loading"; /* * !#en The event which will be triggered before launching a new scene. @@ -1012,7 +970,7 @@ cc.Director.EVENT_BEFORE_SCENE_LOADING = "director_before_scene_loading"; * @readonly * @static */ -cc.Director.EVENT_BEFORE_SCENE_LAUNCH = "director_before_scene_launch"; +Director.EVENT_BEFORE_SCENE_LAUNCH = "director_before_scene_launch"; /** * !#en The event which will be triggered after launching a new scene. @@ -1027,7 +985,7 @@ cc.Director.EVENT_BEFORE_SCENE_LAUNCH = "director_before_scene_launch"; * @readonly * @static */ -cc.Director.EVENT_AFTER_SCENE_LAUNCH = "director_after_scene_launch"; +Director.EVENT_AFTER_SCENE_LAUNCH = "director_after_scene_launch"; /** * !#en The event which will be triggered at the beginning of every frame. @@ -1041,7 +999,7 @@ cc.Director.EVENT_AFTER_SCENE_LAUNCH = "director_after_scene_launch"; * @readonly * @static */ -cc.Director.EVENT_BEFORE_UPDATE = "director_before_update"; +Director.EVENT_BEFORE_UPDATE = "director_before_update"; /** * !#en The event which will be triggered after engine and components update logic. @@ -1055,27 +1013,7 @@ cc.Director.EVENT_BEFORE_UPDATE = "director_before_update"; * @readonly * @static */ -cc.Director.EVENT_AFTER_UPDATE = "director_after_update"; - -/** - * !#en The event is deprecated since v2.0, please use cc.Director.EVENT_BEFORE_DRAW instead - * !#zh 这个事件从 v2.0 开始被废弃,请直接使用 cc.Director.EVENT_BEFORE_DRAW - * @property {String} EVENT_BEFORE_VISIT - * @readonly - * @deprecated since v2.0 - * @static - */ -cc.Director.EVENT_BEFORE_VISIT = "director_before_draw"; - -/** - * !#en The event is deprecated since v2.0, please use cc.Director.EVENT_BEFORE_DRAW instead - * !#zh 这个事件从 v2.0 开始被废弃,请直接使用 cc.Director.EVENT_BEFORE_DRAW - * @property {String} EVENT_AFTER_VISIT - * @readonly - * @deprecated since v2.0 - * @static - */ -cc.Director.EVENT_AFTER_VISIT = "director_before_draw"; +Director.EVENT_AFTER_UPDATE = "director_after_update"; /** * !#en The event which will be triggered before the rendering process. @@ -1089,7 +1027,7 @@ cc.Director.EVENT_AFTER_VISIT = "director_before_draw"; * @readonly * @static */ -cc.Director.EVENT_BEFORE_DRAW = "director_before_draw"; +Director.EVENT_BEFORE_DRAW = "director_before_draw"; /** * !#en The event which will be triggered after the rendering process. @@ -1103,49 +1041,7 @@ cc.Director.EVENT_BEFORE_DRAW = "director_before_draw"; * @readonly * @static */ -cc.Director.EVENT_AFTER_DRAW = "director_after_draw"; - -//Possible OpenGL projections used by director - -/** - * Constant for 2D projection (orthogonal projection) - * @property {Number} PROJECTION_2D - * @default 0 - * @readonly - * @static - * @deprecated since v2.0 - */ -cc.Director.PROJECTION_2D = 0; - -/** - * Constant for 3D projection with a fovy=60, znear=0.5f and zfar=1500. - * @property {Number} PROJECTION_3D - * @default 1 - * @readonly - * @static - * @deprecated since v2.0 - */ -cc.Director.PROJECTION_3D = 1; - -/** - * Constant for custom projection, if cc.Director's projection set to it, it calls "updateProjection" on the projection delegate. - * @property {Number} PROJECTION_CUSTOM - * @default 3 - * @readonly - * @static - * @deprecated since v2.0 - */ -cc.Director.PROJECTION_CUSTOM = 3; - -/** - * Constant for default projection of cc.Director, default projection is 2D projection - * @property {Number} PROJECTION_DEFAULT - * @default cc.Director.PROJECTION_2D - * @readonly - * @static - * @deprecated since v2.0 - */ -cc.Director.PROJECTION_DEFAULT = cc.Director.PROJECTION_2D; +Director.EVENT_AFTER_DRAW = "director_after_draw"; /** * @module cc @@ -1157,6 +1053,6 @@ cc.Director.PROJECTION_DEFAULT = cc.Director.PROJECTION_2D; * @property director * @type {Director} */ -cc.director = new cc.Director(); - -module.exports = cc.director; \ No newline at end of file +let director = cc.director = new Director(); +cc.Director = Director; +export default director; \ No newline at end of file diff --git a/cocos/core/CCGame.js b/cocos/core/CCGame.js index 3198b03478..1232807324 100644 --- a/cocos/core/CCGame.js +++ b/cocos/core/CCGame.js @@ -24,12 +24,10 @@ THE SOFTWARE. ****************************************************************************/ -var EventTarget = require('./event/event-target'); -require('../audio/CCAudioEngine'); -const debug = require('./CCDebug'); -const renderer = require('./renderer/index.js'); -const inputManager = CC_QQPLAY ? require('./platform/BKInputManager') : require('./platform/CCInputManager'); -const dynamicAtlasManager = require('../core/renderer/utils/dynamic-atlas/manager'); +import EventTarget from './event/event-target'; +import inputManager from './platform/event-manager/CCInputManager'; +import debug from './platform/CCDebug'; +import {addon} from './utils/js'; /** * @module cc @@ -282,10 +280,6 @@ var game = { pause: function () { if (this._paused) return; this._paused = true; - // Pause audio engine - if (cc.audioEngine) { - cc.audioEngine._break(); - } // Pause main loop if (this._intervalId) window.cancelAnimFrame(this._intervalId); @@ -301,10 +295,6 @@ var game = { resume: function () { if (!this._paused) return; this._paused = false; - // Resume audio engine - if (cc.audioEngine) { - cc.audioEngine._restore(); - } // Resume main loop this._runMainLoop(); }, @@ -336,11 +326,6 @@ var game = { cc.director.purgeDirector(); - // Clean up audio - if (cc.audioEngine) { - cc.audioEngine.uncacheAll(); - } - cc.director.reset(); game.onStart(); }); @@ -773,11 +758,6 @@ var game = { } renderer.initWebGL(localCanvas, opts); this._renderContext = renderer.device._gl; - - // Enable dynamic atlas manager by default - if (!cc.macro.CLEANUP_IMAGE_CACHE && dynamicAtlasManager) { - dynamicAtlasManager.enabled = true; - } } if (!this._renderContext) { this.renderType = this.RENDER_TYPE_CANVAS; @@ -876,10 +856,7 @@ var game = { }; EventTarget.call(game); -cc.js.addon(game, EventTarget.prototype); +addon(game, EventTarget.prototype); -/** - * @property game - * @type Game - */ -cc.game = module.exports = game; +cc.game = game; +export default game; \ No newline at end of file diff --git a/cocos/core/CCScheduler.js b/cocos/core/CCScheduler.js index a404110d8c..f31402fae3 100644 --- a/cocos/core/CCScheduler.js +++ b/cocos/core/CCScheduler.js @@ -27,8 +27,9 @@ /** * @module cc */ -const js = require('./platform/js'); -const IdGenerater = require('./platform/id-generater'); +import {createMap} from './utils/js'; +import IdGenerater from './utils/id-generater'; + const MAX_POOL_SIZE = 20; var idGenerater = new IdGenerater('Scheduler'); @@ -150,103 +151,106 @@ HashTimerEntry.put = function (entry) { /* * Light weight timer - * @extends cc.Class */ -function CallbackTimer () { - this._lock = false; - this._scheduler = null; - this._elapsed = -1; - this._runForever = false; - this._useDelay = false; - this._timesExecuted = 0; - this._repeat = 0; - this._delay = 0; - this._interval = 0; - - this._target = null; - this._callback = null; -} - -var proto = CallbackTimer.prototype; - -proto.initWithCallback = function (scheduler, callback, target, seconds, repeat, delay) { - this._lock = false; - this._scheduler = scheduler; - this._target = target; - this._callback = callback; - - this._elapsed = -1; - this._interval = seconds; - this._delay = delay; - this._useDelay = (this._delay > 0); - this._repeat = repeat; - this._runForever = (this._repeat === cc.macro.REPEAT_FOREVER); - return true; -}; -/** - * @return {Number} returns interval of timer - */ -proto.getInterval = function(){return this._interval;}; -/** - * @param {Number} interval set interval in seconds - */ -proto.setInterval = function(interval){this._interval = interval;}; - -/** - * triggers the timer - * @param {Number} dt delta time - */ -proto.update = function (dt) { - if (this._elapsed === -1) { - this._elapsed = 0; +class CallbackTimer { + constructor () { + this._lock = false; + this._scheduler = null; + this._elapsed = -1; + this._runForever = false; + this._useDelay = false; this._timesExecuted = 0; - } else { - this._elapsed += dt; - if (this._runForever && !this._useDelay) {//standard timer usage - if (this._elapsed >= this._interval) { - this.trigger(); - this._elapsed = 0; - } - } else {//advanced usage - if (this._useDelay) { - if (this._elapsed >= this._delay) { - this.trigger(); + this._repeat = 0; + this._delay = 0; + this._interval = 0; - this._elapsed -= this._delay; - this._timesExecuted += 1; - this._useDelay = false; - } - } else { + this._target = null; + this._callback = null; + } + + initWithCallback (scheduler, callback, target, seconds, repeat, delay) { + this._lock = false; + this._scheduler = scheduler; + this._target = target; + this._callback = callback; + + this._elapsed = -1; + this._interval = seconds; + this._delay = delay; + this._useDelay = (this._delay > 0); + this._repeat = repeat; + this._runForever = (this._repeat === cc.macro.REPEAT_FOREVER); + return true; + } + /** + * @return {Number} returns interval of timer + */ + getInterval () { + return this._interval; + } + /** + * @param {Number} interval set interval in seconds + */ + setInterval (interval) { + this._interval = interval; + } + + /** + * triggers the timer + * @param {Number} dt delta time + */ + update (dt) { + if (this._elapsed === -1) { + this._elapsed = 0; + this._timesExecuted = 0; + } else { + this._elapsed += dt; + if (this._runForever && !this._useDelay) {//standard timer usage if (this._elapsed >= this._interval) { this.trigger(); - this._elapsed = 0; - this._timesExecuted += 1; } - } + } else {//advanced usage + if (this._useDelay) { + if (this._elapsed >= this._delay) { + this.trigger(); - if (this._callback && !this._runForever && this._timesExecuted > this._repeat) - this.cancel(); + this._elapsed -= this._delay; + this._timesExecuted += 1; + this._useDelay = false; + } + } else { + if (this._elapsed >= this._interval) { + this.trigger(); + + this._elapsed = 0; + this._timesExecuted += 1; + } + } + + if (this._callback && !this._runForever && this._timesExecuted > this._repeat) + this.cancel(); + } } } -}; -proto.getCallback = function(){ - return this._callback; -}; - -proto.trigger = function () { - if (this._target && this._callback) { - this._lock = true; - this._callback.call(this._target, this._elapsed); - this._lock = false; + getCallback(){ + return this._callback; } -}; -proto.cancel = function () { - //override - this._scheduler.unschedule(this._callback, this._target); -}; + trigger () { + if (this._target && this._callback) { + this._lock = true; + this._callback.call(this._target, this._elapsed); + this._lock = false; + } + } + + cancel () { + //override + this._scheduler.unschedule(this._callback, this._target); + } +} var _timers = []; CallbackTimer.get = function () { @@ -280,26 +284,24 @@ CallbackTimer.put = function (timer) { * * @class Scheduler */ -cc.Scheduler = function () { - this._timeScale = 1.0; - this._updatesNegList = []; // list of priority < 0 - this._updates0List = []; // list of priority == 0 - this._updatesPosList = []; // list of priority > 0 - this._hashForUpdates = js.createMap(true); // hash used to fetch quickly the list entries for pause, delete, etc - this._hashForTimers = js.createMap(true); // Used for "selectors with interval" - this._currentTarget = null; - this._currentTargetSalvaged = false; - this._updateHashLocked = false; // If true unschedule will not remove anything from a hash. Elements will only be marked for deletion. +export default class Scheduler { + constructor () { + this._timeScale = 1.0; + this._updatesNegList = []; // list of priority < 0 + this._updates0List = []; // list of priority == 0 + this._updatesPosList = []; // list of priority > 0 + this._hashForUpdates = createMap(true); // hash used to fetch quickly the list entries for pause, delete, etc + this._hashForTimers = createMap(true); // Used for "selectors with interval" + this._currentTarget = null; + this._currentTargetSalvaged = false; + this._updateHashLocked = false; // If true unschedule will not remove anything from a hash. Elements will only be marked for deletion. - this._arrayForTimers = []; // Speed up indexing - //this._arrayForUpdates = []; // Speed up indexing -}; - -cc.Scheduler.prototype = { - constructor: cc.Scheduler, + this._arrayForTimers = []; // Speed up indexing + //this._arrayForUpdates = []; // Speed up indexing + } + //-----------------------private method---------------------- - - _removeHashElement: function (element) { + _removeHashElement (element) { delete this._hashForTimers[element.target._id]; var arr = this._arrayForTimers; for (var i = 0, l = arr.length; i < l; i++) { @@ -309,9 +311,9 @@ cc.Scheduler.prototype = { } } HashTimerEntry.put(element); - }, + } - _removeUpdateFromHash: function (entry) { + _removeUpdateFromHash (entry) { var targetId = entry.target._id; var self = this, element = self._hashForUpdates[targetId]; if (element) { @@ -328,9 +330,9 @@ cc.Scheduler.prototype = { ListEntry.put(listEntry); HashUpdateEntry.put(element); } - }, + } - _priorityIn: function (ppList, listElement, priority) { + _priorityIn (ppList, listElement, priority) { for (var i = 0; i < ppList.length; i++){ if (priority < ppList[i].priority) { ppList.splice(i, 0, listElement); @@ -338,11 +340,11 @@ cc.Scheduler.prototype = { } } ppList.push(listElement); - }, + } - _appendIn: function (ppList, listElement) { + _appendIn (ppList, listElement) { ppList.push(listElement); - }, + } //-----------------------public method------------------------- /** @@ -353,7 +355,7 @@ cc.Scheduler.prototype = { * @method enableForTarget * @param {Object} target */ - enableForTarget: function (target) { + enableForTarget (target) { if (!target._id) { if (target.__instanceId) { cc.warnID(1513); @@ -362,7 +364,7 @@ cc.Scheduler.prototype = { target._id = idGenerater.getNewId(); } } - }, + } /** * !#en @@ -380,9 +382,9 @@ cc.Scheduler.prototype = { * @method setTimeScale * @param {Number} timeScale */ - setTimeScale: function (timeScale) { + setTimeScale (timeScale) { this._timeScale = timeScale; - }, + } /** * !#en Returns time scale of scheduler. @@ -390,9 +392,9 @@ cc.Scheduler.prototype = { * @method getTimeScale * @return {Number} */ - getTimeScale: function () { + getTimeScale () { return this._timeScale; - }, + } /** * !#en 'update' the scheduler. (You should NEVER call this method, unless you know what you are doing.) @@ -400,7 +402,7 @@ cc.Scheduler.prototype = { * @method update * @param {Number} dt delta time */ - update: function (dt) { + update (dt) { this._updateHashLocked = true; if(this._timeScale !== 1) dt *= this._timeScale; @@ -478,7 +480,7 @@ cc.Scheduler.prototype = { this._updateHashLocked = false; this._currentTarget = null; - }, + } /** * !#en @@ -512,7 +514,7 @@ cc.Scheduler.prototype = { * schedule(callback: Function, target: any, interval: number, repeat: number, delay: number, paused?: boolean): void * schedule(callback: Function, target: any, interval: number, paused?: boolean): void */ - schedule: function (callback, target, interval, repeat, delay, paused) { + schedule (callback, target, interval, repeat, delay, paused) { 'use strict'; if (typeof callback !== 'function') { var tmp = callback; @@ -571,7 +573,7 @@ cc.Scheduler.prototype = { if (this._currentTarget === element && this._currentTargetSalvaged) { this._currentTargetSalvaged = false; } - }, + } /** * !#en @@ -586,7 +588,7 @@ cc.Scheduler.prototype = { * @param {Number} priority * @param {Boolean} paused */ - scheduleUpdate: function(target, priority, paused) { + scheduleUpdate(target, priority, paused) { var targetId = target._id; if (!targetId) { if (target.__instanceId) { @@ -633,7 +635,7 @@ cc.Scheduler.prototype = { //update hash entry for quick access this._hashForUpdates[targetId] = HashUpdateEntry.get(ppList, listElement, target, null); - }, + } /** * !#en @@ -646,7 +648,7 @@ cc.Scheduler.prototype = { * @param {Function} callback The callback to be unscheduled * @param {Object} target The target bound to the callback. */ - unschedule: function (callback, target) { + unschedule (callback, target) { //callback, target // explicity handle nil arguments when removing an object @@ -690,7 +692,7 @@ cc.Scheduler.prototype = { } } } - }, + } /** * !#en Unschedules the update callback for a given target. @@ -698,7 +700,7 @@ cc.Scheduler.prototype = { * @method unscheduleUpdate * @param {Object} target The target to be unscheduled. */ - unscheduleUpdate: function (target) { + unscheduleUpdate (target) { if (!target) return; var targetId = target._id; @@ -720,7 +722,7 @@ cc.Scheduler.prototype = { this._removeUpdateFromHash(element.entry); } } - }, + } /** * !#en @@ -730,7 +732,7 @@ cc.Scheduler.prototype = { * @method unscheduleAllForTarget * @param {Object} target The target to be unscheduled. */ - unscheduleAllForTarget: function (target) { + unscheduleAllForTarget (target) { // explicit nullptr handling if (!target){ return; @@ -768,7 +770,7 @@ cc.Scheduler.prototype = { // update selector this.unscheduleUpdate(target); - }, + } /** * !#en @@ -779,9 +781,9 @@ cc.Scheduler.prototype = { * 不用调用此函数,除非你确定你在做什么。 * @method unscheduleAll */ - unscheduleAll: function(){ + unscheduleAll(){ this.unscheduleAllWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM); - }, + } /** * !#en @@ -794,7 +796,7 @@ cc.Scheduler.prototype = { * @param {Number} minPriority The minimum priority of selector to be unscheduled. Which means, all selectors which * priority is higher than minPriority will be unscheduled. */ - unscheduleAllWithMinPriority: function(minPriority){ + unscheduleAllWithMinPriority(minPriority){ // Custom Selectors var i, element, arr = this._arrayForTimers; for(i=arr.length-1; i>=0; i--){ @@ -835,7 +837,7 @@ cc.Scheduler.prototype = { if (temp_length == this._updatesPosList.length) i++; } - }, + } /** * !#en Checks whether a callback for a given target is scheduled. @@ -845,7 +847,7 @@ cc.Scheduler.prototype = { * @param {Object} target The target of the callback. * @return {Boolean} True if the specified callback is invoked, false if not. */ - isScheduled: function(callback, target){ + isScheduled(callback, target){ //key, target //selector, target cc.assertID(callback, 1508); @@ -881,7 +883,7 @@ cc.Scheduler.prototype = { } return false; } - }, + } /** * !#en @@ -892,9 +894,9 @@ cc.Scheduler.prototype = { * 不要调用这个方法,除非你知道你正在做什么。 * @method pauseAllTargets */ - pauseAllTargets: function () { + pauseAllTargets () { return this.pauseAllTargetsWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM); - }, + } /** * !#en @@ -906,7 +908,7 @@ cc.Scheduler.prototype = { * @method pauseAllTargetsWithMinPriority * @param {Number} minPriority */ - pauseAllTargetsWithMinPriority: function (minPriority) { + pauseAllTargetsWithMinPriority (minPriority) { var idsWithSelectors = []; var self = this, element, locArrayForTimers = self._arrayForTimers; @@ -954,7 +956,7 @@ cc.Scheduler.prototype = { } return idsWithSelectors; - }, + } /** * !#en @@ -966,14 +968,14 @@ cc.Scheduler.prototype = { * @method resumeTargets * @param {Array} targetsToResume */ - resumeTargets: function (targetsToResume) { + resumeTargets (targetsToResume) { if (!targetsToResume) return; for (var i = 0; i < targetsToResume.length; i++) { this.resumeTarget(targetsToResume[i]); } - }, + } /** * !#en @@ -987,7 +989,7 @@ cc.Scheduler.prototype = { * @method pauseTarget * @param {Object} target */ - pauseTarget: function (target) { + pauseTarget (target) { cc.assertID(target, 1503); var targetId = target._id; if (!targetId) { @@ -1012,7 +1014,7 @@ cc.Scheduler.prototype = { if (elementUpdate) { elementUpdate.entry.paused = true; } - }, + } /** * !#en @@ -1026,7 +1028,7 @@ cc.Scheduler.prototype = { * @method resumeTarget * @param {Object} target */ - resumeTarget: function (target) { + resumeTarget (target) { cc.assertID(target, 1504); var targetId = target._id; if (!targetId) { @@ -1051,7 +1053,7 @@ cc.Scheduler.prototype = { if (elementUpdate) { elementUpdate.entry.paused = false; } - }, + } /** * !#en Returns whether or not the target is paused. @@ -1060,7 +1062,7 @@ cc.Scheduler.prototype = { * @param {Object} target * @return {Boolean} */ - isTargetPaused: function (target) { + isTargetPaused (target) { cc.assertID(target, 1505); var targetId = target._id; if (!targetId) { @@ -1083,8 +1085,8 @@ cc.Scheduler.prototype = { return elementUpdate.entry.paused; } return false; - }, -}; + } +} /** * !#en Priority level reserved for system services. @@ -1093,7 +1095,7 @@ cc.Scheduler.prototype = { * @type {Number} * @static */ -cc.Scheduler.PRIORITY_SYSTEM = 1 << 31; +Scheduler.PRIORITY_SYSTEM = 1 << 31; /** * !#en Minimum priority level for user scheduling. @@ -1102,6 +1104,4 @@ cc.Scheduler.PRIORITY_SYSTEM = 1 << 31; * @type {Number} * @static */ -cc.Scheduler.PRIORITY_NON_SYSTEM = cc.Scheduler.PRIORITY_SYSTEM + 1; - -module.exports = cc.Scheduler; \ No newline at end of file +Scheduler.PRIORITY_NON_SYSTEM = Scheduler.PRIORITY_SYSTEM + 1; \ No newline at end of file diff --git a/cocos/core/index.js b/cocos/core/index.js index 7ea9643f12..dfa0304ddc 100644 --- a/cocos/core/index.js +++ b/cocos/core/index.js @@ -24,23 +24,12 @@ THE SOFTWARE. ****************************************************************************/ -require('./platform'); -require('./assets'); - -if (!CC_EDITOR || !Editor.isMainProcess) { - require('./CCNode'); - require('./CCPrivateNode'); - require('./CCScene'); - - require('./components'); - require('./graphics'); - require('./collider'); - // CCIntersection can be used separately. - require('./collider/CCIntersection'); - require('./physics'); - require('./camera/CCCamera'); - - require('./3d'); -} - -require('./base-ui/CCWidgetManager'); +import './utils'; +import './data'; +import './event'; +import './platform'; +import './value-types'; +import './vmath'; +import './CCGame'; +import './CCScheduler'; +import './CCDirector'; \ No newline at end of file diff --git a/cocos/core/platform/CCView.js b/cocos/core/platform/CCView.js index acbee0f957..6e78f54b22 100644 --- a/cocos/core/platform/CCView.js +++ b/cocos/core/platform/CCView.js @@ -26,7 +26,6 @@ ****************************************************************************/ import EventTarget from '../event/event-target'; -import * as js from '../utils/js'; import '../data/class'; var __BrowserGetter = { @@ -116,7 +115,7 @@ var _scissorRect = null; * * @class View */ -class View { +class View extends EventTarget { constructor () { super(); diff --git a/cocos/core/predefine.js b/cocos/core/predefine.js deleted file mode 100644 index e58d98d930..0000000000 --- a/cocos/core/predefine.js +++ /dev/null @@ -1,45 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -// predefine some modules for cocos -require('./platform/js'); -require('./value-types'); -require('./utils'); -require('./platform/CCInputManager'); -require('./platform/CCInputExtension'); -require('./event'); -require('./platform/CCSys'); -require('./platform/CCMacro'); -require('./load-pipeline'); -require('./CCDirector'); -require('./renderer'); - -if (!(CC_EDITOR && Editor.isMainProcess)) { - require('./platform/CCView'); - require('./platform/CCScreen'); - require('./CCScheduler'); - require('./event-manager'); -}