移除Core,新增各接口用于sdk扩展

This commit is contained in:
yhh
2020-12-30 16:28:07 +08:00
parent f934890fac
commit d84ffcc2b7
28 changed files with 2008 additions and 653 deletions

View File

@@ -1,6 +1,13 @@
///<reference path="./Collider.ts" />
module es {
export class BoxCollider extends Collider {
/**
* 创建一个BoxCollider并使用x/y组件作为局部Offset
* @param x
* @param y
* @param width
* @param height
*/
constructor(x: number, y: number, width: number, height: number) {
super();
@@ -30,10 +37,12 @@ module es {
* @param height
*/
public setSize(width: number, height: number) {
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (width != box.width || height != box.height) {
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(width, height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
@@ -46,10 +55,12 @@ module es {
* @param width
*/
public setWidth(width: number): BoxCollider {
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (width != box.width) {
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(width, box.height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
@@ -62,15 +73,25 @@ module es {
* @param height
*/
public setHeight(height: number) {
this._colliderRequiresAutoSizing = false;
let box = this.shape as Box;
if (height != box.height) {
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(box.width, height);
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
}
public debugRender(batcher: IBatcher) {
let poly = this.shape as Polygon;
batcher.drawHollowRect(this.bounds, Debug.colliderBounds, 1);
batcher.drawPolygon(this.shape.position, poly.points, Debug.colliderEdge, true, 1);
batcher.drawPixel(this.entity.transform.position, Debug.colliderPosition, 4);
batcher.drawPixel(Vector2.add(this.entity.transform.position, this.shape.center), Debug.colliderCenter, 2);
}
public toString() {
return `[BoxCollider: bounds: ${this.bounds}]`;
}

View File

@@ -1,7 +1,9 @@
module es {
export class CircleCollider extends Collider {
/**
* 创建一个有半径的
* 创建一个有半径的CircleCollider。
* 请注意当指定半径时如果在实体上使用RenderableComponent您将需要设置原点来对齐CircleCollider。
* 例如如果RenderableComponent有一个0,0的原点并且创建了一个半径为1.5f * renderable.width的CircleCollider你可以通过设置originNormalied为中心除以缩放尺寸来偏移原点
*
* @param radius
*/
@@ -24,18 +26,27 @@ module es {
* @param radius
*/
public setRadius(radius: number): CircleCollider {
this._colliderRequiresAutoSizing = false;
let circle = this.shape as Circle;
if (radius != circle.radius) {
circle.radius = radius;
circle._originalRadius = radius;
this._isPositionDirty = true;
if (this.entity && this._isParentEntityAddedToScene)
if (this.entity != null && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
return this;
}
public debugRender(batcher: IBatcher) {
batcher.drawHollowRect(this.bounds, Debug.colliderBounds, 1);
batcher.drawCircle(this.shape.position, (this.shape as Circle).radius, Debug.colliderEdge, 1);
batcher.drawPixel(this.entity.transform.position, Debug.colliderPosition, 4);
batcher.drawPixel(this.shape.position, Debug.colliderCenter, 2);
}
public toString() {
return `[CircleCollider: bounds: ${this.bounds}, radius: ${(this.shape as Circle).radius}]`
}

View File

@@ -116,6 +116,28 @@ module es {
public onAddedToEntity() {
if (this._colliderRequiresAutoSizing) {
Insist.isTrue(this instanceof BoxCollider || this instanceof CircleCollider, "只有框和圆的碰撞器可以自动创建");
let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
if (renderable == null)
console.warn("Collider没有形状也没有RenderableComponent。不知道如何确定它的大小。");
if (renderable != null) {
let renderableBounds = renderable.bounds.clone();
// 我们在这里需要大小*反比例因为当我们自动调整Collider的大小时它需要没有一个缩放的Renderable
let width = renderableBounds.width / this.entity.transform.scale.x;
let height = renderableBounds.height / this.entity.transform.scale.y;
if (this instanceof CircleCollider) {
this.radius = Math.max(width, height) * 0.5;
// 获取Renderable的中心将其转移到本地坐标并将其作为我们碰撞器的localOffset
this.localOffset = Vector2.subtract(renderableBounds.center, this.entity.transform.position);
} else if (this instanceof BoxCollider) {
this.width = width;
this.height = height;
this.localOffset = Vector2.subtract(renderableBounds.center, this.entity.transform.position);
}
}
}
this._isParentEntityAddedToScene = true;

View File

@@ -23,5 +23,13 @@ module es {
Polygon.recenterPolygonVerts(points);
this.shape = new Polygon(points);
}
public debugRender(batcher: IBatcher) {
let poly = this.shape as Polygon;
batcher.drawHollowRect(this.bounds, Debug.colliderBounds, 1);
batcher.drawPolygon(this.shape.position, poly.points, Debug.colliderEdge, true, 1);
batcher.drawPixel(this.entity.transform.position, Debug.colliderPosition, 4);
batcher.drawPixel(this.shape.position, Debug.colliderCenter, 2);
}
}
}