# @chantey/ecs

> Easy ECS is an Entity Component System architecture putting the Developer Experience first, with intuitive queries and component types.

Latest version **1.0.56** (published 2022-01-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install @chantey/ecs
pnpm add @chantey/ecs
yarn add @chantey/ecs
bun add @chantey/ecs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.56 |
| Published | 2022-01-20 |
| First published | 2021-08-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 61 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mrchantey |

## Links

- npm: https://www.npmjs.com/package/@chantey/ecs
- npm.io page: https://npm.io/package/@chantey/ecs

## Dependencies (15)

- [jest](https://npm.io/package/jest.md) ^27.0.6
- [eslint](https://npm.io/package/eslint.md) ^7.32.0
- [ts-jest](https://npm.io/package/ts-jest.md) ^27.0.4
- [ts-node](https://npm.io/package/ts-node.md) ^10.1.0
- [typescript](https://npm.io/package/typescript.md) ^4.3.5
- [@jest/types](https://npm.io/package/@jest/types.md) ^27.0.6
- [@types/jest](https://npm.io/package/@types/jest.md) ^26.0.24
- [@chantey/core](https://npm.io/package/@chantey/core.md) ^1.0.12
- [jest-extended](https://npm.io/package/jest-extended.md) ^0.11.5
- [utility-types](https://npm.io/package/utility-types.md) ^3.10.0
- [eslint-plugin-react](https://npm.io/package/eslint-plugin-react.md) ^7.26.1
- [jest-summary-reporter](https://npm.io/package/jest-summary-reporter.md) ^0.0.2
- [@testing-library/react](https://npm.io/package/@testing-library/react.md) ^12.1.2
- [@typescript-eslint/parser](https://npm.io/package/@typescript-eslint/parser.md) ^5.0.0
- [@typescript-eslint/eslint-plugin](https://npm.io/package/@typescript-eslint/eslint-plugin.md) ^5.0.0

## Recent versions

- 1.0.56 (latest) — 2022-01-20
- 1.0.55 — 2021-12-31
- 1.0.54 — 2021-12-30
- 1.0.53 — 2021-12-29
- 1.0.52 — 2021-12-02
- 1.0.51 — 2021-11-30
- 1.0.50 — 2021-11-19
- 1.0.49 — 2021-11-19
- 1.0.48 — 2021-11-18
- 1.0.47 — 2021-11-18
- 1.0.46 — 2021-11-18
- 1.0.45 — 2021-11-18
- 1.0.44 — 2021-11-18
- 1.0.43 — 2021-09-28
- 1.0.42 — 2021-08-29
- … 39 more at https://npm.io/package/@chantey/ecs/versions

## README

# Easy ECS

Easy ECS is an Entity Component System architecture putting the Developer Experience first, with intuitive queries and component types.

## Overview

The two benefits that have made ECS architecture famous are *performance* and code *simplicity*. We are focusing on the latter, allowing access to the query based workflow without constraining components to value types, or requiring explicit type schemas to be declared. This is especially suited to rendering libraries like babylon.js and three.js, where the data to be worked on is usually a predefined object like a Vector3 or Material.

## Getting Started

```js
//1. define components
const PlayerComp = defineTagComponent()
const EnemyComp = defineTagComponent()
const HealthComp = defineComponent({value:100})
const PositionComp = defineComponent({x:0})

//2. define queries
const PlayerQuery = defineQuery({
    player:PlayerComp
    health:HealthComp,
    position:PositionComp
})
const EnemyQuery = defineQuery({
    enemy:EnemyComp
    health:HealthComp,
    position:PositionComp
})

//3. define systems
const DamageSystem = defineSystem(world=>{
    
    //4. (optional) create 'tuples' for working on related pairs of entities 
    const playerEnemyTuple = createQueryTuple({playerQuery,enemyQuery})
    
    return {
        update:()=>{
            world.query(PlayerQuery).forEach(({position})=>
                position.x += 0.01    
            )

            playerEnemyTuple.forEach(({playerQuery,enemyQuery})=>{
                if(Math.abs(enemyQuery.position.x - playerQuery.position.x) < 5)
                    playerQuery.health.value -= 10
            })
        }
    }
})

//5. define a small module, to be combined with others in the composition of a sketch
const damageModule = defineModule({
    components:{PlayerComp,EnemyComp,PositionComp,HealthComp},
    queries:{PlayerQuery,EnemyQuery},
    systems:{DamageSystem}
})

//6. create your world and entities
const world = createWorld(damageModule)

world.createEntity()
    .addComponent(PlayerComp)
    .addComponent(HealthComp,{value:100})
    .addComponent(PositionComp,{x:30})

world.createEntity()
    .addComponent(EnemyComp)
    .addComponent(HealthComp,{value:20})
    .addComponent(PositionComp,{x:50})

//7. Begin the render loop!
world.start()
setTimeout(world.update,16)
```




## TODO
- Print Component Table
- Queries
  - Currently query maps are for component instances, shouldnt they be for component definitions?
- Default Values
    - This will not work as expected because reference types, use factories?
    - functions not working - vec = defineComponent(()=>new Vector3())
    - it was 'sorta' overriding when i set the values ie entity.set(vec,myVector3)

## Reference
- [Overwatch - Singleton Components](https://youtu.be/W3aieHjyNvw?t=741)
    - 40% of components are singletons

---
_Source: https://npm.io/package/@chantey/ecs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
