@inglorious/engine 0.6.0 → 0.6.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/engine",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!",
5
5
  "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -32,8 +32,8 @@
32
32
  "access": "public"
33
33
  },
34
34
  "dependencies": {
35
- "@inglorious/utils": "1.1.0",
36
- "@inglorious/store": "2.0.0"
35
+ "@inglorious/store": "2.0.0",
36
+ "@inglorious/utils": "1.1.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "prettier": "^3.5.3",
@@ -1,28 +1,38 @@
1
+ import { EventMap } from "@inglorious/store/event-map"
2
+
1
3
  export function entityPoolMiddleware(pools) {
2
- return (api) => (next) => (event) => {
3
- switch (event.type) {
4
- case "spawn": {
5
- pools.acquire(event.payload)
6
- break
7
- }
4
+ return (api) => {
5
+ const types = api.getTypes()
6
+ const eventMap = new EventMap()
8
7
 
9
- case "despawn": {
10
- pools.recycle(event.payload)
11
- break
12
- }
8
+ return (next) => (event) => {
9
+ switch (event.type) {
10
+ case "spawn": {
11
+ const entity = pools.acquire(event.payload)
12
+ const type = types[entity.type]
13
+ eventMap.addEntity(entity.id, type)
14
+ break
15
+ }
13
16
 
14
- default: {
15
- const types = api.getTypes()
16
- pools._pools.values().forEach((pool) => {
17
- pool._activeEntities.forEach((entity) => {
17
+ case "despawn": {
18
+ const entity = pools.recycle(event.payload)
19
+ const type = types[entity.type]
20
+ eventMap.removeEntity(entity.id, type)
21
+ break
22
+ }
23
+
24
+ default: {
25
+ const entityIds = eventMap.getEntitiesForEvent(event.type)
26
+ for (const id of entityIds) {
27
+ const entity = pools.activeEntitiesById.get(id)
18
28
  const type = types[entity.type]
19
29
  const handle = type[event.type]
20
30
  handle?.(entity, event.payload, api)
21
- })
22
- })
31
+ }
32
+ }
23
33
  }
24
- }
25
34
 
26
- return next(event)
35
+ return next(event)
36
+ }
27
37
  }
28
38
  }
@@ -19,6 +19,7 @@ export class EntityPool {
19
19
  }
20
20
  Object.assign(entity, props)
21
21
  this._activeEntities.push(entity)
22
+ return entity
22
23
  }
23
24
 
24
25
  recycle(entity) {
@@ -27,6 +28,7 @@ export class EntityPool {
27
28
  const [entity] = this._activeEntities.splice(index, ITEMS_TO_REMOVE)
28
29
  this._inactiveEntities.push(entity)
29
30
  }
31
+ return entity
30
32
  }
31
33
 
32
34
  getStats() {
@@ -2,15 +2,24 @@ import { EntityPool } from "./entity-pool"
2
2
 
3
3
  export class EntityPools {
4
4
  _pools = new Map()
5
+ _activeEntitiesById = new Map()
5
6
 
6
- acquire(entity) {
7
- this.lazyInit(entity)
8
- this._pools.get(entity.type).acquire(entity)
7
+ get activeEntitiesById() {
8
+ return this._activeEntitiesById
9
9
  }
10
10
 
11
- recycle(entity) {
12
- this.lazyInit(entity)
13
- this._pools.get(entity.type).recycle(entity)
11
+ acquire(props) {
12
+ this.lazyInit(props)
13
+ const entity = this._pools.get(props.type).acquire(props)
14
+ this._activeEntitiesById.set(entity.id, entity)
15
+ return entity
16
+ }
17
+
18
+ recycle(props) {
19
+ this.lazyInit(props)
20
+ const entity = this._pools.get(props.type).recycle(props)
21
+ this._activeEntitiesById.delete(entity.id)
22
+ return entity
14
23
  }
15
24
 
16
25
  getStats() {
@@ -28,10 +37,6 @@ export class EntityPools {
28
37
  }
29
38
 
30
39
  getAllActiveEntities() {
31
- const activeEntities = []
32
- for (const pool of this._pools.values()) {
33
- activeEntities.push(...pool._activeEntities)
34
- }
35
- return activeEntities
40
+ return Array.from(this._activeEntitiesById.values())
36
41
  }
37
42
  }