Files
esengine/source/src/ECS/Transform.ts

179 lines
4.9 KiB
TypeScript
Raw Normal View History

module transform {
export enum Component {
position,
scale,
rotation,
}
}
2020-07-22 20:07:14 +08:00
module es {
export enum DirtyType {
clean,
positionDirty,
scaleDirty,
rotationDirty,
}
2020-07-22 20:07:14 +08:00
export class Transform {
/** 与此转换关联的实体 */
public readonly entity: Entity;
/**
*
*/
public get parent() {
return this._parent;
}
/**
*
* @param value
*/
public set parent(value: Transform) {
this.setParent(value);
}
/**
*
*/
public get childCount() {
return this._children.length;
}
/**
*
*/
public position: Vector2;
/**
*
*/
public rotation: number;
/**
*
*/
public scale: Vector2;
public _parent: Transform;
public hierarchyDirty: DirtyType;
2020-07-22 20:07:14 +08:00
public _children: Transform[];
constructor(entity: Entity) {
this.entity = entity;
this.scale = Vector2.one;
this._children = [];
}
/**
*
* @param index
*/
public getChild(index: number): Transform {
return this._children[index];
}
/**
*
* @param parent
*/
public setParent(parent: Transform): Transform {
if (this._parent == parent)
return this;
if (!this._parent) {
this._parent._children.remove(this);
this._parent._children.push(this);
}
this._parent = parent;
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 {
this.position = new Vector2(x, y);
this.setDirty(DirtyType.positionDirty);
2020-07-22 20:07:14 +08:00
return this;
}
/**
*
* @param degrees
*/
public setRotation(degrees: number): Transform {
this.rotation = degrees;
this.setDirty(DirtyType.rotationDirty);
2020-07-22 20:07:14 +08:00
return this;
}
/**
*
* @param scale
*/
public setScale(scale: Vector2): Transform {
this.scale = scale;
this.setDirty(DirtyType.scaleDirty);
2020-07-22 20:07:14 +08:00
return this;
}
/**
* 使
* @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));
}
/**
*
*/
public roundPosition() {
this.position = this.position.round();
}
public setDirty(dirtyFlagType: DirtyType){
if ((this.hierarchyDirty & dirtyFlagType) == 0){
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;
}
if (!this._children)
this._children = [];
// 告诉子项发生了变换
for (let i = 0; i < this._children.length; i ++)
this._children[i].setDirty(dirtyFlagType);
}
}
2020-07-22 20:07:14 +08:00
/**
* transform属性进行拷贝
* @param transform
*/
public copyFrom(transform: Transform) {
this.position = transform.position;
this.rotation = transform.rotation;
this.scale = transform.scale;
this.setDirty(DirtyType.positionDirty);
this.setDirty(DirtyType.rotationDirty);
this.setDirty(DirtyType.scaleDirty);
}
2020-07-22 20:07:14 +08:00
}
}