@inglorious/engine 13.0.4 → 14.0.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": "13.0.4",
3
+ "version": "14.0.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",
@@ -25,18 +25,19 @@
25
25
  "./*": "./src/*"
26
26
  },
27
27
  "files": [
28
- "src"
28
+ "src",
29
+ "!src/**/*.test.js"
29
30
  ],
30
31
  "publishConfig": {
31
32
  "access": "public"
32
33
  },
33
34
  "dependencies": {
34
- "@inglorious/utils": "3.7.0",
35
- "@inglorious/store": "7.1.4"
35
+ "@inglorious/store": "8.0.1",
36
+ "@inglorious/utils": "3.7.1"
36
37
  },
37
38
  "peerDependencies": {
38
- "@inglorious/utils": "3.7.0",
39
- "@inglorious/store": "7.1.4"
39
+ "@inglorious/store": "8.0.1",
40
+ "@inglorious/utils": "3.7.1"
40
41
  },
41
42
  "devDependencies": {
42
43
  "prettier": "^3.5.3",
@@ -22,9 +22,7 @@ export function camera(params) {
22
22
  params = extend(DEFAULT_PARAMS, params)
23
23
 
24
24
  return {
25
- create(entity, entityId) {
26
- if (entityId !== entity.id) return
27
-
25
+ create(entity) {
28
26
  defaults(entity, params)
29
27
  entity.targetZoom = entity.zoom
30
28
  // Cache the initial size to calculate the viewport in dev mode
@@ -23,10 +23,8 @@ export function modernAcceleration(params) {
23
23
  "moveUpDown",
24
24
  ]),
25
25
 
26
- create(entity, entityId, api) {
27
- type.create?.(entity, entityId, api)
28
-
29
- if (entityId !== entity.id) return
26
+ create(entity, payload, api) {
27
+ type.create?.(entity, payload, api)
30
28
 
31
29
  entity.maxAcceleration ??= params.maxAcceleration
32
30
  entity.movement ??= {}
@@ -32,10 +32,8 @@ export function shooterControls(params) {
32
32
  "turn",
33
33
  ]),
34
34
 
35
- create(entity, entityId, api) {
36
- type.create?.(entity, entityId, api)
37
-
38
- if (entityId !== entity.id) return
35
+ create(entity, payload, api) {
36
+ type.create?.(entity, payload, api)
39
37
 
40
38
  entity.maxSpeed ??= params.maxSpeed
41
39
  entity.maxAngularSpeed ??= params.maxAngularSpeed
@@ -26,10 +26,8 @@ export function tankControls(params) {
26
26
  "turn",
27
27
  ]),
28
28
 
29
- create(entity, entityId, api) {
30
- type.create?.(entity, entityId, api)
31
-
32
- if (entityId !== entity.id) return
29
+ create(entity, payload, api) {
30
+ type.create?.(entity, payload, api)
33
31
 
34
32
  entity.maxSpeed ??= params.maxSpeed
35
33
  entity.maxAngularSpeed ??= params.maxAngularSpeed
@@ -23,10 +23,8 @@ export function modernVelocity(params) {
23
23
  "moveUpDown",
24
24
  ]),
25
25
 
26
- create(entity, entityId, api) {
27
- type.create?.(entity, entityId, api)
28
-
29
- if (entityId !== entity.id) return
26
+ create(entity, payload, api) {
27
+ type.create?.(entity, payload, api)
30
28
 
31
29
  entity.maxSpeed ??= params.maxSpeed
32
30
  entity.movement ??= {}
@@ -31,10 +31,8 @@ export function shooterControls(params) {
31
31
  "turn",
32
32
  ]),
33
33
 
34
- create(entity, entityId, api) {
35
- type.create?.(entity, entityId, api)
36
-
37
- if (entityId !== entity.id) return
34
+ create(entity, payload, api) {
35
+ type.create?.(entity, payload, api)
38
36
 
39
37
  entity.maxSpeed ??= params.maxSpeed
40
38
  entity.maxAngularSpeed ??= params.maxAngularSpeed
@@ -25,10 +25,8 @@ export function tankControls(params) {
25
25
  "turn",
26
26
  ]),
27
27
 
28
- create(entity, entityId, api) {
29
- type.create?.(entity, entityId, api)
30
-
31
- if (entityId !== entity.id) return
28
+ create(entity, payload, api) {
29
+ type.create?.(entity, payload, api)
32
30
 
33
31
  entity.maxSpeed ??= params.maxSpeed
34
32
  entity.maxAngularSpeed ??= params.maxAngularSpeed
@@ -12,9 +12,7 @@ export function fps(params) {
12
12
  params = extend(DEFAULT_PARAMS, params)
13
13
 
14
14
  return {
15
- create(entity, entityId) {
16
- if (entityId !== entity.id) return
17
-
15
+ create(entity) {
18
16
  entity.dt ??= { ...params }
19
17
  },
20
18
 
@@ -7,10 +7,8 @@ export function fsm(states) {
7
7
 
8
8
  return (type) => {
9
9
  return {
10
- create(entity, entityId, api) {
11
- type.create?.(entity, entityId, api)
12
-
13
- if (entityId !== entity.id) return
10
+ create(entity, payload, api) {
11
+ type.create?.(entity, payload, api)
14
12
 
15
13
  entity.state ??= DEFAULT_STATE
16
14
  },
@@ -1,8 +1,6 @@
1
1
  export function gamepadsPoller(targetIds = []) {
2
2
  return {
3
- create(entity, entityId) {
4
- if (entityId !== entity.id) return
5
-
3
+ create(entity) {
6
4
  entity.gamepadStateCache ??= {}
7
5
  },
8
6
 
@@ -3,9 +3,7 @@ export function keyboard() {
3
3
  let currentDocument = null
4
4
 
5
5
  return {
6
- create(entity, entityId, api) {
7
- if (entityId !== entity.id) return
8
-
6
+ create(entity, payload, api) {
9
7
  currentDocument = document.body.ownerDocument || document
10
8
 
11
9
  handleKeyDown = createKeyboardHandler("keyboardKeyDown", api)
@@ -4,9 +4,7 @@ import { zero } from "@inglorious/utils/math/vector.js"
4
4
 
5
5
  export function mouse() {
6
6
  return {
7
- create(entity, entityId) {
8
- if (entityId !== entity.id) return
9
-
7
+ create(entity) {
10
8
  entity.collisions ??= {}
11
9
  entity.collisions.bounds ??= { shape: "point" }
12
10
  },
@@ -9,10 +9,8 @@ export function bouncy(params) {
9
9
  params = extend(DEFAULT_PARAMS, params)
10
10
 
11
11
  return (type) => ({
12
- create(entity, entityId, api) {
13
- type.create?.(entity, entityId, api)
14
-
15
- if (entityId !== entity.id) return
12
+ create(entity, payload, api) {
13
+ type.create?.(entity, payload, api)
16
14
 
17
15
  defaults(entity, params)
18
16
  },
@@ -10,10 +10,8 @@ export function clamped(params) {
10
10
  params = extend(DEFAULT_PARAMS, params)
11
11
 
12
12
  return (type) => ({
13
- create(entity, entityId, api) {
14
- type.create?.(entity, entityId, api)
15
-
16
- if (entityId !== entity.id) return
13
+ create(entity, payload, api) {
14
+ type.create?.(entity, payload, api)
17
15
 
18
16
  entity.collisions ??= {}
19
17
  entity.collisions[params.collisionGroup] ??= {}
@@ -22,10 +22,8 @@ export function jumpable(params) {
22
22
  params = extend(DEFAULT_PARAMS, params)
23
23
 
24
24
  return (type) => ({
25
- create(entity, entityId, api) {
26
- type.create?.(entity, entityId, api)
27
-
28
- if (entityId !== entity.id) return
25
+ create(entity, payload, api) {
26
+ type.create?.(entity, payload, api)
29
27
 
30
28
  defaults(entity, params)
31
29
  entity.jumpsLeft ??= entity.maxJumps
@@ -1,43 +0,0 @@
1
- import { v } from "@inglorious/utils/v.js"
2
- import { expect, test } from "vitest"
3
-
4
- import { seek } from "./seek.js"
5
-
6
- test("it should move toward the target", () => {
7
- const entity = { maxSpeed: 1, position: v(0, 0, 0) }
8
- const target = { position: v(2, 0, 0) }
9
- const dt = 1
10
- const expectedResult = {
11
- position: v(1, 0, 0),
12
- velocity: v(1, 0, 0),
13
- orientation: 0,
14
- }
15
-
16
- expect(seek(entity, target, dt)).toStrictEqual(expectedResult)
17
- })
18
-
19
- test("it should eventually reach the target", () => {
20
- const entity = { maxSpeed: 1, position: v(0, 0, 0) }
21
- const target = { position: v(2, 0, 0) }
22
- const dt = 2
23
- const expectedResult = {
24
- position: v(2, 0, 0),
25
- velocity: v(1, 0, 0),
26
- orientation: 0,
27
- }
28
-
29
- expect(seek(entity, target, dt)).toStrictEqual(expectedResult)
30
- })
31
-
32
- test("it should overshoot when speed is too high", () => {
33
- const entity = { maxSpeed: 10, position: v(0, 0, 0) }
34
- const target = { position: v(2, 0, 0) }
35
- const dt = 1
36
- const expectedResult = {
37
- position: v(10, 0, 0),
38
- velocity: v(10, 0, 0),
39
- orientation: 0,
40
- }
41
-
42
- expect(seek(entity, target, dt)).toStrictEqual(expectedResult)
43
- })
@@ -1,47 +0,0 @@
1
- import { createStore } from "@inglorious/store/store.js"
2
- import { expect, test } from "vitest"
3
-
4
- import { fsm } from "./fsm.js"
5
-
6
- test("it should add a finite state machine", () => {
7
- const config = {
8
- types: {
9
- kitty: [
10
- fsm({
11
- default: {
12
- meow(entity) {
13
- entity.state = "meowing"
14
- },
15
- },
16
- meowing: {
17
- update(entity) {
18
- entity.treats++
19
- },
20
- },
21
- }),
22
- ],
23
- },
24
- entities: {
25
- entity1: {
26
- type: "kitty",
27
- treats: 0,
28
- },
29
- },
30
- }
31
- const afterState = {
32
- entity1: {
33
- id: "entity1",
34
- type: "kitty",
35
- state: "meowing",
36
- treats: 1,
37
- },
38
- }
39
-
40
- const store = createStore(config)
41
- store.notify("meow")
42
- store.notify("update")
43
- store.update()
44
-
45
- const state = store.getState()
46
- expect(state).toStrictEqual(afterState)
47
- })
@@ -1,28 +0,0 @@
1
- import { v } from "@inglorious/utils/v.js"
2
- import { expect, test } from "vitest"
3
-
4
- import { modernMove } from "./modern.js"
5
-
6
- test("it should move following its velocity", () => {
7
- const entity = { maxSpeed: 1, velocity: v(1, 0, 0), position: v(0, 0, 0) }
8
- const dt = 1
9
- const expectedResult = {
10
- velocity: v(1, 0, 0),
11
- position: v(1, 0, 0),
12
- orientation: 0,
13
- }
14
-
15
- expect(modernMove(entity, dt)).toStrictEqual(expectedResult)
16
- })
17
-
18
- test("it should limit the velocity to the max speed", () => {
19
- const entity = { maxSpeed: 1, velocity: v(10, 0, 0), position: v(0, 0, 0) }
20
- const dt = 1
21
- const expectedResult = {
22
- velocity: v(1, 0, 0),
23
- position: v(1, 0, 0),
24
- orientation: 0,
25
- }
26
-
27
- expect(modernMove(entity, dt)).toStrictEqual(expectedResult)
28
- })
@@ -1,81 +0,0 @@
1
- import { v } from "@inglorious/utils/v.js"
2
- import { expect, test } from "vitest"
3
-
4
- import { calculateLandingPosition } from "./position.js"
5
-
6
- test("it should calculate the landing position for a point entity on a platform", () => {
7
- const entity = {
8
- collisions: {
9
- platform: { shape: "point" },
10
- },
11
- }
12
- const target = {
13
- position: v(0, 0, 0),
14
- collisions: {
15
- platform: { size: v(20, 10, 0) },
16
- },
17
- }
18
- const collisionGroup = "platform"
19
-
20
- const py = calculateLandingPosition(entity, target, collisionGroup)
21
-
22
- expect(py).toBe(5)
23
- })
24
-
25
- test("it should calculate the landing position for a circular entity on a platform", () => {
26
- const entity = {
27
- collisions: {
28
- platform: { shape: "circle", radius: 5 },
29
- },
30
- }
31
- const target = {
32
- position: v(0, 0, 0),
33
- collisions: {
34
- platform: { size: v(20, 10, 0) },
35
- },
36
- }
37
- const collisionGroup = "platform"
38
-
39
- const py = calculateLandingPosition(entity, target, collisionGroup)
40
-
41
- expect(py).toBe(10)
42
- })
43
-
44
- test("it should calculate the landing position for a rectangular entity on a platform", () => {
45
- const entity = {
46
- size: v(10, 10, 0),
47
- collisions: {
48
- platform: { shape: "rectangle" },
49
- },
50
- }
51
- const target = {
52
- position: v(0, 0, 0),
53
- collisions: {
54
- platform: { size: v(20, 10, 0) },
55
- },
56
- }
57
- const collisionGroup = "platform"
58
-
59
- const py = calculateLandingPosition(entity, target, collisionGroup)
60
-
61
- expect(py).toBe(10)
62
- })
63
-
64
- test("it should fallback to a rectangular calculation for an unknown entity shape", () => {
65
- const entity = {
66
- collisions: {
67
- platform: { shape: "triangle" },
68
- },
69
- }
70
- const target = {
71
- position: v(0, 0, 0),
72
- collisions: {
73
- platform: { size: v(20, 10, 0) },
74
- },
75
- }
76
- const collisionGroup = "platform"
77
-
78
- const py = calculateLandingPosition(entity, target, collisionGroup)
79
-
80
- expect(py).toBe(5)
81
- })