更新相机强制刷新矩阵

This commit is contained in:
YHH
2020-06-19 00:38:37 +08:00
parent e83bb087ea
commit 09e6ace142
19 changed files with 226 additions and 128 deletions

View File

@@ -2,9 +2,9 @@
class Camera extends Component {
private _zoom;
private _origin: Vector2 = Vector2.zero;
private _transformMatrix: Matrix2D = Matrix2D.identity;
private _inverseTransformMatrix = Matrix2D.identity;
private _projectionMatrix = Matrix2D.identity;
private _transformMatrix: Matrix2D = new Matrix2D();
private _inverseTransformMatrix = new Matrix2D();
private _projectionMatrix = new Matrix2D();
private _minimumZoom = 0.3;
private _maximumZoom = 3;
@@ -145,20 +145,16 @@ class Camera extends Component {
return this;
}
public initialize() {
}
public update(){
}
public setPosition(position: Vector2){
this.entity.transform.setPosition(position);
return this;
}
public forceMatrixUpdate(){
this._areMatrixesDirty = true;
}
public updateMatrixes(){
if (!this._areMatrixesDirty)
return;
@@ -170,6 +166,11 @@ class Camera extends Component {
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
}
if (this.entity.transform.rotation != 0){
tempMat = Matrix2D.createRotation(this.entity.rotation);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
}
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
@@ -181,12 +182,16 @@ class Camera extends Component {
public screenToWorldPoint(screenPosition: Vector2){
this.updateMatrixes();
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
}
public worldToScreenPoint(worldPosition: Vector2){
this.updateMatrixes();
return Vector2.transform(worldPosition, this._transformMatrix);
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
}
public onEntityTransformChanged(comp: ComponentTransform){
this._areMatrixesDirty = true;
}
public destory() {

View File

@@ -49,8 +49,8 @@ class SpriteRenderer extends RenderableComponent {
if (!this.sprite)
return;
this.sprite.x = this.entity.transform.position.x;
this.sprite.y = this.entity.transform.position.y;
this.sprite.x = this.entity.transform.position.x - camera.transform.position.x;
this.sprite.y = this.entity.transform.position.y - camera.transform.position.y;
this.sprite.rotation = this.entity.transform.rotation;
this.sprite.anchorOffsetX = this._origin.x;
this.sprite.anchorOffsetY = this._origin.y;