1.0.56 • Published 2 years ago

@chantey/ecs v1.0.56

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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

//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

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.56

2 years ago

1.0.44

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.52

2 years ago

1.0.43

3 years ago

1.0.40

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.33

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.29

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.12

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago