Last active
April 6, 2016 01:20
-
-
Save timetocode/ed3c6a4b61424f4e8dc89973dcd2c21e to your computer and use it in GitHub Desktop.
Example tests via jasmine.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Existence = require('../Existence') | |
describe('Existence', function() { | |
var existence = null | |
beforeEach(function() { | |
existence = new Existence() | |
}) | |
it('can add/getById entities', function() { | |
var entityA = { id: 1, x: 50, y: 50 } | |
existence.add(entityA) | |
var entityB = existence.getById(1) | |
expect(entityA).toBe(entityB) | |
}) | |
it('throws when invalid entity is added', function() { | |
var invalidAdd = function() { | |
existence.add({ foo: 'hi' }) | |
} | |
expect(invalidAdd).toThrow() | |
}) | |
it('can generate a snapshot', function() { | |
var entityA = { id: 1, x: 50, y: 50 } | |
var entityB = { id: 2, x: 150, y: 150 } | |
var entityC = { id: 3, x: 250, y: 250 } | |
existence.add(entityA) | |
existence.add(entityB) | |
existence.add(entityC) | |
var snapshot = existence.snapshotCurrentState() | |
// snapshots contain shallow copies of entities | |
expect(snapshot.get(1)).toEqual(entityA) | |
expect(snapshot.get(1)).not.toBe(entityA) | |
expect(snapshot.get(2)).toEqual(entityB) | |
expect(snapshot.get(2)).not.toBe(entityB) | |
expect(snapshot.get(3)).toEqual(entityC) | |
expect(snapshot.get(3)).not.toBe(entityC) | |
}) | |
it('saves snapshots in historian when update called', function() { | |
var entityA = { id: 1, x: 50, y: 50 } | |
var entityB = { id: 2, x: 150, y: 150 } | |
var entityC = { id: 3, x: 250, y: 250 } | |
existence.add(entityA) | |
existence.add(entityB) | |
existence.add(entityC) | |
var hypotheticalGameTick = 99 | |
var hypotheticalDeltaTime = 60/1000 | |
existence.update(hypotheticalDeltaTime, hypotheticalGameTick) | |
var snapshot = existence.historian.retrieveSnapshot(hypotheticalGameTick) | |
// snapshots contain shallow copies of entities | |
expect(snapshot.get(1)).toEqual(entityA) | |
expect(snapshot.get(1)).not.toBe(entityA) | |
expect(snapshot.get(2)).toEqual(entityB) | |
expect(snapshot.get(2)).not.toBe(entityB) | |
expect(snapshot.get(3)).toEqual(entityC) | |
expect(snapshot.get(3)).not.toBe(entityC) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Dictionary = require('../Dictionary') | |
function Existence() { | |
this.spatial = null //TODO | |
this.gameObjects = new Dictionary() | |
this.historian = new Historian() | |
} | |
//Existence.prototype.compensate = function(area, latency) | |
Existence.prototype.getObjectsInArea = function(area) { | |
return this.spatial.queryArea(area) | |
} | |
Existence.prototype.getById = function(id) { | |
return this.gameObjects.get(id) | |
} | |
var throwIfInvalid = function(entity) { | |
if (typeof entity.id !== 'undefined' | |
&& typeof entity.x !== 'undefined' | |
&& typeof entity.y !== 'undefined') { | |
//valid | |
} else { | |
throw 'Invalid Entity. Entity must have id, x, y properties.' | |
} | |
} | |
Existence.prototype.add = function(gameObject) { | |
throwIfInvalid(gameObject) | |
this.gameObjects.add(gameObject) | |
} | |
Existence.prototype.snapshotCurrentState = function() { | |
var snapshot = new Dictionary() | |
this.gameObjects.forEach(function(obj) { | |
var shallowCopy = { | |
id: obj.id, | |
x: obj.x, | |
y: obj.y | |
} | |
snapshot.add(shallowCopy) | |
}) | |
return snapshot | |
} | |
Existence.prototype.update = function(delta, tick) { | |
var snapshot = this.snapshotCurrentState() | |
this.historian.record(tick, snapshot) | |
// TODO: delete records from historian (memory leak) | |
} | |
// TODO: move to its own file | |
function Historian() { | |
this.snapshots = new Dictionary() | |
} | |
Historian.prototype.record = function(tick, objects) { | |
this.snapshots.add({ id: tick, objects: objects }) | |
} | |
Historian.prototype.retrieveSnapshot = function(tick) { | |
return this.snapshots.get(tick).objects | |
} | |
module.exports = Existence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment