Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/bee.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit to requiring this to be specified every time? Otherwise the API is more friendly if we have sensible default values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call; this is much nicer

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
Expand Down
7 changes: 3 additions & 4 deletions src/subtype.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ const TILE_SHAPES = {
const RANDOM_TILE_RATE = 0.2;

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;
}

/**
Expand Down
59 changes: 33 additions & 26 deletions test/unit/bee.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* global jest, describe, it, expect */

import Bee from '../../src/bee';
import BeeCell from '../../src/beeCell';
import BeeCell, {FeatureType} from '../../src/beeCell';
import MazeMap from '../../src/mazeMap';

var baseLevel = {
const baseLevel = {
honeyGoal: 1,
map: [
[0]
Expand All @@ -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
Expand Down Expand Up @@ -77,4 +55,33 @@ 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,
});

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);
})
});
});