platjs
TypeScript icon, indicating that this package has built-in type declarations

1.2.6 • Public • Published

platjs

A simple engine for platformer games and rendering

Demo

platjs demo


Installation:

npm install

npm install platjs
import * as plat from "platjs";

script tag

<script src="https://cdn.jsdelivr.net/npm/platjs"></script>

Use:

const { Renderer, ControlledBody, StaticBody } = plat;

// Create a renderer
// This handles physics and rendering for you.
const renderer = new Renderer()
  .mount(document.body)
  .enableFixedPosition()
  .enablePhysics({});

// Create a player
// Giving it a "color" property will make it render as that color.
const player = new ControlledBody({
  x: 30,
  y: 30,
  width: 30,
  height: 30,
  layer: 1,
  color: "blue",
});

// Add the player to the renderer's list of objects to draw / update
renderer.add(player);

// enable keyboard controls
player.bindKeyboardControls({});

// lock the camera to the player (player stays at center of the screen)
renderer.camera.lock(player);

// create a body for the player to land / jump on
renderer.add(
  new StaticBody({ x: 0, y: 500, width: 300, height: 100, color: "black" })
);

// rendering loop
const animationLoop = () => {
  // update physics
  renderer.update();

  // respawn player if needed
  if (player.y - player.height / 2 > renderer.height) {
    player.v.y = 0;
    player.v.x = 0;
    player.x = 30;
    player.y = 30;
  }

  // draw everything
  renderer.render();

  requestAnimationFrame(animationLoop);
};

requestAnimationFrame(animationLoop);

Package Sidebar

Install

npm i platjs

Weekly Downloads

8

Version

1.2.6

License

MIT

Unpacked Size

1.68 MB

Total Files

93

Last publish

Collaborators

  • fezzle