@mpgd/phaser-assets 0.3.2
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/dist/index.d.ts +41 -0
- package/dist/index.js +50 -0
- package/package.json +56 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type Phaser from 'phaser';
|
|
2
|
+
export type PhaserAssetKind = 'image' | 'spritesheet' | 'atlas' | 'audio' | 'json' | 'binary';
|
|
3
|
+
export interface PhaserImageAsset<TKey extends string = string> {
|
|
4
|
+
readonly kind: 'image';
|
|
5
|
+
readonly key: TKey;
|
|
6
|
+
readonly url: string;
|
|
7
|
+
}
|
|
8
|
+
export interface PhaserSpritesheetAsset<TKey extends string = string> {
|
|
9
|
+
readonly kind: 'spritesheet';
|
|
10
|
+
readonly key: TKey;
|
|
11
|
+
readonly url: string;
|
|
12
|
+
readonly frameConfig: Phaser.Types.Loader.FileTypes.ImageFrameConfig;
|
|
13
|
+
}
|
|
14
|
+
export interface PhaserAtlasAsset<TKey extends string = string> {
|
|
15
|
+
readonly kind: 'atlas';
|
|
16
|
+
readonly key: TKey;
|
|
17
|
+
readonly textureUrl: string;
|
|
18
|
+
readonly atlasUrl: string;
|
|
19
|
+
}
|
|
20
|
+
export interface PhaserAudioAsset<TKey extends string = string> {
|
|
21
|
+
readonly kind: 'audio';
|
|
22
|
+
readonly key: TKey;
|
|
23
|
+
readonly urls: string | readonly string[];
|
|
24
|
+
}
|
|
25
|
+
export interface PhaserJsonAsset<TKey extends string = string> {
|
|
26
|
+
readonly kind: 'json';
|
|
27
|
+
readonly key: TKey;
|
|
28
|
+
readonly url: string;
|
|
29
|
+
}
|
|
30
|
+
export interface PhaserBinaryAsset<TKey extends string = string> {
|
|
31
|
+
readonly kind: 'binary';
|
|
32
|
+
readonly key: TKey;
|
|
33
|
+
readonly url: string;
|
|
34
|
+
readonly dataType?: unknown;
|
|
35
|
+
}
|
|
36
|
+
export type PhaserAsset<TKey extends string = string> = PhaserImageAsset<TKey> | PhaserSpritesheetAsset<TKey> | PhaserAtlasAsset<TKey> | PhaserAudioAsset<TKey> | PhaserJsonAsset<TKey> | PhaserBinaryAsset<TKey>;
|
|
37
|
+
export type PhaserAssetManifest<TAsset extends PhaserAsset = PhaserAsset> = readonly TAsset[];
|
|
38
|
+
export declare function createPhaserAssetUrl(path: string, baseUrl: string | URL): string;
|
|
39
|
+
export declare function definePhaserAssetManifest<const TAsset extends PhaserAssetManifest>(assets: TAsset): TAsset;
|
|
40
|
+
export declare function loadPhaserAssets(scene: Phaser.Scene, assets: PhaserAssetManifest): void;
|
|
41
|
+
export declare function loadPhaserAsset(scene: Phaser.Scene, asset: PhaserAsset): void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export function createPhaserAssetUrl(path, baseUrl) {
|
|
2
|
+
return new URL(path, baseUrl).href;
|
|
3
|
+
}
|
|
4
|
+
export function definePhaserAssetManifest(assets) {
|
|
5
|
+
assertUniqueAssetKeys(assets);
|
|
6
|
+
return assets;
|
|
7
|
+
}
|
|
8
|
+
export function loadPhaserAssets(scene, assets) {
|
|
9
|
+
for (const asset of assets) {
|
|
10
|
+
loadPhaserAsset(scene, asset);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function loadPhaserAsset(scene, asset) {
|
|
14
|
+
switch (asset.kind) {
|
|
15
|
+
case 'image':
|
|
16
|
+
scene.load.image(asset.key, asset.url);
|
|
17
|
+
return;
|
|
18
|
+
case 'spritesheet':
|
|
19
|
+
scene.load.spritesheet(asset.key, asset.url, asset.frameConfig);
|
|
20
|
+
return;
|
|
21
|
+
case 'atlas':
|
|
22
|
+
scene.load.atlas(asset.key, asset.textureUrl, asset.atlasUrl);
|
|
23
|
+
return;
|
|
24
|
+
case 'audio':
|
|
25
|
+
scene.load.audio(asset.key, toUrlArray(asset.urls));
|
|
26
|
+
return;
|
|
27
|
+
case 'json':
|
|
28
|
+
scene.load.json(asset.key, asset.url);
|
|
29
|
+
return;
|
|
30
|
+
case 'binary':
|
|
31
|
+
scene.load.binary(asset.key, asset.url, asset.dataType);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
assertNeverAsset(asset);
|
|
35
|
+
}
|
|
36
|
+
function assertUniqueAssetKeys(assets) {
|
|
37
|
+
const keys = new Set();
|
|
38
|
+
for (const asset of assets) {
|
|
39
|
+
if (keys.has(asset.key)) {
|
|
40
|
+
throw new Error(`Duplicate Phaser asset key: ${asset.key}`);
|
|
41
|
+
}
|
|
42
|
+
keys.add(asset.key);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function toUrlArray(value) {
|
|
46
|
+
return typeof value === 'string' ? [value] : [...value];
|
|
47
|
+
}
|
|
48
|
+
function assertNeverAsset(asset) {
|
|
49
|
+
throw new Error(`Unhandled Phaser asset: ${JSON.stringify(asset)}`);
|
|
50
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mpgd/phaser-assets",
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Target-portable Phaser asset manifest and loader helpers for mpgd games.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/imjlk/mpgd-kit.git",
|
|
9
|
+
"directory": "packages/phaser-assets"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/imjlk/mpgd-kit/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/imjlk/mpgd-kit#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"mpgd",
|
|
17
|
+
"phaser",
|
|
18
|
+
"assets",
|
|
19
|
+
"vite",
|
|
20
|
+
"apps-in-toss",
|
|
21
|
+
"capacitor",
|
|
22
|
+
"devvit"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"check": "ttsc --noEmit",
|
|
37
|
+
"lint": "ttsc --noEmit",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"format": "ttsc format",
|
|
40
|
+
"fix": "ttsc fix"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"phaser": ">=4.0.0 <5"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"phaser": "4.2.0",
|
|
47
|
+
"ttsc": "0.16.9",
|
|
48
|
+
"typescript": "7.0.1-rc",
|
|
49
|
+
"vitest": "^4.0.0"
|
|
50
|
+
},
|
|
51
|
+
"main": "./dist/index.js",
|
|
52
|
+
"types": "./dist/index.d.ts",
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
}
|
|
56
|
+
}
|