1- # flecs
1+ # Flecs
2+
3+ [ ![ Build Status] ( https://travis-ci.com/DavidPeicho/flecs.svg?branch=main )] ( https://travis-ci.com/DavidPeicho/flecs )
4+
25
36> 🚧 Flecs is a work-in-progress and might be unstable, use it at your
47> own risks 🚧
58
69Fast & Flexible EntityComponentSystem (ECS) for JavaScript and Typescript, available in browser and Node.js.
710
811Get started with:
9- * The [ Documentation] ( ./doc .md )
10- * The [ Examples] ( ./examples )
12+ * The [ Documentation] ( ./DOC .md )
13+ * The [ Examples] ( ./example )
1114
1215> 🔍 I am currently looking for people to help me to identify their needs in order to drive the development of this [ library further] ( #stable-version ) .
1316
@@ -35,14 +38,117 @@ The library will prioritize stability improvements over feature development.
3538* System Grouping
3639* System Topological Sorting
3740* Automatic Component Registration
41+ * Component Properties Merging
42+ * System Queries Merging
3843* TypeScript Decorators
3944 * For component properties
4045 * For system ordering and configuration
4146
47+ ## Install
48+
49+ Using npm:
50+
51+ ``` sh
52+ npm install flecs
53+ ```
54+
55+ Using yarn
56+
57+ ``` sh
58+ yarn add flecs
59+ ```
60+
61+ The library is distributed as an ES6 module, but also comes with two UMD builds:
62+ * ` fecs/umd/fecs.js ` → Development build with debug assertions
63+ * ` fecs/umd/fecs.min.js ` → Minified production build, without debug assertions
64+
4265## Usage Example
4366
4467``` js
45- class Component {}
68+ import {
69+ ComponentData ,
70+ TagComponent ,
71+ NumberProp ,
72+ RefProp ,
73+ World
74+ } from ' flecs' ;
75+
76+ /**
77+ * Components definition.
78+ */
79+
80+ class Position2D extends ComponentData {}
81+ Position2D .Properties = {
82+ x: NumberProp (),
83+ y: NumberProp ()
84+ };
85+
86+ class FollowTarget extends ComponentData {}
87+ FollowTarget .Properties = {
88+ target: RefProp (),
89+ speed: NumberProp (1.0 )
90+ };
91+
92+ class PlayerTag extends TagComponent {}
93+ class ZombieTag extends TagComponent {}
94+
95+ /**
96+ * Systems definition.
97+ */
98+
99+ class ZombieFollowSystem extends System {
100+
101+ execute (delta ) {
102+ this .queries .zombies .execute ((entity ) => {
103+ const { speed , target } = entity .read (FollowTarget);
104+ const position = entity .write (Position2D);
105+ const deltaX = target .x - position .x ;
106+ const deltaY = target .y - position .y ;
107+ const len = Math .sqrt (deltaX * deltaX + deltaY * deltaY);
108+ if (len >= 0.00001 ) {
109+ position .x += speed * delta * (deltaX / len);
110+ position .y += speed * delta * (deltaY / len);
111+ }
112+ });
113+ }
114+
115+ }
116+ ZombieFollowSystem .Queries = {
117+ // Select entities with all three components `ZombieTag`, `FollowTarget`, and
118+ // `Position2D`.
119+ zombies: [ZombieTag, FollowTarget, Position2D]
120+ }
121+
122+ const world = new World ();
123+
124+ // Creates a player entity.
125+ const playerEntity = world .create ().add (PlayerTag).add (Position2D);
126+ const playerPosition = playerEntity .read ();
127+
128+ // Creates 100 zombies at random positions with a `FollowTarget` component that
129+ // will make them follow our player.
130+ for (let i = 0 ; i < 100 ; ++ i) {
131+ world .create ()
132+ .add (ZombieTag)
133+ .add (Position2D, {
134+ x: Math .floor (Math .random () * 50.0 ) - 100.0 ,
135+ y: Math .floor (Math .random () * 50.0 ) - 100.0
136+ })
137+ .add (FollowTarget, { target: playerPosition })
138+ }
139+
140+ // Runs the animation loop and execute all systems every frame.
141+
142+ let lastTime = 0.0 ;
143+ function loop () {
144+ const currTime = performance .now ();
145+ const deltaTime = currTime - lastTime;
146+ lastTime = currTime;
147+ world .execute (deltaTime);
148+ requestAnimationFrame (loop);
149+ }
150+ lastTime = performance .now ();
151+ loop ();
46152```
47153
48154## Stable Version
@@ -61,7 +167,7 @@ Please feel free to reach out directly in the [Github Issues](https://github.com
61167
62168## Benchmarks
63169
64- TODO
170+ Coming soon.
65171
66172## Contributing
67173
0 commit comments