From 9e0d14da7c7717dd59778317fb3411ba5cde4dfb Mon Sep 17 00:00:00 2001 From: yhh <359807859@qq.com> Date: Tue, 9 Jun 2020 11:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eentity/component=E7=AD=89tran?= =?UTF-8?q?sform=E4=BE=BF=E6=8D=B7=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/libs/framework/framework.d.ts | 49 ++- demo/libs/framework/framework.js | 409 ++++++++++++++++++++++++++ demo/libs/framework/framework.min.js | 2 +- source/bin/framework.d.ts | 49 ++- source/bin/framework.js | 409 ++++++++++++++++++++++++++ source/bin/framework.min.js | 2 +- source/src/ECS/Component.ts | 12 + source/src/ECS/Components/Camera.ts | 93 ++++++ source/src/ECS/Entity.ts | 117 ++++++++ source/src/ECS/Transform.ts | 143 +++++++++ source/src/ECS/Utils/ComponentList.ts | 4 + source/src/Math/MathHelper.ts | 22 ++ source/src/Math/Matrix2D.ts | 2 +- 13 files changed, 1308 insertions(+), 5 deletions(-) diff --git a/demo/libs/framework/framework.d.ts b/demo/libs/framework/framework.d.ts index 53c9717f..5027afdb 100644 --- a/demo/libs/framework/framework.d.ts +++ b/demo/libs/framework/framework.d.ts @@ -21,6 +21,8 @@ declare abstract class Component { entity: Entity; displayRender: egret.DisplayObject; private _enabled; + updateInterval: number; + readonly transform: Transform; enabled: boolean; setEnabled(isEnabled: boolean): this; abstract initialize(): any; @@ -43,6 +45,18 @@ declare class Entity { private _enabled; private _isDestoryed; componentBits: BitSet; + parent: Transform; + position: Vector2; + localPosition: Vector2; + rotation: number; + rotationDegrees: number; + localRotation: number; + localRotationDegrees: number; + scale: Vector2; + localScale: Vector2; + readonly worldInverseTransform: Matrix2D; + readonly localToWorldTransform: Matrix2D; + readonly worldToLocalTransform: Matrix2D; readonly isDestoryed: boolean; enabled: boolean; setEnabled(isEnabled: boolean): this; @@ -52,7 +66,12 @@ declare class Entity { attachToScene(newScene: Scene): void; detachFromScene(): void; addComponent(component: T): T; + hasComponent(type: any): boolean; + getOrCreateComponent(type: T): T; getComponent(type: any): T; + removeComponentForType(type: any): boolean; + removeComponent(component: Component): void; + removeAllComponents(): void; update(): void; onAddedToScene(): void; onRemovedFromScene(): void; @@ -129,10 +148,25 @@ declare class Transform { readonly childCount: number; constructor(entity: Entity); getChild(index: number): Transform; + readonly worldInverseTransform: Matrix2D; + readonly localToWorldTransform: Matrix2D; + readonly worldToLocalTransform: Matrix2D; parent: Transform; setParent(parent: Transform): this; + rotation: number; + localRotation: number; position: Vector2; localPosition: Vector2; + scale: Vector2; + localScale: Vector2; + rotationDegrees: number; + localRotationDegrees: number; + setLocalScale(scale: Vector2): this; + setScale(scale: Vector2): this; + setLocalRotationDegrees(degrees: number): this; + setLocalRotation(radians: number): this; + setRotation(radians: number): this; + setRotationDegrees(degrees: number): this; setLocalPosition(localPosition: Vector2): this; setPosition(position: Vector2): this; setDirty(dirtyFlagType: DirtyType): void; @@ -143,8 +177,18 @@ declare class Camera extends Component { private _origin; private _transformMatrix; private _inverseTransformMatrix; + private _minimumZoom; + private _maximumZoom; + private _areMatrixesDirty; + zoom: number; + minimumZoom: number; + maximumZoom: number; + origin: Vector2; readonly transformMatrix: Matrix2D; constructor(); + setMinimumZoom(minZoom: number): Camera; + setMaximumZoom(maxZoom: number): Camera; + setZoom(zoom: number): this; initialize(): void; update(): void; setPosition(position: Vector2): this; @@ -200,6 +244,7 @@ declare class ComponentList { private _componentsToRemove; private _tempBufferList; constructor(entity: Entity); + readonly count: number; readonly buffer: Component[]; add(component: Component): void; remove(component: Component): void; @@ -266,6 +311,8 @@ declare class Time { declare class MathHelper { static toDegrees(radians: number): number; static toRadians(degrees: number): number; + static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number; + static clamp(value: number, min: number, max: number): number; } declare class Matrix2D { m11: number; @@ -286,7 +333,7 @@ declare class Matrix2D { static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; static multiplyTranslation(matrix: Matrix2D, x: number, y: number): Matrix2D; determinant(): number; - static invert(matrix: Matrix2D, result: Matrix2D): Matrix2D; + static invert(matrix: Matrix2D, result?: Matrix2D): Matrix2D; static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D; static createRotation(radians: number, result?: Matrix2D): Matrix2D; static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D; diff --git a/demo/libs/framework/framework.js b/demo/libs/framework/framework.js index 217ab113..7164625a 100644 --- a/demo/libs/framework/framework.js +++ b/demo/libs/framework/framework.js @@ -241,7 +241,15 @@ Array.prototype.sum = function (selector) { var Component = (function () { function Component() { this._enabled = true; + this.updateInterval = 1; } + Object.defineProperty(Component.prototype, "transform", { + get: function () { + return this.entity.transform; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Component.prototype, "enabled", { get: function () { return this.entity ? this.entity.enabled && this._enabled : this._enabled; @@ -255,6 +263,12 @@ var Component = (function () { Component.prototype.setEnabled = function (isEnabled) { if (this._enabled != isEnabled) { this._enabled = isEnabled; + if (this._enabled) { + this.onEnabled(); + } + else { + this.onDisabled(); + } } return this; }; @@ -293,6 +307,117 @@ var Entity = (function () { this.components = new ComponentList(this); this.componentBits = new BitSet(); } + Object.defineProperty(Entity.prototype, "parent", { + get: function () { + return this.transform.parent; + }, + set: function (value) { + this.transform.setParent(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "position", { + get: function () { + return this.transform.position; + }, + set: function (value) { + this.transform.setPosition(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localPosition", { + get: function () { + return this.transform.localPosition; + }, + set: function (value) { + this.transform.setLocalPosition(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "rotation", { + get: function () { + return this.transform.rotation; + }, + set: function (value) { + this.transform.setRotation(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "rotationDegrees", { + get: function () { + return this.transform.rotationDegrees; + }, + set: function (value) { + this.transform.setRotationDegrees(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localRotation", { + get: function () { + return this.transform.localRotation; + }, + set: function (value) { + this.transform.setLocalRotation(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localRotationDegrees", { + get: function () { + return this.transform.localRotationDegrees; + }, + set: function (value) { + this.transform.setLocalRotationDegrees(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "scale", { + get: function () { + return this.transform.scale; + }, + set: function (value) { + this.transform.setScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localScale", { + get: function () { + return this.transform.scale; + }, + set: function (value) { + this.transform.setScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "worldInverseTransform", { + get: function () { + return this.transform.worldInverseTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localToWorldTransform", { + get: function () { + return this.transform.localToWorldTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "worldToLocalTransform", { + get: function () { + return this.transform.worldToLocalTransform; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Entity.prototype, "isDestoryed", { get: function () { return this._isDestoryed; @@ -354,9 +479,35 @@ var Entity = (function () { component.initialize(); return component; }; + Entity.prototype.hasComponent = function (type) { + return this.components.getComponent(type, false) != null; + }; + Entity.prototype.getOrCreateComponent = function (type) { + var comp = this.components.getComponent(type, true); + if (!comp) { + comp = this.addComponent(type); + } + return comp; + }; Entity.prototype.getComponent = function (type) { return this.components.getComponent(type, false); }; + Entity.prototype.removeComponentForType = function (type) { + var comp = this.getComponent(type); + if (comp) { + this.removeComponent(comp); + return true; + } + return false; + }; + Entity.prototype.removeComponent = function (component) { + this.components.remove(component); + }; + Entity.prototype.removeAllComponents = function () { + for (var i = 0; i < this.components.count; i++) { + this.removeComponent(this.components.buffer[i]); + } + }; Entity.prototype.update = function () { this.components.update(); this.transform.updateTransform(); @@ -521,6 +672,43 @@ var Transform = (function () { Transform.prototype.getChild = function (index) { return this._children[index]; }; + Object.defineProperty(Transform.prototype, "worldInverseTransform", { + get: function () { + this.updateTransform(); + if (this._worldInverseDirty) { + this._worldInverseTransform = Matrix2D.invert(this._worldTransform, this._worldInverseTransform); + this._worldInverseDirty = false; + } + return this._worldInverseTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localToWorldTransform", { + get: function () { + this.updateTransform(); + return this._worldTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "worldToLocalTransform", { + get: function () { + if (this._worldToLocalDirty) { + if (!this.parent) { + this._worldInverseTransform = Matrix2D.identity; + } + else { + this.parent.updateTransform(); + this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform); + } + this._worldToLocalDirty = false; + } + return this._worldToLocalTransform; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Transform.prototype, "parent", { get: function () { return this._parent; @@ -541,6 +729,28 @@ var Transform = (function () { this._parent = parent; return this; }; + Object.defineProperty(Transform.prototype, "rotation", { + get: function () { + this.updateTransform(); + return this._rotation; + }, + set: function (value) { + this.setRotation(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localRotation", { + get: function () { + this.updateTransform(); + return this._localRotation; + }, + set: function (value) { + this.setLocalRotation(value); + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Transform.prototype, "position", { get: function () { this.updateTransform(); @@ -570,6 +780,99 @@ var Transform = (function () { enumerable: true, configurable: true }); + Object.defineProperty(Transform.prototype, "scale", { + get: function () { + this.updateTransform(); + return this._scale; + }, + set: function (value) { + this.setScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localScale", { + get: function () { + this.updateTransform(); + return this._localScale; + }, + set: function (value) { + this.setLocalScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "rotationDegrees", { + get: function () { + return MathHelper.toDegrees(this._rotation); + }, + set: function (value) { + this.setRotation(MathHelper.toRadians(value)); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localRotationDegrees", { + get: function () { + return MathHelper.toDegrees(this._localRotation); + }, + set: function (value) { + this.localRotation = MathHelper.toRadians(value); + }, + enumerable: true, + configurable: true + }); + Transform.prototype.setLocalScale = function (scale) { + this._localScale = scale; + this._localDirty = this._positionDirty = this._localScaleDirty = true; + this.setDirty(DirtyType.scaleDirty); + return this; + }; + Transform.prototype.setScale = function (scale) { + this._scale = scale; + if (this.parent) { + this.localScale = Vector2.divide(scale, this.parent._scale); + } + else { + this.localScale = scale; + } + for (var i = 0; i < this.entity.components.buffer.length; i++) { + var component = this.entity.components.buffer[i]; + if (component.displayRender) { + component.displayRender.scaleX = this.scale.x; + component.displayRender.scaleY = this.scale.y; + } + } + return this; + }; + Transform.prototype.setLocalRotationDegrees = function (degrees) { + return this.setLocalRotation(MathHelper.toRadians(degrees)); + }; + Transform.prototype.setLocalRotation = function (radians) { + this._localRotation = radians; + this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; + this.setDirty(DirtyType.rotationDirty); + return this; + }; + Transform.prototype.setRotation = function (radians) { + this._rotation = radians; + if (this.parent) { + this.localRotation = this.parent.rotation + radians; + } + else { + this.localRotation = radians; + } + for (var i = 0; i < this.entity.components.buffer.length; i++) { + var component = this.entity.components.buffer[i]; + if (component.displayRender) { + component.displayRender.rotation = this.rotation; + } + } + return this; + }; + Transform.prototype.setRotationDegrees = function (degrees) { + return this.setRotation(MathHelper.toRadians(degrees)); + }; Transform.prototype.setLocalPosition = function (localPosition) { if (localPosition == this._localPosition) return this; @@ -664,8 +967,59 @@ var Camera = (function (_super) { var _this = _super.call(this) || this; _this._transformMatrix = Matrix2D.identity; _this._inverseTransformMatrix = Matrix2D.identity; + _this._minimumZoom = 0.3; + _this._maximumZoom = 3; + _this._areMatrixesDirty = true; + _this.setZoom(0); return _this; } + Object.defineProperty(Camera.prototype, "zoom", { + get: function () { + if (this._zoom == 0) + return 1; + if (this._zoom < 1) + return MathHelper.map(this._zoom, this._minimumZoom, 1, -1, 0); + return MathHelper.map(this._zoom, 1, this._maximumZoom, 0, 1); + }, + set: function (value) { + this.setZoom(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Camera.prototype, "minimumZoom", { + get: function () { + return this._minimumZoom; + }, + set: function (value) { + this.setMinimumZoom(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Camera.prototype, "maximumZoom", { + get: function () { + return this._maximumZoom; + }, + set: function (value) { + this.setMaximumZoom(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Camera.prototype, "origin", { + get: function () { + return this._origin; + }, + set: function (value) { + if (this._origin != value) { + this._origin = value; + this._areMatrixesDirty = true; + } + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Camera.prototype, "transformMatrix", { get: function () { this.updateMatrixes(); @@ -674,6 +1028,32 @@ var Camera = (function (_super) { enumerable: true, configurable: true }); + Camera.prototype.setMinimumZoom = function (minZoom) { + if (this._zoom < minZoom) + this._zoom = this.minimumZoom; + this._minimumZoom = minZoom; + return this; + }; + Camera.prototype.setMaximumZoom = function (maxZoom) { + if (this._zoom > maxZoom) + this._zoom = maxZoom; + this._maximumZoom = maxZoom; + return this; + }; + Camera.prototype.setZoom = function (zoom) { + var newZoom = MathHelper.clamp(zoom, -1, 1); + if (newZoom == 0) { + this._zoom = 1; + } + else if (newZoom < 0) { + this._zoom = MathHelper.map(newZoom, -1, 0, this._minimumZoom, 1); + } + else { + this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom); + } + this._areMatrixesDirty = true; + return this; + }; Camera.prototype.initialize = function () { }; Camera.prototype.update = function () { @@ -692,7 +1072,18 @@ var Camera = (function (_super) { return this; }; Camera.prototype.updateMatrixes = function () { + if (!this._areMatrixesDirty) + return; + var tempMat; this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); + if (this._zoom != 1) { + tempMat = Matrix2D.createScale(this._zoom, this._zoom); + this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); + } + tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat); + this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); + this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix); + this._areMatrixesDirty = false; }; Camera.prototype.destory = function () { }; @@ -895,6 +1286,13 @@ var ComponentList = (function () { this._tempBufferList = []; this._entity = entity; } + Object.defineProperty(ComponentList.prototype, "count", { + get: function () { + return this._components.length; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(ComponentList.prototype, "buffer", { get: function () { return this._components; @@ -1200,6 +1598,16 @@ var MathHelper = (function () { MathHelper.toRadians = function (degrees) { return degrees * 0.017453292519943295769236907684886; }; + MathHelper.map = function (value, leftMin, leftMax, rightMin, rightMax) { + return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin); + }; + MathHelper.clamp = function (value, min, max) { + if (value < min) + return min; + if (value > max) + return max; + return value; + }; return MathHelper; }()); var Matrix2D = (function () { @@ -1312,6 +1720,7 @@ var Matrix2D = (function () { return this.m11 * this.m22 - this.m12 * this.m21; }; Matrix2D.invert = function (matrix, result) { + if (result === void 0) { result = Matrix2D.identity; } var det = 1 / matrix.determinant(); result.m11 = matrix.m22 * det; result.m12 = -matrix.m12 * det; diff --git a/demo/libs/framework/framework.min.js b/demo/libs/framework/framework.min.js index 08e86c3f..8f9e6e47 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,o){return e.call(arguments[2],i,o,t)&&n.push(i),n},[]);for(var n=[],i=0,o=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,o){return n.push(e.call(arguments[2],i,o,t)),n},[]);for(var n=[],i=0,o=t.length;ir?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var o=e(t),r=e(i);return n?-n(o,r):o=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){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){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.forEach(function(t){return t.onAddedToScene()}),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,o,r){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=o,this.m32=r}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,o=t.m21*e.m11+t.m22*e.m21,r=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=o,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var o=t.createTranslation(n,i);return t.multiply(e,o)},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),o=Math.sin(e);return n.m11=i,n.m12=o,n.m21=-o,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 o(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(o.prototype=n.prototype,new o)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,o=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,o,i){return e.call(arguments[2],o,i,t)&&n.push(o),n},[]);for(var n=[],o=0,i=t.length;o=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,o,i){return n.push(e.call(arguments[2],o,i,t)),n},[]);for(var n=[],o=0,i=t.length;or?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,o){var i=e(t),r=e(o);return n?-n(i,r):i=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;et&&(this._zoom=t),this._maximumZoom=t,this},e.prototype.setZoom=function(t){var e=MathHelper.clamp(t,-1,1);return this._zoom=0==e?1:e<0?MathHelper.map(e,-1,0,this._minimumZoom,1):MathHelper.map(e,0,1,1,this._maximumZoom),this._areMatrixesDirty=!0,this},e.prototype.initialize=function(){},e.prototype.update=function(){var t=this;SceneManager.getActiveScene().entities.buffer.forEach(function(e){return e.components.buffer.forEach(function(e){e.displayRender&&(-1==t.entity.scene.$children.indexOf(e.displayRender)&&t.entity.scene.stage.addChild(e.displayRender))})})},e.prototype.setPosition=function(t){return this.entity.transform.setPosition(t),this},e.prototype.updateMatrixes=function(){var t;this._areMatrixesDirty&&(this._transformMatrix=Matrix2D.createTranslation(-this.entity.transform.position.x,-this.entity.transform.position.y),1!=this._zoom&&(t=Matrix2D.createScale(this._zoom,this._zoom),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t)),t=Matrix2D.createTranslation(this._origin.x,this._origin.y,t),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t),this._inverseTransformMatrix=Matrix2D.invert(this._transformMatrix),this._areMatrixesDirty=!1)},e.prototype.destory=function(){},e}(Component),EntitySystem=function(){function t(t){this._entities=[],this._matcher=t||Matcher.empty()}return Object.defineProperty(t.prototype,"matcher",{get:function(){return this._matcher},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scene",{get:function(){return this._scene},set:function(t){this._scene=t,this._entities=[]},enumerable:!0,configurable:!0}),t.prototype.initialize=function(){},t.prototype.onChanged=function(t){var e=this._entities.contains(t),n=this._matcher.IsIntersted(t);n&&!e?this.add(t):!n&&e&&this.remove(t)},t.prototype.add=function(t){this._entities.push(t),this.onAdded(t)},t.prototype.onAdded=function(t){},t.prototype.remove=function(t){this._entities.remove(t),this.onRemoved(t)},t.prototype.onRemoved=function(t){},t.prototype.update=function(){this.begin(),this.process(this._entities)},t.prototype.lateUpdate=function(){this.lateProcess(this._entities),this.end()},t.prototype.begin=function(){},t.prototype.process=function(t){},t.prototype.lateProcess=function(t){},t.prototype.end=function(){},t}(),EntityProcessingSystem=function(t){function e(e){return t.call(this,e)||this}return __extends(e,t),e.prototype.lateProcessEntity=function(t){},e.prototype.process=function(t){var e=this;t.forEach(function(t){return e.processEntity(t)})},e.prototype.lateProcess=function(t){var e=this;t.forEach(function(t){return e.lateProcessEntity(t)})},e}(EntitySystem),BitSet=function(){function t(e){void 0===e&&(e=64);var n=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),o=0;o=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 o=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((o=((o=(o>>4&252645135)+(252645135&o))>>8&16711935)+(16711935&o))>>16&65535)+(65535&o)}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){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){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.forEach(function(t){return t.onAddedToScene()}),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.map=function(t,e,n,o,i){return o+(t-e)*(i-o)/(n-e)},t.clamp=function(t,e,n){return tn?n:t},t}(),Matrix2D=function(){function t(t,e,n,o,i,r){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=o,this.m31=i,this.m32=r}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,o=t.m11*e.m12+t.m12*e.m22,i=t.m21*e.m11+t.m22*e.m21,r=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=o,t.m21=i,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,o){var i=t.createTranslation(n,o);return t.multiply(e,i)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(e,n){void 0===n&&(n=t.identity);var o=1/e.determinant();return n.m11=e.m22*o,n.m12=-e.m12*o,n.m21=-e.m21*o,n.m22=e.m11*o,n.m31=(e.m32*e.m21-e.m31*e.m22)*o,n.m32=-(e.m32*e.m11-e.m31*e.m12)*o,n},t.createTranslation=function(e,n,o){return void 0===o&&(o=t.identity),o.m11=1,o.m12=0,o.m21=0,o.m22=1,o.m31=e,o.m32=n,o},t.createRotation=function(e,n){n=t.identity;var o=Math.cos(e),i=Math.sin(e);return n.m11=o,n.m12=i,n.m21=-i,n.m22=o,n},t.createScale=function(e,n,o){return void 0===o&&(o=t.identity),o.m11=e,o.m12=0,o.m21=0,o.m22=n,o.m31=0,o.m32=0,o},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/bin/framework.d.ts b/source/bin/framework.d.ts index 53c9717f..5027afdb 100644 --- a/source/bin/framework.d.ts +++ b/source/bin/framework.d.ts @@ -21,6 +21,8 @@ declare abstract class Component { entity: Entity; displayRender: egret.DisplayObject; private _enabled; + updateInterval: number; + readonly transform: Transform; enabled: boolean; setEnabled(isEnabled: boolean): this; abstract initialize(): any; @@ -43,6 +45,18 @@ declare class Entity { private _enabled; private _isDestoryed; componentBits: BitSet; + parent: Transform; + position: Vector2; + localPosition: Vector2; + rotation: number; + rotationDegrees: number; + localRotation: number; + localRotationDegrees: number; + scale: Vector2; + localScale: Vector2; + readonly worldInverseTransform: Matrix2D; + readonly localToWorldTransform: Matrix2D; + readonly worldToLocalTransform: Matrix2D; readonly isDestoryed: boolean; enabled: boolean; setEnabled(isEnabled: boolean): this; @@ -52,7 +66,12 @@ declare class Entity { attachToScene(newScene: Scene): void; detachFromScene(): void; addComponent(component: T): T; + hasComponent(type: any): boolean; + getOrCreateComponent(type: T): T; getComponent(type: any): T; + removeComponentForType(type: any): boolean; + removeComponent(component: Component): void; + removeAllComponents(): void; update(): void; onAddedToScene(): void; onRemovedFromScene(): void; @@ -129,10 +148,25 @@ declare class Transform { readonly childCount: number; constructor(entity: Entity); getChild(index: number): Transform; + readonly worldInverseTransform: Matrix2D; + readonly localToWorldTransform: Matrix2D; + readonly worldToLocalTransform: Matrix2D; parent: Transform; setParent(parent: Transform): this; + rotation: number; + localRotation: number; position: Vector2; localPosition: Vector2; + scale: Vector2; + localScale: Vector2; + rotationDegrees: number; + localRotationDegrees: number; + setLocalScale(scale: Vector2): this; + setScale(scale: Vector2): this; + setLocalRotationDegrees(degrees: number): this; + setLocalRotation(radians: number): this; + setRotation(radians: number): this; + setRotationDegrees(degrees: number): this; setLocalPosition(localPosition: Vector2): this; setPosition(position: Vector2): this; setDirty(dirtyFlagType: DirtyType): void; @@ -143,8 +177,18 @@ declare class Camera extends Component { private _origin; private _transformMatrix; private _inverseTransformMatrix; + private _minimumZoom; + private _maximumZoom; + private _areMatrixesDirty; + zoom: number; + minimumZoom: number; + maximumZoom: number; + origin: Vector2; readonly transformMatrix: Matrix2D; constructor(); + setMinimumZoom(minZoom: number): Camera; + setMaximumZoom(maxZoom: number): Camera; + setZoom(zoom: number): this; initialize(): void; update(): void; setPosition(position: Vector2): this; @@ -200,6 +244,7 @@ declare class ComponentList { private _componentsToRemove; private _tempBufferList; constructor(entity: Entity); + readonly count: number; readonly buffer: Component[]; add(component: Component): void; remove(component: Component): void; @@ -266,6 +311,8 @@ declare class Time { declare class MathHelper { static toDegrees(radians: number): number; static toRadians(degrees: number): number; + static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number; + static clamp(value: number, min: number, max: number): number; } declare class Matrix2D { m11: number; @@ -286,7 +333,7 @@ declare class Matrix2D { static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; static multiplyTranslation(matrix: Matrix2D, x: number, y: number): Matrix2D; determinant(): number; - static invert(matrix: Matrix2D, result: Matrix2D): Matrix2D; + static invert(matrix: Matrix2D, result?: Matrix2D): Matrix2D; static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D; static createRotation(radians: number, result?: Matrix2D): Matrix2D; static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D; diff --git a/source/bin/framework.js b/source/bin/framework.js index 217ab113..7164625a 100644 --- a/source/bin/framework.js +++ b/source/bin/framework.js @@ -241,7 +241,15 @@ Array.prototype.sum = function (selector) { var Component = (function () { function Component() { this._enabled = true; + this.updateInterval = 1; } + Object.defineProperty(Component.prototype, "transform", { + get: function () { + return this.entity.transform; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Component.prototype, "enabled", { get: function () { return this.entity ? this.entity.enabled && this._enabled : this._enabled; @@ -255,6 +263,12 @@ var Component = (function () { Component.prototype.setEnabled = function (isEnabled) { if (this._enabled != isEnabled) { this._enabled = isEnabled; + if (this._enabled) { + this.onEnabled(); + } + else { + this.onDisabled(); + } } return this; }; @@ -293,6 +307,117 @@ var Entity = (function () { this.components = new ComponentList(this); this.componentBits = new BitSet(); } + Object.defineProperty(Entity.prototype, "parent", { + get: function () { + return this.transform.parent; + }, + set: function (value) { + this.transform.setParent(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "position", { + get: function () { + return this.transform.position; + }, + set: function (value) { + this.transform.setPosition(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localPosition", { + get: function () { + return this.transform.localPosition; + }, + set: function (value) { + this.transform.setLocalPosition(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "rotation", { + get: function () { + return this.transform.rotation; + }, + set: function (value) { + this.transform.setRotation(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "rotationDegrees", { + get: function () { + return this.transform.rotationDegrees; + }, + set: function (value) { + this.transform.setRotationDegrees(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localRotation", { + get: function () { + return this.transform.localRotation; + }, + set: function (value) { + this.transform.setLocalRotation(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localRotationDegrees", { + get: function () { + return this.transform.localRotationDegrees; + }, + set: function (value) { + this.transform.setLocalRotationDegrees(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "scale", { + get: function () { + return this.transform.scale; + }, + set: function (value) { + this.transform.setScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localScale", { + get: function () { + return this.transform.scale; + }, + set: function (value) { + this.transform.setScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "worldInverseTransform", { + get: function () { + return this.transform.worldInverseTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "localToWorldTransform", { + get: function () { + return this.transform.localToWorldTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Entity.prototype, "worldToLocalTransform", { + get: function () { + return this.transform.worldToLocalTransform; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Entity.prototype, "isDestoryed", { get: function () { return this._isDestoryed; @@ -354,9 +479,35 @@ var Entity = (function () { component.initialize(); return component; }; + Entity.prototype.hasComponent = function (type) { + return this.components.getComponent(type, false) != null; + }; + Entity.prototype.getOrCreateComponent = function (type) { + var comp = this.components.getComponent(type, true); + if (!comp) { + comp = this.addComponent(type); + } + return comp; + }; Entity.prototype.getComponent = function (type) { return this.components.getComponent(type, false); }; + Entity.prototype.removeComponentForType = function (type) { + var comp = this.getComponent(type); + if (comp) { + this.removeComponent(comp); + return true; + } + return false; + }; + Entity.prototype.removeComponent = function (component) { + this.components.remove(component); + }; + Entity.prototype.removeAllComponents = function () { + for (var i = 0; i < this.components.count; i++) { + this.removeComponent(this.components.buffer[i]); + } + }; Entity.prototype.update = function () { this.components.update(); this.transform.updateTransform(); @@ -521,6 +672,43 @@ var Transform = (function () { Transform.prototype.getChild = function (index) { return this._children[index]; }; + Object.defineProperty(Transform.prototype, "worldInverseTransform", { + get: function () { + this.updateTransform(); + if (this._worldInverseDirty) { + this._worldInverseTransform = Matrix2D.invert(this._worldTransform, this._worldInverseTransform); + this._worldInverseDirty = false; + } + return this._worldInverseTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localToWorldTransform", { + get: function () { + this.updateTransform(); + return this._worldTransform; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "worldToLocalTransform", { + get: function () { + if (this._worldToLocalDirty) { + if (!this.parent) { + this._worldInverseTransform = Matrix2D.identity; + } + else { + this.parent.updateTransform(); + this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform); + } + this._worldToLocalDirty = false; + } + return this._worldToLocalTransform; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Transform.prototype, "parent", { get: function () { return this._parent; @@ -541,6 +729,28 @@ var Transform = (function () { this._parent = parent; return this; }; + Object.defineProperty(Transform.prototype, "rotation", { + get: function () { + this.updateTransform(); + return this._rotation; + }, + set: function (value) { + this.setRotation(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localRotation", { + get: function () { + this.updateTransform(); + return this._localRotation; + }, + set: function (value) { + this.setLocalRotation(value); + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Transform.prototype, "position", { get: function () { this.updateTransform(); @@ -570,6 +780,99 @@ var Transform = (function () { enumerable: true, configurable: true }); + Object.defineProperty(Transform.prototype, "scale", { + get: function () { + this.updateTransform(); + return this._scale; + }, + set: function (value) { + this.setScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localScale", { + get: function () { + this.updateTransform(); + return this._localScale; + }, + set: function (value) { + this.setLocalScale(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "rotationDegrees", { + get: function () { + return MathHelper.toDegrees(this._rotation); + }, + set: function (value) { + this.setRotation(MathHelper.toRadians(value)); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localRotationDegrees", { + get: function () { + return MathHelper.toDegrees(this._localRotation); + }, + set: function (value) { + this.localRotation = MathHelper.toRadians(value); + }, + enumerable: true, + configurable: true + }); + Transform.prototype.setLocalScale = function (scale) { + this._localScale = scale; + this._localDirty = this._positionDirty = this._localScaleDirty = true; + this.setDirty(DirtyType.scaleDirty); + return this; + }; + Transform.prototype.setScale = function (scale) { + this._scale = scale; + if (this.parent) { + this.localScale = Vector2.divide(scale, this.parent._scale); + } + else { + this.localScale = scale; + } + for (var i = 0; i < this.entity.components.buffer.length; i++) { + var component = this.entity.components.buffer[i]; + if (component.displayRender) { + component.displayRender.scaleX = this.scale.x; + component.displayRender.scaleY = this.scale.y; + } + } + return this; + }; + Transform.prototype.setLocalRotationDegrees = function (degrees) { + return this.setLocalRotation(MathHelper.toRadians(degrees)); + }; + Transform.prototype.setLocalRotation = function (radians) { + this._localRotation = radians; + this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; + this.setDirty(DirtyType.rotationDirty); + return this; + }; + Transform.prototype.setRotation = function (radians) { + this._rotation = radians; + if (this.parent) { + this.localRotation = this.parent.rotation + radians; + } + else { + this.localRotation = radians; + } + for (var i = 0; i < this.entity.components.buffer.length; i++) { + var component = this.entity.components.buffer[i]; + if (component.displayRender) { + component.displayRender.rotation = this.rotation; + } + } + return this; + }; + Transform.prototype.setRotationDegrees = function (degrees) { + return this.setRotation(MathHelper.toRadians(degrees)); + }; Transform.prototype.setLocalPosition = function (localPosition) { if (localPosition == this._localPosition) return this; @@ -664,8 +967,59 @@ var Camera = (function (_super) { var _this = _super.call(this) || this; _this._transformMatrix = Matrix2D.identity; _this._inverseTransformMatrix = Matrix2D.identity; + _this._minimumZoom = 0.3; + _this._maximumZoom = 3; + _this._areMatrixesDirty = true; + _this.setZoom(0); return _this; } + Object.defineProperty(Camera.prototype, "zoom", { + get: function () { + if (this._zoom == 0) + return 1; + if (this._zoom < 1) + return MathHelper.map(this._zoom, this._minimumZoom, 1, -1, 0); + return MathHelper.map(this._zoom, 1, this._maximumZoom, 0, 1); + }, + set: function (value) { + this.setZoom(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Camera.prototype, "minimumZoom", { + get: function () { + return this._minimumZoom; + }, + set: function (value) { + this.setMinimumZoom(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Camera.prototype, "maximumZoom", { + get: function () { + return this._maximumZoom; + }, + set: function (value) { + this.setMaximumZoom(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Camera.prototype, "origin", { + get: function () { + return this._origin; + }, + set: function (value) { + if (this._origin != value) { + this._origin = value; + this._areMatrixesDirty = true; + } + }, + enumerable: true, + configurable: true + }); Object.defineProperty(Camera.prototype, "transformMatrix", { get: function () { this.updateMatrixes(); @@ -674,6 +1028,32 @@ var Camera = (function (_super) { enumerable: true, configurable: true }); + Camera.prototype.setMinimumZoom = function (minZoom) { + if (this._zoom < minZoom) + this._zoom = this.minimumZoom; + this._minimumZoom = minZoom; + return this; + }; + Camera.prototype.setMaximumZoom = function (maxZoom) { + if (this._zoom > maxZoom) + this._zoom = maxZoom; + this._maximumZoom = maxZoom; + return this; + }; + Camera.prototype.setZoom = function (zoom) { + var newZoom = MathHelper.clamp(zoom, -1, 1); + if (newZoom == 0) { + this._zoom = 1; + } + else if (newZoom < 0) { + this._zoom = MathHelper.map(newZoom, -1, 0, this._minimumZoom, 1); + } + else { + this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom); + } + this._areMatrixesDirty = true; + return this; + }; Camera.prototype.initialize = function () { }; Camera.prototype.update = function () { @@ -692,7 +1072,18 @@ var Camera = (function (_super) { return this; }; Camera.prototype.updateMatrixes = function () { + if (!this._areMatrixesDirty) + return; + var tempMat; this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); + if (this._zoom != 1) { + tempMat = Matrix2D.createScale(this._zoom, this._zoom); + this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); + } + tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat); + this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); + this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix); + this._areMatrixesDirty = false; }; Camera.prototype.destory = function () { }; @@ -895,6 +1286,13 @@ var ComponentList = (function () { this._tempBufferList = []; this._entity = entity; } + Object.defineProperty(ComponentList.prototype, "count", { + get: function () { + return this._components.length; + }, + enumerable: true, + configurable: true + }); Object.defineProperty(ComponentList.prototype, "buffer", { get: function () { return this._components; @@ -1200,6 +1598,16 @@ var MathHelper = (function () { MathHelper.toRadians = function (degrees) { return degrees * 0.017453292519943295769236907684886; }; + MathHelper.map = function (value, leftMin, leftMax, rightMin, rightMax) { + return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin); + }; + MathHelper.clamp = function (value, min, max) { + if (value < min) + return min; + if (value > max) + return max; + return value; + }; return MathHelper; }()); var Matrix2D = (function () { @@ -1312,6 +1720,7 @@ var Matrix2D = (function () { return this.m11 * this.m22 - this.m12 * this.m21; }; Matrix2D.invert = function (matrix, result) { + if (result === void 0) { result = Matrix2D.identity; } var det = 1 / matrix.determinant(); result.m11 = matrix.m22 * det; result.m12 = -matrix.m12 * det; diff --git a/source/bin/framework.min.js b/source/bin/framework.min.js index 08e86c3f..8f9e6e47 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,o){return e.call(arguments[2],i,o,t)&&n.push(i),n},[]);for(var n=[],i=0,o=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,o){return n.push(e.call(arguments[2],i,o,t)),n},[]);for(var n=[],i=0,o=t.length;ir?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var o=e(t),r=e(i);return n?-n(o,r):o=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){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){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.forEach(function(t){return t.onAddedToScene()}),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,o,r){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=o,this.m32=r}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,o=t.m21*e.m11+t.m22*e.m21,r=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=o,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var o=t.createTranslation(n,i);return t.multiply(e,o)},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),o=Math.sin(e);return n.m11=i,n.m12=o,n.m21=-o,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 o(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(o.prototype=n.prototype,new o)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,o=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,o,i){return e.call(arguments[2],o,i,t)&&n.push(o),n},[]);for(var n=[],o=0,i=t.length;o=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,o,i){return n.push(e.call(arguments[2],o,i,t)),n},[]);for(var n=[],o=0,i=t.length;or?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,o){var i=e(t),r=e(o);return n?-n(i,r):i=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;et&&(this._zoom=t),this._maximumZoom=t,this},e.prototype.setZoom=function(t){var e=MathHelper.clamp(t,-1,1);return this._zoom=0==e?1:e<0?MathHelper.map(e,-1,0,this._minimumZoom,1):MathHelper.map(e,0,1,1,this._maximumZoom),this._areMatrixesDirty=!0,this},e.prototype.initialize=function(){},e.prototype.update=function(){var t=this;SceneManager.getActiveScene().entities.buffer.forEach(function(e){return e.components.buffer.forEach(function(e){e.displayRender&&(-1==t.entity.scene.$children.indexOf(e.displayRender)&&t.entity.scene.stage.addChild(e.displayRender))})})},e.prototype.setPosition=function(t){return this.entity.transform.setPosition(t),this},e.prototype.updateMatrixes=function(){var t;this._areMatrixesDirty&&(this._transformMatrix=Matrix2D.createTranslation(-this.entity.transform.position.x,-this.entity.transform.position.y),1!=this._zoom&&(t=Matrix2D.createScale(this._zoom,this._zoom),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t)),t=Matrix2D.createTranslation(this._origin.x,this._origin.y,t),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t),this._inverseTransformMatrix=Matrix2D.invert(this._transformMatrix),this._areMatrixesDirty=!1)},e.prototype.destory=function(){},e}(Component),EntitySystem=function(){function t(t){this._entities=[],this._matcher=t||Matcher.empty()}return Object.defineProperty(t.prototype,"matcher",{get:function(){return this._matcher},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scene",{get:function(){return this._scene},set:function(t){this._scene=t,this._entities=[]},enumerable:!0,configurable:!0}),t.prototype.initialize=function(){},t.prototype.onChanged=function(t){var e=this._entities.contains(t),n=this._matcher.IsIntersted(t);n&&!e?this.add(t):!n&&e&&this.remove(t)},t.prototype.add=function(t){this._entities.push(t),this.onAdded(t)},t.prototype.onAdded=function(t){},t.prototype.remove=function(t){this._entities.remove(t),this.onRemoved(t)},t.prototype.onRemoved=function(t){},t.prototype.update=function(){this.begin(),this.process(this._entities)},t.prototype.lateUpdate=function(){this.lateProcess(this._entities),this.end()},t.prototype.begin=function(){},t.prototype.process=function(t){},t.prototype.lateProcess=function(t){},t.prototype.end=function(){},t}(),EntityProcessingSystem=function(t){function e(e){return t.call(this,e)||this}return __extends(e,t),e.prototype.lateProcessEntity=function(t){},e.prototype.process=function(t){var e=this;t.forEach(function(t){return e.processEntity(t)})},e.prototype.lateProcess=function(t){var e=this;t.forEach(function(t){return e.lateProcessEntity(t)})},e}(EntitySystem),BitSet=function(){function t(e){void 0===e&&(e=64);var n=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),o=0;o=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 o=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((o=((o=(o>>4&252645135)+(252645135&o))>>8&16711935)+(16711935&o))>>16&65535)+(65535&o)}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){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){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.forEach(function(t){return t.onAddedToScene()}),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.map=function(t,e,n,o,i){return o+(t-e)*(i-o)/(n-e)},t.clamp=function(t,e,n){return tn?n:t},t}(),Matrix2D=function(){function t(t,e,n,o,i,r){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=o,this.m31=i,this.m32=r}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,o=t.m11*e.m12+t.m12*e.m22,i=t.m21*e.m11+t.m22*e.m21,r=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=o,t.m21=i,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,o){var i=t.createTranslation(n,o);return t.multiply(e,i)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(e,n){void 0===n&&(n=t.identity);var o=1/e.determinant();return n.m11=e.m22*o,n.m12=-e.m12*o,n.m21=-e.m21*o,n.m22=e.m11*o,n.m31=(e.m32*e.m21-e.m31*e.m22)*o,n.m32=-(e.m32*e.m11-e.m31*e.m12)*o,n},t.createTranslation=function(e,n,o){return void 0===o&&(o=t.identity),o.m11=1,o.m12=0,o.m21=0,o.m22=1,o.m31=e,o.m32=n,o},t.createRotation=function(e,n){n=t.identity;var o=Math.cos(e),i=Math.sin(e);return n.m11=o,n.m12=i,n.m21=-i,n.m22=o,n},t.createScale=function(e,n,o){return void 0===o&&(o=t.identity),o.m11=e,o.m12=0,o.m21=0,o.m22=n,o.m31=0,o.m32=0,o},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/src/ECS/Component.ts b/source/src/ECS/Component.ts index 406dc67c..ce5e0371 100644 --- a/source/src/ECS/Component.ts +++ b/source/src/ECS/Component.ts @@ -2,6 +2,12 @@ abstract class Component { public entity: Entity; public displayRender: egret.DisplayObject; private _enabled: boolean = true; + public updateInterval: number = 1; + + public get transform(){ + return this.entity.transform; + } + public get enabled(){ return this.entity ? this.entity.enabled && this._enabled : this._enabled; } @@ -13,6 +19,12 @@ abstract class Component { public setEnabled(isEnabled: boolean){ if (this._enabled != isEnabled){ this._enabled = isEnabled; + + if (this._enabled){ + this.onEnabled(); + }else{ + this.onDisabled(); + } } return this; diff --git a/source/src/ECS/Components/Camera.ts b/source/src/ECS/Components/Camera.ts index 6e44cb44..401cba0e 100644 --- a/source/src/ECS/Components/Camera.ts +++ b/source/src/ECS/Components/Camera.ts @@ -5,6 +5,51 @@ class Camera extends Component { private _transformMatrix: Matrix2D = Matrix2D.identity; private _inverseTransformMatrix = Matrix2D.identity; + private _minimumZoom = 0.3; + private _maximumZoom = 3; + private _areMatrixesDirty = true; + + public get zoom(){ + if (this._zoom == 0) + return 1; + + if (this._zoom < 1) + return MathHelper.map(this._zoom, this._minimumZoom, 1, -1, 0); + + return MathHelper.map(this._zoom, 1, this._maximumZoom, 0, 1); + } + + public set zoom(value: number){ + this.setZoom(value); + } + + public get minimumZoom(){ + return this._minimumZoom; + } + + public set minimumZoom(value: number){ + this.setMinimumZoom(value); + } + + public get maximumZoom(){ + return this._maximumZoom; + } + + public set maximumZoom(value: number){ + this.setMaximumZoom(value); + } + + public get origin(){ + return this._origin; + } + + public set origin(value: Vector2){ + if (this._origin != value){ + this._origin = value; + this._areMatrixesDirty = true; + } + } + public get transformMatrix(){ this.updateMatrixes(); @@ -13,6 +58,39 @@ class Camera extends Component { constructor() { super(); + + this.setZoom(0); + } + + public setMinimumZoom(minZoom: number): Camera{ + if (this._zoom < minZoom) + this._zoom = this.minimumZoom; + + this._minimumZoom = minZoom; + return this; + } + + public setMaximumZoom(maxZoom: number): Camera { + if (this._zoom > maxZoom) + this._zoom = maxZoom; + + this._maximumZoom = maxZoom; + return this; + } + + public setZoom(zoom: number){ + let newZoom = MathHelper.clamp(zoom, -1, 1); + if (newZoom == 0){ + this._zoom = 1; + } else if(newZoom < 0){ + this._zoom = MathHelper.map(newZoom, -1, 0, this._minimumZoom, 1); + } else { + this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom); + } + + this._areMatrixesDirty = true; + + return this; } public initialize() { @@ -37,7 +115,22 @@ class Camera extends Component { } public updateMatrixes(){ + if (!this._areMatrixesDirty) + return; + + let tempMat: Matrix2D; this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); + if (this._zoom != 1){ + tempMat = Matrix2D.createScale(this._zoom, this._zoom); + this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); + } + + tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat); + this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat); + + this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix); + + this._areMatrixesDirty = false; } public destory() { diff --git a/source/src/ECS/Entity.ts b/source/src/ECS/Entity.ts index 63381e41..4c61fe85 100644 --- a/source/src/ECS/Entity.ts +++ b/source/src/ECS/Entity.ts @@ -12,6 +12,90 @@ class Entity { public componentBits: BitSet; + public get parent(){ + return this.transform.parent; + } + + public set parent(value: Transform){ + this.transform.setParent(value); + } + + public get position(){ + return this.transform.position; + } + + public set position(value: Vector2){ + this.transform.setPosition(value); + } + + public get localPosition(){ + return this.transform.localPosition; + } + + public set localPosition(value: Vector2){ + this.transform.setLocalPosition(value); + } + + public get rotation(){ + return this.transform.rotation; + } + + public set rotation(value: number){ + this.transform.setRotation(value); + } + + public get rotationDegrees(){ + return this.transform.rotationDegrees; + } + + public set rotationDegrees(value: number){ + this.transform.setRotationDegrees(value); + } + + public get localRotation(){ + return this.transform.localRotation; + } + + public set localRotation(value: number){ + this.transform.setLocalRotation(value); + } + + public get localRotationDegrees(){ + return this.transform.localRotationDegrees; + } + + public set localRotationDegrees(value: number){ + this.transform.setLocalRotationDegrees(value); + } + + public get scale(){ + return this.transform.scale; + } + + public set scale(value: Vector2){ + this.transform.setScale(value); + } + + public get localScale(){ + return this.transform.scale; + } + + public set localScale(value: Vector2){ + this.transform.setScale(value); + } + + public get worldInverseTransform(){ + return this.transform.worldInverseTransform; + } + + public get localToWorldTransform(){ + return this.transform.localToWorldTransform; + } + + public get worldToLocalTransform(){ + return this.transform.worldToLocalTransform; + } + public get isDestoryed(){ return this._isDestoryed; } @@ -83,10 +167,43 @@ class Entity { return component; } + public hasComponent(type){ + return this.components.getComponent(type, false) != null; + } + + public getOrCreateComponent(type: T){ + let comp = this.components.getComponent(type, true); + if (!comp){ + comp = this.addComponent(type); + } + + return comp; + } + public getComponent(type): T{ return this.components.getComponent(type, false) as T; } + public removeComponentForType(type){ + let comp = this.getComponent(type); + if (comp){ + this.removeComponent(comp); + return true; + } + + return false; + } + + public removeComponent(component: Component){ + this.components.remove(component); + } + + public removeAllComponents(){ + for (let i = 0; i < this.components.count; i ++){ + this.removeComponent(this.components.buffer[i]); + } + } + public update(){ this.components.update(); this.transform.updateTransform(); diff --git a/source/src/ECS/Transform.ts b/source/src/ECS/Transform.ts index 6f0b1590..6bf1b691 100644 --- a/source/src/ECS/Transform.ts +++ b/source/src/ECS/Transform.ts @@ -57,6 +57,36 @@ class Transform { return this._children[index]; } + public get worldInverseTransform(){ + this.updateTransform(); + if (this._worldInverseDirty){ + this._worldInverseTransform = Matrix2D.invert(this._worldTransform, this._worldInverseTransform); + this._worldInverseDirty = false; + } + + return this._worldInverseTransform; + } + + public get localToWorldTransform(){ + this.updateTransform(); + return this._worldTransform; + } + + public get worldToLocalTransform(){ + if (this._worldToLocalDirty){ + if (!this.parent){ + this._worldInverseTransform = Matrix2D.identity; + } else{ + this.parent.updateTransform(); + this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform); + } + + this._worldToLocalDirty = false; + } + + return this._worldToLocalTransform; + } + public get parent(){ return this._parent; } @@ -80,6 +110,24 @@ class Transform { return this; } + public get rotation() { + this.updateTransform(); + return this._rotation; + } + + public set rotation(value: number){ + this.setRotation(value); + } + + public get localRotation(){ + this.updateTransform(); + return this._localRotation; + } + + public set localRotation(value: number){ + this.setLocalRotation(value); + } + public get position(){ this.updateTransform(); if (!this.parent){ @@ -105,6 +153,101 @@ class Transform { this.setLocalPosition(value); } + public get scale(){ + this.updateTransform(); + return this._scale; + } + + public set scale(value: Vector2){ + this.setScale(value); + } + + public get localScale(){ + this.updateTransform(); + return this._localScale; + } + + public set localScale(value: Vector2){ + this.setLocalScale(value); + } + + public get rotationDegrees(){ + return MathHelper.toDegrees(this._rotation); + } + + public set rotationDegrees(value: number){ + this.setRotation(MathHelper.toRadians(value)); + } + + public get localRotationDegrees(){ + return MathHelper.toDegrees(this._localRotation); + } + + public set localRotationDegrees(value: number){ + this.localRotation = MathHelper.toRadians(value); + } + + public setLocalScale(scale: Vector2){ + this._localScale = scale; + this._localDirty = this._positionDirty = this._localScaleDirty = true; + this.setDirty(DirtyType.scaleDirty); + + return this; + } + + public setScale(scale: Vector2){ + this._scale = scale; + if (this.parent){ + this.localScale = Vector2.divide(scale, this.parent._scale); + }else{ + this.localScale = scale; + } + + for (let i = 0; i < this.entity.components.buffer.length; i ++){ + let component = this.entity.components.buffer[i]; + if (component.displayRender){ + component.displayRender.scaleX = this.scale.x; + component.displayRender.scaleY = this.scale.y; + } + } + + return this; + } + + public setLocalRotationDegrees(degrees: number){ + return this.setLocalRotation(MathHelper.toRadians(degrees)); + } + + public setLocalRotation(radians: number){ + this._localRotation = radians; + this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; + this.setDirty(DirtyType.rotationDirty); + + return this; + } + + public setRotation(radians: number){ + this._rotation = radians; + if (this.parent){ + this.localRotation = this.parent.rotation + radians; + } else { + this.localRotation = radians; + } + + for (let i = 0; i < this.entity.components.buffer.length; i ++){ + let component = this.entity.components.buffer[i]; + if (component.displayRender){ + component.displayRender.rotation = this.rotation; + } + } + + return this; + } + + public setRotationDegrees(degrees: number){ + return this.setRotation(MathHelper.toRadians(degrees)); + } + public setLocalPosition(localPosition: Vector2){ if (localPosition == this._localPosition) return this; diff --git a/source/src/ECS/Utils/ComponentList.ts b/source/src/ECS/Utils/ComponentList.ts index a0b7b95e..aae1a345 100644 --- a/source/src/ECS/Utils/ComponentList.ts +++ b/source/src/ECS/Utils/ComponentList.ts @@ -9,6 +9,10 @@ class ComponentList { this._entity = entity; } + public get count(){ + return this._components.length; + } + public get buffer(){ return this._components; } diff --git a/source/src/Math/MathHelper.ts b/source/src/Math/MathHelper.ts index b96e4f4d..94b785ed 100644 --- a/source/src/Math/MathHelper.ts +++ b/source/src/Math/MathHelper.ts @@ -14,4 +14,26 @@ class MathHelper { public static toRadians(degrees: number){ return degrees * 0.017453292519943295769236907684886; } + + /** + * mapps值(在leftMin - leftMax范围内)到rightMin - rightMax范围内的值 + * @param value + * @param leftMin + * @param leftMax + * @param rightMin + * @param rightMax + */ + public static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number){ + return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin); + } + + public static clamp(value: number, min: number, max: number){ + if (value < min) + return min; + + if (value > max) + return max; + + return value; + } } \ No newline at end of file diff --git a/source/src/Math/Matrix2D.ts b/source/src/Math/Matrix2D.ts index 64b71a7f..9e27d0ed 100644 --- a/source/src/Math/Matrix2D.ts +++ b/source/src/Math/Matrix2D.ts @@ -137,7 +137,7 @@ class Matrix2D { return this.m11 * this.m22 - this.m12 * this.m21; } - public static invert(matrix: Matrix2D, result: Matrix2D){ + public static invert(matrix: Matrix2D, result: Matrix2D = Matrix2D.identity){ let det = 1 / matrix.determinant(); result.m11 = matrix.m22 * det;