2020-07-22 23:30:31 +08:00
|
|
|
|
module transform {
|
|
|
|
|
|
export enum Component {
|
|
|
|
|
|
position,
|
|
|
|
|
|
scale,
|
|
|
|
|
|
rotation,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
module es {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
export enum DirtyType {
|
|
|
|
|
|
clean,
|
|
|
|
|
|
positionDirty,
|
|
|
|
|
|
scaleDirty,
|
|
|
|
|
|
rotationDirty,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 16:05:06 +08:00
|
|
|
|
export class Transform {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/** 与此转换关联的实体 */
|
|
|
|
|
|
public readonly entity: Entity;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public hierarchyDirty: DirtyType;
|
|
|
|
|
|
public _localDirty: boolean;
|
|
|
|
|
|
public _localPositionDirty: boolean;
|
|
|
|
|
|
public _localScaleDirty: boolean;
|
|
|
|
|
|
public _localRotationDirty: boolean;
|
|
|
|
|
|
public _positionDirty: boolean;
|
|
|
|
|
|
public _worldToLocalDirty: boolean;
|
|
|
|
|
|
public _worldInverseDirty: boolean;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 值会根据位置、旋转和比例自动重新计算
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-12-03 17:58:25 +08:00
|
|
|
|
public _localTransform: Matrix2D;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 值将自动从本地和父矩阵重新计算。
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-11-23 16:05:06 +08:00
|
|
|
|
public _worldTransform = Matrix2D.identity;
|
|
|
|
|
|
public _rotationMatrix: Matrix2D = Matrix2D.identity;
|
|
|
|
|
|
public _translationMatrix: Matrix2D = Matrix2D.identity;
|
2020-12-03 17:58:25 +08:00
|
|
|
|
public _scaleMatrix: Matrix2D;
|
|
|
|
|
|
public _children: Transform[] = [];
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
constructor(entity: Entity) {
|
|
|
|
|
|
this.entity = entity;
|
2020-08-28 18:04:50 +08:00
|
|
|
|
this.scale = this._localScale = Vector2.one;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 这个转换的所有子元素
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get childCount() {
|
|
|
|
|
|
return this._children.length;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 变换在世界空间的旋转度
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get rotationDegrees(): number {
|
|
|
|
|
|
return MathHelper.toDegrees(this._rotation);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 变换在世界空间的旋转度
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set rotationDegrees(value: number) {
|
|
|
|
|
|
this.setRotation(MathHelper.toRadians(value));
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 旋转相对于父变换旋转的角度
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get localRotationDegrees(): number {
|
|
|
|
|
|
return MathHelper.toDegrees(this._localRotation);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 旋转相对于父变换旋转的角度
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set localRotationDegrees(value: number) {
|
|
|
|
|
|
this.localRotation = MathHelper.toRadians(value);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get localToWorldTransform(): Matrix2D {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.updateTransform();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
return this._worldTransform;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _parent: Transform;
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 获取此转换的父转换
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get parent() {
|
|
|
|
|
|
return this._parent;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 设置此转换的父转换
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set parent(value: Transform) {
|
|
|
|
|
|
this.setParent(value);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 16:05:06 +08:00
|
|
|
|
public _worldToLocalTransform = Matrix2D.identity;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public get worldToLocalTransform(): Matrix2D {
|
|
|
|
|
|
if (this._worldToLocalDirty) {
|
|
|
|
|
|
if (!this.parent) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
this._worldToLocalTransform = Matrix2D.identity;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
this.parent.updateTransform();
|
2020-11-23 16:05:06 +08:00
|
|
|
|
this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._worldToLocalDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this._worldToLocalTransform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 16:05:06 +08:00
|
|
|
|
public _worldInverseTransform = Matrix2D.identity;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
public get worldInverseTransform(): Matrix2D {
|
|
|
|
|
|
this.updateTransform();
|
|
|
|
|
|
if (this._worldInverseDirty) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
this._worldInverseTransform = Matrix2D.invert(this._worldTransform);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this._worldInverseDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this._worldInverseTransform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public _position: Vector2 = Vector2.zero;
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 变换在世界空间中的位置
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get position(): Vector2 {
|
|
|
|
|
|
this.updateTransform();
|
|
|
|
|
|
if (this._positionDirty) {
|
2020-12-03 17:58:25 +08:00
|
|
|
|
if (this.parent == null) {
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this._position = this._localPosition;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.parent.updateTransform();
|
2020-08-28 18:04:50 +08:00
|
|
|
|
Vector2Ext.transformR(this._localPosition, this.parent._worldTransform, this._position);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._positionDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this._position;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 变换在世界空间中的位置
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set position(value: Vector2) {
|
|
|
|
|
|
this.setPosition(value.x, value.y);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _scale: Vector2 = Vector2.one;
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 变换在世界空间的缩放
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get scale(): Vector2 {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.updateTransform();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
return this._scale;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 变换在世界空间的缩放
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set scale(value: Vector2) {
|
|
|
|
|
|
this.setScale(value);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _rotation: number = 0;
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 在世界空间中以弧度旋转的变换
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get rotation(): number {
|
|
|
|
|
|
this.updateTransform();
|
|
|
|
|
|
return this._rotation;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 变换在世界空间的旋转度
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set rotation(value: number) {
|
|
|
|
|
|
this.setRotation(value);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _localPosition: Vector2 = Vector2.zero;
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 转换相对于父转换的位置。如果转换没有父元素,则与transform.position相同
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get localPosition(): Vector2 {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.updateTransform();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
return this._localPosition;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 转换相对于父转换的位置。如果转换没有父元素,则与transform.position相同
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set localPosition(value: Vector2) {
|
|
|
|
|
|
this.setLocalPosition(value);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _localScale: Vector2 = Vector2.one;
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 转换相对于父元素的比例。如果转换没有父元素,则与transform.scale相同
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get localScale(): Vector2 {
|
|
|
|
|
|
this.updateTransform();
|
|
|
|
|
|
return this._localScale;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 转换相对于父元素的比例。如果转换没有父元素,则与transform.scale相同
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set localScale(value: Vector2) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.setLocalScale(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _localRotation: number = 0;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相对于父变换的旋转,变换的旋转。如果转换没有父元素,则与transform.rotation相同
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get localRotation(): number {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.updateTransform();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
return this._localRotation;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 相对于父变换的旋转,变换的旋转。如果转换没有父元素,则与transform.rotation相同
|
|
|
|
|
|
* @param value
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set localRotation(value: number) {
|
|
|
|
|
|
this.setLocalRotation(value);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回在索引处的转换子元素
|
|
|
|
|
|
* @param index
|
|
|
|
|
|
*/
|
|
|
|
|
|
public getChild(index: number): Transform {
|
|
|
|
|
|
return this._children[index];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置此转换的父转换
|
|
|
|
|
|
* @param parent
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setParent(parent: Transform): Transform {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
if (this._parent == parent)
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
|
|
if (!this._parent) {
|
2020-11-30 13:50:18 +08:00
|
|
|
|
let children = new linq.List(this._parent._children);
|
|
|
|
|
|
children.remove(this);
|
|
|
|
|
|
children.add(this);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._parent = parent;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.setDirty(DirtyType.positionDirty);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置转换在世界空间中的位置
|
|
|
|
|
|
* @param x
|
|
|
|
|
|
* @param y
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setPosition(x: number, y: number): Transform {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let position = new Vector2(x, y);
|
2020-07-27 16:10:36 +08:00
|
|
|
|
if (position.equals(this._position))
|
2020-07-24 15:29:07 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
|
|
this._position = position;
|
2020-12-03 17:58:25 +08:00
|
|
|
|
if (this.parent != null) {
|
2020-08-28 18:04:50 +08:00
|
|
|
|
this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
this.localPosition = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
this._positionDirty = false;
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置转换相对于父转换的位置。如果转换没有父元素,则与transform.position相同
|
|
|
|
|
|
* @param localPosition
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setLocalPosition(localPosition: Vector2): Transform {
|
2020-07-27 16:10:36 +08:00
|
|
|
|
if (localPosition.equals(this._localPosition))
|
2020-07-24 15:29:07 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
|
|
this._localPosition = localPosition;
|
|
|
|
|
|
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.setDirty(DirtyType.positionDirty);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置变换在世界空间的旋转度
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param radians
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-24 15:29:07 +08:00
|
|
|
|
public setRotation(radians: number): Transform {
|
|
|
|
|
|
this._rotation = radians;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this.parent) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.localRotation = this.parent.rotation + radians;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.localRotation = radians;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* 设置变换在世界空间的旋转度
|
|
|
|
|
|
* @param degrees
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-24 15:29:07 +08:00
|
|
|
|
public setRotationDegrees(degrees: number): Transform {
|
|
|
|
|
|
return this.setRotation(MathHelper.toRadians(degrees));
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 旋转精灵的顶部,使其朝向位置
|
|
|
|
|
|
* @param pos
|
|
|
|
|
|
*/
|
|
|
|
|
|
public lookAt(pos: Vector2) {
|
|
|
|
|
|
let sign = this.position.x > pos.x ? -1 : 1;
|
|
|
|
|
|
let vectorToAlignTo = Vector2.normalize(Vector2.subtract(this.position, pos));
|
|
|
|
|
|
this.rotation = sign * Math.acos(Vector2.dot(vectorToAlignTo, Vector2.unitY));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 相对于父变换的旋转设置变换的旋转。如果转换没有父元素,则与transform.rotation相同
|
|
|
|
|
|
* @param radians
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public setLocalRotation(radians: number) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._localRotation = radians;
|
|
|
|
|
|
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
|
|
|
|
|
|
this.setDirty(DirtyType.rotationDirty);
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 相对于父变换的旋转设置变换的旋转。如果转换没有父元素,则与transform.rotation相同
|
|
|
|
|
|
* @param degrees
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setLocalRotationDegrees(degrees: number): Transform {
|
|
|
|
|
|
return this.setLocalRotation(MathHelper.toRadians(degrees));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置变换在世界空间中的缩放
|
|
|
|
|
|
* @param scale
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setScale(scale: Vector2): Transform {
|
|
|
|
|
|
this._scale = scale;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this.parent) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.localScale = Vector2.divide(scale, this.parent._scale);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
} else {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.localScale = scale;
|
|
|
|
|
|
}
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置转换相对于父对象的比例。如果转换没有父元素,则与transform.scale相同
|
|
|
|
|
|
* @param scale
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setLocalScale(scale: Vector2): Transform {
|
|
|
|
|
|
this._localScale = scale;
|
|
|
|
|
|
this._localDirty = this._positionDirty = this._localScaleDirty = true;
|
|
|
|
|
|
this.setDirty(DirtyType.scaleDirty);
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 对精灵坐标进行四舍五入
|
|
|
|
|
|
*/
|
|
|
|
|
|
public roundPosition() {
|
2020-12-10 16:15:19 +08:00
|
|
|
|
this.position = Vector2Ext.round(this._position);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public updateTransform() {
|
|
|
|
|
|
if (this.hierarchyDirty != DirtyType.clean) {
|
2020-12-03 17:58:25 +08:00
|
|
|
|
if (this.parent != null)
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.parent.updateTransform();
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._localDirty) {
|
|
|
|
|
|
if (this._localPositionDirty) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._localPositionDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._localRotationDirty) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
this._rotationMatrix = Matrix2D.createRotation(this._localRotation);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._localRotationDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._localScaleDirty) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._localScaleDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._localTransform = this._scaleMatrix.multiply(this._rotationMatrix);
|
|
|
|
|
|
this._localTransform = this._localTransform.multiply(this._translationMatrix);
|
|
|
|
|
|
|
2020-12-03 17:58:25 +08:00
|
|
|
|
if (this.parent == null) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._worldTransform = this._localTransform;
|
|
|
|
|
|
this._rotation = this._localRotation;
|
|
|
|
|
|
this._scale = this._localScale;
|
|
|
|
|
|
this._worldInverseDirty = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._localDirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-03 17:58:25 +08:00
|
|
|
|
if (this.parent != null) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._worldTransform = this._localTransform.multiply(this.parent._worldTransform);
|
|
|
|
|
|
|
|
|
|
|
|
this._rotation = this._localRotation + this.parent._rotation;
|
|
|
|
|
|
this._scale = Vector2.multiply(this.parent._scale, this._localScale);
|
|
|
|
|
|
this._worldInverseDirty = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._worldToLocalDirty = true;
|
|
|
|
|
|
this._positionDirty = true;
|
|
|
|
|
|
this.hierarchyDirty = DirtyType.clean;
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public setDirty(dirtyFlagType: DirtyType) {
|
|
|
|
|
|
if ((this.hierarchyDirty & dirtyFlagType) == 0) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.hierarchyDirty |= dirtyFlagType;
|
|
|
|
|
|
|
|
|
|
|
|
switch (dirtyFlagType) {
|
|
|
|
|
|
case es.DirtyType.positionDirty:
|
|
|
|
|
|
this.entity.onTransformChanged(transform.Component.position);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case es.DirtyType.rotationDirty:
|
|
|
|
|
|
this.entity.onTransformChanged(transform.Component.rotation);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case es.DirtyType.scaleDirty:
|
|
|
|
|
|
this.entity.onTransformChanged(transform.Component.scale);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 告诉子项发生了变换
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._children.length; i++)
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._children[i].setDirty(dirtyFlagType);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从另一个transform属性进行拷贝
|
|
|
|
|
|
* @param transform
|
|
|
|
|
|
*/
|
|
|
|
|
|
public copyFrom(transform: Transform) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._position = transform.position;
|
|
|
|
|
|
this._localPosition = transform._localPosition;
|
|
|
|
|
|
this._rotation = transform._rotation;
|
|
|
|
|
|
this._localRotation = transform._localRotation;
|
|
|
|
|
|
this._scale = transform._scale;
|
|
|
|
|
|
this._localScale = transform._localScale;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.setDirty(DirtyType.positionDirty);
|
|
|
|
|
|
this.setDirty(DirtyType.rotationDirty);
|
|
|
|
|
|
this.setDirty(DirtyType.scaleDirty);
|
|
|
|
|
|
}
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public toString(): string {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
return `[Transform: parent: ${this.parent}, position: ${this.position}, rotation: ${this.rotation},
|
|
|
|
|
|
scale: ${this.scale}, localPosition: ${this._localPosition}, localRotation: ${this._localRotation},
|
|
|
|
|
|
localScale: ${this._localScale}]`;
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|