From 41f2a5433b0c7765b5d7e241b5feee4e9c5b719e Mon Sep 17 00:00:00 2001 From: Josh Lory Date: Fri, 22 Jun 2018 16:32:29 -0700 Subject: [PATCH 1/4] Cleanup: stop special-casing certain level IDs --- src/beeCell.js | 2 +- src/subtype.js | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/beeCell.js b/src/beeCell.js index 613b67d..3a1de3f 100644 --- a/src/beeCell.js +++ b/src/beeCell.js @@ -1,5 +1,5 @@ /** - * @overview BeeCell represents the contets of the grid elements for Bee. + * @overview BeeCell represents the contents of the grid elements for Bee. * Bee BeeCells are more complex than many other kinds of cell; they can be * "hidden" with clouds, they can represent multiple different kinds of * element (flower, hive), some of which can be multiple colors (red, diff --git a/src/subtype.js b/src/subtype.js index f209ec9..5bdae0d 100644 --- a/src/subtype.js +++ b/src/subtype.js @@ -1,5 +1,5 @@ -const Cell = require('./cell') -const DirtDrawer = require('./dirtDrawer') +const Cell = require('./cell'); +const DirtDrawer = require('./dirtDrawer'); const SquareType = require('./tiles').SquareType; const EventEmitter = require('events').EventEmitter; // provided by webpack's node-libs-browser @@ -149,11 +149,6 @@ module.exports = class Subtype extends EventEmitter { tile = 'null' + wallIdx; } - // For the first 3 levels in maze, only show the null0 image. - if (['2_1', '2_2', '2_3'].includes(this.level_.id)) { - this.wallMap[y][x] = 0; - tile = 'null0'; - } return tile; } From 64b1005b60d372ee0d11ee04b05bdc0f8eed6f33 Mon Sep 17 00:00:00 2001 From: Josh Lory Date: Fri, 22 Jun 2018 16:34:27 -0700 Subject: [PATCH 2/4] Smart defaults for flowerType and startDirection Is there a benefit to requiring these to be specified every time? Otherwise the API is more friendly if we have sensible default values. --- src/bee.js | 11 ++++------- src/subtype.js | 7 +++---- test/unit/bee.test.js | 28 +++------------------------- 3 files changed, 10 insertions(+), 36 deletions(-) diff --git a/src/bee.js b/src/bee.js index fa2f79b..6fddf60 100644 --- a/src/bee.js +++ b/src/bee.js @@ -13,15 +13,12 @@ const HONEY_SOUND = 'honey'; const NECTAR_SOUND = 'nectar'; module.exports = class Bee extends Gatherer { - constructor(maze, config) { + constructor(maze, config = {}) { super(maze, config); + const {level} = config; - this.defaultFlowerColor_ = (config.level.flowerType === 'redWithNectar' ? - 'red' : 'purple'); - if (this.defaultFlowerColor_ === 'purple' && - config.level.flowerType !== 'purpleNectarHidden') { - throw new Error(`bad flowerType for Bee: ${config.level.flowerType}`); - } + this.defaultFlowerColor_ = level && level.flowerType === 'redWithNectar' ? + 'red' : 'purple'; // at each location, tracks whether user checked to see if it was a flower or // honeycomb using an if block diff --git a/src/subtype.js b/src/subtype.js index 5bdae0d..0a0118e 100644 --- a/src/subtype.js +++ b/src/subtype.js @@ -31,13 +31,12 @@ const TILE_SHAPES = { }; module.exports = class Subtype extends EventEmitter { - constructor(maze, config) { + constructor(maze, {skin, level} = {}) { super(); this.maze_ = maze; - this.skin_ = config.skin; - this.level_ = config.level; - this.startDirection = config.level.startDirection; + this.skin_ = skin; + this.startDirection = level ? level.startDirection : 0; } /** diff --git a/test/unit/bee.test.js b/test/unit/bee.test.js index be1ad5a..c9db8fb 100644 --- a/test/unit/bee.test.js +++ b/test/unit/bee.test.js @@ -1,8 +1,10 @@ +/* global describe, it, expect */ + import Bee from '../../src/bee'; import BeeCell from '../../src/beeCell'; import MazeMap from '../../src/mazeMap'; -var baseLevel = { +const baseLevel = { honeyGoal: 1, map: [ [0] @@ -15,30 +17,6 @@ var baseLevel = { }; describe("Bee", function () { - it("fails if no flowerType", function () { - var maze = {}; - var config = { - level: baseLevel - }; - delete config.level.flowerType; - expect(() => { - new Bee(maze, config); - }).toThrowError(/bad flowerType for Bee/); - }); - - - it("fails if invalid flowerType", function () { - var maze = {}; - var config = { - level: Object.assign(baseLevel, { - flowerType: 'invalid' - }) - }; - expect(() => { - new Bee(maze, config); - }).toThrowError(/bad flowerType for Bee/); - }); - describe("isRedFlower", function () { /** * Shim a 1x1 maze with the given values and validate that we get the From ba8a15e1184dd1c622163d513fd69d6850f383b1 Mon Sep 17 00:00:00 2001 From: Josh Lory Date: Fri, 22 Jun 2018 16:34:54 -0700 Subject: [PATCH 3/4] Add basic unit test for gathering nectar from a flower with 2 capacity --- test/unit/bee.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/unit/bee.test.js b/test/unit/bee.test.js index c9db8fb..4e21c61 100644 --- a/test/unit/bee.test.js +++ b/test/unit/bee.test.js @@ -1,7 +1,7 @@ /* global describe, it, expect */ import Bee from '../../src/bee'; -import BeeCell from '../../src/beeCell'; +import BeeCell, {FeatureType} from '../../src/beeCell'; import MazeMap from '../../src/mazeMap'; const baseLevel = { @@ -55,4 +55,28 @@ describe("Bee", function () { validate('purpleNectarHidden', 'FC', 1, false, 'overriden cloud'); }); }); + + describe('getting nectar', () => { + let bee; + + it('builds the map', () => { + const map = new MazeMap([ + [new BeeCell(1, FeatureType.FLOWER, 2)], + ]); + + bee = new Bee({ + map, + pegmanX: 0, + pegmanY: 0, + }); + + bee.reset(); + expect(bee.getCell(0, 0).isFlower()).toEqual(true); + + // Can get nectar twice. + expect(bee.tryGetNectar()).toEqual(true); + expect(bee.tryGetNectar()).toEqual(true); + expect(bee.tryGetNectar()).toEqual(false); + }) + }); }); From fa26d054a960741756eae6efbec22b01063ac33a Mon Sep 17 00:00:00 2001 From: Josh Lory Date: Fri, 22 Jun 2018 16:55:19 -0700 Subject: [PATCH 4/4] Verify "flowerEmpty" event is emitted --- test/unit/bee.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/unit/bee.test.js b/test/unit/bee.test.js index 4e21c61..d802764 100644 --- a/test/unit/bee.test.js +++ b/test/unit/bee.test.js @@ -1,4 +1,4 @@ -/* global describe, it, expect */ +/* global jest, describe, it, expect */ import Bee from '../../src/bee'; import BeeCell, {FeatureType} from '../../src/beeCell'; @@ -70,13 +70,18 @@ describe("Bee", function () { pegmanY: 0, }); + const flowerEmptySpy = jest.fn(); + bee.on('flowerEmpty', flowerEmptySpy); bee.reset(); expect(bee.getCell(0, 0).isFlower()).toEqual(true); // Can get nectar twice. expect(bee.tryGetNectar()).toEqual(true); expect(bee.tryGetNectar()).toEqual(true); + + // Getting nectar again returns false, and emits a "flowerEmpty" event. expect(bee.tryGetNectar()).toEqual(false); + expect(flowerEmptySpy).toHaveBeenCalledTimes(1); }) }); });