@inglorious/engine 2.1.3 → 3.0.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.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,18 +30,18 @@
|
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@inglorious/
|
|
34
|
-
"@inglorious/
|
|
33
|
+
"@inglorious/store": "5.0.0",
|
|
34
|
+
"@inglorious/utils": "3.6.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@inglorious/store": "
|
|
38
|
-
"@inglorious/utils": "3.
|
|
37
|
+
"@inglorious/store": "5.0.0",
|
|
38
|
+
"@inglorious/utils": "3.6.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"prettier": "^3.5.3",
|
|
42
42
|
"vite": "^7.1.3",
|
|
43
43
|
"vitest": "^1.6.0",
|
|
44
|
-
"@inglorious/eslint-config": "1.0.
|
|
44
|
+
"@inglorious/eslint-config": "1.0.1"
|
|
45
45
|
},
|
|
46
46
|
"engines": {
|
|
47
47
|
"node": ">= 22"
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export function images() {
|
|
2
|
+
const imageCache = new Map()
|
|
3
|
+
const loadingPromises = new Map()
|
|
4
|
+
|
|
5
|
+
return {
|
|
6
|
+
async init(entity) {
|
|
7
|
+
const images = entity.images || {}
|
|
8
|
+
|
|
9
|
+
await Promise.all(
|
|
10
|
+
Object.entries(images).map(async ([id, { url }]) => {
|
|
11
|
+
const img = await loadImage(url)
|
|
12
|
+
img.id = id
|
|
13
|
+
imageCache.set(id, img)
|
|
14
|
+
}),
|
|
15
|
+
)
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
async load(id, src) {
|
|
19
|
+
if (imageCache.has(id)) {
|
|
20
|
+
return imageCache.get(id)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (loadingPromises.has(id)) {
|
|
24
|
+
return loadingPromises.get(id)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const promise = loadImage(src).then((img) => {
|
|
28
|
+
img.id = id
|
|
29
|
+
imageCache.set(id, img)
|
|
30
|
+
loadingPromises.delete(id)
|
|
31
|
+
return img
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
loadingPromises.set(id, promise)
|
|
35
|
+
return promise
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
get(id) {
|
|
39
|
+
return imageCache.get(id)
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
stop() {
|
|
43
|
+
imageCache.clear()
|
|
44
|
+
loadingPromises.clear()
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function loadImage(src) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const img = new Image()
|
|
52
|
+
img.onload = () => resolve(img)
|
|
53
|
+
img.onerror = () => reject(new Error(`Failed to load image: ${src}`))
|
|
54
|
+
img.src = src
|
|
55
|
+
})
|
|
56
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { scale } from "@inglorious/utils/math/vector.js"
|
|
2
|
+
import { mod, sum } from "@inglorious/utils/math/vectors.js"
|
|
3
|
+
|
|
4
|
+
export function infiniteScroll() {
|
|
5
|
+
return {
|
|
6
|
+
update(entity, dt) {
|
|
7
|
+
entity.position = mod(
|
|
8
|
+
sum(entity.position, scale(entity.velocity, dt)),
|
|
9
|
+
entity.image.loop,
|
|
10
|
+
)
|
|
11
|
+
},
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/core/engine.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { audio } from "@inglorious/engine/behaviors/audio.js"
|
|
2
2
|
import { game } from "@inglorious/engine/behaviors/game.js"
|
|
3
|
+
import { images } from "@inglorious/engine/behaviors/images.js"
|
|
3
4
|
import { createStore } from "@inglorious/store/store.js"
|
|
4
5
|
import { augmentType } from "@inglorious/store/types.js"
|
|
5
6
|
import { isArray } from "@inglorious/utils/data-structures/array.js"
|
|
@@ -23,12 +24,14 @@ const DEFAULT_GAME_CONFIG = {
|
|
|
23
24
|
types: {
|
|
24
25
|
game: [game()],
|
|
25
26
|
audio: [audio()],
|
|
27
|
+
images: [images()],
|
|
26
28
|
},
|
|
27
29
|
|
|
28
30
|
entities: {
|
|
29
31
|
// eslint-disable-next-line no-magic-numbers
|
|
30
32
|
game: { type: "game", size: v(800, 600) },
|
|
31
33
|
audio: { type: "audio", sounds: {} },
|
|
34
|
+
images: { type: "images", images: {} },
|
|
32
35
|
},
|
|
33
36
|
}
|
|
34
37
|
|