新增entity/component等transform便捷方法
This commit is contained in:
49
source/bin/framework.d.ts
vendored
49
source/bin/framework.d.ts
vendored
@@ -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<T extends Component>(component: T): T;
|
||||
hasComponent<T extends Component>(type: any): boolean;
|
||||
getOrCreateComponent<T extends Component>(type: T): T;
|
||||
getComponent<T extends Component>(type: any): T;
|
||||
removeComponentForType<T extends Component>(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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<T extends Component>(type){
|
||||
return this.components.getComponent<T>(type, false) != null;
|
||||
}
|
||||
|
||||
public getOrCreateComponent<T extends Component>(type: T){
|
||||
let comp = this.components.getComponent<T>(type, true);
|
||||
if (!comp){
|
||||
comp = this.addComponent<T>(type);
|
||||
}
|
||||
|
||||
return comp;
|
||||
}
|
||||
|
||||
public getComponent<T extends Component>(type): T{
|
||||
return this.components.getComponent(type, false) as T;
|
||||
}
|
||||
|
||||
public removeComponentForType<T extends Component>(type){
|
||||
let comp = this.getComponent<T>(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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -9,6 +9,10 @@ class ComponentList {
|
||||
this._entity = entity;
|
||||
}
|
||||
|
||||
public get count(){
|
||||
return this._components.length;
|
||||
}
|
||||
|
||||
public get buffer(){
|
||||
return this._components;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user