@tdurtschi/bug
Version:
Bug is a simulation that models a weevil walking around in an HTML canvas.
105 lines • 6.05 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var bug_1 = __importDefault(require("./entities/bug/bug"));
var entity_updater_1 = __importDefault(require("./entity-updater"));
var victor_1 = __importDefault(require("victor"));
var entity_manager_stub_1 = require("../../spec/entity-manager-stub");
var tree_1 = require("./entities/plant/tree/tree");
var intersectionsForEntity = function (entity) {
return entity.update.calls.mostRecent().args[0];
};
describe("Entity Updater", function () {
it("Updates all the Entities", function () {
var entities = [new bug_1.default(), new bug_1.default()];
entities[0].update = jasmine.createSpy("update1");
entities[1].update = jasmine.createSpy("update2");
var entityUpdater = new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities));
entityUpdater.update();
expect(entities[0].update).toHaveBeenCalled();
expect(entities[1].update).toHaveBeenCalled();
});
it("Updates bugs every 4 frames", function () {
var entities = [new bug_1.default()];
entities[0].update = jasmine.createSpy("update");
var entityUpdater = new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities));
new Array(4).fill(0).forEach(function (_) { return entityUpdater.update(); });
expect(entities[0].update).toHaveBeenCalledTimes(1);
new Array(4).fill(0).forEach(function (_) { return entityUpdater.update(); });
expect(entities[0].update).toHaveBeenCalledTimes(2);
});
describe("Collision Detection", function () {
it("Will tell a bug that it's intersecting another bug", function () {
var entities = [
new bug_1.default(0, { pos: new victor_1.default(1, 1) }),
new bug_1.default(1, { pos: new victor_1.default(0, 0) }),
new bug_1.default(2, { pos: new victor_1.default(100000, 100000) }),
];
entities.forEach(function (e) { return (e.update = jasmine.createSpy()); });
new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities)).update();
expect(intersectionsForEntity(entities[0])).toContain(entities[1]);
expect(intersectionsForEntity(entities[1])).toContain(entities[0]);
expect(intersectionsForEntity(entities[2]).length).toBe(0);
});
it("Two bugs that are facing each other at the same x-coord are intersecting", function () {
var entities = [
new bug_1.default(0, { pos: new victor_1.default(0, 0), direction: new victor_1.default(1, 0) }),
new bug_1.default(1, { pos: new victor_1.default(0, 0), direction: new victor_1.default(-1, 0) }),
];
entities.forEach(function (e) { return (e.update = jasmine.createSpy()); });
new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities)).update();
expect(intersectionsForEntity(entities[0])).toContain(entities[1]);
expect(intersectionsForEntity(entities[1])).toContain(entities[0]);
});
it("Two bugs that are facing each other but separated are not intersecting", function () {
var entities = [
new bug_1.default(0, {
pos: new victor_1.default(0, 0),
direction: new victor_1.default(1, 0),
size: new victor_1.default(2, 2),
}),
new bug_1.default(2, {
pos: new victor_1.default(1, 0),
direction: new victor_1.default(-1, 0),
size: new victor_1.default(2, 2),
}),
];
entities.forEach(function (e) { return (e.update = jasmine.createSpy()); });
new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities)).update();
expect(intersectionsForEntity(entities[0]).length).toBe(0);
expect(intersectionsForEntity(entities[1]).length).toBe(0);
});
it("Bugs facing away from each other are not intersecting if they're sorta far away", function () {
var entities = [
new bug_1.default(0, {
pos: new victor_1.default(6, 0),
direction: new victor_1.default(1, 0),
size: new victor_1.default(10, 10),
}),
new bug_1.default(2, {
pos: new victor_1.default(0, 0),
direction: new victor_1.default(-1, 0),
size: new victor_1.default(10, 10),
}),
];
entities.forEach(function (e) { return (e.update = jasmine.createSpy()); });
new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities)).update();
expect(intersectionsForEntity(entities[0]).length).toBe(0);
expect(intersectionsForEntity(entities[1]).length).toBe(0);
});
it("Will tell a bug that it's intersecting a tree", function () {
var entities = [
new bug_1.default(0, { pos: new victor_1.default(10, 1), direction: new victor_1.default(-1, 0) }),
new bug_1.default(0, { pos: new victor_1.default(10, 1), direction: new victor_1.default(1, 0) }),
new tree_1.Tree(1, { pos: new victor_1.default(10, 1) }),
];
entities.forEach(function (e) { return (e.update = jasmine.createSpy()); });
new entity_updater_1.default(entity_manager_stub_1.entityManagerStub(entities)).update();
expect(intersectionsForEntity(entities[0])).toContain(entities[2]);
expect(intersectionsForEntity(entities[1])).toContain(entities[2]);
});
});
});
//# sourceMappingURL=entity-updater.spec.js.map