修复physics register失效问题

This commit is contained in:
yhh
2020-06-16 11:59:40 +08:00
parent 8b21edc65f
commit ced176706b
14 changed files with 169 additions and 18 deletions

View File

@@ -87,7 +87,7 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
declare class Point { declare class Point {
x: number; x: number;
y: number; y: number;
constructor(x: number, y: number); constructor(x?: number, y?: number);
} }
declare class UnweightedGridGraph implements IUnweightedGraph<Point> { declare class UnweightedGridGraph implements IUnweightedGraph<Point> {
private static readonly CARDINAL_DIRS; private static readonly CARDINAL_DIRS;
@@ -153,6 +153,7 @@ declare abstract class Component {
onDisabled(): void; onDisabled(): void;
onEntityTransformChanged(comp: ComponentTransform): void; onEntityTransformChanged(comp: ComponentTransform): void;
update(): void; update(): void;
debugRender(): void;
registerComponent(): void; registerComponent(): void;
deregisterComponent(): void; deregisterComponent(): void;
} }
@@ -420,6 +421,7 @@ declare class BoxCollider extends Collider {
height: number; height: number;
setHeight(height: number): void; setHeight(height: number): void;
constructor(); constructor();
setSize(width: number, height: number): this;
} }
declare class EntitySystem { declare class EntitySystem {
private _scene; private _scene;
@@ -671,7 +673,9 @@ declare class Collisions {
} }
declare class Physics { declare class Physics {
private static _spatialHash; private static _spatialHash;
static spatialHashCellSize: number;
static readonly allLayers: number; static readonly allLayers: number;
static reset(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[]; static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[]; static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
@@ -799,6 +803,10 @@ declare class Pair<T> {
clear(): void; clear(): void;
equals(other: Pair<T>): boolean; 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 { declare class Triangulator {
triangleIndices: number[]; triangleIndices: number[];
private _triPrev; private _triPrev;

View File

@@ -550,8 +550,8 @@ var UnweightedGraph = (function () {
}()); }());
var Point = (function () { var Point = (function () {
function Point(x, y) { function Point(x, y) {
this.x = x; this.x = x ? x : 0;
this.y = y; this.y = y ? y : this.x;
} }
return Point; return Point;
}()); }());
@@ -785,6 +785,8 @@ var Component = (function () {
}; };
Component.prototype.update = function () { Component.prototype.update = function () {
}; };
Component.prototype.debugRender = function () {
};
Component.prototype.registerComponent = function () { Component.prototype.registerComponent = function () {
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false); this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false);
this.entity.scene.entityProcessors.onComponentAdded(this.entity); this.entity.scene.entityProcessors.onComponentAdded(this.entity);
@@ -1105,7 +1107,8 @@ var Scene = (function (_super) {
return this; return this;
}; };
Scene.prototype.initialize = function () { 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) if (this.entityProcessors)
this.entityProcessors.begin(); this.entityProcessors.begin();
}; };
@@ -1845,6 +1848,7 @@ var Collider = (function (_super) {
_this.collidesWithLayers = Physics.allLayers; _this.collidesWithLayers = Physics.allLayers;
_this._isPositionDirty = true; _this._isPositionDirty = true;
_this._isRotationDirty = true; _this._isRotationDirty = true;
_this._localOffset = new Vector2(0, 0);
return _this; return _this;
} }
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
@@ -1995,6 +1999,17 @@ var BoxCollider = (function (_super) {
Physics.updateCollider(this); 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; return BoxCollider;
}(Collider)); }(Collider));
var EntitySystem = (function () { var EntitySystem = (function () {
@@ -3245,6 +3260,9 @@ var Collisions = (function () {
var Physics = (function () { var Physics = (function () {
function Physics() { function Physics() {
} }
Physics.reset = function () {
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
};
Physics.overlapCircleAll = function (center, randius, results, layerMask) { Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; } if (layerMask === void 0) { layerMask = -1; }
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
@@ -3267,6 +3285,7 @@ var Physics = (function () {
this._spatialHash.remove(collider); this._spatialHash.remove(collider);
this._spatialHash.register(collider); this._spatialHash.register(collider);
}; };
Physics.spatialHashCellSize = 100;
Physics.allLayers = -1; Physics.allLayers = -1;
return Physics; return Physics;
}()); }());
@@ -3717,6 +3736,18 @@ var SpatialHash = (function () {
collider.registeredPhysicsBounds = bounds; collider.registeredPhysicsBounds = bounds;
var p1 = this.cellCoords(bounds.x, bounds.y); var p1 = this.cellCoords(bounds.x, bounds.y);
var p2 = this.cellCoords(bounds.right, bounds.bottom); 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) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
@@ -3869,6 +3900,23 @@ var Pair = (function () {
}; };
return Pair; 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 () { var Triangulator = (function () {
function Triangulator() { function Triangulator() {
this.triangleIndices = []; this.triangleIndices = [];

File diff suppressed because one or more lines are too long

View File

@@ -105,11 +105,11 @@ class Main extends eui.UILayer {
new Vector2(0, 10), new Vector2(0, 10),
new Vector2(0, 0)])); new Vector2(0, 0)]));
player.addComponent(new SpawnComponent(EnemyType.worm)); player.addComponent(new SpawnComponent(EnemyType.worm));
player.addComponent(new BoxCollider()); player.addComponent(new BoxCollider()).setSize(100, 100);
player.addComponent(new Mover()); player.addComponent(new Mover());
let player2 = scene.createEntity("player2"); let player2 = scene.createEntity("player2");
player2.addComponent(new BoxCollider()); player2.addComponent(new BoxCollider()).setSize(100, 100);
// Main.emitter.addObserver(CoreEmitterType.Update, ()=>{ // Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
// console.log("update emitter"); // console.log("update emitter");

View File

@@ -17,7 +17,7 @@ class SpawnComponent extends Component implements ITriggerListener {
public update() { public update() {
// console.log("update"); // console.log("update");
this.entity.getComponent<Mover>(Mover).move(new Vector2(1, 0)); this.entity.getComponent<Mover>(Mover).move(new Vector2(0, 0));
} }
public onTriggerEnter(other: Collider, local: Collider){ public onTriggerEnter(other: Collider, local: Collider){

View File

@@ -87,7 +87,7 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
declare class Point { declare class Point {
x: number; x: number;
y: number; y: number;
constructor(x: number, y: number); constructor(x?: number, y?: number);
} }
declare class UnweightedGridGraph implements IUnweightedGraph<Point> { declare class UnweightedGridGraph implements IUnweightedGraph<Point> {
private static readonly CARDINAL_DIRS; private static readonly CARDINAL_DIRS;
@@ -153,6 +153,7 @@ declare abstract class Component {
onDisabled(): void; onDisabled(): void;
onEntityTransformChanged(comp: ComponentTransform): void; onEntityTransformChanged(comp: ComponentTransform): void;
update(): void; update(): void;
debugRender(): void;
registerComponent(): void; registerComponent(): void;
deregisterComponent(): void; deregisterComponent(): void;
} }
@@ -420,6 +421,7 @@ declare class BoxCollider extends Collider {
height: number; height: number;
setHeight(height: number): void; setHeight(height: number): void;
constructor(); constructor();
setSize(width: number, height: number): this;
} }
declare class EntitySystem { declare class EntitySystem {
private _scene; private _scene;
@@ -671,7 +673,9 @@ declare class Collisions {
} }
declare class Physics { declare class Physics {
private static _spatialHash; private static _spatialHash;
static spatialHashCellSize: number;
static readonly allLayers: number; static readonly allLayers: number;
static reset(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[]; static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[]; static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
@@ -799,6 +803,10 @@ declare class Pair<T> {
clear(): void; clear(): void;
equals(other: Pair<T>): boolean; 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 { declare class Triangulator {
triangleIndices: number[]; triangleIndices: number[];
private _triPrev; private _triPrev;

View File

@@ -550,8 +550,8 @@ var UnweightedGraph = (function () {
}()); }());
var Point = (function () { var Point = (function () {
function Point(x, y) { function Point(x, y) {
this.x = x; this.x = x ? x : 0;
this.y = y; this.y = y ? y : this.x;
} }
return Point; return Point;
}()); }());
@@ -785,6 +785,8 @@ var Component = (function () {
}; };
Component.prototype.update = function () { Component.prototype.update = function () {
}; };
Component.prototype.debugRender = function () {
};
Component.prototype.registerComponent = function () { Component.prototype.registerComponent = function () {
this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false); this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this), false);
this.entity.scene.entityProcessors.onComponentAdded(this.entity); this.entity.scene.entityProcessors.onComponentAdded(this.entity);
@@ -1105,7 +1107,8 @@ var Scene = (function (_super) {
return this; return this;
}; };
Scene.prototype.initialize = function () { 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) if (this.entityProcessors)
this.entityProcessors.begin(); this.entityProcessors.begin();
}; };
@@ -1845,6 +1848,7 @@ var Collider = (function (_super) {
_this.collidesWithLayers = Physics.allLayers; _this.collidesWithLayers = Physics.allLayers;
_this._isPositionDirty = true; _this._isPositionDirty = true;
_this._isRotationDirty = true; _this._isRotationDirty = true;
_this._localOffset = new Vector2(0, 0);
return _this; return _this;
} }
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
@@ -1995,6 +1999,17 @@ var BoxCollider = (function (_super) {
Physics.updateCollider(this); 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; return BoxCollider;
}(Collider)); }(Collider));
var EntitySystem = (function () { var EntitySystem = (function () {
@@ -3245,6 +3260,9 @@ var Collisions = (function () {
var Physics = (function () { var Physics = (function () {
function Physics() { function Physics() {
} }
Physics.reset = function () {
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
};
Physics.overlapCircleAll = function (center, randius, results, layerMask) { Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; } if (layerMask === void 0) { layerMask = -1; }
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
@@ -3267,6 +3285,7 @@ var Physics = (function () {
this._spatialHash.remove(collider); this._spatialHash.remove(collider);
this._spatialHash.register(collider); this._spatialHash.register(collider);
}; };
Physics.spatialHashCellSize = 100;
Physics.allLayers = -1; Physics.allLayers = -1;
return Physics; return Physics;
}()); }());
@@ -3717,6 +3736,18 @@ var SpatialHash = (function () {
collider.registeredPhysicsBounds = bounds; collider.registeredPhysicsBounds = bounds;
var p1 = this.cellCoords(bounds.x, bounds.y); var p1 = this.cellCoords(bounds.x, bounds.y);
var p2 = this.cellCoords(bounds.right, bounds.bottom); 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) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
@@ -3869,6 +3900,23 @@ var Pair = (function () {
}; };
return Pair; 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 () { var Triangulator = (function () {
function Triangulator() { function Triangulator() {
this.triangleIndices = []; this.triangleIndices = [];

File diff suppressed because one or more lines are too long

View File

@@ -11,7 +11,7 @@ abstract class Collider extends Component{
public _isRotationDirty = true; public _isRotationDirty = true;
protected _isParentEntityAddedToScene; protected _isParentEntityAddedToScene;
protected _colliderRequiresAutoSizing; protected _colliderRequiresAutoSizing;
protected _localOffset: Vector2; protected _localOffset: Vector2 = new Vector2(0, 0);
protected _isColliderRegisterd; protected _isColliderRegisterd;
public get bounds(): Rectangle { public get bounds(): Rectangle {

View File

@@ -74,7 +74,9 @@ class Scene extends egret.DisplayObjectContainer {
/** 初始化场景 */ /** 初始化场景 */
public initialize(){ public initialize(){
/** 初始化默认相机 */ /** 初始化默认相机 */
this.camera = this.createEntity("camera").addComponent(new Camera()); this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
Physics.reset();
if (this.entityProcessors) if (this.entityProcessors)
this.entityProcessors.begin(); this.entityProcessors.begin();

View File

@@ -2,8 +2,8 @@ class Point {
public x: number; public x: number;
public y: number; public y: number;
constructor(x: number, y: number){ constructor(x?: number, y?: number){
this.x = x; this.x = x ? x : 0;
this.y = y; this.y = y ? y : this.x;
} }
} }

View File

@@ -1,8 +1,14 @@
class Physics { class Physics {
private static _spatialHash: SpatialHash; private static _spatialHash: SpatialHash;
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
public static spatialHashCellSize = 100;
public static readonly allLayers: number = -1; 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){ public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
} }

View File

@@ -35,6 +35,21 @@ class SpatialHash {
collider.registeredPhysicsBounds = bounds; collider.registeredPhysicsBounds = bounds;
let p1 = this.cellCoords(bounds.x, bounds.y); let p1 = this.cellCoords(bounds.x, bounds.y);
let p2 = this.cellCoords(bounds.right, bounds.bottom); 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){ public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask){

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