修复physics register失效问题
This commit is contained in:
10
source/bin/framework.d.ts
vendored
10
source/bin/framework.d.ts
vendored
@@ -87,7 +87,7 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
|
||||
declare class Point {
|
||||
x: number;
|
||||
y: number;
|
||||
constructor(x: number, y: number);
|
||||
constructor(x?: number, y?: number);
|
||||
}
|
||||
declare class UnweightedGridGraph implements IUnweightedGraph<Point> {
|
||||
private static readonly CARDINAL_DIRS;
|
||||
@@ -153,6 +153,7 @@ declare abstract class Component {
|
||||
onDisabled(): void;
|
||||
onEntityTransformChanged(comp: ComponentTransform): void;
|
||||
update(): void;
|
||||
debugRender(): void;
|
||||
registerComponent(): void;
|
||||
deregisterComponent(): void;
|
||||
}
|
||||
@@ -420,6 +421,7 @@ declare class BoxCollider extends Collider {
|
||||
height: number;
|
||||
setHeight(height: number): void;
|
||||
constructor();
|
||||
setSize(width: number, height: number): this;
|
||||
}
|
||||
declare class EntitySystem {
|
||||
private _scene;
|
||||
@@ -671,7 +673,9 @@ declare class Collisions {
|
||||
}
|
||||
declare class Physics {
|
||||
private static _spatialHash;
|
||||
static spatialHashCellSize: number;
|
||||
static readonly allLayers: number;
|
||||
static reset(): void;
|
||||
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
|
||||
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
|
||||
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
|
||||
@@ -799,6 +803,10 @@ declare class Pair<T> {
|
||||
clear(): void;
|
||||
equals(other: Pair<T>): boolean;
|
||||
}
|
||||
declare class RectangleExt {
|
||||
static union(first: Rectangle, point: Point): Rectangle;
|
||||
static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
|
||||
}
|
||||
declare class Triangulator {
|
||||
triangleIndices: number[];
|
||||
private _triPrev;
|
||||
|
||||
@@ -550,8 +550,8 @@ var UnweightedGraph = (function () {
|
||||
}());
|
||||
var Point = (function () {
|
||||
function Point(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.x = x ? x : 0;
|
||||
this.y = y ? y : this.x;
|
||||
}
|
||||
return Point;
|
||||
}());
|
||||
@@ -785,6 +785,8 @@ var Component = (function () {
|
||||
};
|
||||
Component.prototype.update = function () {
|
||||
};
|
||||
Component.prototype.debugRender = function () {
|
||||
};
|
||||
Component.prototype.registerComponent = function () {
|
||||
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false);
|
||||
this.entity.scene.entityProcessors.onComponentAdded(this.entity);
|
||||
@@ -1105,7 +1107,8 @@ var Scene = (function (_super) {
|
||||
return this;
|
||||
};
|
||||
Scene.prototype.initialize = function () {
|
||||
this.camera = this.createEntity("camera").addComponent(new Camera());
|
||||
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
|
||||
Physics.reset();
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.begin();
|
||||
};
|
||||
@@ -1845,6 +1848,7 @@ var Collider = (function (_super) {
|
||||
_this.collidesWithLayers = Physics.allLayers;
|
||||
_this._isPositionDirty = true;
|
||||
_this._isRotationDirty = true;
|
||||
_this._localOffset = new Vector2(0, 0);
|
||||
return _this;
|
||||
}
|
||||
Object.defineProperty(Collider.prototype, "bounds", {
|
||||
@@ -1995,6 +1999,17 @@ var BoxCollider = (function (_super) {
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
};
|
||||
BoxCollider.prototype.setSize = function (width, height) {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
var box = this.shape;
|
||||
if (width != box.width || height != box.height) {
|
||||
box.updateBox(width, height);
|
||||
this._isPositionDirty = true;
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
return BoxCollider;
|
||||
}(Collider));
|
||||
var EntitySystem = (function () {
|
||||
@@ -3245,6 +3260,9 @@ var Collisions = (function () {
|
||||
var Physics = (function () {
|
||||
function Physics() {
|
||||
}
|
||||
Physics.reset = function () {
|
||||
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
|
||||
};
|
||||
Physics.overlapCircleAll = function (center, randius, results, layerMask) {
|
||||
if (layerMask === void 0) { layerMask = -1; }
|
||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||
@@ -3267,6 +3285,7 @@ var Physics = (function () {
|
||||
this._spatialHash.remove(collider);
|
||||
this._spatialHash.register(collider);
|
||||
};
|
||||
Physics.spatialHashCellSize = 100;
|
||||
Physics.allLayers = -1;
|
||||
return Physics;
|
||||
}());
|
||||
@@ -3717,6 +3736,18 @@ var SpatialHash = (function () {
|
||||
collider.registeredPhysicsBounds = bounds;
|
||||
var p1 = this.cellCoords(bounds.x, bounds.y);
|
||||
var p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||
if (!this.gridBounds.contains(new Vector2(p1.x, p1.y))) {
|
||||
this.gridBounds = RectangleExt.union(this.gridBounds, p1);
|
||||
}
|
||||
if (!this.gridBounds.contains(new Vector2(p2.x, p2.y))) {
|
||||
this.gridBounds = RectangleExt.union(this.gridBounds, p2);
|
||||
}
|
||||
for (var x = p1.x; x <= p2.x; x++) {
|
||||
for (var y = p1.y; y <= p2.y; y++) {
|
||||
var c = this.cellAtPosition(x, y, true);
|
||||
c.push(collider);
|
||||
}
|
||||
}
|
||||
};
|
||||
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
|
||||
var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
|
||||
@@ -3869,6 +3900,23 @@ var Pair = (function () {
|
||||
};
|
||||
return Pair;
|
||||
}());
|
||||
var RectangleExt = (function () {
|
||||
function RectangleExt() {
|
||||
}
|
||||
RectangleExt.union = function (first, point) {
|
||||
var rect = new Rectangle(point.x, point.y, 0, 0);
|
||||
return this.unionR(first, rect);
|
||||
};
|
||||
RectangleExt.unionR = function (value1, value2) {
|
||||
var result = new Rectangle();
|
||||
result.x = Math.min(value1.x, value2.x);
|
||||
result.y = Math.min(value1.y, value2.y);
|
||||
result.width = Math.max(value1.right, value2.right) - result.x;
|
||||
result.height = Math.max(value1.bottom, value2.bottom) - result.y;
|
||||
return result;
|
||||
};
|
||||
return RectangleExt;
|
||||
}());
|
||||
var Triangulator = (function () {
|
||||
function Triangulator() {
|
||||
this.triangleIndices = [];
|
||||
|
||||
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
@@ -11,7 +11,7 @@ abstract class Collider extends Component{
|
||||
public _isRotationDirty = true;
|
||||
protected _isParentEntityAddedToScene;
|
||||
protected _colliderRequiresAutoSizing;
|
||||
protected _localOffset: Vector2;
|
||||
protected _localOffset: Vector2 = new Vector2(0, 0);
|
||||
protected _isColliderRegisterd;
|
||||
|
||||
public get bounds(): Rectangle {
|
||||
|
||||
@@ -74,7 +74,9 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
/** 初始化场景 */
|
||||
public initialize(){
|
||||
/** 初始化默认相机 */
|
||||
this.camera = this.createEntity("camera").addComponent(new Camera());
|
||||
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
|
||||
|
||||
Physics.reset();
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.begin();
|
||||
|
||||
@@ -2,8 +2,8 @@ class Point {
|
||||
public x: number;
|
||||
public y: number;
|
||||
|
||||
constructor(x: number, y: number){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
constructor(x?: number, y?: number){
|
||||
this.x = x ? x : 0;
|
||||
this.y = y ? y : this.x;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
class Physics {
|
||||
private static _spatialHash: SpatialHash;
|
||||
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
|
||||
public static spatialHashCellSize = 100;
|
||||
|
||||
public static readonly allLayers: number = -1;
|
||||
|
||||
public static reset(){
|
||||
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
|
||||
}
|
||||
|
||||
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
|
||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||
}
|
||||
|
||||
@@ -35,6 +35,21 @@ class SpatialHash {
|
||||
collider.registeredPhysicsBounds = bounds;
|
||||
let p1 = this.cellCoords(bounds.x, bounds.y);
|
||||
let p2 = this.cellCoords(bounds.right, bounds.bottom);
|
||||
|
||||
if (!this.gridBounds.contains(new Vector2(p1.x, p1.y))){
|
||||
this.gridBounds = RectangleExt.union(this.gridBounds, p1);
|
||||
}
|
||||
|
||||
if (!this.gridBounds.contains(new Vector2(p2.x, p2.y))){
|
||||
this.gridBounds = RectangleExt.union(this.gridBounds, p2);
|
||||
}
|
||||
|
||||
for (let x = p1.x; x <= p2.x; x++){
|
||||
for (let y = p1.y; y <= p2.y; y++){
|
||||
let c = this.cellAtPosition(x, y, true);
|
||||
c.push(collider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask){
|
||||
|
||||
16
source/src/Utils/RectangleExt.ts
Normal file
16
source/src/Utils/RectangleExt.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
class RectangleExt {
|
||||
public static union(first: Rectangle, point: Point){
|
||||
let rect = new Rectangle(point.x, point.y, 0, 0);
|
||||
return this.unionR(first, rect);
|
||||
}
|
||||
|
||||
public static unionR(value1: Rectangle, value2: Rectangle){
|
||||
let result = new Rectangle();
|
||||
result.x = Math.min(value1.x, value2.x);
|
||||
result.y = Math.min(value1.y, value2.y);
|
||||
result.width = Math.max(value1.right, value2.right) - result.x;
|
||||
result.height = Math.max(value1.bottom, value2.bottom) - result.y;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user