BlueGit

projects-timfalken-com meteors

Files

meteors / test / helpers.js

helpers.js

helpers.js 1.3 KiB Download Raw
1'use strict';
2
3const Planet = require('../src/planet.js');
4const Rng = require('../src/rng.js');
5const { GameCore } = require('../src/gameCore.js');
6
7function makeGame (opts)
8{
9 opts = opts || {};
10 const planet = opts.planet || Planet.createPlanet(opts.planetOpts || {
11 jumpsToClear: 2,
12 fallTime: 3,
13 riseTime: 1.5
14 });
15 const rng = typeof opts.seed === 'number' ? Rng.create(opts.seed) : (opts.rng || Rng.create(1));
16 const game = new GameCore(planet, rng);
17 if (opts.noSpawn)
18 { game.spawnInterval = 1e9; }
19 return game;
20}
21
22/** Fill a column with `count` still blocks of rotating colors starting at 0. */
23function fillColumn (game, col, count, colors)
24{
25 colors = colors || [0, 1, 2, 3, 4];
26 const out = [];
27 for (let r = 0; r < count; r++)
28 { out.push(game.place(col, r, colors[r % colors.length])); }
29 return out;
30}
31
32function placeRow (game, row, cols, color)
33{
34 const out = [];
35 for (const c of cols)
36 { out.push(game.place(c, row, color)); }
37 return out;
38}
39
40function grayFlying (game)
41{
42 return game.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY);
43}
44
45function almost (a, b, eps)
46{
47 eps = eps == null ? 1e-6 : eps;
48 return Math.abs(a - b) <= eps;
49}
50
51module.exports = {
52 makeGame,
53 fillColumn,
54 placeRow,
55 grayFlying,
56 almost,
57 Planet,
58 Rng,
59 GameCore
60};