新增renderable组件render方法 去除mathhelper.minof/maxof
This commit is contained in:
12
demo/libs/framework/framework.d.ts
vendored
12
demo/libs/framework/framework.d.ts
vendored
@@ -357,9 +357,9 @@ declare class PolygonMesh extends Mesh {
|
|||||||
}
|
}
|
||||||
declare abstract class RenderableComponent extends Component {
|
declare abstract class RenderableComponent extends Component {
|
||||||
private _isVisible;
|
private _isVisible;
|
||||||
private _areBoundsDirty;
|
protected _areBoundsDirty: boolean;
|
||||||
private _bounds;
|
protected _bounds: Rectangle;
|
||||||
private _localOffset;
|
protected _localOffset: Vector2;
|
||||||
readonly width: number;
|
readonly width: number;
|
||||||
readonly height: number;
|
readonly height: number;
|
||||||
isVisible: boolean;
|
isVisible: boolean;
|
||||||
@@ -369,14 +369,16 @@ declare abstract class RenderableComponent extends Component {
|
|||||||
protected getBounds(): Rectangle;
|
protected getBounds(): Rectangle;
|
||||||
protected onBecameVisible(): void;
|
protected onBecameVisible(): void;
|
||||||
protected onBecameInvisible(): void;
|
protected onBecameInvisible(): void;
|
||||||
|
abstract render(camera: Camera): any;
|
||||||
isVisibleFromCamera(camera: Camera): boolean;
|
isVisibleFromCamera(camera: Camera): boolean;
|
||||||
}
|
}
|
||||||
declare class SpriteRenderer extends RenderableComponent {
|
declare class SpriteRenderer extends RenderableComponent {
|
||||||
private _sprite;
|
private _sprite;
|
||||||
private _origin;
|
private _origin;
|
||||||
|
readonly bounds: any;
|
||||||
sprite: egret.DisplayObject;
|
sprite: egret.DisplayObject;
|
||||||
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
||||||
initialize(): void;
|
render(camera: Camera): void;
|
||||||
}
|
}
|
||||||
interface ITriggerListener {
|
interface ITriggerListener {
|
||||||
onTriggerEnter(other: Collider, local: Collider): any;
|
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 map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
|
||||||
static lerp(value1: number, value2: number, amount: number): number;
|
static lerp(value1: number, value2: number, amount: number): number;
|
||||||
static clamp(value: number, min: number, max: 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;
|
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
|
||||||
}
|
}
|
||||||
declare class Matrix2D {
|
declare class Matrix2D {
|
||||||
|
|||||||
@@ -1492,10 +1492,10 @@ var Camera = (function (_super) {
|
|||||||
if (this.entity.transform.rotation != 0) {
|
if (this.entity.transform.rotation != 0) {
|
||||||
var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
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 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 minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||||
var maxY = MathHelper.maxOf(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.location = new Vector2(minX, minY);
|
||||||
this._bounds.width = maxX - minX;
|
this._bounds.width = maxX - minX;
|
||||||
this._bounds.height = maxY - minY;
|
this._bounds.height = maxY - minY;
|
||||||
@@ -1769,6 +1769,19 @@ var SpriteRenderer = (function (_super) {
|
|||||||
function SpriteRenderer() {
|
function SpriteRenderer() {
|
||||||
return _super !== null && _super.apply(this, arguments) || this;
|
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", {
|
Object.defineProperty(SpriteRenderer.prototype, "sprite", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._sprite;
|
return this._sprite;
|
||||||
@@ -1785,7 +1798,16 @@ var SpriteRenderer = (function (_super) {
|
|||||||
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
||||||
return this;
|
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;
|
return SpriteRenderer;
|
||||||
}(RenderableComponent));
|
}(RenderableComponent));
|
||||||
@@ -2636,12 +2658,6 @@ var MathHelper = (function () {
|
|||||||
return max;
|
return max;
|
||||||
return value;
|
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) {
|
MathHelper.pointOnCirlce = function (circleCenter, radius, angleInDegrees) {
|
||||||
var radians = MathHelper.toRadians(angleInDegrees);
|
var radians = MathHelper.toRadians(angleInDegrees);
|
||||||
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
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 dr = this.right - res.x;
|
||||||
var dt = res.y - this.top;
|
var dt = res.y - this.top;
|
||||||
var db = this.bottom - res.y;
|
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) {
|
if (min == dt) {
|
||||||
res.y = this.top;
|
res.y = this.top;
|
||||||
edgeNormal.y = -1;
|
edgeNormal.y = -1;
|
||||||
@@ -2940,10 +2956,10 @@ var Rectangle = (function () {
|
|||||||
topRight = Vector2.transform(topRight, this._transformMat);
|
topRight = Vector2.transform(topRight, this._transformMat);
|
||||||
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
||||||
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
||||||
var minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||||
var maxY = MathHelper.maxOf(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.location = new Vector2(minX, minY);
|
||||||
this.width = maxX - minX;
|
this.width = maxX - minX;
|
||||||
this.height = maxY - minY;
|
this.height = maxY - minY;
|
||||||
|
|||||||
2
demo/libs/framework/framework.min.js
vendored
2
demo/libs/framework/framework.min.js
vendored
File diff suppressed because one or more lines are too long
12
source/bin/framework.d.ts
vendored
12
source/bin/framework.d.ts
vendored
@@ -357,9 +357,9 @@ declare class PolygonMesh extends Mesh {
|
|||||||
}
|
}
|
||||||
declare abstract class RenderableComponent extends Component {
|
declare abstract class RenderableComponent extends Component {
|
||||||
private _isVisible;
|
private _isVisible;
|
||||||
private _areBoundsDirty;
|
protected _areBoundsDirty: boolean;
|
||||||
private _bounds;
|
protected _bounds: Rectangle;
|
||||||
private _localOffset;
|
protected _localOffset: Vector2;
|
||||||
readonly width: number;
|
readonly width: number;
|
||||||
readonly height: number;
|
readonly height: number;
|
||||||
isVisible: boolean;
|
isVisible: boolean;
|
||||||
@@ -369,14 +369,16 @@ declare abstract class RenderableComponent extends Component {
|
|||||||
protected getBounds(): Rectangle;
|
protected getBounds(): Rectangle;
|
||||||
protected onBecameVisible(): void;
|
protected onBecameVisible(): void;
|
||||||
protected onBecameInvisible(): void;
|
protected onBecameInvisible(): void;
|
||||||
|
abstract render(camera: Camera): any;
|
||||||
isVisibleFromCamera(camera: Camera): boolean;
|
isVisibleFromCamera(camera: Camera): boolean;
|
||||||
}
|
}
|
||||||
declare class SpriteRenderer extends RenderableComponent {
|
declare class SpriteRenderer extends RenderableComponent {
|
||||||
private _sprite;
|
private _sprite;
|
||||||
private _origin;
|
private _origin;
|
||||||
|
readonly bounds: any;
|
||||||
sprite: egret.DisplayObject;
|
sprite: egret.DisplayObject;
|
||||||
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
setSprite(sprite: egret.DisplayObject): SpriteRenderer;
|
||||||
initialize(): void;
|
render(camera: Camera): void;
|
||||||
}
|
}
|
||||||
interface ITriggerListener {
|
interface ITriggerListener {
|
||||||
onTriggerEnter(other: Collider, local: Collider): any;
|
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 map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number;
|
||||||
static lerp(value1: number, value2: number, amount: number): number;
|
static lerp(value1: number, value2: number, amount: number): number;
|
||||||
static clamp(value: number, min: number, max: 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;
|
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
|
||||||
}
|
}
|
||||||
declare class Matrix2D {
|
declare class Matrix2D {
|
||||||
|
|||||||
@@ -1492,10 +1492,10 @@ var Camera = (function (_super) {
|
|||||||
if (this.entity.transform.rotation != 0) {
|
if (this.entity.transform.rotation != 0) {
|
||||||
var topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
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 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 minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||||
var maxY = MathHelper.maxOf(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.location = new Vector2(minX, minY);
|
||||||
this._bounds.width = maxX - minX;
|
this._bounds.width = maxX - minX;
|
||||||
this._bounds.height = maxY - minY;
|
this._bounds.height = maxY - minY;
|
||||||
@@ -1769,6 +1769,19 @@ var SpriteRenderer = (function (_super) {
|
|||||||
function SpriteRenderer() {
|
function SpriteRenderer() {
|
||||||
return _super !== null && _super.apply(this, arguments) || this;
|
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", {
|
Object.defineProperty(SpriteRenderer.prototype, "sprite", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._sprite;
|
return this._sprite;
|
||||||
@@ -1785,7 +1798,16 @@ var SpriteRenderer = (function (_super) {
|
|||||||
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
||||||
return this;
|
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;
|
return SpriteRenderer;
|
||||||
}(RenderableComponent));
|
}(RenderableComponent));
|
||||||
@@ -2636,12 +2658,6 @@ var MathHelper = (function () {
|
|||||||
return max;
|
return max;
|
||||||
return value;
|
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) {
|
MathHelper.pointOnCirlce = function (circleCenter, radius, angleInDegrees) {
|
||||||
var radians = MathHelper.toRadians(angleInDegrees);
|
var radians = MathHelper.toRadians(angleInDegrees);
|
||||||
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
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 dr = this.right - res.x;
|
||||||
var dt = res.y - this.top;
|
var dt = res.y - this.top;
|
||||||
var db = this.bottom - res.y;
|
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) {
|
if (min == dt) {
|
||||||
res.y = this.top;
|
res.y = this.top;
|
||||||
edgeNormal.y = -1;
|
edgeNormal.y = -1;
|
||||||
@@ -2940,10 +2956,10 @@ var Rectangle = (function () {
|
|||||||
topRight = Vector2.transform(topRight, this._transformMat);
|
topRight = Vector2.transform(topRight, this._transformMat);
|
||||||
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
||||||
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
||||||
var minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
var minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||||
var maxY = MathHelper.maxOf(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.location = new Vector2(minX, minY);
|
||||||
this.width = maxX - minX;
|
this.width = maxX - minX;
|
||||||
this.height = maxY - minY;
|
this.height = maxY - minY;
|
||||||
|
|||||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -25,10 +25,10 @@ class Camera extends Component {
|
|||||||
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
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 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 minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
let maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
let minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||||
let maxY = MathHelper.maxOf(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.location = new Vector2(minX, minY);
|
||||||
this._bounds.width = maxX - minX;
|
this._bounds.width = maxX - minX;
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
*/
|
*/
|
||||||
abstract class RenderableComponent extends Component {
|
abstract class RenderableComponent extends Component {
|
||||||
private _isVisible: boolean;
|
private _isVisible: boolean;
|
||||||
private _areBoundsDirty = true;
|
protected _areBoundsDirty = true;
|
||||||
private _bounds: Rectangle;
|
protected _bounds: Rectangle;
|
||||||
private _localOffset: Vector2;
|
protected _localOffset: Vector2;
|
||||||
|
|
||||||
public get width(){
|
public get width(){
|
||||||
return this.getWidth();
|
return this.getWidth();
|
||||||
@@ -54,6 +54,8 @@ abstract class RenderableComponent extends Component {
|
|||||||
|
|
||||||
protected onBecameInvisible(){}
|
protected onBecameInvisible(){}
|
||||||
|
|
||||||
|
public abstract render(camera: Camera);
|
||||||
|
|
||||||
public isVisibleFromCamera(camera: Camera): boolean{
|
public isVisibleFromCamera(camera: Camera): boolean{
|
||||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
this.isVisible = camera.bounds.intersects(this.bounds);
|
||||||
return this.isVisible;
|
return this.isVisible;
|
||||||
|
|||||||
@@ -2,6 +2,19 @@ class SpriteRenderer extends RenderableComponent {
|
|||||||
private _sprite: egret.DisplayObject;
|
private _sprite: egret.DisplayObject;
|
||||||
private _origin: Vector2;
|
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(){
|
public get sprite(){
|
||||||
return this._sprite;
|
return this._sprite;
|
||||||
}
|
}
|
||||||
@@ -18,7 +31,16 @@ class SpriteRenderer extends RenderableComponent {
|
|||||||
return this;
|
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;
|
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){
|
public static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number){
|
||||||
let radians = MathHelper.toRadians(angleInDegrees);
|
let radians = MathHelper.toRadians(angleInDegrees);
|
||||||
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
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 dt = res.y - this.top;
|
||||||
let db = this.bottom - res.y;
|
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){
|
if (min == dt){
|
||||||
res.y = this.top;
|
res.y = this.top;
|
||||||
edgeNormal.y = -1;
|
edgeNormal.y = -1;
|
||||||
@@ -134,10 +134,10 @@ class Rectangle {
|
|||||||
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
bottomLeft = Vector2.transform(bottomLeft, this._transformMat);
|
||||||
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
bottomRight = Vector2.transform(bottomRight, this._transformMat);
|
||||||
|
|
||||||
let minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
let maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||||
let minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||||
let maxY = MathHelper.maxOf(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.location = new Vector2(minX, minY);
|
||||||
this.width = maxX - minX;
|
this.width = maxX - minX;
|
||||||
|
|||||||
@@ -33,6 +33,11 @@ class Vector2 {
|
|||||||
this.y = y ? y : this.x;
|
this.y = y ? y : this.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param value1
|
||||||
|
* @param value2
|
||||||
|
*/
|
||||||
public static add(value1: Vector2, value2: Vector2){
|
public static add(value1: Vector2, value2: Vector2){
|
||||||
let result: Vector2 = new Vector2(0, 0);
|
let result: Vector2 = new Vector2(0, 0);
|
||||||
result.x = value1.x + value2.x;
|
result.x = value1.x + value2.x;
|
||||||
@@ -40,6 +45,11 @@ class Vector2 {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param value1
|
||||||
|
* @param value2
|
||||||
|
*/
|
||||||
public static divide(value1: Vector2, value2: Vector2){
|
public static divide(value1: Vector2, value2: Vector2){
|
||||||
let result: Vector2 = new Vector2(0, 0);
|
let result: Vector2 = new Vector2(0, 0);
|
||||||
result.x = value1.x / value2.x;
|
result.x = value1.x / value2.x;
|
||||||
@@ -47,6 +57,11 @@ class Vector2 {
|
|||||||
return value1;
|
return value1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param value1
|
||||||
|
* @param value2
|
||||||
|
*/
|
||||||
public static multiply(value1: Vector2, value2: Vector2){
|
public static multiply(value1: Vector2, value2: Vector2){
|
||||||
let result: Vector2 = new Vector2(0, 0);
|
let result: Vector2 = new Vector2(0, 0);
|
||||||
result.x = value1.x * value2.x;
|
result.x = value1.x * value2.x;
|
||||||
@@ -54,6 +69,11 @@ class Vector2 {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param value1
|
||||||
|
* @param value2
|
||||||
|
*/
|
||||||
public static subtract(value1: Vector2, value2: Vector2){
|
public static subtract(value1: Vector2, value2: Vector2){
|
||||||
let result: Vector2 = new Vector2(0, 0);
|
let result: Vector2 = new Vector2(0, 0);
|
||||||
result.x = value1.x - value2.x;
|
result.x = value1.x - value2.x;
|
||||||
@@ -68,10 +88,16 @@ class Vector2 {
|
|||||||
this.y *= val;
|
this.y *= val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 返回它的长度 */
|
||||||
public length(){
|
public length(){
|
||||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个新的Vector2
|
||||||
|
* 它包含来自另一个向量的标准化值。
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
public static normalize(value: Vector2){
|
public static normalize(value: Vector2){
|
||||||
let val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
let val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||||
value.x *= val;
|
value.x *= val;
|
||||||
@@ -98,19 +124,39 @@ class Vector2 {
|
|||||||
return (v1 * v1) + (v2 * v2);
|
return (v1 * v1) + (v2 * v2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包含指定向量的线性插值
|
||||||
|
* @param value1 第一个向量
|
||||||
|
* @param value2 第二个向量
|
||||||
|
* @param amount 权重值(0.0到1.0之间)
|
||||||
|
*/
|
||||||
public static lerp(value1: Vector2, value2: Vector2, amount: number){
|
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));
|
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){
|
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));
|
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){
|
public static distance(value1: Vector2, value2: Vector2){
|
||||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 矢量反演的结果
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
public static negate(value: Vector2){
|
public static negate(value: Vector2){
|
||||||
let result: Vector2 = new Vector2();
|
let result: Vector2 = new Vector2();
|
||||||
result.x = -value.x;
|
result.x = -value.x;
|
||||||
|
|||||||
Reference in New Issue
Block a user