@bloopjs/bloop 0.0.10 → 0.0.11
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/jsr.json +1 -1
- package/package.json +2 -2
- package/src/mod.ts +33 -5
- package/test/tape.test.ts +33 -0
package/jsr.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bloopjs/bloop",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@types/bun": "latest"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@bloopjs/engine": "0.0.
|
|
26
|
+
"@bloopjs/engine": "0.0.11"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"typescript": "^5"
|
package/src/mod.ts
CHANGED
|
@@ -29,7 +29,6 @@ type MakeGS<B extends Bag> = BloopSchema<B>;
|
|
|
29
29
|
|
|
30
30
|
export class Bloop<GS extends BloopSchema> {
|
|
31
31
|
#systems: System<GS>[] = [];
|
|
32
|
-
#bag: GS["B"];
|
|
33
32
|
#context: Context<GS>;
|
|
34
33
|
|
|
35
34
|
/**
|
|
@@ -52,9 +51,8 @@ export class Bloop<GS extends BloopSchema> {
|
|
|
52
51
|
);
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
this.#bag = opts.bag ?? {};
|
|
56
54
|
this.#context = {
|
|
57
|
-
bag:
|
|
55
|
+
bag: opts.bag ?? {},
|
|
58
56
|
inputs: new EngineInputs.InputSnapshot(new DataView(new ArrayBuffer(100))),
|
|
59
57
|
time: {
|
|
60
58
|
dt: 0,
|
|
@@ -67,14 +65,44 @@ export class Bloop<GS extends BloopSchema> {
|
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
get bag() {
|
|
70
|
-
return this.#bag;
|
|
68
|
+
return this.#context.bag;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Take a snapshot of the game state
|
|
73
|
+
* @returns linear memory representation of the game state
|
|
74
|
+
*/
|
|
75
|
+
snapshot(): Uint8Array {
|
|
76
|
+
const str = JSON.stringify(this.#context.bag);
|
|
77
|
+
const encoder = new TextEncoder();
|
|
78
|
+
const textBytes = encoder.encode(str);
|
|
79
|
+
|
|
80
|
+
const buffer = new Uint8Array(1024*1024);
|
|
81
|
+
const view = new DataView(buffer.buffer);
|
|
82
|
+
let offset = 0;
|
|
83
|
+
view.setUint32(0, textBytes.length, true);
|
|
84
|
+
offset += 4;
|
|
85
|
+
buffer.set(textBytes, offset);
|
|
86
|
+
offset += textBytes.length;
|
|
87
|
+
return buffer;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
restore(snapshot: Uint8Array) {
|
|
91
|
+
const view = new DataView(snapshot.buffer);
|
|
92
|
+
let offset = 0;
|
|
93
|
+
const length = view.getUint32(0, true);
|
|
94
|
+
offset += 4;
|
|
95
|
+
const bagBytes = snapshot.slice(offset, offset + length);
|
|
96
|
+
const decoder = new TextDecoder();
|
|
97
|
+
const str = decoder.decode(bagBytes);
|
|
98
|
+
this.#context.bag = JSON.parse(str);
|
|
71
99
|
}
|
|
72
100
|
|
|
73
101
|
/**
|
|
74
102
|
* Register a system with the game loop.
|
|
75
103
|
*
|
|
76
104
|
*/
|
|
77
|
-
system(label: string, system: System): number {
|
|
105
|
+
system(label: string, system: System<GS>): number {
|
|
78
106
|
system.label ??= label;
|
|
79
107
|
this.#systems.push(system);
|
|
80
108
|
return this.#systems.length;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { it, expect, describe } from "bun:test";
|
|
2
|
+
import { mount } from "@bloopjs/engine";
|
|
3
|
+
import { Bloop } from "../src/mod";
|
|
4
|
+
|
|
5
|
+
describe("tapes", () => {
|
|
6
|
+
describe("snapshots", () => {
|
|
7
|
+
it("can snapshot the bag", async () => {
|
|
8
|
+
const bloop = Bloop.create({
|
|
9
|
+
bag: {
|
|
10
|
+
cool: 42,
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
bloop.system("inc", {
|
|
15
|
+
update({ bag }) {
|
|
16
|
+
bag.cool++;
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const { runtime } = await mount(bloop);
|
|
21
|
+
runtime.step();
|
|
22
|
+
|
|
23
|
+
expect(bloop.bag.cool).toEqual(43);
|
|
24
|
+
const snapshot = bloop.snapshot();
|
|
25
|
+
|
|
26
|
+
runtime.step();
|
|
27
|
+
expect(bloop.bag.cool).toEqual(44);
|
|
28
|
+
|
|
29
|
+
bloop.restore(snapshot);
|
|
30
|
+
expect(bloop.bag.cool).toEqual(43);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|