import * as settings from './settings.js';
import {
  createView,
  getFrameRenderer,
} from './view.js';
import {
  separateBodies,
  simulateFrame,
} from './simulation.js';
import {
  getRandomBody,
} from './meebas/bodies.js';
import {
  range,
} from './utils/arrays.js';

const { width, height } = settings.tank;

const view = createView(width, height);
const renderFrame = getFrameRenderer(view);

/** @type {Object<string, any>} */
const anyWindow = window;
const MeebaFarm = {};
anyWindow.MeebaFarm = MeebaFarm;

MeebaFarm.bodies = range(settings.simulation.bodies).map(getRandomBody);
separateBodies(MeebaFarm.bodies);

/** @param {number} lastTick */
const simulate = (lastTick) => {
  const thisTick = performance.now();
  MeebaFarm.bodies = simulateFrame(MeebaFarm.bodies, lastTick, thisTick);
  setTimeout(() => simulate(thisTick), 8);
};
const render = () => {
  renderFrame(MeebaFarm.bodies);
  requestAnimationFrame(render);
};

// eslint-disable-next-line no-console
console.log('Simulating with seed:', settings.seed);
simulate(performance.now());
requestAnimationFrame(render);

import * as settings from './settings.js';
import {
  createView,
  getFrameRenderer,
} from './view.js';
import {
  separateBodies,
  simulateFrame,
} from './simulation.js';
import {
  getRandomBody,
} from './meebas/bodies.js';
import {
  range,
} from './utils/arrays.js';

const { width, height } = settings.tank;

const view = createView(width, height);
const renderFrame = getFrameRenderer(view);

/** @type {Object<string, any>} */
const anyWindow = window;
const MeebaFarm = {};
anyWindow.MeebaFarm = MeebaFarm;

MeebaFarm.bodies = range(settings.simulation.bodies).map(getRandomBody);
separateBodies(MeebaFarm.bodies);

/** @type {boolean} */
let isRunning;

/** @param {number} lastTick */
const simulate = (lastTick) => {
  if (isRunning) {
    const thisTick = performance.now();
    MeebaFarm.bodies = simulateFrame(MeebaFarm.bodies, lastTick, thisTick);
    setTimeout(() => simulate(thisTick), 8);
  }
};
const render = () => {
  if (isRunning) {
    renderFrame(MeebaFarm.bodies);
    requestAnimationFrame(render);
  }
};

MeebaFarm.pause = () => {
  isRunning = false;
};

MeebaFarm.resume = () => {
  isRunning = true;
  simulate(performance.now());
  requestAnimationFrame(render);
};

// eslint-disable-next-line no-console
console.log('Simulating with seed:', settings.seed);
MeebaFarm.resume();

Add global methods to pause and resume simulation
Add global methods to pause and resume simulation

These will eventually be hooked up to buttons in the DOM.

Signed-off-by: Zac Delventhal <593147d2808cd66b7e2ad59ca4b0c07d6c52cddd@gmail.com>
