@jael-ecs/core 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -12
- package/dist/SystemManager.d.ts +2 -1
- package/dist/jael-build.cjs +1 -1
- package/dist/jael-build.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# Jael (Just Another ECS Library)
|
|
4
4
|
|
|
5
|
-
[](https://badge.fury.io/js/@jael-ecs%2Fcore)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
[](https://www.typescriptlang.org/)
|
|
8
8
|
|
|
@@ -32,18 +32,18 @@ _A modern, performant, and user-friendly Entity Component System library written
|
|
|
32
32
|
|
|
33
33
|
- **User Friendly API** - Clean, fluent api that's easy to learn
|
|
34
34
|
- **High Performance** - Optimized SparseSet implementation for fast entity lookups
|
|
35
|
-
- **Minimal Bundle size** - Compact bundle size without dependencies.
|
|
35
|
+
- **Minimal Bundle size** - Compact bundle size without dependencies.(34kb 📦)
|
|
36
36
|
|
|
37
37
|
## Installation
|
|
38
38
|
|
|
39
39
|
```bash
|
|
40
|
-
npm install @jael/core
|
|
40
|
+
npm install @jael-ecs/core
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
## Quick Start
|
|
44
44
|
|
|
45
45
|
```typescript
|
|
46
|
-
import { World, System } from "@jael/core";
|
|
46
|
+
import { World, System } from "@jael-ecs/core";
|
|
47
47
|
|
|
48
48
|
// Create your world
|
|
49
49
|
const world = new World();
|
|
@@ -71,15 +71,15 @@ world.addComponent(enemy, "velocity", { dx: -1, dy: 0 });
|
|
|
71
71
|
// Create a system
|
|
72
72
|
const movementSystem: System = {
|
|
73
73
|
priority: 0,
|
|
74
|
-
update(
|
|
74
|
+
update() {
|
|
75
75
|
const query = world.include("position", "velocity");
|
|
76
76
|
|
|
77
77
|
for (const entity of query.entities) {
|
|
78
78
|
const position = entity.get<Position>("position");
|
|
79
79
|
const velocity = entity.get<Velocity>("velocity");
|
|
80
80
|
|
|
81
|
-
position.x += velocity.dx * (
|
|
82
|
-
position.y += velocity.dy * (
|
|
81
|
+
position.x += velocity.dx * (Time.delta || 0.016);
|
|
82
|
+
position.y += velocity.dy * (Time.delta || 0.016);
|
|
83
83
|
}
|
|
84
84
|
},
|
|
85
85
|
};
|
|
@@ -88,8 +88,8 @@ const movementSystem: System = {
|
|
|
88
88
|
world.addSystem(movementSystem);
|
|
89
89
|
|
|
90
90
|
// Game loop
|
|
91
|
-
function gameLoop(
|
|
92
|
-
world.update(
|
|
91
|
+
function gameLoop() {
|
|
92
|
+
world.update();
|
|
93
93
|
}
|
|
94
94
|
```
|
|
95
95
|
|
|
@@ -199,8 +199,9 @@ Systems contain the game logic that processes entities with specific components.
|
|
|
199
199
|
```typescript
|
|
200
200
|
interface System {
|
|
201
201
|
priority: number; // Execution order (lower = earlier)
|
|
202
|
+
init?(): void // Runs when added to the world
|
|
202
203
|
exit?(): void; // Cleanup when removed
|
|
203
|
-
update(
|
|
204
|
+
update(): void; // Main update logic
|
|
204
205
|
}
|
|
205
206
|
```
|
|
206
207
|
|
|
@@ -210,6 +211,10 @@ interface System {
|
|
|
210
211
|
const renderSystem: System = {
|
|
211
212
|
priority: 100, // Render after all other systems
|
|
212
213
|
|
|
214
|
+
init(){
|
|
215
|
+
console.log('This runs first')
|
|
216
|
+
}
|
|
217
|
+
|
|
213
218
|
update(dt) {
|
|
214
219
|
const renderableQuery = world.include("position", "sprite");
|
|
215
220
|
|
|
@@ -404,7 +409,26 @@ class MovementSystem implements System {
|
|
|
404
409
|
this.movementQuery = world.include('position', 'velocity');
|
|
405
410
|
}
|
|
406
411
|
|
|
407
|
-
update(
|
|
412
|
+
update() {
|
|
413
|
+
for (const entity of this.movementQuery.entities) {
|
|
414
|
+
// Process movement
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// ✅ Also good: extend System for js Object:
|
|
420
|
+
|
|
421
|
+
type MovementSystem = { movementQuery: Query | null } & System;
|
|
422
|
+
|
|
423
|
+
const movementSystem: MovementSystem = {
|
|
424
|
+
movementQuery: null;
|
|
425
|
+
priority: 1;
|
|
426
|
+
|
|
427
|
+
init(){
|
|
428
|
+
this.movementQuery = world.include('position', 'velocity');
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
update(){
|
|
408
432
|
for (const entity of this.movementQuery.entities) {
|
|
409
433
|
// Process movement
|
|
410
434
|
}
|
|
@@ -412,7 +436,7 @@ class MovementSystem implements System {
|
|
|
412
436
|
}
|
|
413
437
|
|
|
414
438
|
// ✅ Also good: Use world.include/exclude for simple cases
|
|
415
|
-
update(
|
|
439
|
+
update() {
|
|
416
440
|
const entities = this.world.include('position', 'velocity');
|
|
417
441
|
// ...
|
|
418
442
|
}
|
package/dist/SystemManager.d.ts
CHANGED
package/dist/jael-build.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class h{_listeners=new Map;on(e,t){if(this.contains(e,t))return;const s=this._listeners.get(e);s?s.add(t):this._listeners.set(e,new Set([t]))}off(e,t){if(!this.contains(e,t))return;const s=this._listeners.get(e);s&&s.delete(t)}once(e,t){const s=(i=>{t(i),this.off(e,s)});this.on(e,s)}clearEvent(e){this._listeners.get(e)&&this._listeners.get(e)?.clear()}clearAllEvents(){this._listeners.forEach(e=>e.clear()),this._listeners.clear()}contains(e,t){return this._listeners.get(e)?this._listeners.get(e).has(t):!1}emit(e,t){this._listeners.get(e)&&this._listeners.get(e)?.forEach(s=>{s(t)})}}class u{systemList=[];addSystem(e){this.systemList.push(e),this.reorder()}reorder(){this.systemList.sort((e,t)=>e.priority-t.priority)}has(e){return this.systemList.indexOf(e)>0}removeSystem(e){if(!this.has(e))return;const t=this.systemList.indexOf(e);t>=0&&(this.systemList.splice(t,1),e.exit?.(),this.reorder())}}class d{denseValues=[];sparse=new Map;[Symbol.iterator](){let e=this.values.length;const t={value:void 0,done:!1};return{next:()=>(t.value=this.values[--e],t.done=e<0,t)}}get values(){return this.denseValues}first(){return this.denseValues[0]}add(e){this.has(e)||(this.denseValues.push(e),this.sparse.set(e,this.denseValues.length-1))}indexOf(e){return this.sparse.get(e)?this.sparse.get(e):-1}remove(e){if(!this.has(e))return;const t=this.sparse.get(e);this.sparse.delete(e);const s=this.denseValues[this.denseValues.length-1];s!==e&&(this.denseValues[t]=s,this.sparse.set(s,t)),this.denseValues.pop()}forEach(e){for(let t of this)e(t)}size(){return this.denseValues.length}clear(){for(let e of this)this.remove(e)}has(e){return this.sparse.has(e)}}class m{id;_world;constructor(e,t){this.id=t,this._world=e}add(e,t){this._world.addComponent(this,e,t)}remove(e){this._world.removeComponent(this,e)}has(e){return this._world.componentManager.hasComponent(this,e)}get(e){return this._world.componentManager.getComponent(this,e)}}class c extends h{entityMap=new d;nextId=0;_world;constructor(e){super(),this._world=e}get entities(){return this.entityMap}create(){const e=this.nextId++,t=new m(this._world,e);return this.entities.add(t),this.emit("create",t),t}exist(e){return this.entities.has(e)}size(){return this.entities.size()}destroy(e){return this.entities.remove(e),this.emit("destroy",e),e}}class l extends h{componentSet=new Map;world;constructor(e){super(),this.world=e,this.world.on("entityDestroyed",t=>{t&&t.entity&&this.componentSet.get(t.entity.id)&&this.componentSet.delete(t.entity.id)})}addComponent(e,t,s){const i=this.componentSet.get(e.id);i?i[t]=s:this.componentSet.set(e.id,{[t]:s}),this.emit("add",{entity:e,component:t})}getComponent(e,t){return this.hasComponent(e,t)?this.componentSet.get(e.id)[t]:void 0}hasComponent(e,t){const s=this.componentSet.get(e.id);return s?t in s:!1}removeComponent(e,t){if(!this.componentSet.get(e.id))return;const s=this.componentSet.get(e.id);s&&s[t]!==void 0&&(delete s[t],Object.keys(s).length===0&&this.componentSet.delete(e.id),this.emit("remove",{entity:e,component:t}))}}class a{config;entityMap;world;constructor(e,t){this.config=e,this.world=t,this.entityMap=new d}hasComponents(e){return this.config.include?.every(t=>e.has(t))&&this.config.exclude?.every(t=>!e.has(t))}get entities(){return this.entityMap}include(...e){return this.world.include(...e)}exclude(...e){return this.world.exclude(...e)}_checkExistingEntities(){for(let e of this.entities)this.world.exist(e)||this.entityMap.remove(e)}checkEntities(){for(let e of this.world.entities)e&&this.hasComponents(e)&&this.entityMap.add(e);this._checkExistingEntities()}static getHash(e){const t=e.include?.map(n=>n.trim()).filter(n=>n).join("_"),s=e.exclude?.map(n=>n.trim()).filter(n=>n).join("_"),i="in_"+t+"_out_"+s;let o=0;for(const n of i)o=(o<<5)-o+n.charCodeAt(0),o|=0;return o}}class p extends h{_startTime=0;_oldTime=0;_requestId=0;running=!1;delta=0;elapsed=0;constructor(){super()}_loop(){let e=0;if(this.running){const t=performance.now();e=(t-this._oldTime)/1e3,this._oldTime=t,this.elapsed+=e}this.delta=e,this.emit("update"),this._requestId=requestAnimationFrame(this._loop.bind(this))}start(){this._startTime=performance.now(),this._oldTime=this._startTime,this.elapsed=0,this.delta=0,this.running=!0,this._loop()}stop(){this.running=!1,cancelAnimationFrame(this._requestId),this._requestId=0}}let g=new p;class f extends h{entityManager;componentManager;systemManager;queries;constructor(){super(),this.entityManager=new c(this),this.componentManager=new l(this),this.systemManager=new u,this.entityManager.on("create",e=>{e&&this.emit("entityCreated",{entity:e}),this._updateQueries()}),this.entityManager.on("destroy",e=>{e&&this.emit("entityDestroyed",{entity:e}),this._updateQueries()}),this.componentManager.on("add",e=>{this.emit("componentAdded",{entity:e.entity,component:e.component}),this._updateQueries()}),this.componentManager.on("remove",e=>{this.emit("componentRemoved",{entity:e.entity,component:e.component}),this._updateQueries()}),this.queries=new Map}get entities(){return this.entityManager.entities}query(e){const t=a.getHash(e);let i=this.queries.get(t);return i||(i=new a(e,this),this.queries.set(t,i),this._updateQueries()),i}_updateQueries(){this.queries.forEach(e=>e.checkEntities())}exist(e){return this.entityManager.exist(e)}include(...e){return this.query({include:e,exclude:[]})}exclude(...e){return this.query({include:[],exclude:e})}create(){return this.entityManager.create()}destroy(e){this.entityManager.destroy(e)}addSystem(e){this.systemManager.addSystem(e)}removeSystem(e){this.systemManager.removeSystem(e)}addComponent(e,t,s){this.componentManager.addComponent(e,t,s)}removeComponent(e,t){this.componentManager.removeComponent(e,t)}update(){this.systemManager.systemList.forEach(e=>{e.update()}),this.emit("updated")}}exports.ComponentManager=l;exports.EntityManager=c;exports.EventRegistry=h;exports.Query=a;exports.SparseSet=d;exports.SystemManager=u;exports.Time=g;exports.World=f;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class h{_listeners=new Map;on(e,t){if(this.contains(e,t))return;const s=this._listeners.get(e);s?s.add(t):this._listeners.set(e,new Set([t]))}off(e,t){if(!this.contains(e,t))return;const s=this._listeners.get(e);s&&s.delete(t)}once(e,t){const s=(i=>{t(i),this.off(e,s)});this.on(e,s)}clearEvent(e){this._listeners.get(e)&&this._listeners.get(e)?.clear()}clearAllEvents(){this._listeners.forEach(e=>e.clear()),this._listeners.clear()}contains(e,t){return this._listeners.get(e)?this._listeners.get(e).has(t):!1}emit(e,t){this._listeners.get(e)&&this._listeners.get(e)?.forEach(s=>{s(t)})}}class u{systemList=[];addSystem(e){this.systemList.push(e),e.init?.(),this.reorder()}reorder(){this.systemList.sort((e,t)=>e.priority-t.priority)}has(e){return this.systemList.indexOf(e)>0}removeSystem(e){if(!this.has(e))return;const t=this.systemList.indexOf(e);t>=0&&(this.systemList.splice(t,1),e.exit?.(),this.reorder())}}class d{denseValues=[];sparse=new Map;[Symbol.iterator](){let e=this.values.length;const t={value:void 0,done:!1};return{next:()=>(t.value=this.values[--e],t.done=e<0,t)}}get values(){return this.denseValues}first(){return this.denseValues[0]}add(e){this.has(e)||(this.denseValues.push(e),this.sparse.set(e,this.denseValues.length-1))}indexOf(e){return this.sparse.get(e)?this.sparse.get(e):-1}remove(e){if(!this.has(e))return;const t=this.sparse.get(e);this.sparse.delete(e);const s=this.denseValues[this.denseValues.length-1];s!==e&&(this.denseValues[t]=s,this.sparse.set(s,t)),this.denseValues.pop()}forEach(e){for(let t of this)e(t)}size(){return this.denseValues.length}clear(){for(let e of this)this.remove(e)}has(e){return this.sparse.has(e)}}class m{id;_world;constructor(e,t){this.id=t,this._world=e}add(e,t){this._world.addComponent(this,e,t)}remove(e){this._world.removeComponent(this,e)}has(e){return this._world.componentManager.hasComponent(this,e)}get(e){return this._world.componentManager.getComponent(this,e)}}class c extends h{entityMap=new d;nextId=0;_world;constructor(e){super(),this._world=e}get entities(){return this.entityMap}create(){const e=this.nextId++,t=new m(this._world,e);return this.entities.add(t),this.emit("create",t),t}exist(e){return this.entities.has(e)}size(){return this.entities.size()}destroy(e){return this.entities.remove(e),this.emit("destroy",e),e}}class l extends h{componentSet=new Map;world;constructor(e){super(),this.world=e,this.world.on("entityDestroyed",t=>{t&&t.entity&&this.componentSet.get(t.entity.id)&&this.componentSet.delete(t.entity.id)})}addComponent(e,t,s){const i=this.componentSet.get(e.id);i?i[t]=s:this.componentSet.set(e.id,{[t]:s}),this.emit("add",{entity:e,component:t})}getComponent(e,t){return this.hasComponent(e,t)?this.componentSet.get(e.id)[t]:void 0}hasComponent(e,t){const s=this.componentSet.get(e.id);return s?t in s:!1}removeComponent(e,t){if(!this.componentSet.get(e.id))return;const s=this.componentSet.get(e.id);s&&s[t]!==void 0&&(delete s[t],Object.keys(s).length===0&&this.componentSet.delete(e.id),this.emit("remove",{entity:e,component:t}))}}class a{config;entityMap;world;constructor(e,t){this.config=e,this.world=t,this.entityMap=new d}hasComponents(e){return this.config.include?.every(t=>e.has(t))&&this.config.exclude?.every(t=>!e.has(t))}get entities(){return this.entityMap}include(...e){return this.world.include(...e)}exclude(...e){return this.world.exclude(...e)}_checkExistingEntities(){for(let e of this.entities)this.world.exist(e)||this.entityMap.remove(e)}checkEntities(){for(let e of this.world.entities)e&&this.hasComponents(e)&&this.entityMap.add(e);this._checkExistingEntities()}static getHash(e){const t=e.include?.map(n=>n.trim()).filter(n=>n).join("_"),s=e.exclude?.map(n=>n.trim()).filter(n=>n).join("_"),i="in_"+t+"_out_"+s;let o=0;for(const n of i)o=(o<<5)-o+n.charCodeAt(0),o|=0;return o}}class p extends h{_startTime=0;_oldTime=0;_requestId=0;running=!1;delta=0;elapsed=0;constructor(){super()}_loop(){let e=0;if(this.running){const t=performance.now();e=(t-this._oldTime)/1e3,this._oldTime=t,this.elapsed+=e}this.delta=e,this.emit("update"),this._requestId=requestAnimationFrame(this._loop.bind(this))}start(){this._startTime=performance.now(),this._oldTime=this._startTime,this.elapsed=0,this.delta=0,this.running=!0,this._loop()}stop(){this.running=!1,cancelAnimationFrame(this._requestId),this._requestId=0}}let g=new p;class f extends h{entityManager;componentManager;systemManager;queries;constructor(){super(),this.entityManager=new c(this),this.componentManager=new l(this),this.systemManager=new u,this.entityManager.on("create",e=>{e&&this.emit("entityCreated",{entity:e}),this._updateQueries()}),this.entityManager.on("destroy",e=>{e&&this.emit("entityDestroyed",{entity:e}),this._updateQueries()}),this.componentManager.on("add",e=>{this.emit("componentAdded",{entity:e.entity,component:e.component}),this._updateQueries()}),this.componentManager.on("remove",e=>{this.emit("componentRemoved",{entity:e.entity,component:e.component}),this._updateQueries()}),this.queries=new Map}get entities(){return this.entityManager.entities}query(e){const t=a.getHash(e);let i=this.queries.get(t);return i||(i=new a(e,this),this.queries.set(t,i),this._updateQueries()),i}_updateQueries(){this.queries.forEach(e=>e.checkEntities())}exist(e){return this.entityManager.exist(e)}include(...e){return this.query({include:e,exclude:[]})}exclude(...e){return this.query({include:[],exclude:e})}create(){return this.entityManager.create()}destroy(e){this.entityManager.destroy(e)}addSystem(e){this.systemManager.addSystem(e)}removeSystem(e){this.systemManager.removeSystem(e)}addComponent(e,t,s){this.componentManager.addComponent(e,t,s)}removeComponent(e,t){this.componentManager.removeComponent(e,t)}update(){this.systemManager.systemList.forEach(e=>{e.update()}),this.emit("updated")}}exports.ComponentManager=l;exports.EntityManager=c;exports.EventRegistry=h;exports.Query=a;exports.SparseSet=d;exports.SystemManager=u;exports.Time=g;exports.World=f;
|
package/dist/jael-build.js
CHANGED