UNPKG

@tdurtschi/bug

Version:

Bug is a simulation that models a weevil walking around in an HTML canvas.

124 lines 5.13 kB
"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("../bug")); var Victor = require("victor"); var GroundWalk_1 = require("./GroundWalk"); var wall_1 = __importDefault(require("../../wall/wall")); var vectors_1 = require("../../../util/vectors"); var BugInstinct_1 = require("./BugInstinct"); var tree_1 = require("../../plant/tree/tree"); describe("Ground Walking", function () { it("will turn around when there is an obstruction ahead", function () { var input = [new wall_1.default()]; var bug = new bug_1.default(0, { size: new Victor(2, 1), pos: new Victor(0, 0), direction: new Victor(1, 0), }); new GroundWalk_1.GroundWalk(bug).do(input); vectors_1.expectEquals(bug.direction, new Victor(-1, 0)); }); it("Turns around along its x-axis", function () { var input = [ new wall_1.default(1, { pos: new Victor(0, 0), size: new Victor(10, 50), }), ]; var bug = new bug_1.default(0, { pos: new Victor(0, 0), size: new Victor(10, 0), direction: new Victor(1, 0), }); new GroundWalk_1.GroundWalk(bug).do(input); vectors_1.expectEquals(bug.pos, new Victor(-11, 0)); }); it("will change to climbing mode when it reaches a tree if toggle is true", function () { var input = [new tree_1.Tree(1, { pos: new Victor(20, 0) })]; var bug = new bug_1.default(0, { pos: new Victor(10, 0), size: new Victor(30, 20), direction: new Victor(-1, 0), }); bug.bugInstinct = new BugInstinct_1.DummyBugInstinct({ WillClimb: true }); new GroundWalk_1.GroundWalk(bug).do(input); var direction = bug.direction; expect(bug.climbingOn.plant).toEqual(input[0]); vectors_1.expectEquals(direction, new Victor(0, 1)); }); it("will stay on the ground when it reaches a tree if toggle is false", function () { var input = [new tree_1.Tree(1, { pos: new Victor(20, 0) })]; var bug = new bug_1.default(0, { pos: new Victor(10, 0), size: new Victor(30, 20), direction: new Victor(-1, 0), }); bug.bugInstinct = new BugInstinct_1.DummyBugInstinct({ WillClimb: false }); new GroundWalk_1.GroundWalk(bug).do(input); var direction = bug.direction; expect(bug.climbingOn).not.toBeDefined(); vectors_1.expectEquals(direction, new Victor(-1, 0)); vectors_1.expectEquals(bug.pos, new Victor(9, 0)); }); it("Emits an event when it starts climbing a tree", function (done) { var input = [new tree_1.Tree(1, { pos: new Victor(20, 0) })]; var bug = new bug_1.default(0, { pos: new Victor(10, 0), size: new Victor(30, 20), direction: new Victor(-1, 0), }); bug.bugInstinct = new BugInstinct_1.DummyBugInstinct({ WillClimb: true }); bug.zIndexChanged.subscribe(done); new GroundWalk_1.GroundWalk(bug).do(input); }); it("finishes after a specified number of updates", function () { var bug = new bug_1.default(); bug.finishBehavior = jasmine.createSpy("finishBehavior"); var groundWalk = new GroundWalk_1.GroundWalk(bug, 3); groundWalk.do([]); groundWalk.do([]); expect(bug.finishBehavior).not.toHaveBeenCalled(); groundWalk.do([]); expect(bug.finishBehavior).toHaveBeenCalled(); }); it("turns around when crossing another bug's path", function () { var bug = new bug_1.default(0, { direction: new Victor(1, 0), }); var otherBug = new bug_1.default(1, { direction: new Victor(-1, 0), }); new GroundWalk_1.GroundWalk(bug).do([otherBug]); vectors_1.expectEquals(bug.direction, new Victor(-1, 0)); }); }); describe("Walking mode", function () { it("Travels at a given speed", function () { var bug = new bug_1.default(0, { direction: new Victor(1, 0), speed: 17, }); new GroundWalk_1.GroundWalk(bug).do([]); expect(bug.pos.x).toEqual(17); }); describe("directions", function () { it("travels in +x direction when 'direction' is (1,0)", function () { var bug = new bug_1.default(0, { direction: new Victor(1, 0), }); new GroundWalk_1.GroundWalk(bug).do([]); expect(bug.pos.x).toEqual(1); }); it("travels in -x direction when 'direction' is (-1,0)", function () { var bug = new bug_1.default(0, { direction: new Victor(-1, 0), }); new GroundWalk_1.GroundWalk(bug).do([]); expect(bug.pos.x).toEqual(-1); }); }); }); //# sourceMappingURL=GroundWalk.spec.js.map