移除 transform

This commit is contained in:
yhh
2020-07-01 16:55:10 +08:00
parent 7f02272304
commit f36a1cdb27
18 changed files with 9509 additions and 22635 deletions

View File

@@ -45,10 +45,6 @@ abstract class Component extends egret.DisplayObjectContainer {
}
public onEntityTransformChanged(comp: ComponentTransform){
}
public update(){
}

View File

@@ -1,57 +1,32 @@
class Mesh extends Component {
private _verts: VertexPosition[];
private _primitiveCount: number;
private _triangles: number[];
private _topLeftVertPosition: Vector2;
private _width;
private _height;
///<reference path="./RenderableComponent.ts" />
class Mesh extends RenderableComponent {
private _mesh: egret.Mesh;
public initialize() {
constructor(){
super();
this._mesh = new egret.Mesh();
}
public setVertPosition(positions: Vector2[]){
let createVerts = !this._verts || this._verts.length != positions.length;
if (createVerts)
this._verts = new Array(positions.length);
for (let i = 0; i < this._verts.length; i ++){
this._verts[i] = new VertexPosition(positions[i]);
}
public setTexture(texture: egret.Texture): Mesh{
this._mesh.texture = texture;
return this;
}
public setTriangles(triangles: number[]){
this._primitiveCount = triangles.length / 3;
this._triangles = triangles;
return this;
public onAddedToEntity(){
this.addChild(this._mesh);
}
public recalculateBounds(){
this._topLeftVertPosition = new Vector2(Number.MAX_VALUE, Number.MAX_VALUE);
let max = new Vector2(Number.MIN_VALUE, Number.MIN_VALUE);
for (let i = 0; i < this._verts.length; i ++){
this._topLeftVertPosition.x = Math.min(this._topLeftVertPosition.x, this._verts[i].position.x);
this._topLeftVertPosition.y = Math.min(this._topLeftVertPosition.y, this._verts[i].position.y);
max.x = Math.max(max.x, this._verts[i].position.x);
max.y = Math.max(max.y, this._verts[i].position.y);
}
this._width = max.x - this._topLeftVertPosition.x;
this._height = max.y - this._topLeftVertPosition.y;
return this;
public onRemovedFromEntity(){
this.removeChild(this._mesh);
}
public render(){
public render(camera: Camera){
this.x = this.entity.position.x - camera.position.x + camera.origin.x;
this.y = this.entity.position.y - camera.position.y + camera.origin.y;
}
}
class VertexPosition{
public position: Vector2;
constructor(position: Vector2){
this.position = position;
public reset() {
}
}

View File

@@ -104,23 +104,6 @@ abstract class Collider extends Component{
this._isParentEntityAddedToScene = false;
}
public onEntityTransformChanged(comp: ComponentTransform){
switch (comp){
case ComponentTransform.position:
this._isPositionDirty = true;
break;
case ComponentTransform.scale:
this._isPositionDirty = true;
break;
case ComponentTransform.rotation:
this._isRotationDirty = true;
break;
}
if (this._isColliderRegistered)
Physics.updateCollider(this);
}
public onEnabled(){
this.registerColliderWithPhysicsSystem();
this._isPositionDirty = this._isRotationDirty = true;

View File

@@ -1,12 +0,0 @@
class PolygonMesh extends Mesh {
constructor(points: Vector2[], arePointsCCW: boolean = true){
super();
let triangulator = new Triangulator();
triangulator.triangulate(points, arePointsCCW);
this.setVertPosition(points);
this.setTriangles(triangulator.triangleIndices);
this.recalculateBounds();
}
}

View File

@@ -1,3 +1,4 @@
///<reference path="./PooledComponent.ts" />
/**
* 所有可渲染组件的基类
*/
@@ -52,8 +53,4 @@ abstract class RenderableComponent extends PooledComponent implements IRenderabl
this.isVisible = camera.getBounds().intersects(this.getBounds());
return this.isVisible;
}
public onEntityTransformChanged(comp: ComponentTransform){
this._areBoundsDirty = true;
}
}

View File

@@ -1,5 +0,0 @@
class ScreenSpaceCamera extends Camera {
protected updateMatrixes(){
}
}

View File

@@ -193,10 +193,6 @@ class Entity extends egret.DisplayObjectContainer {
this.components.removeAllComponents();
}
public onTransformChanged(comp: ComponentTransform){
this.components.onEntityTransformChanged(comp);
}
public destroy(){
this._isDestoryed = true;
this.scene.entities.remove(this);

View File

@@ -1,345 +0,0 @@
enum DirtyType{
clean,
positionDirty,
scaleDirty,
rotationDirty,
}
enum ComponentTransform{
position,
scale,
rotation
}
class Transform {
/** 相关联的实体 */
public readonly entity: Entity;
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;
private _hierachyDirty: DirtyType;
private _localDirty: boolean;
private _localPositionDirty: boolean;
private _localScaleDirty: boolean;
private _localRotationDirty: boolean;
private _positionDirty: boolean;
private _worldToLocalDirty: boolean;
private _worldInverseDirty: boolean;
public get childCount(){
return this._children.length;
}
constructor(entity: Entity){
this.entity = entity;
this._scale = this._localScale = Vector2.one;
this._children = [];
}
public getChild(index: number){
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 = new Matrix2D();
} 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;
}
public set parent(value: Transform){
this.setParent(value);
}
public setParent(parent: Transform){
if (this._parent == parent)
return this;
if (this._parent)
this._parent._children.remove(this);
if (parent)
parent._children.push(this);
this._parent = parent;
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._positionDirty){
if (!this.parent){
this._position = this._localPosition;
}else{
this.parent.updateTransform();
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
}
this._positionDirty = false;
}
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 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;
}
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;
}
return this;
}
public setRotationDegrees(degrees: number){
return this.setRotation(MathHelper.toRadians(degrees));
}
public setLocalPosition(localPosition: Vector2){
if (localPosition == this._localPosition)
return this;
this._localPosition = localPosition;
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
this.setDirty(DirtyType.positionDirty);
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;
}
this._positionDirty = false;
return this;
}
public setDirty(dirtyFlagType: DirtyType){
if ((this._hierachyDirty & dirtyFlagType) == 0){
this._hierachyDirty |= dirtyFlagType;
switch (dirtyFlagType){
case DirtyType.positionDirty:
this.entity.onTransformChanged(ComponentTransform.position);
break;
case DirtyType.rotationDirty:
this.entity.onTransformChanged(ComponentTransform.rotation);
break;
case DirtyType.scaleDirty:
this.entity.onTransformChanged(ComponentTransform.scale);
break;
}
if (this._children == null)
this._children = [];
for (let i = 0; i < this._children.length; i ++){
this._children[i].setDirty(dirtyFlagType);
}
}
}
public roundPosition(){
this.position = this._position.round();
}
public updateTransform(){
if (this._hierachyDirty != DirtyType.clean){
if (this.parent)
this.parent.updateTransform();
if (this._localDirty){
if (this._localPositionDirty){
this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y);
this._localPositionDirty = false;
}
if (this._localRotationDirty){
this._rotationMatrix = Matrix2D.createRotation(this._localRotation);
this._localRotationDirty = false;
}
if (this._localScaleDirty){
this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y);
this._localScaleDirty = false;
}
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;
this._worldInverseDirty = true;
}
this._localDirty = false;
}
if (this.parent){
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);
this._worldInverseDirty = true;
}
this._worldToLocalDirty = true;
this._positionDirty = true;
this._hierachyDirty = DirtyType.clean;
}
}
}

View File

@@ -171,16 +171,4 @@ class ComponentList {
component.update();
}
}
public onEntityTransformChanged(comp){
for (let i = 0; i < this._components.length; i++){
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
}
for (let i = 0; i < this._componentsToAdd.length; i ++){
if (this._componentsToAdd[i].enabled)
this._componentsToAdd[i].onEntityTransformChanged(comp);
}
}
}

View File

@@ -1,3 +0,0 @@
abstract class GraphicsResource {
}

View File

@@ -1,27 +0,0 @@
class BloomSettings {
public readonly threshold;
public readonly blurAmount;
public readonly intensity;
public readonly baseIntensity;
public readonly saturation;
public readonly baseStaturation;
constructor(bloomThreshold: number, blurAmount: number, bloomIntensity: number,baseIntensity: number,
bloomSaturation: number, baseSaturation: number){
this.threshold = bloomThreshold;
this.blurAmount = blurAmount;
this.intensity = bloomIntensity;
this.baseIntensity = baseIntensity;
this.saturation = bloomSaturation;
this.baseStaturation = baseSaturation;
}
public static presetSettings: BloomSettings[] = [
new BloomSettings(0.1, 0.6, 2, 1, 1, 0),
new BloomSettings(0, 3, 1, 1, 1, 1),
new BloomSettings(0.5, 8, 2, 1, 0, 1),
new BloomSettings(0.25, 8, 1.3, 1, 1, 0),
new BloomSettings(0, 2, 1, 0.1, 1, 1),
new BloomSettings(0.5, 2, 1, 1, 1, 1)
];
}