新增renderable组件render方法 去除mathhelper.minof/maxof
This commit is contained in:
@@ -25,10 +25,10 @@ class Camera extends Component {
|
||||
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);
|
||||
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
|
||||
this._bounds.location = new Vector2(minX, minY);
|
||||
this._bounds.width = maxX - minX;
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
*/
|
||||
abstract class RenderableComponent extends Component {
|
||||
private _isVisible: boolean;
|
||||
private _areBoundsDirty = true;
|
||||
private _bounds: Rectangle;
|
||||
private _localOffset: Vector2;
|
||||
protected _areBoundsDirty = true;
|
||||
protected _bounds: Rectangle;
|
||||
protected _localOffset: Vector2;
|
||||
|
||||
public get width(){
|
||||
return this.getWidth();
|
||||
@@ -54,6 +54,8 @@ abstract class RenderableComponent extends Component {
|
||||
|
||||
protected onBecameInvisible(){}
|
||||
|
||||
public abstract render(camera: Camera);
|
||||
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
||||
return this.isVisible;
|
||||
|
||||
@@ -2,6 +2,19 @@ class SpriteRenderer extends RenderableComponent {
|
||||
private _sprite: egret.DisplayObject;
|
||||
private _origin: Vector2;
|
||||
|
||||
public get bounds(){
|
||||
if (this._areBoundsDirty){
|
||||
if (this._sprite){
|
||||
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin,
|
||||
this.entity.transform.scale, this.entity.transform.rotation, this._sprite.width,
|
||||
this._sprite.height);
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this.bounds;
|
||||
}
|
||||
}
|
||||
|
||||
public get sprite(){
|
||||
return this._sprite;
|
||||
}
|
||||
@@ -18,7 +31,16 @@ class SpriteRenderer extends RenderableComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public initialize() {
|
||||
|
||||
public render(camera: Camera) {
|
||||
if (!this.sprite)
|
||||
return;
|
||||
|
||||
this.sprite.x = this.entity.transform.position.x;
|
||||
this.sprite.y = this.entity.transform.position.y;
|
||||
this.sprite.rotation = this.entity.transform.rotation;
|
||||
this.sprite.anchorOffsetX = this._origin.x;
|
||||
this.sprite.anchorOffsetY = this._origin.y;
|
||||
this.sprite.scaleX = this.entity.transform.scale.x;
|
||||
this.sprite.scaleY = this.entity.transform.scale.y;
|
||||
}
|
||||
}
|
||||
@@ -45,14 +45,6 @@ class MathHelper {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static minOf(a: number, b: number, c: number, d: number){
|
||||
return Math.min(a, Math.min(b, Math.min(c, d)));
|
||||
}
|
||||
|
||||
public static maxOf(a: number, b: number, c: number, d: number){
|
||||
return Math.max(a, Math.max(b, Math.max(c, d)));
|
||||
}
|
||||
|
||||
public static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number){
|
||||
let radians = MathHelper.toRadians(angleInDegrees);
|
||||
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
||||
|
||||
@@ -73,7 +73,7 @@ class Rectangle {
|
||||
let dt = res.y - this.top;
|
||||
let db = this.bottom - res.y;
|
||||
|
||||
let min = MathHelper.minOf(dl, dr, dt, db);
|
||||
let min = Math.min(dl, dr, dt, db);
|
||||
if (min == dt){
|
||||
res.y = this.top;
|
||||
edgeNormal.y = -1;
|
||||
@@ -134,10 +134,10 @@ class Rectangle {
|
||||
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
||||
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
||||
|
||||
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);
|
||||
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
|
||||
this.location = new Vector2(minX, minY);
|
||||
this.width = maxX - minX;
|
||||
|
||||
@@ -33,6 +33,11 @@ class Vector2 {
|
||||
this.y = y ? y : this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static add(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x + value2.x;
|
||||
@@ -40,6 +45,11 @@ class Vector2 {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static divide(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x / value2.x;
|
||||
@@ -47,6 +57,11 @@ class Vector2 {
|
||||
return value1;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static multiply(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x * value2.x;
|
||||
@@ -54,6 +69,11 @@ class Vector2 {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static subtract(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x - value2.x;
|
||||
@@ -68,10 +88,16 @@ class Vector2 {
|
||||
this.y *= val;
|
||||
}
|
||||
|
||||
/** 返回它的长度 */
|
||||
public length(){
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的Vector2
|
||||
* 它包含来自另一个向量的标准化值。
|
||||
* @param value
|
||||
*/
|
||||
public static normalize(value: Vector2){
|
||||
let val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||
value.x *= val;
|
||||
@@ -98,19 +124,39 @@ class Vector2 {
|
||||
return (v1 * v1) + (v2 * v2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含指定向量的线性插值
|
||||
* @param value1 第一个向量
|
||||
* @param value2 第二个向量
|
||||
* @param amount 权重值(0.0到1.0之间)
|
||||
*/
|
||||
public static lerp(value1: Vector2, value2: Vector2, amount: number){
|
||||
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param position
|
||||
* @param matrix
|
||||
*/
|
||||
public static transform(position: Vector2, matrix: Matrix2D){
|
||||
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量之间的距离
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static distance(value1: Vector2, value2: Vector2){
|
||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 矢量反演的结果
|
||||
* @param value
|
||||
*/
|
||||
public static negate(value: Vector2){
|
||||
let result: Vector2 = new Vector2();
|
||||
result.x = -value.x;
|
||||
|
||||
Reference in New Issue
Block a user