diff --git a/demo/libs/framework/framework.d.ts b/demo/libs/framework/framework.d.ts index 3f9d0423..d99afe4c 100644 --- a/demo/libs/framework/framework.d.ts +++ b/demo/libs/framework/framework.d.ts @@ -20,6 +20,9 @@ declare interface Array { declare abstract class Component { entity: Entity; displayRender: egret.DisplayObject; + private _enabled; + enabled: boolean; + setEnabled(isEnabled: boolean): this; abstract initialize(): any; update(): void; bind(displayRender: egret.DisplayObject): this; @@ -30,11 +33,15 @@ declare class Entity { readonly transform: Transform; readonly components: Component[]; private _updateOrder; + private _enabled; + enabled: boolean; + setEnabled(isEnabled: boolean): this; constructor(name: string); updateOrder: number; setUpdateOrder(updateOrder: number): this; attachToScene(newScene: Scene): void; addComponent(component: T): T; + getComponent(): T; update(): void; destory(): void; } @@ -44,9 +51,15 @@ declare class Scene extends egret.DisplayObjectContainer { private _projectionMatrix; private _transformMatrix; private _matrixTransformMatrix; + readonly entityProcessors: EntitySystem[]; constructor(displayObject: egret.DisplayObject); createEntity(name: string): Entity; addEntity(entity: Entity): Entity; + destoryAllEntities(): void; + findEntity(name: string): Entity; + addEntityProcessor(processor: EntitySystem): EntitySystem; + removeEntityProcessor(processor: EntitySystem): void; + getEntityProcessor(): T; setActive(): Scene; initialize(): void; onActive(): void; @@ -118,6 +131,31 @@ declare class Camera extends Component { updateMatrixes(): void; destory(): void; } +declare class EntitySystem { + private _scene; + private _entities; + private _matcher; + readonly matcher: Matcher; + scene: Scene; + constructor(matcher?: Matcher); + initialize(): void; + update(): void; + lateUpdate(): void; + protected begin(): void; + protected process(entities: Entity[]): void; + protected lateProcess(entities: Entity[]): void; + protected end(): void; +} +declare abstract class EntityProcessingSystem extends EntitySystem { + constructor(matcher: Matcher); + abstract processEntity(entity: Entity): any; + lateProcessEntity(entity: Entity): void; + protected process(entities: Entity[]): void; + protected lateProcess(entities: Entity[]): void; +} +declare class Matcher { + static empty(): Matcher; +} 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 9fb3cb0a..3886dae2 100644 --- a/demo/libs/framework/framework.js +++ b/demo/libs/framework/framework.js @@ -240,7 +240,24 @@ Array.prototype.sum = function (selector) { }; var Component = (function () { function Component() { + this._enabled = true; } + Object.defineProperty(Component.prototype, "enabled", { + get: function () { + return this.entity ? this.entity.enabled && this._enabled : this._enabled; + }, + set: function (value) { + this.setEnabled(value); + }, + enumerable: true, + configurable: true + }); + Component.prototype.setEnabled = function (isEnabled) { + if (this._enabled != isEnabled) { + this._enabled = isEnabled; + } + return this; + }; Component.prototype.update = function () { }; Component.prototype.bind = function (displayRender) { @@ -252,10 +269,27 @@ var Component = (function () { var Entity = (function () { function Entity(name) { this._updateOrder = 0; + this._enabled = true; this.name = name; this.transform = new Transform(this); this.components = []; } + Object.defineProperty(Entity.prototype, "enabled", { + get: function () { + return this._enabled; + }, + set: function (value) { + this.setEnabled(value); + }, + enumerable: true, + configurable: true + }); + Entity.prototype.setEnabled = function (isEnabled) { + if (this._enabled != isEnabled) { + this._enabled = isEnabled; + } + return this; + }; Object.defineProperty(Entity.prototype, "updateOrder", { get: function () { return this._updateOrder; @@ -287,6 +321,9 @@ var Entity = (function () { component.initialize(); return component; }; + Entity.prototype.getComponent = function () { + return this.components.firstOrDefault(function (component) { return component instanceof Component; }); + }; Entity.prototype.update = function () { this.components.forEach(function (component) { return component.update(); }); this.transform.updateTransform(); @@ -307,8 +344,8 @@ var Scene = (function (_super) { var _this = _super.call(this) || this; _this.entities = []; displayObject.stage.addChild(_this); - _this.camera = _this.createEntity("camera").addComponent(new Camera()); _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); + _this.entityProcessors = []; _this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this); _this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this); _this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this); @@ -324,18 +361,39 @@ var Scene = (function (_super) { entity.scene = this; return entity; }; + Scene.prototype.destoryAllEntities = function () { + this.entities.forEach(function (entity) { return entity.destory(); }); + }; + Scene.prototype.findEntity = function (name) { + return this.entities.firstOrDefault(function (entity) { return entity.name == name; }); + }; + Scene.prototype.addEntityProcessor = function (processor) { + processor.scene = this; + this.entityProcessors.push(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; }); + }; Scene.prototype.setActive = function () { SceneManager.setActiveScene(this); return this; }; Scene.prototype.initialize = function () { + this.camera = this.createEntity("camera").addComponent(new Camera()); + this.entityProcessors.forEach(function (processor) { return processor.initialize(); }); }; Scene.prototype.onActive = function () { }; Scene.prototype.onDeactive = function () { }; Scene.prototype.update = function () { + this.entityProcessors.forEach(function (processor) { return processor.update(); }); this.entities.forEach(function (entity) { return entity.update(); }); + this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); }); }; Scene.prototype.prepRenderState = function () { this._projectionMatrix.m11 = 2 / this.stage.width; @@ -561,6 +619,74 @@ var Camera = (function (_super) { }; return Camera; }(Component)); +var EntitySystem = (function () { + function EntitySystem(matcher) { + this._entities = []; + this._matcher = matcher ? matcher : Matcher.empty(); + } + Object.defineProperty(EntitySystem.prototype, "matcher", { + get: function () { + return this._matcher; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(EntitySystem.prototype, "scene", { + get: function () { + return this._scene; + }, + set: function (value) { + this._scene = value; + this._entities = []; + }, + enumerable: true, + configurable: true + }); + EntitySystem.prototype.initialize = function () { + }; + EntitySystem.prototype.update = function () { + this.begin(); + this.process(this._entities); + }; + EntitySystem.prototype.lateUpdate = function () { + this.lateProcess(this._entities); + this.end(); + }; + EntitySystem.prototype.begin = function () { + }; + EntitySystem.prototype.process = function (entities) { + }; + EntitySystem.prototype.lateProcess = function (entities) { + }; + EntitySystem.prototype.end = function () { + }; + return EntitySystem; +}()); +var EntityProcessingSystem = (function (_super) { + __extends(EntityProcessingSystem, _super); + function EntityProcessingSystem(matcher) { + return _super.call(this, matcher) || this; + } + EntityProcessingSystem.prototype.lateProcessEntity = function (entity) { + }; + EntityProcessingSystem.prototype.process = function (entities) { + var _this = this; + entities.forEach(function (entity) { return _this.processEntity(entity); }); + }; + EntityProcessingSystem.prototype.lateProcess = function (entities) { + var _this = this; + entities.forEach(function (entity) { return _this.lateProcessEntity(entity); }); + }; + return EntityProcessingSystem; +}(EntitySystem)); +var Matcher = (function () { + function Matcher() { + } + Matcher.empty = function () { + return new Matcher(); + }; + return Matcher; +}()); var MathHelper = (function () { function MathHelper() { } diff --git a/demo/libs/framework/framework.min.js b/demo/libs/framework/framework.min.js index f0173442..43fd6dcb 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,n){t.__proto__=n}||function(t,n){for(var i in n)n.hasOwnProperty(i)&&(t[i]=n[i])};return function(n,i){function e(){this.constructor=n}t(n,i),n.prototype=null===i?Object.create(i):(e.prototype=i.prototype,new e)}}(),Array.prototype.findIndex=function(t){return function(t,n){for(var i=0,e=t.length;i-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,n){var i=t.findIndex(n);return-1==i?null:t[i]}(this,t)},Array.prototype.find=function(t){return function(t,n){return t.firstOrDefault(n)}(this,t)},Array.prototype.where=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(i,e,r){return n.call(arguments[2],e,r,t)&&i.push(e),i},[]);for(var i=[],e=0,r=t.length;e=0&&t.splice(i,1)}while(i>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,n){var i=t.findIndex(function(t){return t===n});return i>=0&&(t.splice(i,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,n){t.splice(n,1)}(this,t)},Array.prototype.removeRange=function(t,n){return function(t,n,i){t.splice(n,i)}(this,t,n)},Array.prototype.select=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(i,e,r){return i.push(n.call(arguments[2],e,r,t)),i},[]);for(var i=[],e=0,r=t.length;eo?1:-1}),t}(this,t,n)},Array.prototype.orderByDescending=function(t,n){return function(t,n,i){return t.sort(function(t,e){var r=n(t),o=n(e);return i?-i(r,o):r=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function n(n){var i=t.call(this)||this;return i.entities=[],n.stage.addChild(i),i.camera=i.createEntity("camera").addComponent(new Camera),i._projectionMatrix=new Matrix2D(0,0,0,0,0,0),i.addEventListener(egret.Event.ACTIVATE,i.onActive,i),i.addEventListener(egret.Event.DEACTIVATE,i.onDeactive,i),i.addEventListener(egret.Event.ENTER_FRAME,i.update,i),i}return __extends(n,t),n.prototype.createEntity=function(t){var n=new Entity(t);return n.transform.position=new Vector2(0,0),this.addEntity(n)},n.prototype.addEntity=function(t){return this.entities.push(t),t.scene=this,t},n.prototype.setActive=function(){return SceneManager.setActiveScene(this),this},n.prototype.initialize=function(){},n.prototype.onActive=function(){},n.prototype.onDeactive=function(){},n.prototype.update=function(){this.entities.forEach(function(t){return t.update()})},n.prototype.prepRenderState=function(){this._projectionMatrix.m11=2/this.stage.width,this._projectionMatrix.m22=-2/this.stage.height,this._transformMatrix=this.camera.transformMatrix,this._matrixTransformMatrix=Matrix2D.multiply(this._transformMatrix,this._projectionMatrix)},n.prototype.destory=function(){this.removeEventListener(egret.Event.DEACTIVATE,this.onDeactive,this),this.removeEventListener(egret.Event.ACTIVATE,this.onActive,this),this.camera.destory(),this.camera=null,this.entities.forEach(function(t){return t.destory()}),this.entities.length=0},n}(egret.DisplayObjectContainer),SceneManager=function(){function t(){}return t.createScene=function(t,n){return n.name=t,this._loadedScenes.set(t,n),n},t.setActiveScene=function(t){if(this._activeScene){if(this._activeScene==t)return;this._lastScene=this._activeScene,this._activeScene.destory()}return this._activeScene=t,this._activeScene.initialize(),t},t.getActiveScene=function(){return this._activeScene},t._loadedScenes=new Map,t}();!function(t){t[t.clean=0]="clean",t[t.positionDirty=1]="positionDirty",t[t.scaleDirty=2]="scaleDirty",t[t.rotationDirty=3]="rotationDirty"}(DirtyType||(DirtyType={}));var Transform=function(){function t(t){this._localRotation=0,this._worldTransform=Matrix2D.identity,this._worldToLocalTransform=Matrix2D.identity,this._worldInverseTransform=Matrix2D.identity,this._rotation=0,this.entity=t,this._scale=this._localScale=Vector2.One,this._children=[]}return Object.defineProperty(t.prototype,"childCount",{get:function(){return this._children.length},enumerable:!0,configurable:!0}),t.prototype.getChild=function(t){return this._children[t]},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},set:function(t){this.setParent(t)},enumerable:!0,configurable:!0}),t.prototype.setParent=function(t){return this._parent==t?this:(this._parent&&this._parent._children.remove(this),t&&t._children.push(this),this._parent=t,this)},Object.defineProperty(t.prototype,"position",{get:function(){return this.updateTransform(),this.parent?(this.parent.updateTransform(),this._position=Vector2.transform(this._localPosition,this.parent._worldTransform)):this._position=this._localPosition,this._position},set:function(t){this.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.updateTransform(),this._localPosition},set:function(t){this.setLocalPosition(t)},enumerable:!0,configurable:!0}),t.prototype.setLocalPosition=function(t){return t==this._localPosition?this:(this._localPosition=t,this._localDirty=this._positionDirty=this._localPositionDirty=this._localRotationDirty=this._localScaleDirty=!0,this)},t.prototype.setPosition=function(t){if(t==this._position)return this;this._position=t,this.parent?this.localPosition=Vector2.transform(this._position,this._worldToLocalTransform):this.localPosition=t;for(var n=0;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 n.entities=[],e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=[],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){return this.entities.push(t),t.scene=this,t},e.prototype.destoryAllEntities=function(){this.entities.forEach(function(t){return t.destory()})},e.prototype.findEntity=function(t){return this.entities.firstOrDefault(function(e){return e.name==t})},e.prototype.addEntityProcessor=function(t){return t.scene=this,this.entityProcessors.push(t),t},e.prototype.removeEntityProcessor=function(t){this.entityProcessors.remove(t)},e.prototype.getEntityProcessor=function(){return this.entityProcessors.firstOrDefault(function(t){return t instanceof EntitySystem})},e.prototype.setActive=function(){return SceneManager.setActiveScene(this),this},e.prototype.initialize=function(){this.camera=this.createEntity("camera").addComponent(new Camera),this.entityProcessors.forEach(function(t){return t.initialize()})},e.prototype.onActive=function(){},e.prototype.onDeactive=function(){},e.prototype.update=function(){this.entityProcessors.forEach(function(t){return t.update()}),this.entities.forEach(function(t){return t.update()}),this.entityProcessors.forEach(function(t){return t.lateUpdate()})},e.prototype.prepRenderState=function(){this._projectionMatrix.m11=2/this.stage.width,this._projectionMatrix.m22=-2/this.stage.height,this._transformMatrix=this.camera.transformMatrix,this._matrixTransformMatrix=Matrix2D.multiply(this._transformMatrix,this._projectionMatrix)},e.prototype.destory=function(){this.removeEventListener(egret.Event.DEACTIVATE,this.onDeactive,this),this.removeEventListener(egret.Event.ACTIVATE,this.onActive,this),this.camera.destory(),this.camera=null,this.entities.forEach(function(t){return t.destory()}),this.entities.length=0},e}(egret.DisplayObjectContainer),SceneManager=function(){function t(){}return t.createScene=function(t,e){return e.name=t,this._loadedScenes.set(t,e),e},t.setActiveScene=function(t){if(this._activeScene){if(this._activeScene==t)return;this._lastScene=this._activeScene,this._activeScene.destory()}return this._activeScene=t,this._activeScene.initialize(),t},t.getActiveScene=function(){return this._activeScene},t._loadedScenes=new Map,t}();!function(t){t[t.clean=0]="clean",t[t.positionDirty=1]="positionDirty",t[t.scaleDirty=2]="scaleDirty",t[t.rotationDirty=3]="rotationDirty"}(DirtyType||(DirtyType={}));var Transform=function(){function t(t){this._localRotation=0,this._worldTransform=Matrix2D.identity,this._worldToLocalTransform=Matrix2D.identity,this._worldInverseTransform=Matrix2D.identity,this._rotation=0,this.entity=t,this._scale=this._localScale=Vector2.One,this._children=[]}return Object.defineProperty(t.prototype,"childCount",{get:function(){return this._children.length},enumerable:!0,configurable:!0}),t.prototype.getChild=function(t){return this._children[t]},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},set:function(t){this.setParent(t)},enumerable:!0,configurable:!0}),t.prototype.setParent=function(t){return this._parent==t?this:(this._parent&&this._parent._children.remove(this),t&&t._children.push(this),this._parent=t,this)},Object.defineProperty(t.prototype,"position",{get:function(){return this.updateTransform(),this.parent?(this.parent.updateTransform(),this._position=Vector2.transform(this._localPosition,this.parent._worldTransform)):this._position=this._localPosition,this._position},set:function(t){this.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.updateTransform(),this._localPosition},set:function(t){this.setLocalPosition(t)},enumerable:!0,configurable:!0}),t.prototype.setLocalPosition=function(t){return t==this._localPosition?this:(this._localPosition=t,this._localDirty=this._positionDirty=this._localPositionDirty=this._localRotationDirty=this._localScaleDirty=!0,this)},t.prototype.setPosition=function(t){if(t==this._position)return this;this._position=t,this.parent?this.localPosition=Vector2.transform(this._position,this._worldToLocalTransform):this.localPosition=t;for(var e=0;e(); + if (spawner.numAlive <= 0) + spawner.enabled = true; + + if (!spawner.enabled) + return; + + console.log("cooldown", spawner.cooldown); + if (spawner.cooldown == -1){ + spawner.cooldown /= 4; + } + + spawner.cooldown -= 0.001; + if (spawner.cooldown <= 0){ + // CreateEnemy + spawner.numSpawned ++; + spawner.numAlive ++; + + if (spawner.numAlive > 0) + spawner.enabled = false; + } + } +} \ No newline at end of file diff --git a/demo/src/game/TestComponent.ts b/demo/src/game/TestComponent.ts deleted file mode 100644 index 0ad69e0a..00000000 --- a/demo/src/game/TestComponent.ts +++ /dev/null @@ -1,14 +0,0 @@ -class TestComponent extends Component { - constructor(displayRender: egret.DisplayObject){ - super(); - this.bind(displayRender); - } - - public initialize(){ - // console.log("initialize"); - } - - public update(){ - // console.log("update"); - } -} \ No newline at end of file diff --git a/source/bin/framework.d.ts b/source/bin/framework.d.ts index 3f9d0423..d99afe4c 100644 --- a/source/bin/framework.d.ts +++ b/source/bin/framework.d.ts @@ -20,6 +20,9 @@ declare interface Array { declare abstract class Component { entity: Entity; displayRender: egret.DisplayObject; + private _enabled; + enabled: boolean; + setEnabled(isEnabled: boolean): this; abstract initialize(): any; update(): void; bind(displayRender: egret.DisplayObject): this; @@ -30,11 +33,15 @@ declare class Entity { readonly transform: Transform; readonly components: Component[]; private _updateOrder; + private _enabled; + enabled: boolean; + setEnabled(isEnabled: boolean): this; constructor(name: string); updateOrder: number; setUpdateOrder(updateOrder: number): this; attachToScene(newScene: Scene): void; addComponent(component: T): T; + getComponent(): T; update(): void; destory(): void; } @@ -44,9 +51,15 @@ declare class Scene extends egret.DisplayObjectContainer { private _projectionMatrix; private _transformMatrix; private _matrixTransformMatrix; + readonly entityProcessors: EntitySystem[]; constructor(displayObject: egret.DisplayObject); createEntity(name: string): Entity; addEntity(entity: Entity): Entity; + destoryAllEntities(): void; + findEntity(name: string): Entity; + addEntityProcessor(processor: EntitySystem): EntitySystem; + removeEntityProcessor(processor: EntitySystem): void; + getEntityProcessor(): T; setActive(): Scene; initialize(): void; onActive(): void; @@ -118,6 +131,31 @@ declare class Camera extends Component { updateMatrixes(): void; destory(): void; } +declare class EntitySystem { + private _scene; + private _entities; + private _matcher; + readonly matcher: Matcher; + scene: Scene; + constructor(matcher?: Matcher); + initialize(): void; + update(): void; + lateUpdate(): void; + protected begin(): void; + protected process(entities: Entity[]): void; + protected lateProcess(entities: Entity[]): void; + protected end(): void; +} +declare abstract class EntityProcessingSystem extends EntitySystem { + constructor(matcher: Matcher); + abstract processEntity(entity: Entity): any; + lateProcessEntity(entity: Entity): void; + protected process(entities: Entity[]): void; + protected lateProcess(entities: Entity[]): void; +} +declare class Matcher { + static empty(): Matcher; +} 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 9fb3cb0a..3886dae2 100644 --- a/source/bin/framework.js +++ b/source/bin/framework.js @@ -240,7 +240,24 @@ Array.prototype.sum = function (selector) { }; var Component = (function () { function Component() { + this._enabled = true; } + Object.defineProperty(Component.prototype, "enabled", { + get: function () { + return this.entity ? this.entity.enabled && this._enabled : this._enabled; + }, + set: function (value) { + this.setEnabled(value); + }, + enumerable: true, + configurable: true + }); + Component.prototype.setEnabled = function (isEnabled) { + if (this._enabled != isEnabled) { + this._enabled = isEnabled; + } + return this; + }; Component.prototype.update = function () { }; Component.prototype.bind = function (displayRender) { @@ -252,10 +269,27 @@ var Component = (function () { var Entity = (function () { function Entity(name) { this._updateOrder = 0; + this._enabled = true; this.name = name; this.transform = new Transform(this); this.components = []; } + Object.defineProperty(Entity.prototype, "enabled", { + get: function () { + return this._enabled; + }, + set: function (value) { + this.setEnabled(value); + }, + enumerable: true, + configurable: true + }); + Entity.prototype.setEnabled = function (isEnabled) { + if (this._enabled != isEnabled) { + this._enabled = isEnabled; + } + return this; + }; Object.defineProperty(Entity.prototype, "updateOrder", { get: function () { return this._updateOrder; @@ -287,6 +321,9 @@ var Entity = (function () { component.initialize(); return component; }; + Entity.prototype.getComponent = function () { + return this.components.firstOrDefault(function (component) { return component instanceof Component; }); + }; Entity.prototype.update = function () { this.components.forEach(function (component) { return component.update(); }); this.transform.updateTransform(); @@ -307,8 +344,8 @@ var Scene = (function (_super) { var _this = _super.call(this) || this; _this.entities = []; displayObject.stage.addChild(_this); - _this.camera = _this.createEntity("camera").addComponent(new Camera()); _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); + _this.entityProcessors = []; _this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this); _this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this); _this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this); @@ -324,18 +361,39 @@ var Scene = (function (_super) { entity.scene = this; return entity; }; + Scene.prototype.destoryAllEntities = function () { + this.entities.forEach(function (entity) { return entity.destory(); }); + }; + Scene.prototype.findEntity = function (name) { + return this.entities.firstOrDefault(function (entity) { return entity.name == name; }); + }; + Scene.prototype.addEntityProcessor = function (processor) { + processor.scene = this; + this.entityProcessors.push(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; }); + }; Scene.prototype.setActive = function () { SceneManager.setActiveScene(this); return this; }; Scene.prototype.initialize = function () { + this.camera = this.createEntity("camera").addComponent(new Camera()); + this.entityProcessors.forEach(function (processor) { return processor.initialize(); }); }; Scene.prototype.onActive = function () { }; Scene.prototype.onDeactive = function () { }; Scene.prototype.update = function () { + this.entityProcessors.forEach(function (processor) { return processor.update(); }); this.entities.forEach(function (entity) { return entity.update(); }); + this.entityProcessors.forEach(function (processor) { return processor.lateUpdate(); }); }; Scene.prototype.prepRenderState = function () { this._projectionMatrix.m11 = 2 / this.stage.width; @@ -561,6 +619,74 @@ var Camera = (function (_super) { }; return Camera; }(Component)); +var EntitySystem = (function () { + function EntitySystem(matcher) { + this._entities = []; + this._matcher = matcher ? matcher : Matcher.empty(); + } + Object.defineProperty(EntitySystem.prototype, "matcher", { + get: function () { + return this._matcher; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(EntitySystem.prototype, "scene", { + get: function () { + return this._scene; + }, + set: function (value) { + this._scene = value; + this._entities = []; + }, + enumerable: true, + configurable: true + }); + EntitySystem.prototype.initialize = function () { + }; + EntitySystem.prototype.update = function () { + this.begin(); + this.process(this._entities); + }; + EntitySystem.prototype.lateUpdate = function () { + this.lateProcess(this._entities); + this.end(); + }; + EntitySystem.prototype.begin = function () { + }; + EntitySystem.prototype.process = function (entities) { + }; + EntitySystem.prototype.lateProcess = function (entities) { + }; + EntitySystem.prototype.end = function () { + }; + return EntitySystem; +}()); +var EntityProcessingSystem = (function (_super) { + __extends(EntityProcessingSystem, _super); + function EntityProcessingSystem(matcher) { + return _super.call(this, matcher) || this; + } + EntityProcessingSystem.prototype.lateProcessEntity = function (entity) { + }; + EntityProcessingSystem.prototype.process = function (entities) { + var _this = this; + entities.forEach(function (entity) { return _this.processEntity(entity); }); + }; + EntityProcessingSystem.prototype.lateProcess = function (entities) { + var _this = this; + entities.forEach(function (entity) { return _this.lateProcessEntity(entity); }); + }; + return EntityProcessingSystem; +}(EntitySystem)); +var Matcher = (function () { + function Matcher() { + } + Matcher.empty = function () { + return new Matcher(); + }; + return Matcher; +}()); var MathHelper = (function () { function MathHelper() { } diff --git a/source/bin/framework.min.js b/source/bin/framework.min.js index f0173442..43fd6dcb 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,n){t.__proto__=n}||function(t,n){for(var i in n)n.hasOwnProperty(i)&&(t[i]=n[i])};return function(n,i){function e(){this.constructor=n}t(n,i),n.prototype=null===i?Object.create(i):(e.prototype=i.prototype,new e)}}(),Array.prototype.findIndex=function(t){return function(t,n){for(var i=0,e=t.length;i-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,n){var i=t.findIndex(n);return-1==i?null:t[i]}(this,t)},Array.prototype.find=function(t){return function(t,n){return t.firstOrDefault(n)}(this,t)},Array.prototype.where=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(i,e,r){return n.call(arguments[2],e,r,t)&&i.push(e),i},[]);for(var i=[],e=0,r=t.length;e=0&&t.splice(i,1)}while(i>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,n){var i=t.findIndex(function(t){return t===n});return i>=0&&(t.splice(i,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,n){t.splice(n,1)}(this,t)},Array.prototype.removeRange=function(t,n){return function(t,n,i){t.splice(n,i)}(this,t,n)},Array.prototype.select=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(i,e,r){return i.push(n.call(arguments[2],e,r,t)),i},[]);for(var i=[],e=0,r=t.length;eo?1:-1}),t}(this,t,n)},Array.prototype.orderByDescending=function(t,n){return function(t,n,i){return t.sort(function(t,e){var r=n(t),o=n(e);return i?-i(r,o):r=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function n(n){var i=t.call(this)||this;return i.entities=[],n.stage.addChild(i),i.camera=i.createEntity("camera").addComponent(new Camera),i._projectionMatrix=new Matrix2D(0,0,0,0,0,0),i.addEventListener(egret.Event.ACTIVATE,i.onActive,i),i.addEventListener(egret.Event.DEACTIVATE,i.onDeactive,i),i.addEventListener(egret.Event.ENTER_FRAME,i.update,i),i}return __extends(n,t),n.prototype.createEntity=function(t){var n=new Entity(t);return n.transform.position=new Vector2(0,0),this.addEntity(n)},n.prototype.addEntity=function(t){return this.entities.push(t),t.scene=this,t},n.prototype.setActive=function(){return SceneManager.setActiveScene(this),this},n.prototype.initialize=function(){},n.prototype.onActive=function(){},n.prototype.onDeactive=function(){},n.prototype.update=function(){this.entities.forEach(function(t){return t.update()})},n.prototype.prepRenderState=function(){this._projectionMatrix.m11=2/this.stage.width,this._projectionMatrix.m22=-2/this.stage.height,this._transformMatrix=this.camera.transformMatrix,this._matrixTransformMatrix=Matrix2D.multiply(this._transformMatrix,this._projectionMatrix)},n.prototype.destory=function(){this.removeEventListener(egret.Event.DEACTIVATE,this.onDeactive,this),this.removeEventListener(egret.Event.ACTIVATE,this.onActive,this),this.camera.destory(),this.camera=null,this.entities.forEach(function(t){return t.destory()}),this.entities.length=0},n}(egret.DisplayObjectContainer),SceneManager=function(){function t(){}return t.createScene=function(t,n){return n.name=t,this._loadedScenes.set(t,n),n},t.setActiveScene=function(t){if(this._activeScene){if(this._activeScene==t)return;this._lastScene=this._activeScene,this._activeScene.destory()}return this._activeScene=t,this._activeScene.initialize(),t},t.getActiveScene=function(){return this._activeScene},t._loadedScenes=new Map,t}();!function(t){t[t.clean=0]="clean",t[t.positionDirty=1]="positionDirty",t[t.scaleDirty=2]="scaleDirty",t[t.rotationDirty=3]="rotationDirty"}(DirtyType||(DirtyType={}));var Transform=function(){function t(t){this._localRotation=0,this._worldTransform=Matrix2D.identity,this._worldToLocalTransform=Matrix2D.identity,this._worldInverseTransform=Matrix2D.identity,this._rotation=0,this.entity=t,this._scale=this._localScale=Vector2.One,this._children=[]}return Object.defineProperty(t.prototype,"childCount",{get:function(){return this._children.length},enumerable:!0,configurable:!0}),t.prototype.getChild=function(t){return this._children[t]},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},set:function(t){this.setParent(t)},enumerable:!0,configurable:!0}),t.prototype.setParent=function(t){return this._parent==t?this:(this._parent&&this._parent._children.remove(this),t&&t._children.push(this),this._parent=t,this)},Object.defineProperty(t.prototype,"position",{get:function(){return this.updateTransform(),this.parent?(this.parent.updateTransform(),this._position=Vector2.transform(this._localPosition,this.parent._worldTransform)):this._position=this._localPosition,this._position},set:function(t){this.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.updateTransform(),this._localPosition},set:function(t){this.setLocalPosition(t)},enumerable:!0,configurable:!0}),t.prototype.setLocalPosition=function(t){return t==this._localPosition?this:(this._localPosition=t,this._localDirty=this._positionDirty=this._localPositionDirty=this._localRotationDirty=this._localScaleDirty=!0,this)},t.prototype.setPosition=function(t){if(t==this._position)return this;this._position=t,this.parent?this.localPosition=Vector2.transform(this._position,this._worldToLocalTransform):this.localPosition=t;for(var n=0;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 n.entities=[],e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=[],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){return this.entities.push(t),t.scene=this,t},e.prototype.destoryAllEntities=function(){this.entities.forEach(function(t){return t.destory()})},e.prototype.findEntity=function(t){return this.entities.firstOrDefault(function(e){return e.name==t})},e.prototype.addEntityProcessor=function(t){return t.scene=this,this.entityProcessors.push(t),t},e.prototype.removeEntityProcessor=function(t){this.entityProcessors.remove(t)},e.prototype.getEntityProcessor=function(){return this.entityProcessors.firstOrDefault(function(t){return t instanceof EntitySystem})},e.prototype.setActive=function(){return SceneManager.setActiveScene(this),this},e.prototype.initialize=function(){this.camera=this.createEntity("camera").addComponent(new Camera),this.entityProcessors.forEach(function(t){return t.initialize()})},e.prototype.onActive=function(){},e.prototype.onDeactive=function(){},e.prototype.update=function(){this.entityProcessors.forEach(function(t){return t.update()}),this.entities.forEach(function(t){return t.update()}),this.entityProcessors.forEach(function(t){return t.lateUpdate()})},e.prototype.prepRenderState=function(){this._projectionMatrix.m11=2/this.stage.width,this._projectionMatrix.m22=-2/this.stage.height,this._transformMatrix=this.camera.transformMatrix,this._matrixTransformMatrix=Matrix2D.multiply(this._transformMatrix,this._projectionMatrix)},e.prototype.destory=function(){this.removeEventListener(egret.Event.DEACTIVATE,this.onDeactive,this),this.removeEventListener(egret.Event.ACTIVATE,this.onActive,this),this.camera.destory(),this.camera=null,this.entities.forEach(function(t){return t.destory()}),this.entities.length=0},e}(egret.DisplayObjectContainer),SceneManager=function(){function t(){}return t.createScene=function(t,e){return e.name=t,this._loadedScenes.set(t,e),e},t.setActiveScene=function(t){if(this._activeScene){if(this._activeScene==t)return;this._lastScene=this._activeScene,this._activeScene.destory()}return this._activeScene=t,this._activeScene.initialize(),t},t.getActiveScene=function(){return this._activeScene},t._loadedScenes=new Map,t}();!function(t){t[t.clean=0]="clean",t[t.positionDirty=1]="positionDirty",t[t.scaleDirty=2]="scaleDirty",t[t.rotationDirty=3]="rotationDirty"}(DirtyType||(DirtyType={}));var Transform=function(){function t(t){this._localRotation=0,this._worldTransform=Matrix2D.identity,this._worldToLocalTransform=Matrix2D.identity,this._worldInverseTransform=Matrix2D.identity,this._rotation=0,this.entity=t,this._scale=this._localScale=Vector2.One,this._children=[]}return Object.defineProperty(t.prototype,"childCount",{get:function(){return this._children.length},enumerable:!0,configurable:!0}),t.prototype.getChild=function(t){return this._children[t]},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},set:function(t){this.setParent(t)},enumerable:!0,configurable:!0}),t.prototype.setParent=function(t){return this._parent==t?this:(this._parent&&this._parent._children.remove(this),t&&t._children.push(this),this._parent=t,this)},Object.defineProperty(t.prototype,"position",{get:function(){return this.updateTransform(),this.parent?(this.parent.updateTransform(),this._position=Vector2.transform(this._localPosition,this.parent._worldTransform)):this._position=this._localPosition,this._position},set:function(t){this.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.updateTransform(),this._localPosition},set:function(t){this.setLocalPosition(t)},enumerable:!0,configurable:!0}),t.prototype.setLocalPosition=function(t){return t==this._localPosition?this:(this._localPosition=t,this._localDirty=this._positionDirty=this._localPositionDirty=this._localRotationDirty=this._localScaleDirty=!0,this)},t.prototype.setPosition=function(t){if(t==this._position)return this;this._position=t,this.parent?this.localPosition=Vector2.transform(this._position,this._worldToLocalTransform):this.localPosition=t;for(var e=0;e(): T{ + return this.components.firstOrDefault(component => component instanceof Component) as T; + } + public update(){ this.components.forEach(component => component.update()); this.transform.updateTransform(); diff --git a/source/src/ECS/Scene.ts b/source/src/ECS/Scene.ts index 73522acc..01c668ba 100644 --- a/source/src/ECS/Scene.ts +++ b/source/src/ECS/Scene.ts @@ -7,12 +7,14 @@ class Scene extends egret.DisplayObjectContainer { private _transformMatrix: Matrix2D; private _matrixTransformMatrix: Matrix2D; + public readonly entityProcessors: EntitySystem[]; + constructor(displayObject: egret.DisplayObject){ super(); displayObject.stage.addChild(this); - /** 初始化默认相机 */ - this.camera = this.createEntity("camera").addComponent(new Camera()); this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); + this.entityProcessors = []; + this.addEventListener(egret.Event.ACTIVATE, this.onActive, this); this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this); this.addEventListener(egret.Event.ENTER_FRAME, this.update, this); @@ -31,6 +33,32 @@ class Scene extends egret.DisplayObjectContainer { return entity; } + public destoryAllEntities(){ + this.entities.forEach(entity => entity.destory()); + } + + public findEntity(name: string): Entity{ + return this.entities.firstOrDefault(entity => entity.name == name); + } + + /** + * 在场景中添加一个EntitySystem处理器 + * @param processor 处理器 + */ + public addEntityProcessor(processor: EntitySystem){ + processor.scene = this; + this.entityProcessors.push(processor); + return processor; + } + + public removeEntityProcessor(processor: EntitySystem){ + this.entityProcessors.remove(processor); + } + + public getEntityProcessor(): T { + return this.entityProcessors.firstOrDefault(processor => processor instanceof EntitySystem) as T; + } + public setActive(): Scene{ SceneManager.setActiveScene(this); @@ -39,7 +67,9 @@ class Scene extends egret.DisplayObjectContainer { /** 初始化场景 */ public initialize(){ - + /** 初始化默认相机 */ + this.camera = this.createEntity("camera").addComponent(new Camera()); + this.entityProcessors.forEach(processor => processor.initialize()); } /** 场景激活 */ @@ -53,7 +83,9 @@ class Scene extends egret.DisplayObjectContainer { } public update(){ + this.entityProcessors.forEach(processor => processor.update()); this.entities.forEach(entity => entity.update()); + this.entityProcessors.forEach(processor => processor.lateUpdate()); } public prepRenderState(){ diff --git a/source/src/ECS/Systems/EntityProcessingSystem.ts b/source/src/ECS/Systems/EntityProcessingSystem.ts new file mode 100644 index 00000000..1af60791 --- /dev/null +++ b/source/src/ECS/Systems/EntityProcessingSystem.ts @@ -0,0 +1,23 @@ +/// +/** + * 基本实体处理系统。将其用作处理具有特定组件的许多实体的基础 + */ +abstract class EntityProcessingSystem extends EntitySystem { + constructor(matcher: Matcher) { + super(matcher); + } + + public abstract processEntity(entity: Entity); + + public lateProcessEntity(entity: Entity) { + + } + + protected process(entities: Entity[]) { + entities.forEach(entity => this.processEntity(entity)); + } + + protected lateProcess(entities: Entity[]) { + entities.forEach(entity => this.lateProcessEntity(entity)); + } +} \ No newline at end of file diff --git a/source/src/ECS/Systems/EntitySystem.ts b/source/src/ECS/Systems/EntitySystem.ts new file mode 100644 index 00000000..5d4d5108 --- /dev/null +++ b/source/src/ECS/Systems/EntitySystem.ts @@ -0,0 +1,52 @@ +class EntitySystem { + private _scene: Scene; + private _entities: Entity[] = []; + private _matcher: Matcher; + + public get matcher(){ + return this._matcher; + } + + public get scene(){ + return this._scene; + } + + public set scene(value: Scene){ + this._scene = value; + this._entities = []; + } + + constructor(matcher?: Matcher){ + this._matcher = matcher ? matcher : Matcher.empty(); + } + + public initialize(){ + + } + + public update(){ + this.begin(); + this.process(this._entities); + } + + public lateUpdate(){ + this.lateProcess(this._entities); + this.end(); + } + + protected begin(){ + + } + + protected process(entities: Entity[]){ + + } + + protected lateProcess(entities: Entity[]){ + + } + + protected end(){ + + } +} \ No newline at end of file diff --git a/source/src/ECS/Utils/Matcher.ts b/source/src/ECS/Utils/Matcher.ts new file mode 100644 index 00000000..04c7cc4d --- /dev/null +++ b/source/src/ECS/Utils/Matcher.ts @@ -0,0 +1,5 @@ +class Matcher{ + public static empty(){ + return new Matcher(); + } +} \ No newline at end of file