npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angry-pixel

v2.0.14

Published

Lightweight 2D game engine made with TypeScript.

Downloads

518

Readme

Angry Pixel

NPM Version License Docs Actions

What is Angry Pixel Engine?

Angry Pixel Engine is a 2D game engine for browser written in Typescript.

Main features:

  • Entity-Component-System based architecture
  • WebGL rendering
  • Sprite-based graphics and frame-by-frame animations
  • Text rendering based on bitmap fonts
  • Basic 2D lightning system
  • Polygonal collision detection and physical responses based on speed and acceleration.
  • Keyboard, mouse, gamepad and touch screen input support
  • Ability to create desktop/mobile games using frameworks such as Electron.js
  • Tilemaps: rendering based on comma separated values, and automatic collider generation. Support for JSON files exported from Tiled.
  • Dependency Injection

Getting Started

Let's create a scene where the Angry Pixel logo bounces off the edges of the screen in a DVD screensaver fashion.

Installation

npm i angry-pixel

or

yarn add angry-pixel

Initialize

First we create an instance of the Game class:

import { Game, GameConfig } from "angry-pixel";

const config: GameConfig = {
    containerNode: document.querySelector("#app"),
    width: 1920,
    height: 1080,
    canvasColor: "#00D9D9",
};

const game = new Game(config);

Create a Scene

Then we will create the MainScene class, which extends the Scene base class. This class represents a scene in our game, and has three main functions:

  • To load assets.
  • To create the initial entities.
  • To know which are the necessary systems.

For the moment we only implement the function loadAssets to load an image that we will use later:

import { Scene } from "angry-pixel";

class MainScene extends Scene {
    // within this method we load the assets
    loadAssets(): void {
        this.assetManager.loadImage("logo.png");
    }
}

And we add this scene to our game:

// arguments: the scene class, the scene name, opening scene = true
game.addScene(MainScene, "MainScene", true);

Create a Component

Then we will create the MoveAndBounce component which has the necessary attributes to define the movement of our entity.

import { Vector2 } from "angry-pixel";

class MoveAndBounce {
    boundaries: number[] = [476, -476, 896, -896]; // top, bottom, left, right
    direction: Vector2 = new Vector2(1, 1); // the direction in wich the entity will move
    speed: number = 200; // pixels per second
}

Create a System

Once we have created our component, we will need a system that executes the business logic. Extending the base class GameSystem, we will create our MoveAndBounceSystem, which, using the EntityManager, obtains all the entities that have the MoveAndBounce component, and executes the business logic necessary for the entity to move by bouncing on the edges of the screen:

import { GameSystem, Transform } from "angry-pixel";

class MoveAndBounceSystem extends GameSystem {
    onUpdate(): void {
        this.entityManager.search(MoveAndBounce).forEach(({ component, entity }) => {
            const transform = this.entityManager.getComponent(entity, Transform);
            const { boundaries, direction, speed } = component;

            if (transform.position.y >= boundaries[0] || transform.position.y <= boundaries[1]) {
                direction.y *= -1;
            }

            if (transform.position.x >= boundaries[2] || transform.position.x <= boundaries[3]) {
                direction.x *= -1;
            }

            transform.position.x += direction.x * speed * this.timeManager.deltaTime;
            transform.position.y += direction.y * speed * this.timeManager.deltaTime;
        });
    }
}

Once the system is created, we can add it to the scene

import { Scene } from "angry-pixel";

class MainScene extends Scene {
    loadAssets(): void {
        this.assetManager.loadImage("logo.png");
    }

    // within this method we load the systems of the scene
    loadSystems(): void {
        this.systems.push(MoveAndBounceSystem);
    }
}

Create the entities

Finally, we need to create two entities, one that represents our logo, to which we want to apply the behavior of moving and bouncing, and another one that represents the camera of our game. For this we will use the EntityManager, specifically the createEntity method. This method accepts an array of components, the components can be concrete instances (this is useful when it is necessary to pass parameters by constructor) or classes (if it is not necessary to pass parameters by constructor, we can pass only the class).

import { Camera, Scene, SpriteRenderer, Transform } from "angry-pixel";

class MainScene extends Scene {
    loadAssets(): void {
        this.assetManager.loadImage("logo.png");
    }

    loadSystems(): void {
        this.systems.push(MoveAndBounceSystem);
    }

    // within this method we create the entities
    setup(): void {
        // camera
        this.entityManager.createEntity([new Transform(), new Camera({ layers: ["Logo"] })]);

        // logo
        this.entityManager.createEntity([
            new Transform(),
            new MoveAndBounce(),
            new SpriteRenderer({
                layer: "Logo",
                image: this.assetManager.getImage("logo.png"),
            }),
        ]);
    }
}

Run the game

Now we can start the game:

game.run();

Check this example live

🎮 https://angrypixel.gg/angry-pixel-logo-bounce

API DOCS

🔎 https://angrypixel.gg/engine/api-docs