新增renderable组件render方法 去除mathhelper.minof/maxof

This commit is contained in:
yhh
2020-06-18 10:49:32 +08:00
parent 9c293979a4
commit 231276e39b
12 changed files with 162 additions and 68 deletions

View File

@@ -357,9 +357,9 @@ declare class PolygonMesh extends Mesh {
}
declare abstract class RenderableComponent extends Component {
private _isVisible;
private _areBoundsDirty;
private _bounds;
private _localOffset;
protected _areBoundsDirty: boolean;
protected _bounds: Rectangle;
protected _localOffset: Vector2;
readonly width: number;
readonly height: number;
isVisible: boolean;
@@ -369,14 +369,16 @@ declare abstract class RenderableComponent extends Component {
protected getBounds(): Rectangle;
protected onBecameVisible(): void;
protected onBecameInvisible(): void;
abstract render(camera: Camera): any;
isVisibleFromCamera(camera: Camera): boolean;
}
declare class SpriteRenderer extends RenderableComponent {
private _sprite;
private _origin;
readonly bounds: any;
sprite: egret.DisplayObject;
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
initialize(): void;
render(camera: Camera): void;
}
interface ITriggerListener {
onTriggerEnter(other: Collider, local: Collider): any;
@@ -561,8 +563,6 @@ declare class MathHelper {
static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
static lerp(value1: number, value2: number, amount: number): number;
static clamp(value: number, min: number, max: number): number;
static minOf(a: number, b: number, c: number, d: number): number;
static maxOf(a: number, b: number, c: number, d: number): number;
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
}
declare class Matrix2D {

View File

@@ -1492,10 +1492,10 @@ var Camera = (function (_super) {
if (this.entity.transform.rotation != 0) {
var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
var bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
var minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = MathHelper.maxOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = Math.max(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;
@@ -1769,6 +1769,19 @@ var SpriteRenderer = (function (_super) {
function SpriteRenderer() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(SpriteRenderer.prototype, "bounds", {
get: function () {
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;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(SpriteRenderer.prototype, "sprite", {
get: function () {
return this._sprite;
@@ -1785,7 +1798,16 @@ var SpriteRenderer = (function (_super) {
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
return this;
};
SpriteRenderer.prototype.initialize = function () {
SpriteRenderer.prototype.render = function (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;
};
return SpriteRenderer;
}(RenderableComponent));
@@ -2636,12 +2658,6 @@ var MathHelper = (function () {
return max;
return value;
};
MathHelper.minOf = function (a, b, c, d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
};
MathHelper.maxOf = function (a, b, c, d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
};
MathHelper.pointOnCirlce = function (circleCenter, radius, angleInDegrees) {
var radians = MathHelper.toRadians(angleInDegrees);
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
@@ -2881,7 +2897,7 @@ var Rectangle = (function () {
var dr = this.right - res.x;
var dt = res.y - this.top;
var db = this.bottom - res.y;
var min = MathHelper.minOf(dl, dr, dt, db);
var min = Math.min(dl, dr, dt, db);
if (min == dt) {
res.y = this.top;
edgeNormal.y = -1;
@@ -2940,10 +2956,10 @@ var Rectangle = (function () {
topRight = Vector2.transform(topRight, this._transformMat);
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
bottomRight = Vector2.transform(bottomRight, this._transformMat);
var minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = MathHelper.maxOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this.location = new Vector2(minX, minY);
this.width = maxX - minX;
this.height = maxY - minY;

File diff suppressed because one or more lines are too long

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;