0.3.0 • Published 12 months ago

@peasy-lib/peasy-engine v0.3.0

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
12 months ago

Peasy Engine

This is the repository for Peasy Engine, a small-ish and relatively easy to use engine library.

Introduction

Peasy Engine provides the possibility to create (fixed step) engines that will invoke a callback at a desired interval.

First look

import { Engine } from '@peasy-lib/peasy-engine';

Engine.create((deltaTime: number) => {
  // Called 60 times per second with deltaTime = 16.667
});

Getting started

If you've got a build process and are using npm, install Peasy Engine with

npm i @peasy-lib/peasy-engine

and import it into whichever files you want to use it

import { Engine } from '@peasy-lib/peasy-engine';

If you don't have a build process or don't want to install it, use a script tag

<script src="https://unpkg.com/@peasy-lib/peasy-engine">

to make Engine available.

Features and syntax

Peasy Engine enables creating one or more (fixed step) engines and specify individual callbacks and callback intervals for each one. In addition, an engine can be manually started, stopped and paused.

function update(deltaTime: number) {
  // Update physics based on delta time
}

function render() {
  if (Input.is('menu')) {
    physicsEngine.pause();
  } else if (Input.is('close-menu')) {
    physicsEngine.start();
  }

  // Draw/create ui
}

const renderEngine = Engine.create(render);
const physicsEngine = Engine.create({ fps: 240, callback: update, started: false });

// Create world

physicsEngine.start();

// Clean up
renderEngine.destroy();
physicsEngine.destroy();

Setting oneTime to true will make an engine fire its callback once and then self-destruct.

const trapEngine = Engine.create({ ms: 5000, callback: triggerTrap, oneTime: true });

trapEngine.pause(); // Now time is paused for this engine

trapEngine.start(); // Time starts ticking again

In addition to controlling individual engines, Peasy Engine provides methods to control all engines that haven't been created with isolated set to true.

const renderEngine = Engine.create({ fps: 60, callback: render, isolated: true });
Engine.create({ fps: 240, callback: updatePhysics });
Engine.create({ ms: 5000, callback: triggerTrap, oneTime: true });

// Pause and start all engines except renderEngine
if (Input.is('menu')) {
  Engine.pause();
} else if (Input.is('close-menu')) {
  Engine.start();
}

// Clean up
renderEngine.destroy();
Engine.destroy();

Development and contributing

If you're interested in contributing, please see the development guidelines.

0.3.0

12 months ago

0.2.0

1 year ago

0.1.0

1 year ago