コード例 #1
0
ファイル: Game.js プロジェクト: easco/c3tr
function getInitialModel() {
  const worldHeight = 768;
  const worldWidth = 1024;

  const world = Util.logTime('World generation', () =>
    DefaultWorld.generate(worldWidth, worldHeight)
  );

  const entities = Util.logTime('World population', () =>
    DefaultWorld.populate(world)
  );

  const carlPos = DefaultWorld.startingPosition(world, entities);

  return {
    state: {
      entities: entities.concat(Carl.create({ position: carlPos })),
      focus: Focus.GAME,
      keydown: null,
      messages: [],
      worldHeight,
      worldWidth
    },
    world
  };
}
コード例 #2
0
ファイル: Game.js プロジェクト: easco/c3tr
function init() {
  let drawFrame = null;
  let model = getInitialModel();

  function draw() {
    if (drawFrame) window.cancelAnimationFrame(drawFrame);
    drawFrame = window.requestAnimationFrame(renderViews.bind(null, model));
  }

  function handleKey(keydown) {
    window.removeEventListener('keydown', handleKey);
    model = Util.merge(model, { state: Util.merge(model.state, { keydown }) });
    model = update(model);
    renderViews(model);
    window.requestAnimationFrame(() => {
      window.addEventListener('keydown', handleKey);
    });
  }

  window.addEventListener('keydown', handleKey);

  window.addEventListener('resize', () => {
    resizeField(window.innerWidth, window.innerHeight);
    draw();
  });

  resizeField(window.innerWidth, window.innerHeight);
  Util.logTime('Initial render', () => renderViews(model));
}