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 if (opts.strictInvariants !== false)
20 { game.strictInvariants = true; }
21 return game;
22}
23
24/** Fill a column with `count` still blocks of rotating colors starting at 0. */
25function fillColumn (game, col, count, colors)
26{
27 colors = colors || [0, 1, 2, 3, 4];
28 const out = [];
29 for (let r = 0; r < count; r++)
30 { out.push(game.place(col, r, colors[r % colors.length])); }
31 return out;
32}
33
34function placeRow (game, row, cols, color)
35{
36 const out = [];
37 for (const c of cols)
38 { out.push(game.place(c, row, color)); }
39 return out;
40}
41
42function grayFlying (game)
43{
44 return game.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY);
45}
46
47function almost (a, b, eps)
48{
49 eps = eps == null ? 1e-6 : eps;
50 return Math.abs(a - b) <= eps;
51}
52
53module.exports = {
54 makeGame,
55 fillColumn,
56 placeRow,
57 grayFlying,
58 almost,
59 Planet,
60 Rng,
61 GameCore
62};