@atrament/web 1.1.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 +1 -1
- package/package.json +4 -2
- package/src/index.js +1 -1
- 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/umd/atrament.js +1 -1
- package/umd/atrament.js.map +1 -1
- /package/src/interfaces/{loader-web.js → loader-web-fetch.js} +0 -0
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ If you are looking for example of a web application based on Atrament, check out
|
|
|
12
12
|
|
|
13
13
|
or
|
|
14
14
|
|
|
15
|
-
```<script src="https://unpkg.com/@atrament/web@1.
|
|
15
|
+
```<script src="https://unpkg.com/@atrament/web@1.2.0/umd/atrament.js"></script>```
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
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,8 +19,10 @@
|
|
|
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
27
|
"nanostores": "0.10.3"
|
|
26
28
|
},
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import atrament from '@atrament/core';
|
|
2
2
|
|
|
3
|
-
import loader from './interfaces/loader-web';
|
|
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,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
|
};
|