新增entity/component等transform便捷方法

This commit is contained in:
yhh
2020-06-09 11:09:26 +08:00
parent 262e16bb88
commit 9e0d14da7c
13 changed files with 1308 additions and 5 deletions

View File

@@ -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;

View File

@@ -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() {

View File

@@ -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();

View File

@@ -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;

View File

@@ -9,6 +9,10 @@ class ComponentList {
this._entity = entity;
}
public get count(){
return this._components.length;
}
public get buffer(){
return this._components;
}

View File

@@ -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;
}
}

View File

@@ -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;