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

@arancini/systems

v6.6.1

Published

Systems for arancini

Downloads

52

Readme

@arancini/systems

Systems helpers for arancini.

Installation

@arancini/systems can either be used via the umbrella package arancini, or installed separately.

> npm install @arancini/systems

Introduction

In arancini/systems, systems are classes that extend System. Systems are added to an Executor, and are updated when calling executor.update().

import { World } from "arancini";
import { Executor, System } from "arancini/systems";

type Entity = {
  position?: { x: number; y: number };
  velocity?: { x: number; y: number };
};

// Create a World
const world = new World<Entity>();

// Create an Executor
const executor = new Executor(world);

// Define a System
class MovementSystem extends System<Entity> {
  moving = this.query((e) => e.has("position", "velocity"));

  onUpdate(delta: number, time: number) {
    for (const entity of this.moving) {
      const position = entity.get("position");
      const velocity = entity.get("velocity");

      position.x += velocity.x;
      position.y += velocity.y;
    }
  }

  onInit() {
    // Called when the Executor is initialised, or when the system is added to an already initialised Executor
  }

  onDestroy() {
    // Called when the Executor is destroyed, or the system is removed
  }
}

// Add the system to the executor
executor.add(MovementSystem);

// Initialise the executor
executor.init();

// Update all systems, optionally passing a delta time
executor.update(1 / 60);

// Destroy the executor
executor.destroy();

System priority

Systems can be added with a priority. The order systems run in is first determined by priority, then by the order systems were added.

executor.add(MovementSystem, { priority: 10 });

Required system queries

If a system should only run when a query has results, you can mark the query as required. Then, onUpdate will only be called if the query has at least one result.

Note: onInit and onDestroy will be called regardless of whether required queries have results.

class ExampleSystem extends System<Entity> {
  requiredQuery = this.query((e) => e.has("position"), { required: true });

  onUpdate() {
    // we can safely assume that the query has at least one result
    const { x, y } = this.requiredQuery.first;
  }
}

Singleton Queries

The this.singleton utility method creates a query for a single component, and sets the property to the given component on the first matching entity.

class CameraRigSystem extends System<Entity> {
  camera = this.singleton("camera", { required: true });

  onUpdate() {
    console.log(camera);
  }
}

Note: Singleton components must be defined on a top-level property of the system. The property must not be a ES2022 private field (prefixed with #).

Attaching systems to other systems

Systems can be attached to other systems with this.attach. This is useful for sharing logic or system state that doesn't belong in a component.

class ExampleSystem extends System<Entity> {
  otherSystem = this.attach(OtherSystem);

  onInit() {
    this.otherSystem.foo();
  }
}