新增numberdictionary的散列键

This commit is contained in:
yhh
2020-06-16 20:22:22 +08:00
parent 447ea4efe4
commit 9e6e5eccc8
8 changed files with 49 additions and 8 deletions

View File

@@ -411,6 +411,7 @@ declare abstract class Collider extends Component {
overlaps(other: Collider): any; overlaps(other: Collider): any;
collidesWith(collider: Collider, motion: Vector2): CollisionResult; collidesWith(collider: Collider, motion: Vector2): CollisionResult;
onAddedToEntity(): void; onAddedToEntity(): void;
onRemovedFromEntity(): void;
onEntityTransformChanged(comp: ComponentTransform): void; onEntityTransformChanged(comp: ComponentTransform): void;
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
@@ -777,6 +778,7 @@ declare class RaycastResultParser {
declare class NumberDictionary { declare class NumberDictionary {
private _store; private _store;
private getKey; private getKey;
private intToUint;
add(x: number, y: number, list: Collider[]): void; add(x: number, y: number, list: Collider[]): void;
remove(obj: Collider): void; remove(obj: Collider): void;
tryGetValue(x: number, y: number): Collider[]; tryGetValue(x: number, y: number): Collider[];

View File

@@ -1911,7 +1911,7 @@ var Collider = (function (_super) {
console.error("Only box and circle colliders can be created automatically"); console.error("Only box and circle colliders can be created automatically");
} }
var renderable = this.entity.getComponent(RenderableComponent); var renderable = this.entity.getComponent(RenderableComponent);
if (!renderable) { if (renderable) {
var renderbaleBounds = renderable.bounds; var renderbaleBounds = renderable.bounds;
var width = renderbaleBounds.width / this.entity.transform.scale.x; var width = renderbaleBounds.width / this.entity.transform.scale.x;
var height = renderbaleBounds.height / this.entity.transform.scale.y; var height = renderbaleBounds.height / this.entity.transform.scale.y;
@@ -1926,6 +1926,10 @@ var Collider = (function (_super) {
this._isParentEntityAddedToScene = true; this._isParentEntityAddedToScene = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
}; };
Collider.prototype.onRemovedFromEntity = function () {
this.unregisterColliderWithPhysicsSystem();
this._isParentEntityAddedToScene = false;
};
Collider.prototype.onEntityTransformChanged = function (comp) { Collider.prototype.onEntityTransformChanged = function (comp) {
switch (comp) { switch (comp) {
case ComponentTransform.position: case ComponentTransform.position:
@@ -3844,7 +3848,13 @@ var NumberDictionary = (function () {
this._store = new Map(); this._store = new Map();
} }
NumberDictionary.prototype.getKey = function (x, y) { NumberDictionary.prototype.getKey = function (x, y) {
return x << 32 | y; return x << 32 | this.intToUint(y);
};
NumberDictionary.prototype.intToUint = function (i) {
if (i >= 0)
return i;
else
return 4294967296 + i;
}; };
NumberDictionary.prototype.add = function (x, y, list) { NumberDictionary.prototype.add = function (x, y, list) {
this._store.set(this.getKey(x, y), list); this._store.set(this.getKey(x, y), list);

File diff suppressed because one or more lines are too long

View File

@@ -411,6 +411,7 @@ declare abstract class Collider extends Component {
overlaps(other: Collider): any; overlaps(other: Collider): any;
collidesWith(collider: Collider, motion: Vector2): CollisionResult; collidesWith(collider: Collider, motion: Vector2): CollisionResult;
onAddedToEntity(): void; onAddedToEntity(): void;
onRemovedFromEntity(): void;
onEntityTransformChanged(comp: ComponentTransform): void; onEntityTransformChanged(comp: ComponentTransform): void;
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
@@ -777,6 +778,7 @@ declare class RaycastResultParser {
declare class NumberDictionary { declare class NumberDictionary {
private _store; private _store;
private getKey; private getKey;
private intToUint;
add(x: number, y: number, list: Collider[]): void; add(x: number, y: number, list: Collider[]): void;
remove(obj: Collider): void; remove(obj: Collider): void;
tryGetValue(x: number, y: number): Collider[]; tryGetValue(x: number, y: number): Collider[];

View File

@@ -1911,7 +1911,7 @@ var Collider = (function (_super) {
console.error("Only box and circle colliders can be created automatically"); console.error("Only box and circle colliders can be created automatically");
} }
var renderable = this.entity.getComponent(RenderableComponent); var renderable = this.entity.getComponent(RenderableComponent);
if (!renderable) { if (renderable) {
var renderbaleBounds = renderable.bounds; var renderbaleBounds = renderable.bounds;
var width = renderbaleBounds.width / this.entity.transform.scale.x; var width = renderbaleBounds.width / this.entity.transform.scale.x;
var height = renderbaleBounds.height / this.entity.transform.scale.y; var height = renderbaleBounds.height / this.entity.transform.scale.y;
@@ -1926,6 +1926,10 @@ var Collider = (function (_super) {
this._isParentEntityAddedToScene = true; this._isParentEntityAddedToScene = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
}; };
Collider.prototype.onRemovedFromEntity = function () {
this.unregisterColliderWithPhysicsSystem();
this._isParentEntityAddedToScene = false;
};
Collider.prototype.onEntityTransformChanged = function (comp) { Collider.prototype.onEntityTransformChanged = function (comp) {
switch (comp) { switch (comp) {
case ComponentTransform.position: case ComponentTransform.position:
@@ -3844,7 +3848,13 @@ var NumberDictionary = (function () {
this._store = new Map(); this._store = new Map();
} }
NumberDictionary.prototype.getKey = function (x, y) { NumberDictionary.prototype.getKey = function (x, y) {
return x << 32 | y; return x << 32 | this.intToUint(y);
};
NumberDictionary.prototype.intToUint = function (i) {
if (i >= 0)
return i;
else
return 4294967296 + i;
}; };
NumberDictionary.prototype.add = function (x, y, list) { NumberDictionary.prototype.add = function (x, y, list) {
this._store.set(this.getKey(x, y), list); this._store.set(this.getKey(x, y), list);

File diff suppressed because one or more lines are too long

View File

@@ -79,7 +79,7 @@ abstract class Collider extends Component{
} }
let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent); let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
if (!renderable){ if (renderable){
let renderbaleBounds = renderable.bounds; let renderbaleBounds = renderable.bounds;
let width = renderbaleBounds.width / this.entity.transform.scale.x; let width = renderbaleBounds.width / this.entity.transform.scale.x;
@@ -98,6 +98,11 @@ abstract class Collider extends Component{
this._isParentEntityAddedToScene = true; this._isParentEntityAddedToScene = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
public onRemovedFromEntity(){
this.unregisterColliderWithPhysicsSystem();
this._isParentEntityAddedToScene = false;
}
public onEntityTransformChanged(comp: ComponentTransform){ public onEntityTransformChanged(comp: ComponentTransform){
switch (comp){ switch (comp){

View File

@@ -126,8 +126,20 @@ class RaycastResultParser {
class NumberDictionary { class NumberDictionary {
private _store: Map<number, Collider[]> = new Map<number, Collider[]>(); private _store: Map<number, Collider[]> = new Map<number, Collider[]>();
/**
* 根据x和y值计算并返回散列键
* @param x
* @param y
*/
private getKey(x: number, y: number): number { private getKey(x: number, y: number): number {
return x << 32 | y; return x << 32 | this.intToUint(y);
}
private intToUint(i){
if (i >= 0)
return i;
else
return 4294967296 + i;
} }
public add(x: number, y: number, list: Collider[]) { public add(x: number, y: number, list: Collider[]) {