@antha/asset 0.0.8 → 0.1.1

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
@@ -1,9 +1,55 @@
1
1
  # @antha/asset
2
2
 
3
- A pre-built [Antha](https://www.npmjs.com/package/antha) mod for handling asset loading.
3
+ A pre-built [Antha game engine](https://www.npmjs.com/package/@antha/engine) mod for defining, caching, and cleaning up game assets.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```sh
8
8
  npm i @antha/asset
9
9
  ```
10
+
11
+ ## Usage
12
+
13
+ <!-- example-link: src/readme-examples/loading-asset.example.ts -->
14
+
15
+ ```TypeScript
16
+ import {AnthaEngine, defineAnthaMod} from '@antha/engine';
17
+ import {type AnthaAssetModState, createAnthaAssetMod, defineAsset} from '@antha/asset';
18
+
19
+ type GameState = AnthaAssetModState & {
20
+ hasLoadedTitle: boolean;
21
+ };
22
+
23
+ const titleAsset = defineAsset({
24
+ name: 'title',
25
+ maxProgress: 1,
26
+ load({incrementProgressCallback}) {
27
+ incrementProgressCallback();
28
+
29
+ return {
30
+ value: 'Antha',
31
+ };
32
+ },
33
+ });
34
+ const engine = new AnthaEngine<GameState>({
35
+ initState: {
36
+ hasLoadedTitle: false,
37
+ },
38
+ mods: [
39
+ createAnthaAssetMod(),
40
+ defineAnthaMod<GameState>({
41
+ modName: 'game-logic',
42
+ async execute({state}) {
43
+ if (state.assetLoader && !state.hasLoadedTitle) {
44
+ state.hasLoadedTitle = true;
45
+ await state.assetLoader.bulkLoadAssets([
46
+ titleAsset,
47
+ ]);
48
+ }
49
+ },
50
+ }),
51
+ ],
52
+ });
53
+
54
+ engine.startLoop();
55
+ ```
@@ -23,56 +23,58 @@ export const AnthaAssetLoadingScreen = defineElement()({
23
23
  hostClasses: {
24
24
  'antha-asset-loading-screen-completed': ({ inputs }) => inputs.completed,
25
25
  },
26
- styles: ({ hostClasses }) => css `
27
- :host {
28
- position: fixed;
29
- inset: 0;
30
- display: flex;
31
- flex-direction: column;
32
- align-items: center;
33
- justify-content: center;
34
- background-color: black;
35
- color: white;
36
- z-index: 9999;
37
- gap: 24px;
38
- opacity: 1;
39
- transition: opacity ${loadingScreenFadeMs}ms ease-in;
40
- }
26
+ styles: ({ hostClasses }) => {
27
+ return css `
28
+ :host {
29
+ position: fixed;
30
+ inset: 0;
31
+ display: flex;
32
+ flex-direction: column;
33
+ align-items: center;
34
+ justify-content: center;
35
+ background-color: black;
36
+ color: white;
37
+ z-index: 9999;
38
+ gap: 24px;
39
+ opacity: 1;
40
+ transition: opacity ${loadingScreenFadeMs}ms ease-in;
41
+ }
41
42
 
42
- .loading-text {
43
- font-size: 24px;
44
- position: relative;
45
- }
43
+ .loading-text {
44
+ font-size: 24px;
45
+ position: relative;
46
+ }
46
47
 
47
- .dots {
48
- font-family: monospace;
49
- position: absolute;
50
- left: 100%;
51
- bottom: 0;
52
- }
48
+ .dots {
49
+ font-family: monospace;
50
+ position: absolute;
51
+ left: 100%;
52
+ bottom: 0;
53
+ }
53
54
 
54
- .progress-track {
55
- width: 300px;
56
- height: 1em;
57
- overflow: hidden;
58
- border: 4px solid white;
59
- }
55
+ .progress-track {
56
+ width: 300px;
57
+ height: 1em;
58
+ overflow: hidden;
59
+ border: 4px solid white;
60
+ }
60
61
 
61
- .current-resource-name {
62
- font-size: 14px;
63
- opacity: 0.7;
64
- }
62
+ .current-resource-name {
63
+ font-size: 14px;
64
+ opacity: 0.7;
65
+ }
65
66
 
66
- .progress-fill {
67
- height: 100%;
68
- background-color: white;
69
- transition: width ${loadingScreenProgressGrowMs}ms ease-in;
70
- }
67
+ .progress-fill {
68
+ height: 100%;
69
+ background-color: white;
70
+ transition: width ${loadingScreenProgressGrowMs}ms ease-in;
71
+ }
71
72
 
72
- ${hostClasses['antha-asset-loading-screen-completed'].selector} {
73
- opacity: 0;
74
- }
75
- `,
73
+ ${hostClasses['antha-asset-loading-screen-completed'].selector} {
74
+ opacity: 0;
75
+ }
76
+ `;
77
+ },
76
78
  render({ inputs }) {
77
79
  const dotCount = inputs.dotCount % 4;
78
80
  const dots = '.'.repeat(dotCount) + '\u00A0'.repeat(3 - dotCount);
@@ -166,6 +166,7 @@ export declare class AssetLoader extends ListenTarget<AssetLoaderProgressUpdateE
166
166
  destroy(): Promise<void>;
167
167
  /** Loads multiple assets. */
168
168
  bulkLoadAssets(assets: ReadonlyArray<Readonly<Asset>>, options?: Readonly<AssetBulkLoaderLoadOptions>): Promise<ReadonlyArray<unknown>>;
169
- private dispatchProgressUpdate;
169
+ /** Dispatch a loading progress event for the active bulk load. */
170
+ protected dispatchProgressUpdate(detail: ConstructorParameters<typeof AssetLoaderProgressUpdateEvent>[0]['detail']): void;
170
171
  }
171
172
  export {};
@@ -111,16 +111,18 @@ export class AssetLoader extends ListenTarget {
111
111
  chunkSize: options.maxParallelism,
112
112
  })
113
113
  : [[...assets]];
114
- const createIncrementProgressCallback = (asset) => (amount) => {
115
- currentProgress += amount ?? 1;
116
- if (!options.hideLoadingScreen) {
117
- this.dispatchProgressUpdate({
118
- current: currentProgress,
119
- total: maxProgress,
120
- currentResourceName: asset.name,
121
- complete: false,
122
- });
123
- }
114
+ const createIncrementProgressCallback = (asset) => {
115
+ return (amount) => {
116
+ currentProgress += amount ?? 1;
117
+ if (!options.hideLoadingScreen) {
118
+ this.dispatchProgressUpdate({
119
+ current: currentProgress,
120
+ total: maxProgress,
121
+ currentResourceName: asset.name,
122
+ complete: false,
123
+ });
124
+ }
125
+ };
124
126
  };
125
127
  const results = (await awaitedBlockingMap(chunkedAssets, async (assetChunk) => {
126
128
  return await Promise.all(assetChunk.map(async (asset) => {
@@ -164,6 +166,7 @@ export class AssetLoader extends ListenTarget {
164
166
  }
165
167
  return results;
166
168
  }
169
+ /** Dispatch a loading progress event for the active bulk load. */
167
170
  dispatchProgressUpdate(detail) {
168
171
  this.dispatch(new AssetLoaderProgressUpdateEvent({
169
172
  detail,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antha/asset",
3
- "version": "0.0.8",
3
+ "version": "0.1.1",
4
4
  "description": "An Antha mod for handling asset loading.",
5
5
  "keywords": [
6
6
  "vir",
@@ -34,20 +34,20 @@
34
34
  "test:docs": "virmator docs check"
35
35
  },
36
36
  "dependencies": {
37
- "@augment-vir/assert": "^31.73.1",
38
- "@augment-vir/common": "^31.73.1",
39
- "typed-event-target": "^4.3.1"
37
+ "@augment-vir/assert": "^32.2.2",
38
+ "@augment-vir/common": "^32.2.2",
39
+ "typed-event-target": "^4.3.3"
40
40
  },
41
41
  "devDependencies": {
42
- "@augment-vir/test": "^31.73.1",
43
- "@web/dev-server-esbuild": "^1.0.5",
44
- "@web/test-runner": "^0.20.2",
45
- "@web/test-runner-playwright": "^0.11.1",
42
+ "@augment-vir/test": "^32.2.2",
43
+ "@web/dev-server-esbuild": "^2.0.0",
44
+ "@web/test-runner": "^1.0.0",
45
+ "@web/test-runner-playwright": "^1.0.0",
46
46
  "istanbul-smart-text-reporter": "^1.1.5",
47
47
  "pixi.js": "^8.19.0"
48
48
  },
49
49
  "peerDependencies": {
50
- "@antha/engine": "^0.0.8",
50
+ "@antha/engine": "^0.1.1",
51
51
  "element-vir": ">=26"
52
52
  },
53
53
  "engines": {