@atrament/web 1.0.0 → 1.2.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 +10 -2
- package/package.json +12 -10
- package/src/index.js +1 -1
- package/src/interfaces/loader-web-fetch.js +23 -0
- package/src/interfaces/loader-web-fflate.js +77 -0
- package/src/interfaces/loader-web-jszip.js +53 -0
- package/src/interfaces/sound-howler.js +32 -15
- package/src/interfaces/state-nanostores.js +28 -28
- package/umd/atrament.js +1 -1
- package/umd/atrament.js.map +1 -1
- package/src/interfaces/loader-fetch.js +0 -11
package/README.md
CHANGED
|
@@ -6,6 +6,14 @@ If you want to make customized Atrament build, check out [atrament-core](https:/
|
|
|
6
6
|
|
|
7
7
|
If you are looking for example of a web application based on Atrament, check out [atrament-preact-ui](https://github.com/technix/atrament-preact-ui).
|
|
8
8
|
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```npm install @atrament/web```
|
|
12
|
+
|
|
13
|
+
or
|
|
14
|
+
|
|
15
|
+
```<script src="https://unpkg.com/@atrament/web@1.2.0/umd/atrament.js"></script>```
|
|
16
|
+
|
|
9
17
|
## Usage
|
|
10
18
|
|
|
11
19
|
Initialization and game start:
|
|
@@ -17,7 +25,7 @@ atrament.init({
|
|
|
17
25
|
settings: {}
|
|
18
26
|
});
|
|
19
27
|
|
|
20
|
-
atrament.game.init('/path/to', 'inkfile.ink.json');
|
|
28
|
+
await atrament.game.init('/path/to', 'inkfile.ink.json');
|
|
21
29
|
await atrament.game.start();
|
|
22
30
|
atrament.game.continueStory();
|
|
23
31
|
```
|
|
@@ -27,7 +35,7 @@ State in components:
|
|
|
27
35
|
```
|
|
28
36
|
import { useStore } from '@nanostores/preact';
|
|
29
37
|
|
|
30
|
-
const gameState = useStore(atrament.store
|
|
38
|
+
const gameState = useStore(atrament.store);
|
|
31
39
|
```
|
|
32
40
|
|
|
33
41
|
## API Documentation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atrament/web",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Atrament, a text game engine - build for web browsers",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -19,25 +19,27 @@
|
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@atrament/core": "1.
|
|
22
|
+
"@atrament/core": "1.2.0",
|
|
23
|
+
"fflate": "0.8.2",
|
|
23
24
|
"howler": "2.2.4",
|
|
25
|
+
"jszip": "3.10.1",
|
|
24
26
|
"localforage": "1.10.0",
|
|
25
|
-
"nanostores": "0.
|
|
27
|
+
"nanostores": "0.10.3"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
|
-
"@babel/core": "7.
|
|
29
|
-
"@babel/eslint-parser": "7.
|
|
30
|
-
"@babel/preset-env": "7.
|
|
31
|
-
"@babel/register": "7.
|
|
30
|
+
"@babel/core": "7.24.5",
|
|
31
|
+
"@babel/eslint-parser": "7.24.5",
|
|
32
|
+
"@babel/preset-env": "7.24.5",
|
|
33
|
+
"@babel/register": "7.23.7",
|
|
32
34
|
"babel-loader": "9.1.3",
|
|
33
35
|
"eslint": "8.49.0",
|
|
34
36
|
"eslint-config-airbnb-base": "15.0.0",
|
|
35
|
-
"eslint-plugin-import": "2.
|
|
37
|
+
"eslint-plugin-import": "2.29.1",
|
|
36
38
|
"jest": "29.7.0",
|
|
37
39
|
"jest-mock-console": "2.0.0",
|
|
38
40
|
"path": "0.12.7",
|
|
39
|
-
"terser-webpack-plugin": "5.3.
|
|
40
|
-
"webpack": "5.
|
|
41
|
+
"terser-webpack-plugin": "5.3.10",
|
|
42
|
+
"webpack": "5.91.0",
|
|
41
43
|
"webpack-cli": "5.1.4"
|
|
42
44
|
}
|
|
43
45
|
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import atrament from '@atrament/core';
|
|
2
2
|
|
|
3
|
-
import loader from './interfaces/loader-
|
|
3
|
+
import loader from './interfaces/loader-web-fflate';
|
|
4
4
|
import persistent from './interfaces/persistent-localforage';
|
|
5
5
|
import sound from './interfaces/sound-howler';
|
|
6
6
|
import state from './interfaces/state-nanostores';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
let $gamePath = '';
|
|
2
|
+
|
|
3
|
+
async function init(path) {
|
|
4
|
+
$gamePath = path;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function getAssetPath(filename) {
|
|
8
|
+
return `${$gamePath}/${filename}`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function loadInk(filename) {
|
|
12
|
+
const response = await fetch(getAssetPath(filename));
|
|
13
|
+
if (response.ok) {
|
|
14
|
+
return response.text();
|
|
15
|
+
}
|
|
16
|
+
throw new Error(`Failed to load ink file: ${filename}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
init,
|
|
21
|
+
getAssetPath,
|
|
22
|
+
loadInk
|
|
23
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/* eslint-disable no-await-in-loop */
|
|
2
|
+
|
|
3
|
+
import { unzip } from 'fflate';
|
|
4
|
+
|
|
5
|
+
let $gameFileType = 'plain';
|
|
6
|
+
let $gamePath = '';
|
|
7
|
+
let $zipContent = {};
|
|
8
|
+
|
|
9
|
+
let $onProgress = () => {};
|
|
10
|
+
|
|
11
|
+
async function fetchWithProgress(path) {
|
|
12
|
+
const response = await fetch(path, { responseType: 'arraybuffer' });
|
|
13
|
+
const reader = response.body.getReader();
|
|
14
|
+
const contentLength = +response.headers.get('Content-Length');
|
|
15
|
+
const chunks = [];
|
|
16
|
+
let receivedLength = 0;
|
|
17
|
+
for (;;) {
|
|
18
|
+
const { done, value } = await reader.read();
|
|
19
|
+
if (done) {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
chunks.push(value);
|
|
23
|
+
receivedLength += value.length;
|
|
24
|
+
const percent = Math.round((receivedLength * 100) / contentLength);
|
|
25
|
+
$onProgress({ percent, receivedLength, contentLength });
|
|
26
|
+
}
|
|
27
|
+
const chunksAll = new Uint8Array(receivedLength);
|
|
28
|
+
let position = 0;
|
|
29
|
+
chunks.forEach((chunk) => {
|
|
30
|
+
chunksAll.set(chunk, position);
|
|
31
|
+
position += chunk.length;
|
|
32
|
+
});
|
|
33
|
+
return chunksAll;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function init(path) {
|
|
37
|
+
if (path.endsWith('.zip')) {
|
|
38
|
+
$gameFileType = 'zip';
|
|
39
|
+
}
|
|
40
|
+
if ($gameFileType === 'zip') {
|
|
41
|
+
$zipContent = {}; // clear zip content
|
|
42
|
+
const zipContent = await fetchWithProgress(path);
|
|
43
|
+
await unzip(
|
|
44
|
+
zipContent,
|
|
45
|
+
(err, unzipped) => Object.entries(unzipped).forEach(([filepath, content]) => {
|
|
46
|
+
$zipContent[filepath] = URL.createObjectURL(new Blob([content]));
|
|
47
|
+
}));
|
|
48
|
+
} else {
|
|
49
|
+
$gamePath = path;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getAssetPath(filename) {
|
|
54
|
+
if ($gameFileType === 'zip') {
|
|
55
|
+
return $zipContent[filename];
|
|
56
|
+
}
|
|
57
|
+
return `${$gamePath}/${filename}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function loadInk(filename) {
|
|
61
|
+
const response = await fetch(getAssetPath(filename));
|
|
62
|
+
if (response.ok) {
|
|
63
|
+
return response.text();
|
|
64
|
+
}
|
|
65
|
+
throw new Error(`Failed to load ink file: ${filename}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function onProgress(cb) {
|
|
69
|
+
$onProgress = cb;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export default {
|
|
73
|
+
init,
|
|
74
|
+
getAssetPath,
|
|
75
|
+
loadInk,
|
|
76
|
+
onProgress
|
|
77
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import JSZip from 'jszip';
|
|
2
|
+
|
|
3
|
+
let $gameFileType = 'plain';
|
|
4
|
+
let $gamePath = '';
|
|
5
|
+
let $zipContent = {};
|
|
6
|
+
|
|
7
|
+
async function $handleFile(path, entry) {
|
|
8
|
+
if (entry.dir) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const content = await entry.async('blob');
|
|
12
|
+
const dataUrl = URL.createObjectURL(content);
|
|
13
|
+
$zipContent[path] = dataUrl;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function init(path) {
|
|
17
|
+
if (path.endsWith('.zip')) {
|
|
18
|
+
$gameFileType = 'zip';
|
|
19
|
+
}
|
|
20
|
+
if ($gameFileType === 'zip') {
|
|
21
|
+
$zipContent = {}; // clear zip content
|
|
22
|
+
const response = await fetch(path, { responseType: 'arraybuffer' });
|
|
23
|
+
const zipFileContent = await JSZip.loadAsync(response.arrayBuffer());
|
|
24
|
+
const fileHandlers = [];
|
|
25
|
+
zipFileContent.forEach((relativePath, content) => {
|
|
26
|
+
fileHandlers.push($handleFile(relativePath, content));
|
|
27
|
+
});
|
|
28
|
+
await Promise.all(fileHandlers);
|
|
29
|
+
} else {
|
|
30
|
+
$gamePath = path;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getAssetPath(filename) {
|
|
35
|
+
if ($gameFileType === 'zip') {
|
|
36
|
+
return $zipContent[filename];
|
|
37
|
+
}
|
|
38
|
+
return `${$gamePath}/${filename}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function loadInk(filename) {
|
|
42
|
+
const response = await fetch(getAssetPath(filename));
|
|
43
|
+
if (response.ok) {
|
|
44
|
+
return response.text();
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`Failed to load ink file: ${filename}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default {
|
|
50
|
+
init,
|
|
51
|
+
getAssetPath,
|
|
52
|
+
loadInk
|
|
53
|
+
};
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { Howl, Howler } from 'howler';
|
|
2
2
|
|
|
3
3
|
const soundCache = {};
|
|
4
|
+
const musicCache = {};
|
|
5
|
+
|
|
6
|
+
// stop playback of given sound file or all cached sound files
|
|
7
|
+
function $stopPlayback(file, cache) {
|
|
8
|
+
if (file) {
|
|
9
|
+
if (cache[file]) {
|
|
10
|
+
cache[file].stop();
|
|
11
|
+
}
|
|
12
|
+
} else {
|
|
13
|
+
Object.keys(cache).forEach((s) => {
|
|
14
|
+
if (s && s.fade) {
|
|
15
|
+
s.fade(undefined, 0, 1);
|
|
16
|
+
}
|
|
17
|
+
cache[s].stop();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
4
21
|
|
|
5
22
|
/*
|
|
6
23
|
init({
|
|
@@ -55,20 +72,30 @@ function playSound(snd) {
|
|
|
55
72
|
soundCache[snd].play();
|
|
56
73
|
}
|
|
57
74
|
|
|
75
|
+
/*
|
|
76
|
+
stopSound(snd)
|
|
77
|
+
sound once.
|
|
78
|
+
- snd: path to a sound file.
|
|
79
|
+
If no parameter is supplied, all sounds should be stopped
|
|
80
|
+
*/
|
|
81
|
+
function stopSound(snd) {
|
|
82
|
+
$stopPlayback(snd, soundCache);
|
|
83
|
+
}
|
|
84
|
+
|
|
58
85
|
/*
|
|
59
86
|
playMusic(mus, loop)
|
|
60
87
|
- mus: path to a music file
|
|
61
88
|
- loop: is track looped? true by default
|
|
62
89
|
*/
|
|
63
90
|
function playMusic(mus, loop = true) {
|
|
64
|
-
if (!
|
|
65
|
-
|
|
91
|
+
if (!musicCache[mus]) {
|
|
92
|
+
musicCache[mus] = new Howl({
|
|
66
93
|
src: [mus],
|
|
67
94
|
html5: true, // streaming
|
|
68
95
|
loop
|
|
69
96
|
});
|
|
70
97
|
}
|
|
71
|
-
|
|
98
|
+
musicCache[mus].play();
|
|
72
99
|
}
|
|
73
100
|
|
|
74
101
|
/*
|
|
@@ -77,18 +104,7 @@ function playMusic(mus, loop = true) {
|
|
|
77
104
|
If no parameter is supplied, all music should be stopped
|
|
78
105
|
*/
|
|
79
106
|
function stopMusic(mus) {
|
|
80
|
-
|
|
81
|
-
if (soundCache[mus]) {
|
|
82
|
-
soundCache[mus].stop();
|
|
83
|
-
}
|
|
84
|
-
} else {
|
|
85
|
-
Object.keys(soundCache).forEach((s) => {
|
|
86
|
-
if (s && s.fade) {
|
|
87
|
-
s.fade(undefined, 0, 1);
|
|
88
|
-
}
|
|
89
|
-
soundCache[s].stop();
|
|
90
|
-
});
|
|
91
|
-
}
|
|
107
|
+
$stopPlayback(mus, musicCache);
|
|
92
108
|
}
|
|
93
109
|
|
|
94
110
|
export default {
|
|
@@ -98,6 +114,7 @@ export default {
|
|
|
98
114
|
setVolume,
|
|
99
115
|
getVolume,
|
|
100
116
|
playSound,
|
|
117
|
+
stopSound,
|
|
101
118
|
playMusic,
|
|
102
119
|
stopMusic
|
|
103
120
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { map
|
|
1
|
+
import { map } from 'nanostores';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const $state = map({
|
|
4
4
|
settings: {},
|
|
5
5
|
game: {},
|
|
6
6
|
metadata: {},
|
|
@@ -9,42 +9,42 @@ const atramentState = map({
|
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
function store() {
|
|
12
|
-
return
|
|
12
|
+
return $state;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function get() {
|
|
16
|
-
return
|
|
16
|
+
return $state.get();
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
const setKey =
|
|
20
|
-
|
|
21
|
-
return
|
|
22
|
-
}
|
|
19
|
+
const setKey = (key, value) => {
|
|
20
|
+
$state.setKey(key, value);
|
|
21
|
+
return $state.get();
|
|
22
|
+
};
|
|
23
23
|
|
|
24
|
-
const toggleKey =
|
|
25
|
-
|
|
26
|
-
return
|
|
27
|
-
}
|
|
24
|
+
const toggleKey = (key) => {
|
|
25
|
+
$state.setKey(key, !$state.get()[key]);
|
|
26
|
+
return $state.get();
|
|
27
|
+
};
|
|
28
28
|
|
|
29
|
-
const appendKey =
|
|
30
|
-
|
|
31
|
-
return
|
|
32
|
-
}
|
|
29
|
+
const appendKey = (key, value) => {
|
|
30
|
+
$state.setKey(key, [...$state.get()[key], value]);
|
|
31
|
+
return $state.get();
|
|
32
|
+
};
|
|
33
33
|
|
|
34
|
-
const setSubkey =
|
|
35
|
-
|
|
36
|
-
return
|
|
37
|
-
}
|
|
34
|
+
const setSubkey = (key, subkey, value) => {
|
|
35
|
+
$state.setKey(key, { ...$state.get()[key], [subkey]: value });
|
|
36
|
+
return $state.get();
|
|
37
|
+
};
|
|
38
38
|
|
|
39
|
-
const toggleSubkey =
|
|
40
|
-
|
|
41
|
-
return
|
|
42
|
-
}
|
|
39
|
+
const toggleSubkey = (key, subkey) => {
|
|
40
|
+
$state.setKey(key, { ...$state.get()[key], [subkey]: !$state.get()[key][subkey] });
|
|
41
|
+
return $state.get();
|
|
42
|
+
};
|
|
43
43
|
|
|
44
|
-
const appendSubkey =
|
|
45
|
-
|
|
46
|
-
return
|
|
47
|
-
}
|
|
44
|
+
const appendSubkey = (key, subkey, value) => {
|
|
45
|
+
$state.setKey(key, { ...$state.get()[key], [subkey]: [...$state.get()[key][subkey], value] });
|
|
46
|
+
return $state.get();
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
export default {
|
|
50
50
|
store,
|