From 11af0a31a7238aa21c2eccc3628623537489bc49 Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Mon, 8 Jun 2020 21:53:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eentityprocessor=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=AE=9E=E4=BD=93=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/libs/framework/framework.d.ts | 30 ++++- demo/libs/framework/framework.js | 118 +++++++++++++++++--- demo/libs/framework/framework.min.js | 2 +- demo/src/game/SpawnerSystem.ts | 9 +- source/bin/framework.d.ts | 30 ++++- source/bin/framework.js | 118 +++++++++++++++++--- source/bin/framework.min.js | 2 +- source/package-lock.json | 2 +- source/package.json | 4 +- source/src/ECS/Component.ts | 7 +- source/src/ECS/Entity.ts | 4 +- source/src/ECS/Scene.ts | 22 ++-- source/src/ECS/Utils/BitSet.ts | 14 ++- source/src/ECS/Utils/EntityList.ts | 4 +- source/src/ECS/Utils/EntityProcessorList.ts | 69 ++++++++++++ source/src/ECS/Utils/Time.ts | 15 +++ source/src/Extension.ts | 2 +- 17 files changed, 386 insertions(+), 66 deletions(-) create mode 100644 source/src/ECS/Utils/EntityProcessorList.ts create mode 100644 source/src/ECS/Utils/Time.ts diff --git a/demo/libs/framework/framework.d.ts b/demo/libs/framework/framework.d.ts index 1f0f31c7..c9b3698c 100644 --- a/demo/libs/framework/framework.d.ts +++ b/demo/libs/framework/framework.d.ts @@ -27,6 +27,7 @@ declare abstract class Component { update(): void; bind(displayRender: egret.DisplayObject): this; registerComponent(): void; + deregisterComponent(): void; } declare class Entity { name: string; @@ -43,7 +44,7 @@ declare class Entity { setUpdateOrder(updateOrder: number): this; attachToScene(newScene: Scene): void; addComponent(component: T): T; - getComponent(): T; + getComponent(type: any): T; update(): void; destory(): void; } @@ -53,7 +54,7 @@ declare class Scene extends egret.DisplayObjectContainer { private _projectionMatrix; private _transformMatrix; private _matrixTransformMatrix; - readonly entityProcessors: EntitySystem[]; + readonly entityProcessors: EntityProcessorList; constructor(displayObject: egret.DisplayObject); createEntity(name: string): Entity; addEntity(entity: Entity): Entity; @@ -173,7 +174,7 @@ declare class BitSet { intersects(set: BitSet): boolean; isEmpty(): boolean; nextSetBit(from: number): number; - set(pos: number): void; + set(pos: number, value?: boolean): void; } declare class ComponentTypeManager { private static _componentTypesMask; @@ -196,6 +197,22 @@ declare class EntityList { removeAllEntities(): void; updateLists(): void; } +declare class EntityProcessorList { + private _processors; + add(processor: EntitySystem): void; + remove(processor: EntitySystem): void; + onComponentAdded(entity: Entity): void; + onComponentRemoved(entity: Entity): void; + onEntityAdded(entity: Entity): void; + onEntityRemoved(entity: Entity): void; + protected notifyEntityChanged(entity: Entity): void; + protected removeFromProcessors(entity: Entity): void; + begin(): void; + update(): void; + lateUpdate(): void; + end(): void; + getProcessor(): T; +} declare class Matcher { protected allSet: BitSet; protected exclusionSet: BitSet; @@ -203,6 +220,13 @@ declare class Matcher { static empty(): Matcher; IsIntersted(e: Entity): boolean; } +declare class Time { + static unscaledDeltaTime: any; + static deltaTime: number; + static timeScale: number; + private static _lastTime; + static update(currentTime: number): void; +} declare class MathHelper { static toDegrees(radians: number): number; static toRadians(degrees: number): number; diff --git a/demo/libs/framework/framework.js b/demo/libs/framework/framework.js index 76051810..360c5edd 100644 --- a/demo/libs/framework/framework.js +++ b/demo/libs/framework/framework.js @@ -76,7 +76,7 @@ Array.prototype.findAll = function (predicate) { Array.prototype.contains = function (value) { function contains(array, value) { for (var i = 0, len = array.length; i < len; i++) { - if (JSON.stringify(array[i]) == JSON.stringify(value)) { + if (array[i] == value) { return true; } } @@ -265,9 +265,12 @@ var Component = (function () { return this; }; Component.prototype.registerComponent = function () { - var _this = this; + this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false); + this.entity.scene.entityProcessors.onComponentAdded(this.entity); + }; + Component.prototype.deregisterComponent = function () { this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)); - this.entity.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(_this.entity); }); + this.entity.scene.entityProcessors.onComponentRemoved(this.entity); }; return Component; }()); @@ -328,8 +331,8 @@ var Entity = (function () { component.initialize(); return component; }; - Entity.prototype.getComponent = function () { - return this.components.firstOrDefault(function (component) { return component instanceof Component; }); + Entity.prototype.getComponent = function (type) { + return this.components.firstOrDefault(function (component) { return component instanceof type; }); }; Entity.prototype.update = function () { this.components.forEach(function (component) { return component.update(); }); @@ -351,7 +354,7 @@ var Scene = (function (_super) { var _this = _super.call(this) || this; displayObject.stage.addChild(_this); _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); - _this.entityProcessors = []; + _this.entityProcessors = new EntityProcessorList(); _this.entities = new EntityList(_this); _this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this); _this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this); @@ -380,14 +383,14 @@ var Scene = (function (_super) { }; Scene.prototype.addEntityProcessor = function (processor) { processor.scene = this; - this.entityProcessors.push(processor); + this.entityProcessors.add(processor); return processor; }; Scene.prototype.removeEntityProcessor = function (processor) { this.entityProcessors.remove(processor); }; Scene.prototype.getEntityProcessor = function () { - return this.entityProcessors.firstOrDefault(function (processor) { return processor instanceof EntitySystem; }); + return this.entityProcessors.getProcessor(); }; Scene.prototype.setActive = function () { SceneManager.setActiveScene(this); @@ -395,17 +398,21 @@ var Scene = (function (_super) { }; Scene.prototype.initialize = function () { this.camera = this.createEntity("camera").addComponent(new Camera()); - this.entityProcessors.forEach(function (processor) { return processor.initialize(); }); + if (this.entityProcessors) + this.entityProcessors.begin(); }; Scene.prototype.onActive = function () { }; Scene.prototype.onDeactive = function () { }; Scene.prototype.update = function () { + Time.update(egret.getTimer()); this.entities.updateLists(); - this.entityProcessors.forEach(function (processor) { return processor.update(); }); + if (this.entityProcessors) + this.entityProcessors.update(); this.entities.update(); - this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); }); + if (this.entityProcessors) + this.entityProcessors.lateUpdate(); }; Scene.prototype.prepRenderState = function () { this._projectionMatrix.m11 = 2 / this.stage.width; @@ -784,7 +791,7 @@ var BitSet = (function () { }; BitSet.prototype.isEmpty = function () { for (var i = this._bits.length - 1; i >= 0; i--) { - if (this._bits[i] != 0) + if (this._bits[i]) return false; } return true; @@ -805,10 +812,16 @@ var BitSet = (function () { } return -1; }; - BitSet.prototype.set = function (pos) { - var offset = pos >> 6; - this.ensure(offset); - this._bits[offset] |= 1 << pos; + BitSet.prototype.set = function (pos, value) { + if (value === void 0) { value = true; } + if (value) { + var offset = pos >> 6; + this.ensure(offset); + this._bits[offset] |= 1 << pos; + } + else { + this.clear(pos); + } }; BitSet.LONG_MASK = 0x3f; return BitSet; @@ -895,7 +908,7 @@ var EntityList = (function () { this._tempEntityList.forEach(function (entity) { _this._entities.remove(entity); entity.scene = null; - _this.scene.entityProcessors.forEach(function (processor) { return processor.remove(entity); }); + _this.scene.entityProcessors.onEntityRemoved(entity); }); this._tempEntityList.length = 0; } @@ -906,13 +919,69 @@ var EntityList = (function () { this._tempEntityList.forEach(function (entity) { _this._entities.push(entity); entity.scene = _this.scene; - _this.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(entity); }); + _this.scene.entityProcessors.onEntityAdded(entity); }); this._tempEntityList.length = 0; } }; return EntityList; }()); +var EntityProcessorList = (function () { + function EntityProcessorList() { + this._processors = []; + } + EntityProcessorList.prototype.add = function (processor) { + this._processors.push(processor); + }; + EntityProcessorList.prototype.remove = function (processor) { + this._processors.remove(processor); + }; + EntityProcessorList.prototype.onComponentAdded = function (entity) { + this.notifyEntityChanged(entity); + }; + EntityProcessorList.prototype.onComponentRemoved = function (entity) { + this.notifyEntityChanged(entity); + }; + EntityProcessorList.prototype.onEntityAdded = function (entity) { + this.notifyEntityChanged(entity); + }; + EntityProcessorList.prototype.onEntityRemoved = function (entity) { + this.removeFromProcessors(entity); + }; + EntityProcessorList.prototype.notifyEntityChanged = function (entity) { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].onChanged(entity); + } + }; + EntityProcessorList.prototype.removeFromProcessors = function (entity) { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].remove(entity); + } + }; + EntityProcessorList.prototype.begin = function () { + }; + EntityProcessorList.prototype.update = function () { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].update(); + } + }; + EntityProcessorList.prototype.lateUpdate = function () { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].lateUpdate(); + } + }; + EntityProcessorList.prototype.end = function () { + }; + EntityProcessorList.prototype.getProcessor = function () { + for (var i = 0; i < this._processors.length; i++) { + var processor = this._processors[i]; + if (processor instanceof EntitySystem) + return processor; + } + return null; + }; + return EntityProcessorList; +}()); var Matcher = (function () { function Matcher() { this.allSet = new BitSet(); @@ -937,6 +1006,19 @@ var Matcher = (function () { }; return Matcher; }()); +var Time = (function () { + function Time() { + } + Time.update = function (currentTime) { + var dt = (currentTime - this._lastTime) / 1000; + this.deltaTime = dt * this.timeScale; + this.unscaledDeltaTime = dt; + this._lastTime = currentTime; + }; + Time.timeScale = 1; + Time._lastTime = 0; + return Time; +}()); var MathHelper = (function () { function MathHelper() { } diff --git a/demo/libs/framework/framework.min.js b/demo/libs/framework/framework.min.js index b9104ab8..03168566 100644 --- a/demo/libs/framework/framework.min.js +++ b/demo/libs/framework/framework.min.js @@ -1 +1 @@ -window.framework={},window.__extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,i=t.length;n-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var n=t.findIndex(e);return-1==n?null:t[n]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return e.call(arguments[2],i,r,t)&&n.push(i),n},[]);for(var n=[],i=0,r=t.length;i=0&&t.splice(n,1)}while(n>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var n=t.findIndex(function(t){return t===e});return n>=0&&(t.splice(n,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,n){t.splice(e,n)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return n.push(e.call(arguments[2],i,r,t)),n},[]);for(var n=[],i=0,r=t.length;io?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var r=e(t),o=e(i);return n?-n(r,o):r=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var n=t.call(this)||this;return e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=[],n.entities=new EntityList(n),n.addEventListener(egret.Event.ACTIVATE,n.onActive,n),n.addEventListener(egret.Event.DEACTIVATE,n.onDeactive,n),n.addEventListener(egret.Event.ENTER_FRAME,n.update,n),n}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;e>6;0!=(e&t.LONG_MASK)&&n++,this._bits=new Array(n)}return t.prototype.and=function(t){for(var e,n=Math.min(this._bits.length,t._bits.length),i=0;i=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var n=this._bits[e];if(0!=n)if(-1!=n){var i=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((i=((i=(i>>4&252645135)+(252645135&i))>>8&16711935)+(16711935&i))>>16&65535)+(65535&i)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(0!=this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,n=1<>6;this.ensure(e),this._bits[e]|=1<0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.forEach(function(t){return t.remove(e)})}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.forEach(function(t){return t.onChanged(e)})}),this._tempEntityList.length=0}},t}(),Matcher=function(){function t(){this.allSet=new BitSet,this.exclusionSet=new BitSet,this.oneSet=new BitSet}return t.empty=function(){return new t},t.prototype.IsIntersted=function(t){if(!this.allSet.isEmpty())for(var e=this.allSet.nextSetBit(0);e>=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t}(),Matrix2D=function(){function t(t,e,n,i,r,o){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=n,this.m22=i,this.m31=r,this.m32=o}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),n=Math.sin(t);this.m11=e,this.m12=n,this.m21=-n,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var n=t.m11*e.m11+t.m12*e.m21,i=t.m11*e.m12+t.m12*e.m22,r=t.m21*e.m11+t.m22*e.m21,o=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=n,t.m12=i,t.m21=r,t.m22=o,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var r=t.createTranslation(n,i);return t.multiply(e,r)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(t,e){var n=1/t.determinant();return e.m11=t.m22*n,e.m12=-t.m12*n,e.m21=-t.m21*n,e.m22=t.m11*n,e.m31=(t.m32*t.m21-t.m31*t.m22)*n,e.m32=-(t.m32*t.m11-t.m31*t.m12)*n,e},t.createTranslation=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=1,i.m12=0,i.m21=0,i.m22=1,i.m31=e,i.m32=n,i},t.createRotation=function(e,n){n=t.identity;var i=Math.cos(e),r=Math.sin(e);return n.m11=i,n.m12=r,n.m21=-r,n.m22=i,n},t.createScale=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=e,i.m12=0,i.m21=0,i.m22=n,i.m31=0,i.m32=0,i},t._identity=new t(1,0,0,1,0,0),t}(),Vector2=function(){function t(t,e){this.x=0,this.y=0,this.x=t,this.y=e}return Object.defineProperty(t,"One",{get:function(){return this.unitVector2},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.x+=e.x,t.y+=e.y,t},t.divide=function(t,e){return t.x/=e.x,t.y/=e.y,t},t.multiply=function(t,e){return t.x*=e.x,t.y*=e.y,t},t.subtract=function(t,e){return t.x-=e.x,t.y-=e.y,t},t.prototype.normalize=function(){var t=1/Math.sqrt(this.x*this.x+this.y*this.y);this.x*=t,this.y*=t},t.transform=function(e,n){return new t(e.x*n.m11+e.y*n.m21,e.x*n.m12+e.y*n.m22)},t.unitVector2=new t(1,1),t}(); \ No newline at end of file +window.framework={},window.__extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,i=t.length;n-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var n=t.findIndex(e);return-1==n?null:t[n]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return e.call(arguments[2],i,r,t)&&n.push(i),n},[]);for(var n=[],i=0,r=t.length;i=0&&t.splice(n,1)}while(n>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var n=t.findIndex(function(t){return t===e});return n>=0&&(t.splice(n,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,n){t.splice(e,n)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return n.push(e.call(arguments[2],i,r,t)),n},[]);for(var n=[],i=0,r=t.length;io?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var r=e(t),o=e(i);return n?-n(r,o):r=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var n=t.call(this)||this;return e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=new EntityProcessorList,n.entities=new EntityList(n),n.addEventListener(egret.Event.ACTIVATE,n.onActive,n),n.addEventListener(egret.Event.DEACTIVATE,n.onDeactive,n),n.addEventListener(egret.Event.ENTER_FRAME,n.update,n),n}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;e>6;0!=(e&t.LONG_MASK)&&n++,this._bits=new Array(n)}return t.prototype.and=function(t){for(var e,n=Math.min(this._bits.length,t._bits.length),i=0;i=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var n=this._bits[e];if(0!=n)if(-1!=n){var i=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((i=((i=(i>>4&252645135)+(252645135&i))>>8&16711935)+(16711935&i))>>16&65535)+(65535&i)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,n=1<>6;this.ensure(n),this._bits[n]|=1<0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.onEntityRemoved(e)}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.onEntityAdded(e)}),this._tempEntityList.length=0}},t}(),EntityProcessorList=function(){function t(){this._processors=[]}return t.prototype.add=function(t){this._processors.push(t)},t.prototype.remove=function(t){this._processors.remove(t)},t.prototype.onComponentAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onComponentRemoved=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityRemoved=function(t){this.removeFromProcessors(t)},t.prototype.notifyEntityChanged=function(t){for(var e=0;e=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),Time=function(){function t(){}return t.update=function(t){var e=(t-this._lastTime)/1e3;this.deltaTime=e*this.timeScale,this.unscaledDeltaTime=e,this._lastTime=t},t.timeScale=1,t._lastTime=0,t}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t}(),Matrix2D=function(){function t(t,e,n,i,r,o){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=n,this.m22=i,this.m31=r,this.m32=o}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),n=Math.sin(t);this.m11=e,this.m12=n,this.m21=-n,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var n=t.m11*e.m11+t.m12*e.m21,i=t.m11*e.m12+t.m12*e.m22,r=t.m21*e.m11+t.m22*e.m21,o=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=n,t.m12=i,t.m21=r,t.m22=o,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var r=t.createTranslation(n,i);return t.multiply(e,r)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(t,e){var n=1/t.determinant();return e.m11=t.m22*n,e.m12=-t.m12*n,e.m21=-t.m21*n,e.m22=t.m11*n,e.m31=(t.m32*t.m21-t.m31*t.m22)*n,e.m32=-(t.m32*t.m11-t.m31*t.m12)*n,e},t.createTranslation=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=1,i.m12=0,i.m21=0,i.m22=1,i.m31=e,i.m32=n,i},t.createRotation=function(e,n){n=t.identity;var i=Math.cos(e),r=Math.sin(e);return n.m11=i,n.m12=r,n.m21=-r,n.m22=i,n},t.createScale=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=e,i.m12=0,i.m21=0,i.m22=n,i.m31=0,i.m32=0,i},t._identity=new t(1,0,0,1,0,0),t}(),Vector2=function(){function t(t,e){this.x=0,this.y=0,this.x=t,this.y=e}return Object.defineProperty(t,"One",{get:function(){return this.unitVector2},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.x+=e.x,t.y+=e.y,t},t.divide=function(t,e){return t.x/=e.x,t.y/=e.y,t},t.multiply=function(t,e){return t.x*=e.x,t.y*=e.y,t},t.subtract=function(t,e){return t.x-=e.x,t.y-=e.y,t},t.prototype.normalize=function(){var t=1/Math.sqrt(this.x*this.x+this.y*this.y);this.x*=t,this.y*=t},t.transform=function(e,n){return new t(e.x*n.m11+e.y*n.m21,e.x*n.m12+e.y*n.m22)},t.unitVector2=new t(1,1),t}(); \ No newline at end of file diff --git a/demo/src/game/SpawnerSystem.ts b/demo/src/game/SpawnerSystem.ts index 190bdfc7..019cbef2 100644 --- a/demo/src/game/SpawnerSystem.ts +++ b/demo/src/game/SpawnerSystem.ts @@ -4,7 +4,10 @@ class SpawnerSystem extends EntityProcessingSystem { } public processEntity(entity: Entity){ - let spawner = entity.getComponent(); + let spawner = entity.getComponent(SpawnComponent); + if (!spawner) + return; + if (spawner.numAlive <= 0) spawner.enabled = true; @@ -13,11 +16,13 @@ class SpawnerSystem extends EntityProcessingSystem { console.log("cooldown", spawner.cooldown); if (spawner.cooldown == -1){ + spawner.cooldown = Math.random() * 60; spawner.cooldown /= 4; } - spawner.cooldown -= 0.001; + spawner.cooldown -= Time.deltaTime; if (spawner.cooldown <= 0){ + spawner.cooldown = Math.random() * 60; // CreateEnemy spawner.numSpawned ++; spawner.numAlive ++; diff --git a/source/bin/framework.d.ts b/source/bin/framework.d.ts index 1f0f31c7..c9b3698c 100644 --- a/source/bin/framework.d.ts +++ b/source/bin/framework.d.ts @@ -27,6 +27,7 @@ declare abstract class Component { update(): void; bind(displayRender: egret.DisplayObject): this; registerComponent(): void; + deregisterComponent(): void; } declare class Entity { name: string; @@ -43,7 +44,7 @@ declare class Entity { setUpdateOrder(updateOrder: number): this; attachToScene(newScene: Scene): void; addComponent(component: T): T; - getComponent(): T; + getComponent(type: any): T; update(): void; destory(): void; } @@ -53,7 +54,7 @@ declare class Scene extends egret.DisplayObjectContainer { private _projectionMatrix; private _transformMatrix; private _matrixTransformMatrix; - readonly entityProcessors: EntitySystem[]; + readonly entityProcessors: EntityProcessorList; constructor(displayObject: egret.DisplayObject); createEntity(name: string): Entity; addEntity(entity: Entity): Entity; @@ -173,7 +174,7 @@ declare class BitSet { intersects(set: BitSet): boolean; isEmpty(): boolean; nextSetBit(from: number): number; - set(pos: number): void; + set(pos: number, value?: boolean): void; } declare class ComponentTypeManager { private static _componentTypesMask; @@ -196,6 +197,22 @@ declare class EntityList { removeAllEntities(): void; updateLists(): void; } +declare class EntityProcessorList { + private _processors; + add(processor: EntitySystem): void; + remove(processor: EntitySystem): void; + onComponentAdded(entity: Entity): void; + onComponentRemoved(entity: Entity): void; + onEntityAdded(entity: Entity): void; + onEntityRemoved(entity: Entity): void; + protected notifyEntityChanged(entity: Entity): void; + protected removeFromProcessors(entity: Entity): void; + begin(): void; + update(): void; + lateUpdate(): void; + end(): void; + getProcessor(): T; +} declare class Matcher { protected allSet: BitSet; protected exclusionSet: BitSet; @@ -203,6 +220,13 @@ declare class Matcher { static empty(): Matcher; IsIntersted(e: Entity): boolean; } +declare class Time { + static unscaledDeltaTime: any; + static deltaTime: number; + static timeScale: number; + private static _lastTime; + static update(currentTime: number): void; +} declare class MathHelper { static toDegrees(radians: number): number; static toRadians(degrees: number): number; diff --git a/source/bin/framework.js b/source/bin/framework.js index 76051810..360c5edd 100644 --- a/source/bin/framework.js +++ b/source/bin/framework.js @@ -76,7 +76,7 @@ Array.prototype.findAll = function (predicate) { Array.prototype.contains = function (value) { function contains(array, value) { for (var i = 0, len = array.length; i < len; i++) { - if (JSON.stringify(array[i]) == JSON.stringify(value)) { + if (array[i] == value) { return true; } } @@ -265,9 +265,12 @@ var Component = (function () { return this; }; Component.prototype.registerComponent = function () { - var _this = this; + this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false); + this.entity.scene.entityProcessors.onComponentAdded(this.entity); + }; + Component.prototype.deregisterComponent = function () { this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)); - this.entity.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(_this.entity); }); + this.entity.scene.entityProcessors.onComponentRemoved(this.entity); }; return Component; }()); @@ -328,8 +331,8 @@ var Entity = (function () { component.initialize(); return component; }; - Entity.prototype.getComponent = function () { - return this.components.firstOrDefault(function (component) { return component instanceof Component; }); + Entity.prototype.getComponent = function (type) { + return this.components.firstOrDefault(function (component) { return component instanceof type; }); }; Entity.prototype.update = function () { this.components.forEach(function (component) { return component.update(); }); @@ -351,7 +354,7 @@ var Scene = (function (_super) { var _this = _super.call(this) || this; displayObject.stage.addChild(_this); _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); - _this.entityProcessors = []; + _this.entityProcessors = new EntityProcessorList(); _this.entities = new EntityList(_this); _this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this); _this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this); @@ -380,14 +383,14 @@ var Scene = (function (_super) { }; Scene.prototype.addEntityProcessor = function (processor) { processor.scene = this; - this.entityProcessors.push(processor); + this.entityProcessors.add(processor); return processor; }; Scene.prototype.removeEntityProcessor = function (processor) { this.entityProcessors.remove(processor); }; Scene.prototype.getEntityProcessor = function () { - return this.entityProcessors.firstOrDefault(function (processor) { return processor instanceof EntitySystem; }); + return this.entityProcessors.getProcessor(); }; Scene.prototype.setActive = function () { SceneManager.setActiveScene(this); @@ -395,17 +398,21 @@ var Scene = (function (_super) { }; Scene.prototype.initialize = function () { this.camera = this.createEntity("camera").addComponent(new Camera()); - this.entityProcessors.forEach(function (processor) { return processor.initialize(); }); + if (this.entityProcessors) + this.entityProcessors.begin(); }; Scene.prototype.onActive = function () { }; Scene.prototype.onDeactive = function () { }; Scene.prototype.update = function () { + Time.update(egret.getTimer()); this.entities.updateLists(); - this.entityProcessors.forEach(function (processor) { return processor.update(); }); + if (this.entityProcessors) + this.entityProcessors.update(); this.entities.update(); - this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); }); + if (this.entityProcessors) + this.entityProcessors.lateUpdate(); }; Scene.prototype.prepRenderState = function () { this._projectionMatrix.m11 = 2 / this.stage.width; @@ -784,7 +791,7 @@ var BitSet = (function () { }; BitSet.prototype.isEmpty = function () { for (var i = this._bits.length - 1; i >= 0; i--) { - if (this._bits[i] != 0) + if (this._bits[i]) return false; } return true; @@ -805,10 +812,16 @@ var BitSet = (function () { } return -1; }; - BitSet.prototype.set = function (pos) { - var offset = pos >> 6; - this.ensure(offset); - this._bits[offset] |= 1 << pos; + BitSet.prototype.set = function (pos, value) { + if (value === void 0) { value = true; } + if (value) { + var offset = pos >> 6; + this.ensure(offset); + this._bits[offset] |= 1 << pos; + } + else { + this.clear(pos); + } }; BitSet.LONG_MASK = 0x3f; return BitSet; @@ -895,7 +908,7 @@ var EntityList = (function () { this._tempEntityList.forEach(function (entity) { _this._entities.remove(entity); entity.scene = null; - _this.scene.entityProcessors.forEach(function (processor) { return processor.remove(entity); }); + _this.scene.entityProcessors.onEntityRemoved(entity); }); this._tempEntityList.length = 0; } @@ -906,13 +919,69 @@ var EntityList = (function () { this._tempEntityList.forEach(function (entity) { _this._entities.push(entity); entity.scene = _this.scene; - _this.scene.entityProcessors.forEach(function (processor) { return processor.onChanged(entity); }); + _this.scene.entityProcessors.onEntityAdded(entity); }); this._tempEntityList.length = 0; } }; return EntityList; }()); +var EntityProcessorList = (function () { + function EntityProcessorList() { + this._processors = []; + } + EntityProcessorList.prototype.add = function (processor) { + this._processors.push(processor); + }; + EntityProcessorList.prototype.remove = function (processor) { + this._processors.remove(processor); + }; + EntityProcessorList.prototype.onComponentAdded = function (entity) { + this.notifyEntityChanged(entity); + }; + EntityProcessorList.prototype.onComponentRemoved = function (entity) { + this.notifyEntityChanged(entity); + }; + EntityProcessorList.prototype.onEntityAdded = function (entity) { + this.notifyEntityChanged(entity); + }; + EntityProcessorList.prototype.onEntityRemoved = function (entity) { + this.removeFromProcessors(entity); + }; + EntityProcessorList.prototype.notifyEntityChanged = function (entity) { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].onChanged(entity); + } + }; + EntityProcessorList.prototype.removeFromProcessors = function (entity) { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].remove(entity); + } + }; + EntityProcessorList.prototype.begin = function () { + }; + EntityProcessorList.prototype.update = function () { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].update(); + } + }; + EntityProcessorList.prototype.lateUpdate = function () { + for (var i = 0; i < this._processors.length; i++) { + this._processors[i].lateUpdate(); + } + }; + EntityProcessorList.prototype.end = function () { + }; + EntityProcessorList.prototype.getProcessor = function () { + for (var i = 0; i < this._processors.length; i++) { + var processor = this._processors[i]; + if (processor instanceof EntitySystem) + return processor; + } + return null; + }; + return EntityProcessorList; +}()); var Matcher = (function () { function Matcher() { this.allSet = new BitSet(); @@ -937,6 +1006,19 @@ var Matcher = (function () { }; return Matcher; }()); +var Time = (function () { + function Time() { + } + Time.update = function (currentTime) { + var dt = (currentTime - this._lastTime) / 1000; + this.deltaTime = dt * this.timeScale; + this.unscaledDeltaTime = dt; + this._lastTime = currentTime; + }; + Time.timeScale = 1; + Time._lastTime = 0; + return Time; +}()); var MathHelper = (function () { function MathHelper() { } diff --git a/source/bin/framework.min.js b/source/bin/framework.min.js index b9104ab8..03168566 100644 --- a/source/bin/framework.min.js +++ b/source/bin/framework.min.js @@ -1 +1 @@ -window.framework={},window.__extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,i=t.length;n-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var n=t.findIndex(e);return-1==n?null:t[n]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return e.call(arguments[2],i,r,t)&&n.push(i),n},[]);for(var n=[],i=0,r=t.length;i=0&&t.splice(n,1)}while(n>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var n=t.findIndex(function(t){return t===e});return n>=0&&(t.splice(n,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,n){t.splice(e,n)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return n.push(e.call(arguments[2],i,r,t)),n},[]);for(var n=[],i=0,r=t.length;io?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var r=e(t),o=e(i);return n?-n(r,o):r=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var n=t.call(this)||this;return e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=[],n.entities=new EntityList(n),n.addEventListener(egret.Event.ACTIVATE,n.onActive,n),n.addEventListener(egret.Event.DEACTIVATE,n.onDeactive,n),n.addEventListener(egret.Event.ENTER_FRAME,n.update,n),n}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;e>6;0!=(e&t.LONG_MASK)&&n++,this._bits=new Array(n)}return t.prototype.and=function(t){for(var e,n=Math.min(this._bits.length,t._bits.length),i=0;i=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var n=this._bits[e];if(0!=n)if(-1!=n){var i=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((i=((i=(i>>4&252645135)+(252645135&i))>>8&16711935)+(16711935&i))>>16&65535)+(65535&i)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(0!=this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,n=1<>6;this.ensure(e),this._bits[e]|=1<0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.forEach(function(t){return t.remove(e)})}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.forEach(function(t){return t.onChanged(e)})}),this._tempEntityList.length=0}},t}(),Matcher=function(){function t(){this.allSet=new BitSet,this.exclusionSet=new BitSet,this.oneSet=new BitSet}return t.empty=function(){return new t},t.prototype.IsIntersted=function(t){if(!this.allSet.isEmpty())for(var e=this.allSet.nextSetBit(0);e>=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t}(),Matrix2D=function(){function t(t,e,n,i,r,o){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=n,this.m22=i,this.m31=r,this.m32=o}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),n=Math.sin(t);this.m11=e,this.m12=n,this.m21=-n,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var n=t.m11*e.m11+t.m12*e.m21,i=t.m11*e.m12+t.m12*e.m22,r=t.m21*e.m11+t.m22*e.m21,o=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=n,t.m12=i,t.m21=r,t.m22=o,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var r=t.createTranslation(n,i);return t.multiply(e,r)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(t,e){var n=1/t.determinant();return e.m11=t.m22*n,e.m12=-t.m12*n,e.m21=-t.m21*n,e.m22=t.m11*n,e.m31=(t.m32*t.m21-t.m31*t.m22)*n,e.m32=-(t.m32*t.m11-t.m31*t.m12)*n,e},t.createTranslation=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=1,i.m12=0,i.m21=0,i.m22=1,i.m31=e,i.m32=n,i},t.createRotation=function(e,n){n=t.identity;var i=Math.cos(e),r=Math.sin(e);return n.m11=i,n.m12=r,n.m21=-r,n.m22=i,n},t.createScale=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=e,i.m12=0,i.m21=0,i.m22=n,i.m31=0,i.m32=0,i},t._identity=new t(1,0,0,1,0,0),t}(),Vector2=function(){function t(t,e){this.x=0,this.y=0,this.x=t,this.y=e}return Object.defineProperty(t,"One",{get:function(){return this.unitVector2},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.x+=e.x,t.y+=e.y,t},t.divide=function(t,e){return t.x/=e.x,t.y/=e.y,t},t.multiply=function(t,e){return t.x*=e.x,t.y*=e.y,t},t.subtract=function(t,e){return t.x-=e.x,t.y-=e.y,t},t.prototype.normalize=function(){var t=1/Math.sqrt(this.x*this.x+this.y*this.y);this.x*=t,this.y*=t},t.transform=function(e,n){return new t(e.x*n.m11+e.y*n.m21,e.x*n.m12+e.y*n.m22)},t.unitVector2=new t(1,1),t}(); \ No newline at end of file +window.framework={},window.__extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,i=t.length;n-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var n=t.findIndex(e);return-1==n?null:t[n]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return e.call(arguments[2],i,r,t)&&n.push(i),n},[]);for(var n=[],i=0,r=t.length;i=0&&t.splice(n,1)}while(n>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var n=t.findIndex(function(t){return t===e});return n>=0&&(t.splice(n,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,n){t.splice(e,n)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,r){return n.push(e.call(arguments[2],i,r,t)),n},[]);for(var n=[],i=0,r=t.length;io?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var r=e(t),o=e(i);return n?-n(r,o):r=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var n=t.call(this)||this;return e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=new EntityProcessorList,n.entities=new EntityList(n),n.addEventListener(egret.Event.ACTIVATE,n.onActive,n),n.addEventListener(egret.Event.DEACTIVATE,n.onDeactive,n),n.addEventListener(egret.Event.ENTER_FRAME,n.update,n),n}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;e>6;0!=(e&t.LONG_MASK)&&n++,this._bits=new Array(n)}return t.prototype.and=function(t){for(var e,n=Math.min(this._bits.length,t._bits.length),i=0;i=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var n=this._bits[e];if(0!=n)if(-1!=n){var i=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((i=((i=(i>>4&252645135)+(252645135&i))>>8&16711935)+(16711935&i))>>16&65535)+(65535&i)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,n=1<>6;this.ensure(n),this._bits[n]|=1<0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.onEntityRemoved(e)}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.onEntityAdded(e)}),this._tempEntityList.length=0}},t}(),EntityProcessorList=function(){function t(){this._processors=[]}return t.prototype.add=function(t){this._processors.push(t)},t.prototype.remove=function(t){this._processors.remove(t)},t.prototype.onComponentAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onComponentRemoved=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityRemoved=function(t){this.removeFromProcessors(t)},t.prototype.notifyEntityChanged=function(t){for(var e=0;e=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),Time=function(){function t(){}return t.update=function(t){var e=(t-this._lastTime)/1e3;this.deltaTime=e*this.timeScale,this.unscaledDeltaTime=e,this._lastTime=t},t.timeScale=1,t._lastTime=0,t}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t}(),Matrix2D=function(){function t(t,e,n,i,r,o){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=n,this.m22=i,this.m31=r,this.m32=o}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),n=Math.sin(t);this.m11=e,this.m12=n,this.m21=-n,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var n=t.m11*e.m11+t.m12*e.m21,i=t.m11*e.m12+t.m12*e.m22,r=t.m21*e.m11+t.m22*e.m21,o=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=n,t.m12=i,t.m21=r,t.m22=o,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var r=t.createTranslation(n,i);return t.multiply(e,r)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(t,e){var n=1/t.determinant();return e.m11=t.m22*n,e.m12=-t.m12*n,e.m21=-t.m21*n,e.m22=t.m11*n,e.m31=(t.m32*t.m21-t.m31*t.m22)*n,e.m32=-(t.m32*t.m11-t.m31*t.m12)*n,e},t.createTranslation=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=1,i.m12=0,i.m21=0,i.m22=1,i.m31=e,i.m32=n,i},t.createRotation=function(e,n){n=t.identity;var i=Math.cos(e),r=Math.sin(e);return n.m11=i,n.m12=r,n.m21=-r,n.m22=i,n},t.createScale=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=e,i.m12=0,i.m21=0,i.m22=n,i.m31=0,i.m32=0,i},t._identity=new t(1,0,0,1,0,0),t}(),Vector2=function(){function t(t,e){this.x=0,this.y=0,this.x=t,this.y=e}return Object.defineProperty(t,"One",{get:function(){return this.unitVector2},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.x+=e.x,t.y+=e.y,t},t.divide=function(t,e){return t.x/=e.x,t.y/=e.y,t},t.multiply=function(t,e){return t.x*=e.x,t.y*=e.y,t},t.subtract=function(t,e){return t.x-=e.x,t.y-=e.y,t},t.prototype.normalize=function(){var t=1/Math.sqrt(this.x*this.x+this.y*this.y);this.x*=t,this.y*=t},t.transform=function(e,n){return new t(e.x*n.m11+e.y*n.m21,e.x*n.m12+e.y*n.m22)},t.unitVector2=new t(1,1),t}(); \ No newline at end of file diff --git a/source/package-lock.json b/source/package-lock.json index a81e3255..31ba235f 100644 --- a/source/package-lock.json +++ b/source/package-lock.json @@ -1,5 +1,5 @@ { - "name": "egret-ecs", + "name": "@esengine/egret-framework", "version": "1.0.0", "lockfileVersion": 1, "requires": true, diff --git a/source/package.json b/source/package.json index cf73534a..61ab97ed 100644 --- a/source/package.json +++ b/source/package.json @@ -28,5 +28,7 @@ "watchify": "^3.9.0", "gulp-inject-string": "^1.1.2" }, - "publishConfig": { "registry": "https://npm.pkg.github.com/359807859@qq.com" } + "publishConfig": { + "registry": "https://npm.pkg.github.com/359807859@qq.com" + } } diff --git a/source/src/ECS/Component.ts b/source/src/ECS/Component.ts index 138f6d0f..d919d38e 100644 --- a/source/src/ECS/Component.ts +++ b/source/src/ECS/Component.ts @@ -33,7 +33,12 @@ abstract class Component { /** 内部使用 运行时不应该调用 */ public registerComponent(){ + this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false); + this.entity.scene.entityProcessors.onComponentAdded(this.entity); + } + + public deregisterComponent(){ this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)); - this.entity.scene.entityProcessors.forEach(processor => processor.onChanged(this.entity)); + this.entity.scene.entityProcessors.onComponentRemoved(this.entity); } } \ No newline at end of file diff --git a/source/src/ECS/Entity.ts b/source/src/ECS/Entity.ts index 1eeeaf08..08c9e3ee 100644 --- a/source/src/ECS/Entity.ts +++ b/source/src/ECS/Entity.ts @@ -70,8 +70,8 @@ class Entity { return component; } - public getComponent(): T{ - return this.components.firstOrDefault(component => component instanceof Component) as T; + public getComponent(type): T{ + return this.components.firstOrDefault(component => component instanceof type) as T; } public update(){ diff --git a/source/src/ECS/Scene.ts b/source/src/ECS/Scene.ts index 661b582e..1aa29325 100644 --- a/source/src/ECS/Scene.ts +++ b/source/src/ECS/Scene.ts @@ -7,13 +7,13 @@ class Scene extends egret.DisplayObjectContainer { private _transformMatrix: Matrix2D; private _matrixTransformMatrix: Matrix2D; - public readonly entityProcessors: EntitySystem[]; + public readonly entityProcessors: EntityProcessorList; constructor(displayObject: egret.DisplayObject){ super(); displayObject.stage.addChild(this); this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); - this.entityProcessors = []; + this.entityProcessors = new EntityProcessorList(); this.entities = new EntityList(this); this.addEventListener(egret.Event.ACTIVATE, this.onActive, this); @@ -53,7 +53,7 @@ class Scene extends egret.DisplayObjectContainer { */ public addEntityProcessor(processor: EntitySystem){ processor.scene = this; - this.entityProcessors.push(processor); + this.entityProcessors.add(processor); return processor; } @@ -62,7 +62,7 @@ class Scene extends egret.DisplayObjectContainer { } public getEntityProcessor(): T { - return this.entityProcessors.firstOrDefault(processor => processor instanceof EntitySystem) as T; + return this.entityProcessors.getProcessor(); } public setActive(): Scene{ @@ -75,7 +75,9 @@ class Scene extends egret.DisplayObjectContainer { public initialize(){ /** 初始化默认相机 */ this.camera = this.createEntity("camera").addComponent(new Camera()); - this.entityProcessors.forEach(processor => processor.initialize()); + + if (this.entityProcessors) + this.entityProcessors.begin(); } /** 场景激活 */ @@ -89,11 +91,17 @@ class Scene extends egret.DisplayObjectContainer { } public update(){ + Time.update(egret.getTimer()); + this.entities.updateLists(); - this.entityProcessors.forEach(processor => processor.update()); + if (this.entityProcessors) + this.entityProcessors.update() + this.entities.update(); - this.entityProcessors.forEach(processor => processor.lateUpdate()); + + if (this.entityProcessors) + this.entityProcessors.lateUpdate(); } public prepRenderState(){ diff --git a/source/src/ECS/Utils/BitSet.ts b/source/src/ECS/Utils/BitSet.ts index 3bbd10e8..d75c2f35 100644 --- a/source/src/ECS/Utils/BitSet.ts +++ b/source/src/ECS/Utils/BitSet.ts @@ -94,7 +94,7 @@ class BitSet{ public isEmpty(): boolean{ for (let i = this._bits.length - 1; i >= 0; i --){ - if (this._bits[i] != 0) + if (this._bits[i]) return false; } @@ -121,9 +121,13 @@ class BitSet{ return -1; } - public set(pos: number){ - let offset = pos >> 6; - this.ensure(offset); - this._bits[offset] |= 1 << pos; + public set(pos: number, value: boolean = true){ + if (value){ + let offset = pos >> 6; + this.ensure(offset); + this._bits[offset] |= 1 << pos; + }else{ + this.clear(pos); + } } } \ No newline at end of file diff --git a/source/src/ECS/Utils/EntityList.ts b/source/src/ECS/Utils/EntityList.ts index 47415e07..909a3dca 100644 --- a/source/src/ECS/Utils/EntityList.ts +++ b/source/src/ECS/Utils/EntityList.ts @@ -69,7 +69,7 @@ class EntityList{ this._entities.remove(entity); entity.scene = null; - this.scene.entityProcessors.forEach(processor => processor.remove(entity)); + this.scene.entityProcessors.onEntityRemoved(entity); }); this._tempEntityList.length = 0; @@ -83,7 +83,7 @@ class EntityList{ this._entities.push(entity); entity.scene = this.scene; - this.scene.entityProcessors.forEach(processor => processor.onChanged(entity)); + this.scene.entityProcessors.onEntityAdded(entity) }); this._tempEntityList.length = 0; diff --git a/source/src/ECS/Utils/EntityProcessorList.ts b/source/src/ECS/Utils/EntityProcessorList.ts new file mode 100644 index 00000000..eedd5fdd --- /dev/null +++ b/source/src/ECS/Utils/EntityProcessorList.ts @@ -0,0 +1,69 @@ +class EntityProcessorList { + private _processors: EntitySystem[] = []; + + public add(processor: EntitySystem){ + this._processors.push(processor); + } + + public remove(processor: EntitySystem){ + this._processors.remove(processor); + } + + public onComponentAdded(entity: Entity){ + this.notifyEntityChanged(entity); + } + + public onComponentRemoved(entity: Entity){ + this.notifyEntityChanged(entity); + } + + public onEntityAdded(entity: Entity){ + this.notifyEntityChanged(entity); + } + + public onEntityRemoved(entity: Entity){ + this.removeFromProcessors(entity); + } + + protected notifyEntityChanged(entity: Entity){ + for (let i = 0; i < this._processors.length; i ++){ + this._processors[i].onChanged(entity); + } + } + + protected removeFromProcessors(entity: Entity){ + for (let i = 0; i < this._processors.length; i ++){ + this._processors[i].remove(entity); + } + } + + public begin(){ + + } + + public update(){ + for (let i = 0; i < this._processors.length; i++){ + this._processors[i].update(); + } + } + + public lateUpdate(){ + for (let i = 0; i < this._processors.length; i ++){ + this._processors[i].lateUpdate(); + } + } + + public end(){ + + } + + public getProcessor(): T{ + for (let i = 0; i < this._processors.length; i ++){ + let processor = this._processors[i]; + if (processor instanceof EntitySystem) + return processor as T; + } + + return null; + } +} \ No newline at end of file diff --git a/source/src/ECS/Utils/Time.ts b/source/src/ECS/Utils/Time.ts new file mode 100644 index 00000000..2eac3e94 --- /dev/null +++ b/source/src/ECS/Utils/Time.ts @@ -0,0 +1,15 @@ +class Time { + public static unscaledDeltaTime; + public static deltaTime: number; + public static timeScale = 1; + + private static _lastTime = 0; + + public static update(currentTime: number){ + let dt = (currentTime - this._lastTime) / 1000; + this.deltaTime = dt * this.timeScale; + this.unscaledDeltaTime = dt; + + this._lastTime = currentTime; + } +} \ No newline at end of file diff --git a/source/src/Extension.ts b/source/src/Extension.ts index 04d9c913..3a7ce9d4 100644 --- a/source/src/Extension.ts +++ b/source/src/Extension.ts @@ -174,7 +174,7 @@ Array.prototype.findAll = function (predicate) { Array.prototype.contains = function (value) { function contains(array, value) { for (let i = 0, len = array.length; i < len; i++) { - if (JSON.stringify(array[i]) == JSON.stringify(value)) { + if (array[i] == value) { return true; } }