Git Product home page Git Product logo

concept-ecs's Introduction

Proof of Concept ECS

An attempt at making an ECS that allows for simple multithreading in Javascript.

Simple Example

import {createWorld, createComponent, createEntity, addComponent, createQuery, removeComponent, removeEntity} from 'concept-ecs'

const maxEntityCount = 1_000_000

// The world object stores all state required by the ECS
const world = createWorld(maxEntityCount)

// Components are based on Typed Arrays. The createComponent function takes a schema of TypedArray constructors
const Position = createComponent(world, {x: Float32Array, y: Float32Array})
const Velocity = createComponent(world, {value: Float32Array})

// Entities are indexes which are used to store data in any component
const entityId = createEntity(world)
addComponent(world, Position, entityId)
addComponent(world, Velocity, entityId)
Velocity.value[entityId] = 2

// Queries will find all entities with specified components
const query = createQuery(world, [Position, Velocity])

// Systems can be simple functions which iterate over query results
function system() {
    const [entities] = query.run()
    for (let index = 0; index < entities.length; index += 1) {
        const entityId = entities[index]
        Position.x[entityId] += Velocity.value[entityId]
        console.log(Position.x[entityId])
    }
}

system()

// All components must be removed from an entity before the entity can be removed
removeComponent(world, Position, entityId)
removeComponent(world, Velocity, entityId)

// Entity ID's that are removed are placed on a stack and will be resused first before new entity ID's are issued
removeEntity(world, entityId)

How does it work

This ECS implementation utilizes typed arrays based on SharedArrayBuffers which can be shared between threads for simple multi-threading. All state is stored on either the World object, components, or query objects. The world object contains a map object of what components each entity has. Queries are run by doing a simple binary comparison on this map, which makes them quite fast.

Why use this library

This implementation is the only one that I'm aware of that stores all required state in SharedArrayBuffers which makes it the only available JS ECS which can be multithreaded. Because all state is confined to the world object, it also makes this library easy to use, reason about, and incorporate into projects. The use of typed arrays also makes the performance quite good. Its also only 200 lines of code, which makes it easy to understand or modify for your own needs.

What isn't included

This library only provides the Entities, Components, and Systems. It does not have any glue code for managing threading, serialization, networking, or anything else. I believe those are functions better served by an engine.

concept-ecs's People

Contributors

briancw avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.