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

react-entity-component-system

v2.0.0

Published

Entity Component System for React to make games or other interactive components.

Downloads

20

Readme

React Entity Component System

An ECS hook for React to make games or other interactive components.

yarn add react-entity-component-system

Play Breakout Demo

Why

It's fun to build games with React, and people have become successful at it. The ECS pattern is well known and battle tested for game development. This library is a loose implementation for React. You can check out the Breakout storybook story to see a fairly complex example.

Usage

import React from 'react'
import { useEntityComponentSystem } from 'react-entity-component-system'

ECS in general has three basic concepts:

  1. Entities represent things in a scene
  2. Components (not React components) are data structures, composed to create entities.
  3. Systems are functions that operate on entities during every update

In this implementation, an entity is defined as a plain object with at least a Renderer property (a React component). Other properties are components that will be passed as props to the Renderer:

const counterEntity = {
  Renderer: props => <h4>{props.count}</h4>,
  count: 0,
}

A system is just a function that operates on entities in the scene each frame. Systems are allowed to mutate the entities' components (thanks to immer!):

function frameCounterSystem({ entities }) {
  entities.forEach(entity => entity.count++)
}

The useEntityComponentSystem hook manages the CRUD (creating, rendering, updating and destroying the entities). Pass it some initial entities and systems, receive the renderable result and an updater function:

const initialEntities = [counterEntity]
const systems = [frameCounterSystem]

export default function BasicECS() {
  const [entities, updater] = useEntityComponentSystem(initialEntities, systems)

  return (
    <div>
      <button onClick={() => updater()}>Next Frame</button>
      {entities}
    </div>
  )
}

When the updater is called, all the systems are called with the following object as the first argument:

const systemArgs = {
  entities: Object.values(entitiesDraft),
  entitiesMap: entitiesDraft,
  createEntity,
  destroyEntity,
  ...userArgs,
}

The updater takes and object or function (which should return an object) and passes it to the systems (...userArgs as shown above) so they can have access to many other things. For example, the provided useKeysDown hook:

const keysDown = useKeysDown()
updater({
  keysDown,
})

Typically, the updater is called inside a loop via the provided useGameLoop hook. Be sure the function you pass is not redefined during every render. In most cases, you should wrap the updater call in React.useCallback:

function Game(scene) {
  const [entities, updater] = useEntityComponentSystem(...scene)
  const update = useCallback(elapsedTime => updater({ elapsedTime }), [updater])
  useGameLoop(update)

  return <>{entities}</>
}

See this example and more in the stories folder

Storybook

While react-entity-component-system is in development, you can check out the storybook to get a better sense of how things work.

git clone https://github.com/mattblackdev/react-entity-component-system.git
cd react-entity-component-system
yarn
yarn start

API

useEntityComponentSystem

useGameLoop

useGameEvents

useKeysDown

Debug

Contributing

I welcome any ideas and would really love some help with:

  1. Adding Typescript types
  2. Performance benchmarking and optimizations
  3. More game engine API like:
    • Keeping track of "entity filters" for systems
    • MatterJS or other physics lib integration