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

minecs

v0.0.18

Published

Welcome to minECS, a fully serializable entity component system designed for type safety and efficient state management. Originally forked from bitECS, minECS utilizes schema classes to enhance the structure and reliability of your game's data.

Downloads

245

Readme

minECS: Typed State Entity Component System

Welcome to minECS, a fully serializable entity component system designed for type safety and efficient state management. Originally forked from bitECS, minECS utilizes schema classes to enhance the structure and reliability of your game's data.

Defining Components

In minECS, components are easily defined using the Schema class and the @Component decorator. These components automatically register when you import them into your project. Here are examples of how to define components for position and velocity:

@Component()
class Position extends Schema {
  @type("number")
  @defaultValue(0)
  x: number;

  @type("number")
  @defaultValue(0)
  y: number;
}

@Component()
class Velocity extends Schema {
  @type("number")
  @defaultValue(0)
  x: number;

  @type("number")
  @defaultValue(0)
  y: number;
}

Systems

Systems in minECS can operate manually or within the engine. Use the @System decorator to specify which components a system requires. Systems are dynamically recreated for each world instance to facilitate specific interactions:

@System(Position, Velocity)
class MovementSystem extends SystemImpl {
  run = (world: World, entity: number) => {
    const position = world(Position, entity);
    const velocity = world(Velocity, entity);

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

Managing Worlds

Worlds manage the complete state of your game, letting you add and remove entities and components:

const world = createWorld();
const entity = addEntity(world);

addComponent(world, Position, entity);
addComponent(world, Velocity, entity, {
  x: 30,
  y: 30,
});

console.log({ ...world(Position, entity) }); // { x: 0, y: 0, type: "Position" }

stepWorld(world);

console.log({ ...world(Position, entity) }); //  { x: 30, y: 30, type: "Position" }

Serialization

minECS supports multiple serialization formats: JSON, Binary, and base64. This feature allows you to save and restore game states easily:

const json = serializeWorld(SerialMode.JSON, world);
const buffer = serializeWorld(SerialMode.BINARY, world);
const base64 = serializeWorld(SerialMode.BASE64, world);

const newWorldFromJSON = deserializeWorld(json);
const newWorldFromBinary = deserializeWorld(buffer);
const newWorldFromBase64 = deserializeWorld(base64);

Additional Features and Systems

In addition to the core functionalities, minECS offers a suite of advanced features to provide more control and flexibility in game development. These include system execution controls, manual and automated system runs, system initialization and cleanup, advanced component types, and detailed entity management. Explore these features to fully leverage the power of minECS in your projects.

System Execution Control with Depth

In minECS, the depth property of a system determines the order and automatic execution behavior. Systems with different depth values are executed in ascending order, where lower numbers run first.

@System(...)
class SomeSystem extends SystemImpl {
  static depth = 0;  // Lower numbers run first, executed automatically
}

Manual Execution Only: If a system is assigned a depth less than 0 it will not run automatically during the standard run cycle. Instead, it must be run manually. This is useful for systems that require explicit control or should only execute under specific conditions:

@System(...)
class ManualOnlySystem extends SystemImpl {
  static depth = -1;  // This system will only run when manually triggered

  run = (world: World, eid: number) => {
    // Implementation details
  };
}

To run a system manually, you can use the run or runAll methods of the system instance, providing flexibility in how and when certain parts of your game logic are executed:

const world = createWorld();
const system = getSystem(world, ManualOnlySystem);
system.runAll(world); // Runs the system manually for all entities
system.run(world, entity); // Runs the system manually for a specific entity

System Initialization

Systems can have an initialization function that runs once when a new entity that matches the system's criteria is added:

@System(...)
class SomeInitializationSystem extends SystemImpl {
  init = (world: World, eid: number) => {
    // Initialization code here
  };
}

System Cleanup

Systems can define a cleanup function that executes when a component is removed or an entity is deleted. This helps in managing state cleanup reliably:

@System(...)
class SomeCleanupSystem extends SystemImpl {
  cleanup = (world: World, eid: number) => {
    // Cleanup code here
  };
}

Advanced Component Types

Enhance your components with nested structures, nullable options, and various data types for more complex scenarios:

@Component()
class Complex extends Schema {
  @type(Position)
  position: Position;

  @type("string")
  @defaultValue("hello")
  message: string;

  @type(["number"])
  numbers: number[];

  @type("boolean")
  @defaultValue(true)
  flag: boolean;

  @type("object")
  obj: any;

  @type("string")
  @nullable()
  nullableString: string | null;
}

@Component()
class NestedComplex extends Schema {
  @type(Complex)
  complex: Complex;

  @type([Complex])
  complexes: Complex[];
}

Entity and Component Management

The library provides comprehensive functions to manage entities and components dynamically:

const world = createWorld();
const entity = addEntity(world);
addComponent(world, SomeComponent, entity);
removeComponent(world, SomeComponent, entity);
removeEntity(world, entity); // Cleans up all components associated with the entity

These functions give you full control over the entities and components in the system, ensuring that you can dynamically adjust the game world as needed.