新增相机bounds与可渲染组件bounds 并添加可渲染组件的剔除方法
This commit is contained in:
@@ -8,6 +8,42 @@ class Camera extends Component {
|
||||
private _minimumZoom = 0.3;
|
||||
private _maximumZoom = 3;
|
||||
private _areMatrixesDirty = true;
|
||||
private _inset: CameraInset;
|
||||
private _bounds: Rectangle;
|
||||
private _areBoundsDirty = true;
|
||||
|
||||
public get bounds(){
|
||||
if (this._areMatrixesDirty)
|
||||
this.updateMatrixes();
|
||||
|
||||
if (this._areBoundsDirty){
|
||||
let stage = this.entity.scene.stage;
|
||||
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
|
||||
let bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom));
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
||||
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
|
||||
|
||||
let minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
let maxY = MathHelper.maxOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
|
||||
this._bounds.location = new Vector2(minX, minY);
|
||||
this._bounds.width = maxX - minX;
|
||||
this._bounds.height = maxY - minY;
|
||||
}else{
|
||||
this._bounds.location = topLeft;
|
||||
this._bounds.width = bottomRight.x - topLeft.x;
|
||||
this._bounds.height = bottomRight.y - topLeft.y;
|
||||
}
|
||||
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
public get zoom(){
|
||||
if (this._zoom == 0)
|
||||
@@ -51,11 +87,17 @@ class Camera extends Component {
|
||||
}
|
||||
|
||||
public get transformMatrix(){
|
||||
this.updateMatrixes();
|
||||
|
||||
if (this._areBoundsDirty)
|
||||
this.updateMatrixes();
|
||||
return this._transformMatrix;
|
||||
}
|
||||
|
||||
public get inverseTransformMatrix(){
|
||||
if (this._areBoundsDirty)
|
||||
this.updateMatrixes();
|
||||
return this._inverseTransformMatrix;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
@@ -123,10 +165,28 @@ class Camera extends Component {
|
||||
|
||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
||||
|
||||
this._areBoundsDirty = true;
|
||||
this._areMatrixesDirty = false;
|
||||
}
|
||||
|
||||
public screenToWorldPoint(screenPosition: Vector2){
|
||||
this.updateMatrixes();
|
||||
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
||||
}
|
||||
|
||||
public worldToScreenPoint(worldPosition: Vector2){
|
||||
this.updateMatrixes();
|
||||
return Vector2.transform(worldPosition, this._transformMatrix);
|
||||
}
|
||||
|
||||
public destory() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class CameraInset {
|
||||
public left;
|
||||
public right;
|
||||
public top;
|
||||
public bottom;
|
||||
}
|
||||
@@ -2,5 +2,48 @@
|
||||
* 所有可渲染组件的基类
|
||||
*/
|
||||
abstract class RenderableComponent extends Component {
|
||||
|
||||
private _isVisible: boolean;
|
||||
private _areBoundsDirty = true;
|
||||
private _bounds: Rectangle;
|
||||
private _localOffset: Vector2;
|
||||
|
||||
public get width(){
|
||||
return this.bounds.width;
|
||||
}
|
||||
|
||||
public get height(){
|
||||
return this.bounds.height;
|
||||
}
|
||||
|
||||
public get isVisible(){
|
||||
return this._isVisible;
|
||||
}
|
||||
|
||||
public set isVisible(value: boolean){
|
||||
this._isVisible = value;
|
||||
|
||||
if (this._isVisible)
|
||||
this.onBecameVisible();
|
||||
else
|
||||
this.onBecameInvisible();
|
||||
}
|
||||
|
||||
public get bounds(): Rectangle{
|
||||
if (this._areBoundsDirty){
|
||||
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, Vector2.Zero,
|
||||
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
protected onBecameVisible(){}
|
||||
|
||||
protected onBecameInvisible(){}
|
||||
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
||||
return this.isVisible;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user