daishihmr / bulletml.js2

JavaScript BulletML library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bulletml.js 2.0

usage

<!-- import library -->
<script src="./build/bulletml.js"></script>

<script>
window.onload = async () => {

  // load xml file
  const res = await fetch(url);
  const xml = await res.text();

  // parse
  const bulletml = BulletML.parse(xml);

  // player
  const player = { x: 0, y: 0 };

  // create manager
  const manager = new BulletML.Manager({ player });

  // enemy
  const enemy = new BulletML.Bullet();

  // run
  manager.run(enemy, bulletml);

  const bullets = [];

  // on fire bullet
  manager.onFire = (params) => {
    const { bullet } = params;
    console.log(`${ bullet.x }, ${ bullet.y }`);

    // on execute <vanish> tag
    bullet.onVanish = () => {
      doVanishProcess();
    };

    bullets.push(bullet);
  };

  let last = Date.now();
  const loop = () => {
    requestAnimationFrame(loop);

    const now = Date.now();
    const deltaTime = now - last;
    last = now;

    // update 1 frame
    manager.update(deltaTime);

    bullets.forEach((bullet) => {

      if (isOutOfScreen(bullet)) {
        // delete bullet
        bullet.destroy();
      }

    });
  };
  loop();

};
</script>

build

rollup --config

watch

rollup --config --watch

About

JavaScript BulletML library.


Languages

Language:JavaScript 90.5%Language:HTML 9.5%