# @peasy-lib/peasy-engine

> An easy peasy engine library.

Latest version **0.3.0** (published 2023-05-27) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install @peasy-lib/peasy-engine
pnpm add @peasy-lib/peasy-engine
yarn add @peasy-lib/peasy-engine
bun add @peasy-lib/peasy-engine
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2023-05-27 |
| First published | 2023-04-14 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.17.0 |
| Dependencies | 0 |
| Unpacked size | 32.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Jürgen Wenzel |
| Maintainers | jwenzel |

## Links

- npm: https://www.npmjs.com/package/@peasy-lib/peasy-engine
- Repository: https://github.com/peasy-lib/peasy-lib.git#main
- Homepage: https://github.com/peasy-lib/peasy-lib/tree/main/packages/peasy-engine#readme
- Issues: https://github.com/peasy-lib/peasy-lib/issues
- npm.io page: https://npm.io/package/@peasy-lib/peasy-engine

## Recent versions

- 0.3.0 (latest) — 2023-05-27
- 0.2.0 — 2023-05-04
- 0.1.0 — 2023-04-14

## README

# 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

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

Engine.create((deltaTime: number) => {
  // Called 60 times per second with deltaTime = 16.667
});
```
<!-- TODO: Finish examples above -->

## 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

```ts
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

```html
<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.

```ts
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.
```ts
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`.
```ts
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](DEVELOPMENT.md).

---
_Source: https://npm.io/package/@peasy-lib/peasy-engine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
