diff --git a/demo/libs/framework/framework.d.ts b/demo/libs/framework/framework.d.ts index 79b3b9fd..951abb1d 100644 --- a/demo/libs/framework/framework.d.ts +++ b/demo/libs/framework/framework.d.ts @@ -17,17 +17,33 @@ declare interface Array { groupBy(keySelector: Function): Array; sum(selector: any): any; } +declare abstract class Component { + entity: Entity; + displayRender: egret.DisplayObject; + abstract initialize(): any; + update(): void; + bind(displayRender: egret.DisplayObject): this; +} declare class Entity { name: string; scene: Scene; readonly transform: Transform; + readonly components: Component[]; + private _updateOrder; constructor(name: string); + updateOrder: number; + setUpdateOrder(updateOrder: number): this; attachToScene(newScene: Scene): void; + addComponent(component: T): T; + update(): void; destory(): void; } declare class Scene extends egret.DisplayObjectContainer { camera: Camera; entities: Entity[]; + private _projectionMatrix; + private _transformMatrix; + private _matrixTransformMatrix; constructor(displayObject: egret.DisplayObject); createEntity(name: string): Entity; addEntity(entity: Entity): Entity; @@ -35,6 +51,8 @@ declare class Scene extends egret.DisplayObjectContainer { initialize(): void; onActive(): void; onDeactive(): void; + update(): void; + prepRenderState(): void; destory(): void; } declare class SceneManager { @@ -43,25 +61,52 @@ declare class SceneManager { private static _activeScene; static createScene(name: string, scene: Scene): Scene; static setActiveScene(scene: Scene): Scene; + static getActiveScene(): Scene; } declare class Transform { readonly entity: Entity; private _children; private _parent; + private _localPosition; + private _localRotation; + private _localScale; + private _translationMatrix; + private _rotationMatrix; + private _scaleMatrix; + private _worldTransform; + private _worldToLocalTransform; + private _worldInverseTransform; + private _rotation; + private _position; + private _scale; + private _localTransform; readonly childCount: number; constructor(entity: Entity); getChild(index: number): Transform; parent: Transform; setParent(parent: Transform): this; + position: Vector2; + localPosition: Vector2; + setLocalPosition(localPosition: Vector2): this; + setPosition(position: Vector2): this; + updateTransform(): void; } -declare class Camera { - private _displayContent; - constructor(displayObject: egret.DisplayObject); +declare class Camera extends Component { + private _zoom; + private _origin; + private _transformMatrix; + private _inverseTransformMatrix; + readonly transformMatrix: Matrix2D; + constructor(); + initialize(): void; + update(): void; + setPosition(position: Vector2): this; + updateMatrixes(): void; destory(): void; } declare class MathHelper { - static ToDegrees(radians: number): number; - static ToRadians(degrees: number): number; + static toDegrees(radians: number): number; + static toRadians(degrees: number): number; } declare class Matrix2D { m11: number; @@ -80,14 +125,23 @@ declare class Matrix2D { static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; static divide(matrix1: Matrix2D, matrix2: Matrix2D): 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 createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D; + static createRotation(radians: number, result?: Matrix2D): Matrix2D; + static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D; } declare class Vector2 { x: number; y: number; + private static readonly unitVector2; + static readonly One: Vector2; constructor(x: number, y: number); static add(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2; static subtract(value1: Vector2, value2: Vector2): Vector2; normalize(): void; + static transform(position: Vector2, matrix: Matrix2D): Vector2; } diff --git a/demo/libs/framework/framework.js b/demo/libs/framework/framework.js index fb856024..a7e78b2d 100644 --- a/demo/libs/framework/framework.js +++ b/demo/libs/framework/framework.js @@ -238,11 +238,42 @@ Array.prototype.sum = function (selector) { } return sum(this, selector); }; +var Component = (function () { + function Component() { + } + Component.prototype.update = function () { + }; + Component.prototype.bind = function (displayRender) { + this.displayRender = displayRender; + return this; + }; + return Component; +}()); var Entity = (function () { function Entity(name) { + this._updateOrder = 0; this.name = name; this.transform = new Transform(this); + this.components = []; } + Object.defineProperty(Entity.prototype, "updateOrder", { + get: function () { + return this._updateOrder; + }, + set: function (value) { + this.setUpdateOrder(value); + }, + enumerable: true, + configurable: true + }); + Entity.prototype.setUpdateOrder = function (updateOrder) { + if (this._updateOrder != updateOrder) { + this._updateOrder = updateOrder; + if (this.scene) { + } + return this; + } + }; Entity.prototype.attachToScene = function (newScene) { this.scene = newScene; newScene.entities.push(this); @@ -250,6 +281,15 @@ var Entity = (function () { this.transform.getChild(i).entity.attachToScene(newScene); } }; + Entity.prototype.addComponent = function (component) { + component.entity = this; + this.components.push(component); + component.initialize(); + return component; + }; + Entity.prototype.update = function () { + this.components.forEach(function (component) { return component.update(); }); + }; Entity.prototype.destory = function () { this.scene.entities.remove(this); this.transform.parent = null; @@ -265,13 +305,17 @@ var Scene = (function (_super) { function Scene(displayObject) { var _this = _super.call(this) || this; _this.entities = []; - _this.camera = new Camera(displayObject); + displayObject.stage.addChild(_this); + _this.camera = _this.createEntity("camera").addComponent(new Camera()); + _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); _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); return _this; } Scene.prototype.createEntity = function (name) { var entity = new Entity(name); + entity.transform.position = new Vector2(0, 0); return this.addEntity(entity); }; Scene.prototype.addEntity = function (entity) { @@ -289,6 +333,15 @@ var Scene = (function (_super) { }; Scene.prototype.onDeactive = function () { }; + Scene.prototype.update = function () { + this.entities.forEach(function (entity) { return entity.update(); }); + }; + Scene.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); + }; Scene.prototype.destory = function () { this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this); this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this); @@ -318,12 +371,21 @@ var SceneManager = (function () { this._activeScene.initialize(); return scene; }; + SceneManager.getActiveScene = function () { + return this._activeScene; + }; SceneManager._loadedScenes = new Map(); return SceneManager; }()); var Transform = (function () { function Transform(entity) { + this._localRotation = 0; + this._worldTransform = Matrix2D.identity; + this._worldToLocalTransform = Matrix2D.identity; + this._worldInverseTransform = Matrix2D.identity; + this._rotation = 0; this.entity = entity; + this._scale = this._localScale = Vector2.One; this._children = []; } Object.defineProperty(Transform.prototype, "childCount", { @@ -356,30 +418,138 @@ var Transform = (function () { this._parent = parent; return this; }; + Object.defineProperty(Transform.prototype, "position", { + get: function () { + this.updateTransform(); + if (!this.parent) { + this._position = this._localPosition; + } + else { + this.parent.updateTransform(); + this._position = Vector2.transform(this._localPosition, this.parent._worldTransform); + } + return this._position; + }, + set: function (value) { + this.setPosition(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localPosition", { + get: function () { + this.updateTransform(); + return this._localPosition; + }, + set: function (value) { + this.setLocalPosition(value); + }, + enumerable: true, + configurable: true + }); + Transform.prototype.setLocalPosition = function (localPosition) { + if (localPosition == this._localPosition) + return this; + this._localPosition = localPosition; + return this; + }; + Transform.prototype.setPosition = function (position) { + if (position == this._position) + return this; + this._position = position; + if (this.parent) { + this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform); + } + else { + this.localPosition = position; + } + for (var i = 0; i < this.entity.components.length; i++) { + var component = this.entity.components[i]; + if (component.displayRender) { + component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x; + component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y; + } + } + return this; + }; + Transform.prototype.updateTransform = function () { + this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y); + this._rotationMatrix = Matrix2D.createRotation(this._localRotation); + this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y); + this._localTransform = Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix); + this._localTransform = Matrix2D.multiply(this._localTransform, this._translationMatrix); + if (!this.parent) { + this._worldTransform = this._localTransform; + this._rotation = this._localRotation; + this._scale = this._localScale; + } + else { + this._worldTransform = Matrix2D.multiply(this._localTransform, this.parent._worldTransform); + this._rotation = this._localRotation + this.parent._rotation; + this._scale = Vector2.multiply(this.parent._scale, this._localScale); + } + }; return Transform; }()); -var Camera = (function () { - function Camera(displayObject) { - this._displayContent = displayObject; +var Camera = (function (_super) { + __extends(Camera, _super); + function Camera() { + var _this = _super.call(this) || this; + _this._transformMatrix = Matrix2D.identity; + _this._inverseTransformMatrix = Matrix2D.identity; + return _this; } + Object.defineProperty(Camera.prototype, "transformMatrix", { + get: function () { + this.updateMatrixes(); + return this._transformMatrix; + }, + enumerable: true, + configurable: true + }); + Camera.prototype.initialize = function () { + }; + Camera.prototype.update = function () { + var _this = this; + SceneManager.getActiveScene().entities.forEach(function (entity) { return entity.components.forEach(function (component) { + if (component.displayRender) { + var has = _this.entity.scene.$children.indexOf(component.displayRender); + if (has == -1) { + _this.entity.scene.stage.addChild(component.displayRender); + } + } + }); }); + }; + Camera.prototype.setPosition = function (position) { + this.entity.transform.setPosition(position); + return this; + }; + Camera.prototype.updateMatrixes = function () { + this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); + }; Camera.prototype.destory = function () { - this._displayContent = null; }; return Camera; -}()); +}(Component)); var MathHelper = (function () { function MathHelper() { } - MathHelper.ToDegrees = function (radians) { + MathHelper.toDegrees = function (radians) { return radians * 57.295779513082320876798154814105; }; - MathHelper.ToRadians = function (degrees) { + MathHelper.toRadians = function (degrees) { return degrees * 0.017453292519943295769236907684886; }; return MathHelper; }()); var Matrix2D = (function () { function Matrix2D(m11, m12, m21, m22, m31, m32) { + this.m11 = 0; + this.m12 = 0; + this.m21 = 0; + this.m22 = 0; + this.m31 = 0; + this.m32 = 0; this.m11 = m11; this.m12 = m12; this.m21 = m21; @@ -422,10 +592,10 @@ var Matrix2D = (function () { }); Object.defineProperty(Matrix2D.prototype, "rotationDegrees", { get: function () { - return MathHelper.ToDegrees(this.rotation); + return MathHelper.toDegrees(this.rotation); }, set: function (value) { - this.rotation = MathHelper.ToRadians(value); + this.rotation = MathHelper.toRadians(value); }, enumerable: true, configurable: true @@ -474,14 +644,70 @@ var Matrix2D = (function () { matrix1.m32 = m32; return matrix1; }; + Matrix2D.multiplyTranslation = function (matrix, x, y) { + var trans = Matrix2D.createTranslation(x, y); + return Matrix2D.multiply(matrix, trans); + }; + Matrix2D.prototype.determinant = function () { + return this.m11 * this.m22 - this.m12 * this.m21; + }; + Matrix2D.invert = function (matrix, result) { + var det = 1 / matrix.determinant(); + result.m11 = matrix.m22 * det; + result.m12 = -matrix.m12 * det; + result.m21 = -matrix.m21 * det; + result.m22 = matrix.m11 * det; + result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det; + result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det; + return result; + }; + Matrix2D.createTranslation = function (xPosition, yPosition, result) { + if (result === void 0) { result = Matrix2D.identity; } + result.m11 = 1; + result.m12 = 0; + result.m21 = 0; + result.m22 = 1; + result.m31 = xPosition; + result.m32 = yPosition; + return result; + }; + Matrix2D.createRotation = function (radians, result) { + result = Matrix2D.identity; + var val1 = Math.cos(radians); + var val2 = Math.sin(radians); + result.m11 = val1; + result.m12 = val2; + result.m21 = -val2; + result.m22 = val1; + return result; + }; + Matrix2D.createScale = function (xScale, yScale, result) { + if (result === void 0) { result = Matrix2D.identity; } + result.m11 = xScale; + result.m12 = 0; + result.m21 = 0; + result.m22 = yScale; + result.m31 = 0; + result.m32 = 0; + return result; + }; Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0); return Matrix2D; }()); var Vector2 = (function () { function Vector2(x, y) { + this.x = 0; + this.y = 0; this.x = x; this.y = y; } + Object.defineProperty(Vector2, "One", { + get: function () { + return this.unitVector2; + }, + enumerable: true, + configurable: true + }); Vector2.add = function (value1, value2) { value1.x += value2.x; value1.y += value2.y; @@ -507,5 +733,9 @@ var Vector2 = (function () { this.x *= val; this.y *= val; }; + Vector2.transform = function (position, matrix) { + return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); + }; + Vector2.unitVector2 = new Vector2(1, 1); return Vector2; }()); diff --git a/demo/libs/framework/framework.min.js b/demo/libs/framework/framework.min.js index e6a977b3..608e5e2c 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 e in n)n.hasOwnProperty(e)&&(t[e]=n[e])};return function(n,e){function r(){this.constructor=n}t(n,e),n.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}(),Array.prototype.findIndex=function(t){return function(t,n){for(var e=0,r=t.length;e-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,n){var e=t.findIndex(n);return-1==e?null:t[e]}(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(e,r,i){return n.call(arguments[2],r,i,t)&&e.push(r),e},[]);for(var e=[],r=0,i=t.length;r=0&&t.splice(e,1)}while(e>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,n){var e=t.findIndex(function(t){return t===n});return e>=0&&(t.splice(e,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,e){t.splice(n,e)}(this,t,n)},Array.prototype.select=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(e,r,i){return e.push(n.call(arguments[2],r,i,t)),e},[]);for(var e=[],r=0,i=t.length;ro?1:-1}),t}(this,t,n)},Array.prototype.orderByDescending=function(t,n){return function(t,n,e){return t.sort(function(t,r){var i=n(t),o=n(r);return e?-e(i,o):i=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function n(n){var e=t.call(this)||this;return e.entities=[],e.camera=new Camera(n),e.addEventListener(egret.Event.ACTIVATE,e.onActive,e),e.addEventListener(egret.Event.DEACTIVATE,e.onDeactive,e),e}return __extends(n,t),n.prototype.createEntity=function(t){var n=new Entity(t);return 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.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._loadedScenes=new Map,t}(),Transform=function(){function t(t){this.entity=t,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)},t}(),Camera=function(){function t(t){this._displayContent=t}return t.prototype.destory=function(){this._displayContent=null},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,n,e,r,i,o){this.m11=t,this.m12=n,this.m21=e,this.m22=r,this.m31=i,this.m32=o}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var n=Math.cos(t),e=Math.sin(t);this.m11=n,this.m12=e,this.m21=-e,this.m22=n},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,n){return t.m11+=n.m11,t.m12+=n.m12,t.m21+=n.m21,t.m22+=n.m22,t.m31+=n.m31,t.m32+=n.m32,t},t.divide=function(t,n){return t.m11/=n.m11,t.m12/=n.m12,t.m21/=n.m21,t.m22/=n.m22,t.m31/=n.m31,t.m32/=n.m32,t},t.multiply=function(t,n){var e=t.m11*n.m11+t.m12*n.m21,r=t.m11*n.m12+t.m12*n.m22,i=t.m21*n.m11+t.m22*n.m21,o=t.m21*n.m12+t.m22*n.m22,u=t.m31*n.m11+t.m32*n.m21+n.m31,c=t.m31*n.m12+t.m32*n.m22+n.m32;return t.m11=e,t.m12=r,t.m21=i,t.m22=o,t.m31=u,t.m32=c,t},t._identity=new t(1,0,0,1,0,0),t}(),Vector2=function(){function t(t,n){this.x=t,this.y=n}return t.add=function(t,n){return t.x+=n.x,t.y+=n.y,t},t.divide=function(t,n){return t.x/=n.x,t.y/=n.y,t},t.multiply=function(t,n){return t.x*=n.x,t.y*=n.y,t},t.subtract=function(t,n){return t.x-=n.x,t.y-=n.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}(); \ No newline at end of file +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 e in n)n.hasOwnProperty(e)&&(t[e]=n[e])};return function(n,e){function r(){this.constructor=n}t(n,e),n.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}(),Array.prototype.findIndex=function(t){return function(t,n){for(var e=0,r=t.length;e-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,n){var e=t.findIndex(n);return-1==e?null:t[e]}(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(e,r,i){return n.call(arguments[2],r,i,t)&&e.push(r),e},[]);for(var e=[],r=0,i=t.length;r=0&&t.splice(e,1)}while(e>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,n){var e=t.findIndex(function(t){return t===n});return e>=0&&(t.splice(e,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,e){t.splice(n,e)}(this,t,n)},Array.prototype.select=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(e,r,i){return e.push(n.call(arguments[2],r,i,t)),e},[]);for(var e=[],r=0,i=t.length;ro?1:-1}),t}(this,t,n)},Array.prototype.orderByDescending=function(t,n){return function(t,n,e){return t.sort(function(t,r){var i=n(t),o=n(r);return e?-e(i,o):i=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function n(n){var e=t.call(this)||this;return e.entities=[],n.stage.addChild(e),e.camera=e.createEntity("camera").addComponent(new Camera),e._projectionMatrix=new Matrix2D(0,0,0,0,0,0),e.addEventListener(egret.Event.ACTIVATE,e.onActive,e),e.addEventListener(egret.Event.DEACTIVATE,e.onDeactive,e),e.addEventListener(egret.Event.ENTER_FRAME,e.update,e),e}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}(),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)},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 { groupBy(keySelector: Function): Array; sum(selector: any): any; } +declare abstract class Component { + entity: Entity; + displayRender: egret.DisplayObject; + abstract initialize(): any; + update(): void; + bind(displayRender: egret.DisplayObject): this; +} declare class Entity { name: string; scene: Scene; readonly transform: Transform; + readonly components: Component[]; + private _updateOrder; constructor(name: string); + updateOrder: number; + setUpdateOrder(updateOrder: number): this; attachToScene(newScene: Scene): void; + addComponent(component: T): T; + update(): void; destory(): void; } declare class Scene extends egret.DisplayObjectContainer { camera: Camera; entities: Entity[]; + private _projectionMatrix; + private _transformMatrix; + private _matrixTransformMatrix; constructor(displayObject: egret.DisplayObject); createEntity(name: string): Entity; addEntity(entity: Entity): Entity; @@ -35,6 +51,8 @@ declare class Scene extends egret.DisplayObjectContainer { initialize(): void; onActive(): void; onDeactive(): void; + update(): void; + prepRenderState(): void; destory(): void; } declare class SceneManager { @@ -43,25 +61,52 @@ declare class SceneManager { private static _activeScene; static createScene(name: string, scene: Scene): Scene; static setActiveScene(scene: Scene): Scene; + static getActiveScene(): Scene; } declare class Transform { readonly entity: Entity; private _children; private _parent; + private _localPosition; + private _localRotation; + private _localScale; + private _translationMatrix; + private _rotationMatrix; + private _scaleMatrix; + private _worldTransform; + private _worldToLocalTransform; + private _worldInverseTransform; + private _rotation; + private _position; + private _scale; + private _localTransform; readonly childCount: number; constructor(entity: Entity); getChild(index: number): Transform; parent: Transform; setParent(parent: Transform): this; + position: Vector2; + localPosition: Vector2; + setLocalPosition(localPosition: Vector2): this; + setPosition(position: Vector2): this; + updateTransform(): void; } -declare class Camera { - private _displayContent; - constructor(displayObject: egret.DisplayObject); +declare class Camera extends Component { + private _zoom; + private _origin; + private _transformMatrix; + private _inverseTransformMatrix; + readonly transformMatrix: Matrix2D; + constructor(); + initialize(): void; + update(): void; + setPosition(position: Vector2): this; + updateMatrixes(): void; destory(): void; } declare class MathHelper { - static ToDegrees(radians: number): number; - static ToRadians(degrees: number): number; + static toDegrees(radians: number): number; + static toRadians(degrees: number): number; } declare class Matrix2D { m11: number; @@ -80,14 +125,23 @@ declare class Matrix2D { static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; static divide(matrix1: Matrix2D, matrix2: Matrix2D): 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 createTranslation(xPosition: number, yPosition: number, result?: Matrix2D): Matrix2D; + static createRotation(radians: number, result?: Matrix2D): Matrix2D; + static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D; } declare class Vector2 { x: number; y: number; + private static readonly unitVector2; + static readonly One: Vector2; constructor(x: number, y: number); static add(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2; static subtract(value1: Vector2, value2: Vector2): Vector2; normalize(): void; + static transform(position: Vector2, matrix: Matrix2D): Vector2; } diff --git a/source/bin/framework.js b/source/bin/framework.js index fb856024..a7e78b2d 100644 --- a/source/bin/framework.js +++ b/source/bin/framework.js @@ -238,11 +238,42 @@ Array.prototype.sum = function (selector) { } return sum(this, selector); }; +var Component = (function () { + function Component() { + } + Component.prototype.update = function () { + }; + Component.prototype.bind = function (displayRender) { + this.displayRender = displayRender; + return this; + }; + return Component; +}()); var Entity = (function () { function Entity(name) { + this._updateOrder = 0; this.name = name; this.transform = new Transform(this); + this.components = []; } + Object.defineProperty(Entity.prototype, "updateOrder", { + get: function () { + return this._updateOrder; + }, + set: function (value) { + this.setUpdateOrder(value); + }, + enumerable: true, + configurable: true + }); + Entity.prototype.setUpdateOrder = function (updateOrder) { + if (this._updateOrder != updateOrder) { + this._updateOrder = updateOrder; + if (this.scene) { + } + return this; + } + }; Entity.prototype.attachToScene = function (newScene) { this.scene = newScene; newScene.entities.push(this); @@ -250,6 +281,15 @@ var Entity = (function () { this.transform.getChild(i).entity.attachToScene(newScene); } }; + Entity.prototype.addComponent = function (component) { + component.entity = this; + this.components.push(component); + component.initialize(); + return component; + }; + Entity.prototype.update = function () { + this.components.forEach(function (component) { return component.update(); }); + }; Entity.prototype.destory = function () { this.scene.entities.remove(this); this.transform.parent = null; @@ -265,13 +305,17 @@ var Scene = (function (_super) { function Scene(displayObject) { var _this = _super.call(this) || this; _this.entities = []; - _this.camera = new Camera(displayObject); + displayObject.stage.addChild(_this); + _this.camera = _this.createEntity("camera").addComponent(new Camera()); + _this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); _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); return _this; } Scene.prototype.createEntity = function (name) { var entity = new Entity(name); + entity.transform.position = new Vector2(0, 0); return this.addEntity(entity); }; Scene.prototype.addEntity = function (entity) { @@ -289,6 +333,15 @@ var Scene = (function (_super) { }; Scene.prototype.onDeactive = function () { }; + Scene.prototype.update = function () { + this.entities.forEach(function (entity) { return entity.update(); }); + }; + Scene.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); + }; Scene.prototype.destory = function () { this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this); this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this); @@ -318,12 +371,21 @@ var SceneManager = (function () { this._activeScene.initialize(); return scene; }; + SceneManager.getActiveScene = function () { + return this._activeScene; + }; SceneManager._loadedScenes = new Map(); return SceneManager; }()); var Transform = (function () { function Transform(entity) { + this._localRotation = 0; + this._worldTransform = Matrix2D.identity; + this._worldToLocalTransform = Matrix2D.identity; + this._worldInverseTransform = Matrix2D.identity; + this._rotation = 0; this.entity = entity; + this._scale = this._localScale = Vector2.One; this._children = []; } Object.defineProperty(Transform.prototype, "childCount", { @@ -356,30 +418,138 @@ var Transform = (function () { this._parent = parent; return this; }; + Object.defineProperty(Transform.prototype, "position", { + get: function () { + this.updateTransform(); + if (!this.parent) { + this._position = this._localPosition; + } + else { + this.parent.updateTransform(); + this._position = Vector2.transform(this._localPosition, this.parent._worldTransform); + } + return this._position; + }, + set: function (value) { + this.setPosition(value); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transform.prototype, "localPosition", { + get: function () { + this.updateTransform(); + return this._localPosition; + }, + set: function (value) { + this.setLocalPosition(value); + }, + enumerable: true, + configurable: true + }); + Transform.prototype.setLocalPosition = function (localPosition) { + if (localPosition == this._localPosition) + return this; + this._localPosition = localPosition; + return this; + }; + Transform.prototype.setPosition = function (position) { + if (position == this._position) + return this; + this._position = position; + if (this.parent) { + this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform); + } + else { + this.localPosition = position; + } + for (var i = 0; i < this.entity.components.length; i++) { + var component = this.entity.components[i]; + if (component.displayRender) { + component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x; + component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y; + } + } + return this; + }; + Transform.prototype.updateTransform = function () { + this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y); + this._rotationMatrix = Matrix2D.createRotation(this._localRotation); + this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y); + this._localTransform = Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix); + this._localTransform = Matrix2D.multiply(this._localTransform, this._translationMatrix); + if (!this.parent) { + this._worldTransform = this._localTransform; + this._rotation = this._localRotation; + this._scale = this._localScale; + } + else { + this._worldTransform = Matrix2D.multiply(this._localTransform, this.parent._worldTransform); + this._rotation = this._localRotation + this.parent._rotation; + this._scale = Vector2.multiply(this.parent._scale, this._localScale); + } + }; return Transform; }()); -var Camera = (function () { - function Camera(displayObject) { - this._displayContent = displayObject; +var Camera = (function (_super) { + __extends(Camera, _super); + function Camera() { + var _this = _super.call(this) || this; + _this._transformMatrix = Matrix2D.identity; + _this._inverseTransformMatrix = Matrix2D.identity; + return _this; } + Object.defineProperty(Camera.prototype, "transformMatrix", { + get: function () { + this.updateMatrixes(); + return this._transformMatrix; + }, + enumerable: true, + configurable: true + }); + Camera.prototype.initialize = function () { + }; + Camera.prototype.update = function () { + var _this = this; + SceneManager.getActiveScene().entities.forEach(function (entity) { return entity.components.forEach(function (component) { + if (component.displayRender) { + var has = _this.entity.scene.$children.indexOf(component.displayRender); + if (has == -1) { + _this.entity.scene.stage.addChild(component.displayRender); + } + } + }); }); + }; + Camera.prototype.setPosition = function (position) { + this.entity.transform.setPosition(position); + return this; + }; + Camera.prototype.updateMatrixes = function () { + this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); + }; Camera.prototype.destory = function () { - this._displayContent = null; }; return Camera; -}()); +}(Component)); var MathHelper = (function () { function MathHelper() { } - MathHelper.ToDegrees = function (radians) { + MathHelper.toDegrees = function (radians) { return radians * 57.295779513082320876798154814105; }; - MathHelper.ToRadians = function (degrees) { + MathHelper.toRadians = function (degrees) { return degrees * 0.017453292519943295769236907684886; }; return MathHelper; }()); var Matrix2D = (function () { function Matrix2D(m11, m12, m21, m22, m31, m32) { + this.m11 = 0; + this.m12 = 0; + this.m21 = 0; + this.m22 = 0; + this.m31 = 0; + this.m32 = 0; this.m11 = m11; this.m12 = m12; this.m21 = m21; @@ -422,10 +592,10 @@ var Matrix2D = (function () { }); Object.defineProperty(Matrix2D.prototype, "rotationDegrees", { get: function () { - return MathHelper.ToDegrees(this.rotation); + return MathHelper.toDegrees(this.rotation); }, set: function (value) { - this.rotation = MathHelper.ToRadians(value); + this.rotation = MathHelper.toRadians(value); }, enumerable: true, configurable: true @@ -474,14 +644,70 @@ var Matrix2D = (function () { matrix1.m32 = m32; return matrix1; }; + Matrix2D.multiplyTranslation = function (matrix, x, y) { + var trans = Matrix2D.createTranslation(x, y); + return Matrix2D.multiply(matrix, trans); + }; + Matrix2D.prototype.determinant = function () { + return this.m11 * this.m22 - this.m12 * this.m21; + }; + Matrix2D.invert = function (matrix, result) { + var det = 1 / matrix.determinant(); + result.m11 = matrix.m22 * det; + result.m12 = -matrix.m12 * det; + result.m21 = -matrix.m21 * det; + result.m22 = matrix.m11 * det; + result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det; + result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det; + return result; + }; + Matrix2D.createTranslation = function (xPosition, yPosition, result) { + if (result === void 0) { result = Matrix2D.identity; } + result.m11 = 1; + result.m12 = 0; + result.m21 = 0; + result.m22 = 1; + result.m31 = xPosition; + result.m32 = yPosition; + return result; + }; + Matrix2D.createRotation = function (radians, result) { + result = Matrix2D.identity; + var val1 = Math.cos(radians); + var val2 = Math.sin(radians); + result.m11 = val1; + result.m12 = val2; + result.m21 = -val2; + result.m22 = val1; + return result; + }; + Matrix2D.createScale = function (xScale, yScale, result) { + if (result === void 0) { result = Matrix2D.identity; } + result.m11 = xScale; + result.m12 = 0; + result.m21 = 0; + result.m22 = yScale; + result.m31 = 0; + result.m32 = 0; + return result; + }; Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0); return Matrix2D; }()); var Vector2 = (function () { function Vector2(x, y) { + this.x = 0; + this.y = 0; this.x = x; this.y = y; } + Object.defineProperty(Vector2, "One", { + get: function () { + return this.unitVector2; + }, + enumerable: true, + configurable: true + }); Vector2.add = function (value1, value2) { value1.x += value2.x; value1.y += value2.y; @@ -507,5 +733,9 @@ var Vector2 = (function () { this.x *= val; this.y *= val; }; + Vector2.transform = function (position, matrix) { + return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); + }; + Vector2.unitVector2 = new Vector2(1, 1); return Vector2; }()); diff --git a/source/bin/framework.min.js b/source/bin/framework.min.js index e6a977b3..608e5e2c 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 e in n)n.hasOwnProperty(e)&&(t[e]=n[e])};return function(n,e){function r(){this.constructor=n}t(n,e),n.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}(),Array.prototype.findIndex=function(t){return function(t,n){for(var e=0,r=t.length;e-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,n){var e=t.findIndex(n);return-1==e?null:t[e]}(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(e,r,i){return n.call(arguments[2],r,i,t)&&e.push(r),e},[]);for(var e=[],r=0,i=t.length;r=0&&t.splice(e,1)}while(e>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,n){var e=t.findIndex(function(t){return t===n});return e>=0&&(t.splice(e,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,e){t.splice(n,e)}(this,t,n)},Array.prototype.select=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(e,r,i){return e.push(n.call(arguments[2],r,i,t)),e},[]);for(var e=[],r=0,i=t.length;ro?1:-1}),t}(this,t,n)},Array.prototype.orderByDescending=function(t,n){return function(t,n,e){return t.sort(function(t,r){var i=n(t),o=n(r);return e?-e(i,o):i=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function n(n){var e=t.call(this)||this;return e.entities=[],e.camera=new Camera(n),e.addEventListener(egret.Event.ACTIVATE,e.onActive,e),e.addEventListener(egret.Event.DEACTIVATE,e.onDeactive,e),e}return __extends(n,t),n.prototype.createEntity=function(t){var n=new Entity(t);return 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.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._loadedScenes=new Map,t}(),Transform=function(){function t(t){this.entity=t,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)},t}(),Camera=function(){function t(t){this._displayContent=t}return t.prototype.destory=function(){this._displayContent=null},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,n,e,r,i,o){this.m11=t,this.m12=n,this.m21=e,this.m22=r,this.m31=i,this.m32=o}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var n=Math.cos(t),e=Math.sin(t);this.m11=n,this.m12=e,this.m21=-e,this.m22=n},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,n){return t.m11+=n.m11,t.m12+=n.m12,t.m21+=n.m21,t.m22+=n.m22,t.m31+=n.m31,t.m32+=n.m32,t},t.divide=function(t,n){return t.m11/=n.m11,t.m12/=n.m12,t.m21/=n.m21,t.m22/=n.m22,t.m31/=n.m31,t.m32/=n.m32,t},t.multiply=function(t,n){var e=t.m11*n.m11+t.m12*n.m21,r=t.m11*n.m12+t.m12*n.m22,i=t.m21*n.m11+t.m22*n.m21,o=t.m21*n.m12+t.m22*n.m22,u=t.m31*n.m11+t.m32*n.m21+n.m31,c=t.m31*n.m12+t.m32*n.m22+n.m32;return t.m11=e,t.m12=r,t.m21=i,t.m22=o,t.m31=u,t.m32=c,t},t._identity=new t(1,0,0,1,0,0),t}(),Vector2=function(){function t(t,n){this.x=t,this.y=n}return t.add=function(t,n){return t.x+=n.x,t.y+=n.y,t},t.divide=function(t,n){return t.x/=n.x,t.y/=n.y,t},t.multiply=function(t,n){return t.x*=n.x,t.y*=n.y,t},t.subtract=function(t,n){return t.x-=n.x,t.y-=n.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}(); \ No newline at end of file +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 e in n)n.hasOwnProperty(e)&&(t[e]=n[e])};return function(n,e){function r(){this.constructor=n}t(n,e),n.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}(),Array.prototype.findIndex=function(t){return function(t,n){for(var e=0,r=t.length;e-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,n){var e=t.findIndex(n);return-1==e?null:t[e]}(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(e,r,i){return n.call(arguments[2],r,i,t)&&e.push(r),e},[]);for(var e=[],r=0,i=t.length;r=0&&t.splice(e,1)}while(e>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,n){var e=t.findIndex(function(t){return t===n});return e>=0&&(t.splice(e,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,e){t.splice(n,e)}(this,t,n)},Array.prototype.select=function(t){return function(t,n){if("function"==typeof t.reduce)return t.reduce(function(e,r,i){return e.push(n.call(arguments[2],r,i,t)),e},[]);for(var e=[],r=0,i=t.length;ro?1:-1}),t}(this,t,n)},Array.prototype.orderByDescending=function(t,n){return function(t,n,e){return t.sort(function(t,r){var i=n(t),o=n(r);return e?-e(i,o):i=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function n(n){var e=t.call(this)||this;return e.entities=[],n.stage.addChild(e),e.camera=e.createEntity("camera").addComponent(new Camera),e._projectionMatrix=new Matrix2D(0,0,0,0,0,0),e.addEventListener(egret.Event.ACTIVATE,e.onActive,e),e.addEventListener(egret.Event.DEACTIVATE,e.onDeactive,e),e.addEventListener(egret.Event.ENTER_FRAME,e.update,e),e}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}(),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)},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 +class Camera extends Component { + private _zoom; + private _origin: Vector2; + private _transformMatrix: Matrix2D = Matrix2D.identity; + private _inverseTransformMatrix = Matrix2D.identity; - constructor(displayObject: egret.DisplayObject){ - this._displayContent = displayObject; + public get transformMatrix(){ + this.updateMatrixes(); + + return this._transformMatrix; } - public destory(){ - this._displayContent = null; + constructor() { + super(); + } + + public initialize() { + + } + + public update(){ + SceneManager.getActiveScene().entities.forEach(entity => entity.components.forEach(component => { + if (component.displayRender){ + let has = this.entity.scene.$children.indexOf(component.displayRender) + if (has == -1){ + this.entity.scene.stage.addChild(component.displayRender); + } + } + })); + } + + public setPosition(position: Vector2){ + this.entity.transform.setPosition(position); + + return this; + } + + public updateMatrixes(){ + this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y); + } + + public destory() { + } } \ No newline at end of file diff --git a/source/src/ECS/Entity.ts b/source/src/ECS/Entity.ts index 89c4d8a0..b68e1163 100644 --- a/source/src/ECS/Entity.ts +++ b/source/src/ECS/Entity.ts @@ -4,10 +4,33 @@ class Entity { public scene: Scene; /** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */ public readonly transform: Transform; + /** 当前附加到此实体的所有组件的列表 */ + public readonly components: Component[]; + private _updateOrder: number = 0; constructor(name: string){ this.name = name; this.transform = new Transform(this); + this.components = []; + } + + public get updateOrder(){ + return this._updateOrder; + } + + public set updateOrder(value: number){ + this.setUpdateOrder(value); + } + + public setUpdateOrder(updateOrder: number){ + if (this._updateOrder != updateOrder){ + this._updateOrder = updateOrder; + if (this.scene){ + + } + + return this; + } } public attachToScene(newScene: Scene){ @@ -19,6 +42,17 @@ class Entity { } } + public addComponent(component: T): T{ + component.entity = this; + this.components.push(component); + component.initialize(); + return component; + } + + public update(){ + this.components.forEach(component => component.update()); + } + public destory(){ this.scene.entities.remove(this); this.transform.parent = null; diff --git a/source/src/ECS/Scene.ts b/source/src/ECS/Scene.ts index ad0ec165..73522acc 100644 --- a/source/src/ECS/Scene.ts +++ b/source/src/ECS/Scene.ts @@ -3,17 +3,24 @@ class Scene extends egret.DisplayObjectContainer { public camera: Camera; public entities: Entity[] = []; + private _projectionMatrix: Matrix2D; + private _transformMatrix: Matrix2D; + private _matrixTransformMatrix: Matrix2D; + constructor(displayObject: egret.DisplayObject){ super(); + displayObject.stage.addChild(this); /** 初始化默认相机 */ - this.camera = new Camera(displayObject); + this.camera = this.createEntity("camera").addComponent(new Camera()); + this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0); 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); } public createEntity(name: string){ let entity = new Entity(name); - + entity.transform.position = new Vector2(0, 0); return this.addEntity(entity); } @@ -45,6 +52,18 @@ class Scene extends egret.DisplayObjectContainer { } + public update(){ + this.entities.forEach(entity => entity.update()); + } + + public prepRenderState(){ + 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); + } + public destory(){ this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this); this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this); diff --git a/source/src/ECS/SceneManager.ts b/source/src/ECS/SceneManager.ts index b6b3c0b7..6af4d2bd 100644 --- a/source/src/ECS/SceneManager.ts +++ b/source/src/ECS/SceneManager.ts @@ -33,4 +33,8 @@ class SceneManager { this._activeScene.initialize(); return scene; } + + public static getActiveScene(){ + return this._activeScene; + } } \ No newline at end of file diff --git a/source/src/ECS/Transform.ts b/source/src/ECS/Transform.ts index 3d90558f..190e5c4c 100644 --- a/source/src/ECS/Transform.ts +++ b/source/src/ECS/Transform.ts @@ -4,12 +4,31 @@ class Transform { private _children: Transform[]; private _parent: Transform; + private _localPosition: Vector2; + private _localRotation: number = 0; + private _localScale: Vector2; + + private _translationMatrix: Matrix2D; + private _rotationMatrix: Matrix2D; + private _scaleMatrix: Matrix2D; + + private _worldTransform = Matrix2D.identity; + private _worldToLocalTransform = Matrix2D.identity; + private _worldInverseTransform = Matrix2D.identity; + + private _rotation: number = 0; + private _position: Vector2; + private _scale: Vector2; + + private _localTransform; + public get childCount(){ return this._children.length; } constructor(entity: Entity){ this.entity = entity; + this._scale = this._localScale = Vector2.One; this._children = []; } @@ -39,4 +58,83 @@ class Transform { return this; } + + public get position(){ + this.updateTransform(); + if (!this.parent){ + this._position = this._localPosition; + }else{ + this.parent.updateTransform(); + this._position = Vector2.transform(this._localPosition, this.parent._worldTransform); + } + + return this._position; + } + + public set position(value: Vector2){ + this.setPosition(value); + } + + public get localPosition(){ + this.updateTransform(); + return this._localPosition; + } + + public set localPosition(value: Vector2){ + this.setLocalPosition(value); + } + + public setLocalPosition(localPosition: Vector2){ + if (localPosition == this._localPosition) + return this; + + this._localPosition = localPosition; + + return this; + } + + public setPosition(position: Vector2){ + if (position == this._position) + return this; + + this._position = position; + if (this.parent){ + this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform); + }else{ + this.localPosition = position; + } + + for (let i = 0; i < this.entity.components.length; i ++){ + let component = this.entity.components[i]; + if (component.displayRender){ + component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x; + component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y; + } + } + + return this; + } + + public updateTransform(){ + if (this.parent) + this.parent.updateTransform(); + + this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y); + this._rotationMatrix = Matrix2D.createRotation(this._localRotation); + this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y); + + this._localTransform = Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix); + this._localTransform = Matrix2D.multiply(this._localTransform, this._translationMatrix); + + if (!this.parent){ + this._worldTransform = this._localTransform; + this._rotation = this._localRotation; + this._scale = this._localScale; + }else{ + this._worldTransform = Matrix2D.multiply(this._localTransform, this.parent._worldTransform); + + this._rotation = this._localRotation + this.parent._rotation; + this._scale = Vector2.multiply( this.parent._scale, this._localScale); + } + } } \ No newline at end of file diff --git a/source/src/Math/MathHelper.ts b/source/src/Math/MathHelper.ts index bbcbcd17..b96e4f4d 100644 --- a/source/src/Math/MathHelper.ts +++ b/source/src/Math/MathHelper.ts @@ -3,7 +3,7 @@ class MathHelper { * 将弧度转换成角度。 * @param radians 用弧度表示的角 */ - public static ToDegrees(radians: number){ + public static toDegrees(radians: number){ return radians * 57.295779513082320876798154814105; } @@ -11,7 +11,7 @@ class MathHelper { * 将角度转换为弧度 * @param degrees */ - public static ToRadians(degrees: number){ + public static toRadians(degrees: number){ return degrees * 0.017453292519943295769236907684886; } } \ No newline at end of file diff --git a/source/src/Math/Matrix2D.ts b/source/src/Math/Matrix2D.ts index d4db15aa..64b71a7f 100644 --- a/source/src/Math/Matrix2D.ts +++ b/source/src/Math/Matrix2D.ts @@ -2,14 +2,14 @@ * 表示右手3 * 3的浮点矩阵,可以存储平移、缩放和旋转信息。 */ class Matrix2D { - public m11: number; - public m12: number; + public m11: number = 0; + public m12: number = 0; - public m21: number; - public m22: number; + public m21: number = 0; + public m22: number = 0; - public m31: number; - public m32: number; + public m31: number = 0; + public m32: number = 0; private static _identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0); @@ -60,11 +60,11 @@ class Matrix2D { * 以度为单位的旋转存储在这个矩阵中 */ public get rotationDegrees(){ - return MathHelper.ToDegrees(this.rotation); + return MathHelper.toDegrees(this.rotation); } public set rotationDegrees(value: number){ - this.rotation = MathHelper.ToRadians(value); + this.rotation = MathHelper.toRadians(value); } public get scale(){ @@ -127,4 +127,68 @@ class Matrix2D { matrix1.m32 = m32; return matrix1; } + + public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){ + let trans = Matrix2D.createTranslation(x, y); + return Matrix2D.multiply(matrix, trans); + } + + public determinant(){ + return this.m11 * this.m22 - this.m12 * this.m21; + } + + public static invert(matrix: Matrix2D, result: Matrix2D){ + let det = 1 / matrix.determinant(); + + result.m11 = matrix.m22 * det; + result.m12 = -matrix.m12 * det; + + result.m21 = -matrix.m21 * det; + result.m22 = matrix.m11 * det; + + result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det; + result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det; + + return result; + } + + public static createTranslation(xPosition: number, yPosition: number, result: Matrix2D = Matrix2D.identity){ + result.m11 = 1; + result.m12 = 0; + + result.m21 = 0; + result.m22 = 1; + + result.m31 = xPosition; + result.m32 = yPosition; + + return result; + } + + public static createRotation(radians: number, result?: Matrix2D){ + result = Matrix2D.identity; + + let val1 = Math.cos(radians); + let val2 = Math.sin(radians); + + result.m11 = val1; + result.m12 = val2; + result.m21 = -val2; + result.m22 = val1; + + return result; + } + + public static createScale(xScale: number, yScale: number, result: Matrix2D = Matrix2D.identity){ + result.m11 = xScale; + result.m12 = 0; + + result.m21 = 0; + result.m22 = yScale; + + result.m31 = 0; + result.m32 = 0; + + return result; + } } \ No newline at end of file diff --git a/source/src/Math/Vector2.ts b/source/src/Math/Vector2.ts index 1f1aebad..15ea5983 100644 --- a/source/src/Math/Vector2.ts +++ b/source/src/Math/Vector2.ts @@ -1,7 +1,13 @@ /** 2d 向量 */ class Vector2 { - public x: number; - public y: number; + public x: number = 0; + public y: number = 0; + + private static readonly unitVector2 = new Vector2(1, 1); + + public static get One(){ + return this.unitVector2; + } /** * 从两个值构造一个带有X和Y的二维向量。 @@ -43,4 +49,8 @@ class Vector2 { this.x *= val; this.y *= val; } + + public static transform(position: Vector2, matrix: Matrix2D){ + return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); + } } \ No newline at end of file