Skip to content

Commit

Permalink
Made the CollisionManager disposable.
Browse files Browse the repository at this point in the history
- On dispose the CollisionManager unmonitors all of its collidables and unbinds all of their OnDisposed events.

#45
  • Loading branch information
NTaylorMullen committed Oct 15, 2013
1 parent 2441c68 commit d1cd466
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
21 changes: 21 additions & 0 deletions EndGate/EndGate.Core.JS/Collision/CollisionManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var EndGate;
(function (EndGate) {
/// <reference path="../Interfaces/IUpdateable.ts" />
/// <reference path="../Interfaces/IDisposable.ts" />
/// <reference path="../Interfaces/ITyped.ts" />
/// <reference path="Assets/QuadTree.ts" />
/// <reference path="CollisionConfiguration.ts" />
Expand All @@ -22,6 +23,7 @@ var EndGate;
this._nonStaticCollidables = [];
this._quadTree = new Collision.Assets._.QuadTree(configuration);
this._enabled = false;
this._disposed = false;
this._onCollision = new EndGate.EventHandler2();
}
Object.defineProperty(CollisionManager.prototype, "OnCollision", {
Expand Down Expand Up @@ -120,6 +122,25 @@ var EndGate;
}
};

/**
* Destroys removes all monitored collidables and destroys the collision manager.
*/
CollisionManager.prototype.Dispose = function () {
if (!this._disposed) {
this._disposed = true;

for (var i = 0; i < this._collidables.length; i++) {
this.Unmonitor(this._collidables[i].Collidable);
}

this._collidables = [];
this._nonStaticCollidables = [];
this._quadTree = null;
} else {
throw new Error("CollisionManager cannot be disposed more than once");
}
};

CollisionManager.prototype.HashIds = function (c1, c2) {
return Math.min(c1._id, c2._id).toString() + Math.max(c2._id, c1._id).toString();
};
Expand Down
25 changes: 24 additions & 1 deletion EndGate/EndGate.Core.JS/Collision/CollisionManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="../Interfaces/IUpdateable.ts" />
/// <reference path="../Interfaces/IDisposable.ts" />
/// <reference path="../Interfaces/ITyped.ts" />
/// <reference path="Assets/QuadTree.ts" />
/// <reference path="CollisionConfiguration.ts" />
Expand All @@ -17,13 +18,14 @@ module EndGate.Collision {
/**
* Defines a manager that will check for collisions between objects that it is monitoring.
*/
export class CollisionManager implements IUpdateable, EndGate._.ITyped {
export class CollisionManager implements IUpdateable, IDisposable, EndGate._.ITyped {
public _type: string = "CollisionManager";
private _collidables: ICollidableMappings[];
private _nonStaticCollidables: Collidable[];
public _quadTree: Assets._.QuadTree;
private _onCollision: EventHandler2<Collidable, Collidable>;
private _enabled: boolean;
private _disposed: boolean;

/**
* Creates a new instance of CollisionManager.
Expand All @@ -33,6 +35,7 @@ module EndGate.Collision {
this._nonStaticCollidables = [];
this._quadTree = new Assets._.QuadTree(configuration);
this._enabled = false;
this._disposed = false;
this._onCollision = new EventHandler2<Collidable, Collidable>();
}

Expand Down Expand Up @@ -147,6 +150,26 @@ module EndGate.Collision {
}
}

/**
* Destroys removes all monitored collidables and destroys the collision manager.
*/
public Dispose(): void {
if (!this._disposed) {
this._disposed = true;

for (var i = 0; i < this._collidables.length; i++) {
this.Unmonitor(this._collidables[i].Collidable);
}

this._collidables = [];
this._nonStaticCollidables = [];
this._quadTree = null;
}
else {
throw new Error("CollisionManager cannot be disposed more than once");
}
}

private HashIds(c1: Collidable, c2: Collidable): string {
return Math.min(c1._id, c2._id).toString() + Math.max(c2._id, c1._id).toString();
}
Expand Down
1 change: 1 addition & 0 deletions EndGate/EndGate.Core.JS/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var EndGate;
Game.prototype.Dispose = function () {
this.Scene.Dispose();
this.Map.Dispose();
this.CollisionManager.Dispose();
GameRunnerInstance.Unregister(this);
};
Game._gameIds = 0;
Expand Down
1 change: 1 addition & 0 deletions EndGate/EndGate.Core.JS/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ module EndGate {
public Dispose(): void {
this.Scene.Dispose();
this.Map.Dispose();
this.CollisionManager.Dispose();
GameRunnerInstance.Unregister(this);
}
}
Expand Down

0 comments on commit d1cd466

Please sign in to comment.