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

ecs-machina

v0.2.0

Published

Entity-Component-System Engine

Downloads

15

Readme

Coverage Status dependencies Status Maintainability

ECS-Machina

A zero-dependency TypeScript Entity-Component-System Library

Installation

npm install ecs-machina

Documentation

ECS-Machina is first and foremost a TypeScript library; it's meant to be used in TypeScript projects to take full advantage of its typing system.

I created this library to be used in TIC-80 projects, and so it has 0 dependency and is ES5-compatible. You can "install" it by copy-pasting the whole index.ts file in your own project.

World

// Create a world to hold your entities and components
const world = new World()

Components

Components must be declared through the Component() abstract factory. It returns a Component Factory, that can be used to create new components and to query the World.

// With explicit types
const Position = Component<{ x: number; y: number }>()

// With default values, and implied types
const Velocity = Component({ dx: 0, dy: 0 })

// You can also declare "tag" Components that have no attributes
const IsMonster = Component()

Entities

/*
 * Add Entities to your World
 */

// Without any component
const entityA = world.spawn()

// With one or several components
const entityB = world.spawn(
  Position({ x: 10, y: 25 }),
  Velocity({ x: 0, y: 1 })
)

// Or with the default values, if components provide them
const entityC = world.spawn(Position())

Queries & Systems

ECS-Machina does not have Systems, but has a simple query() function and a runSystems() helper.

// You can create a "system" like this, a simple function that
// takes the whole World as a parameter.
function movementSystem(world: World) {
  const entities = world.query(Position, Velocity)
  for (const [e, pos, vel] of entities) {
    console.log(`Moving entity ${e}`)

    // Component instances are correctly typed for efficient auto-completion
    pos.x += vel.dx
    pos.y += vel.dy
  }
}

function attackSystem(world: World) {
  /* ... */
}

// Call `runSystems` to execute Systems within your World context
world.runSystems(movementSystem, attackSystem)

Philosophy and goals

TypeScript First

Games built with an ECS engine have a lot of different components and systems. Strong typings (and efficient autocompletion) is an essential feature to not get lost in your own code.

Code reusability

We have a strong decoupling between the World, the Components, and the Systems. The goal is to let you write code that you can truly reuse throughout your projects.

Fast systems

The world.query() function has an efficient and transparent caching system, to provide the fastest iteration speed possible.

The trade-off is that adding/removing/updating entity is relatively slower, because the cache needs to be updated each time.

Strong unit tests

This is a work-in-progress (as ECS-Machina itself is under development), but we aim for 100% of code coverage.

Credits

This library was inspired by PECS, a PICO-8 ECS library.