@inglorious/engine 0.9.0 → 0.10.0

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 CHANGED
@@ -71,7 +71,7 @@ Here is a complete example showing how to set up and run a game using the engine
71
71
  <!DOCTYPE html>
72
72
  <html lang="en">
73
73
  <body>
74
- <canvas id="canvas"></canvas>
74
+ <canvas id="canvas" width="800" height="600"></canvas>
75
75
 
76
76
  <script type="text/javascript">
77
77
  window.process = { env: "development" }
@@ -84,7 +84,7 @@ Here is a complete example showing how to set up and run a game using the engine
84
84
  "@inglorious/utils/": "https://unpkg.com/@inglorious%2Futils@latest/src/",
85
85
  "@inglorious/store/": "https://unpkg.com/@inglorious%2Fstore@latest/src/",
86
86
  "@inglorious/engine/": "https://unpkg.com/@inglorious%2Fengine@latest/src/",
87
- "@inglorious/renderers/": "https://unpkg.com/@inglorious%2Frenderer-2d@latest/src/",
87
+ "@inglorious/renderer-2d/": "https://unpkg.com/@inglorious%2Frenderer-2d@latest/src/",
88
88
  "game": "/game.js"
89
89
  }
90
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/engine",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
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",
@@ -30,8 +30,8 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@inglorious/store": "3.0.0",
34
- "@inglorious/utils": "2.1.0"
33
+ "@inglorious/utils": "2.2.0",
34
+ "@inglorious/store": "3.0.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "prettier": "^3.5.3",
@@ -1,5 +1,13 @@
1
1
  export function game() {
2
2
  return {
3
+ pause(entity) {
4
+ entity.paused = true
5
+ },
6
+
7
+ resume(entity) {
8
+ entity.paused = false
9
+ },
10
+
3
11
  keyboardKeyUp(entity, code) {
4
12
  switch (code) {
5
13
  case "KeyC":
@@ -22,7 +22,7 @@ export function mouse() {
22
22
 
23
23
  entity.position = position
24
24
 
25
- clampToBounds(entity, game.bounds)
25
+ clampToBounds(entity, game.size)
26
26
  },
27
27
 
28
28
  mouseClick(entity, position, api) {
@@ -74,20 +74,17 @@ function createHandler(type, parent, api) {
74
74
  }
75
75
 
76
76
  // For move and click events, the payload is the calculated position.
77
- const payload = calculatePosition({
78
- clientX: event.clientX,
79
- clientY: event.clientY,
80
- parent,
81
- })
77
+ const payload = calculatePosition(event, parent)
82
78
  api.notify(type, payload)
83
79
  }
84
80
  }
85
81
 
86
- function calculatePosition({ clientX, clientY, parent }) {
87
- const bounds = parent.getBoundingClientRect()
82
+ function calculatePosition(event, parent) {
83
+ const { clientX, clientY } = event
84
+ const { left, bottom } = parent.getBoundingClientRect()
88
85
 
89
- const x = clientX - bounds.left
90
- const z = bounds.bottom - clientY
86
+ const x = clientX - left
87
+ const z = bottom - clientY
91
88
 
92
89
  return [x, NO_Y, z]
93
90
  }
@@ -28,7 +28,7 @@ export function clamped(params) {
28
28
  merge(entity, {
29
29
  position: clampToBounds(
30
30
  entity,
31
- game.bounds,
31
+ game.size,
32
32
  params.collisionGroup,
33
33
  params.depthAxis,
34
34
  ),
@@ -5,6 +5,7 @@ import { createStore } from "@inglorious/store/store.js"
5
5
  import { augmentType } from "@inglorious/store/types.js"
6
6
  import { isArray } from "@inglorious/utils/data-structures/array.js"
7
7
  import { extendWith } from "@inglorious/utils/data-structures/objects.js"
8
+ import { isVector } from "@inglorious/utils/math/linear-algebra/vector.js"
8
9
 
9
10
  import { coreEvents } from "./core-events.js"
10
11
  import { disconnectDevTools, initDevTools, sendAction } from "./dev-tools.js"
@@ -28,7 +29,7 @@ const DEFAULT_GAME_CONFIG = {
28
29
 
29
30
  entities: {
30
31
  // eslint-disable-next-line no-magic-numbers
31
- game: { type: "game", bounds: [0, 0, 800, 600] },
32
+ game: { type: "game", size: [800, 600] },
32
33
  audio: { type: "audio", sounds: {} },
33
34
  },
34
35
  }
@@ -100,7 +101,6 @@ export class Engine {
100
101
  start() {
101
102
  this._api.notify("start")
102
103
  this._loop.start(this, ONE_SECOND / this._config.loop.fps)
103
- this.isRunning = true
104
104
  }
105
105
 
106
106
  /**
@@ -110,7 +110,6 @@ export class Engine {
110
110
  this._api.notify("stop")
111
111
  this._store.update(this._api)
112
112
  this._loop.stop()
113
- this.isRunning = false
114
113
  }
115
114
 
116
115
  /**
@@ -148,7 +147,12 @@ export class Engine {
148
147
  }
149
148
 
150
149
  function merger(targetValue, sourceValue) {
151
- if (isArray(targetValue) && isArray(sourceValue)) {
150
+ if (
151
+ isArray(targetValue) &&
152
+ !isVector(targetValue) &&
153
+ isArray(sourceValue) &&
154
+ !isVector(sourceValue)
155
+ ) {
152
156
  return [...targetValue, ...sourceValue]
153
157
  }
154
158
  }
package/src/main.js CHANGED
@@ -2,8 +2,8 @@ import { Engine } from "@inglorious/engine/core/engine.js"
2
2
  import { createRenderer } from "@inglorious/renderer-2d/index.js"
3
3
  import game from "game"
4
4
 
5
- const canvas = document.getElementById("canvas")
6
5
  window.addEventListener("load", async () => {
6
+ const canvas = document.getElementById("canvas")
7
7
  const renderer = createRenderer(canvas)
8
8
  const engine = new Engine(renderer, game)
9
9
  await engine.init()
@@ -9,20 +9,21 @@ import {
9
9
  import { sum } from "@inglorious/utils/math/linear-algebra/vectors.js"
10
10
  import { abs } from "@inglorious/utils/math/numbers.js"
11
11
 
12
+ const ORIGIN = 0
12
13
  const DOUBLE = 2
13
14
  const HALF = 2
14
15
  const X = 0
15
16
  const Z = 2
16
17
 
17
- export function bounce(entity, dt, [minX, minZ, maxX, maxZ]) {
18
+ export function bounce(entity, dt, [maxX, maxZ]) {
18
19
  const [x, , z] = entity.position
19
20
 
20
21
  const velocity = createVector(entity.maxSpeed, entity.orientation)
21
- if (x < minX || x >= maxX) {
22
+ if (x < ORIGIN || x >= maxX) {
22
23
  velocity[X] = -velocity[X]
23
24
  }
24
25
 
25
- if (z < minZ || z >= maxZ) {
26
+ if (z < ORIGIN || z >= maxZ) {
26
27
  velocity[Z] = -velocity[Z]
27
28
  }
28
29
 
@@ -33,7 +34,7 @@ export function bounce(entity, dt, [minX, minZ, maxX, maxZ]) {
33
34
  }
34
35
 
35
36
  const ClampToBoundsByShape = {
36
- rectangle(entity, [minX, minZ, maxX, maxZ], collisionGroup) {
37
+ rectangle(entity, [maxX, maxZ], collisionGroup) {
37
38
  const [width, height, depth] =
38
39
  entity.collisions[collisionGroup].size ?? entity.size
39
40
 
@@ -43,31 +44,31 @@ const ClampToBoundsByShape = {
43
44
 
44
45
  return clamp(
45
46
  entity.position,
46
- [minX + halfWidth, minZ + halfHeight, minZ + halfDepth],
47
+ [halfWidth, halfHeight, halfDepth],
47
48
  [maxX - halfWidth, maxZ - halfHeight, maxZ - halfDepth],
48
49
  )
49
50
  },
50
51
 
51
- circle(entity, [minX, minY, maxX, maxY], collisionGroup, depthAxis = "y") {
52
+ circle(entity, [maxX, maxY], collisionGroup, depthAxis = "y") {
52
53
  const radius = entity.collisions[collisionGroup].radius ?? entity.radius
53
54
 
54
55
  if (depthAxis === "z") {
55
56
  return clamp(
56
57
  entity.position,
57
- [minX + radius, minY + radius, minY],
58
+ [radius, radius, ORIGIN],
58
59
  [maxX - radius, maxY - radius, maxY],
59
60
  )
60
61
  }
61
62
 
62
63
  return clamp(
63
64
  entity.position,
64
- [minX + radius, minY, minY + radius],
65
+ [radius, ORIGIN, radius],
65
66
  [maxX - radius, maxY, maxY - radius],
66
67
  )
67
68
  },
68
69
 
69
- point(entity, [minX, minZ, maxX, maxZ]) {
70
- return clamp(entity.position, [minX, minZ, minZ], [maxX, maxZ, maxZ])
70
+ point(entity, [maxX, maxZ]) {
71
+ return clamp(entity.position, zero(), [maxX, maxZ, maxZ])
71
72
  },
72
73
  }
73
74
 
@@ -82,7 +83,7 @@ export function clampToBounds(
82
83
  return handler(entity, bounds, collisionGroup, depthAxis)
83
84
  }
84
85
 
85
- export function flip(entity, [minX, minZ, maxX, maxZ]) {
86
+ export function flip(entity, [maxX, maxZ]) {
86
87
  const [x, , z] = entity.position
87
88
 
88
89
  entity.collisions ??= {}
@@ -111,20 +112,20 @@ export function flip(entity, [minX, minZ, maxX, maxZ]) {
111
112
  const direction = fromAngle(entity.orientation)
112
113
 
113
114
  if (
114
- left < minX ||
115
+ left < ORIGIN ||
115
116
  right >= maxX ||
116
- bottom < minZ ||
117
+ bottom < ORIGIN ||
117
118
  top >= maxZ ||
118
- back < minZ ||
119
+ back < ORIGIN ||
119
120
  front >= maxZ
120
121
  ) {
121
- if (left < minX) {
122
+ if (left < ORIGIN) {
122
123
  direction[X] = abs(direction[X])
123
124
  } else if (right >= maxX) {
124
125
  direction[X] = -abs(direction[X])
125
126
  }
126
127
 
127
- if (back < minZ) {
128
+ if (back < ORIGIN) {
128
129
  direction[Z] = abs(direction[Z])
129
130
  } else if (front >= maxZ) {
130
131
  direction[Z] = -abs(direction[Z])