@bloopjs/bloop 0.0.9 → 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/context.ts +2 -2
- package/src/data/schema.ts +1 -1
- package/src/mod.ts +37 -9
- package/src/system.ts +2 -2
- package/test/inputs.test.ts +3 -0
- 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/context.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { InputSnapshot } from "@bloopjs/engine";
|
|
2
|
-
import type {
|
|
2
|
+
import type { BloopSchema } from "./data/schema";
|
|
3
3
|
|
|
4
4
|
export type Context<
|
|
5
|
-
GS extends
|
|
5
|
+
GS extends BloopSchema = BloopSchema,
|
|
6
6
|
// Q extends Query<GS["CS"]> = Query<GS["CS"]>,
|
|
7
7
|
// QS extends readonly Query<GS["CS"]>[] = readonly Query<GS["CS"]>[],
|
|
8
8
|
> = {
|
package/src/data/schema.ts
CHANGED
package/src/mod.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BloopSchema } from "./data/schema";
|
|
2
2
|
import type { Bag } from "./data/bag";
|
|
3
3
|
import type { System } from "./system";
|
|
4
4
|
import { EngineInputs, type PlatformEvent } from "@bloopjs/engine";
|
|
@@ -25,15 +25,14 @@ export type BloopOpts<B extends Bag> = {
|
|
|
25
25
|
// schema: GS;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
type MakeGS<B extends Bag> =
|
|
28
|
+
type MakeGS<B extends Bag> = BloopSchema<B>;
|
|
29
29
|
|
|
30
|
-
export class Bloop<GS extends
|
|
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
|
/**
|
|
36
|
-
* Bloop.create() is the way to create a new
|
|
35
|
+
* Bloop.create() is the way to create a new bloop instance.
|
|
37
36
|
*/
|
|
38
37
|
static create<
|
|
39
38
|
// S extends Schema,
|
|
@@ -52,9 +51,8 @@ export class Bloop<GS extends GameSchema> {
|
|
|
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 GameSchema> {
|
|
|
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;
|
package/src/system.ts
CHANGED
|
@@ -5,9 +5,9 @@ import type {
|
|
|
5
5
|
MouseWheelEvent,
|
|
6
6
|
} from "@bloopjs/engine";
|
|
7
7
|
import type { Context } from "./context";
|
|
8
|
-
import type {
|
|
8
|
+
import type { BloopSchema } from "./data/schema";
|
|
9
9
|
|
|
10
|
-
export type System<GS extends
|
|
10
|
+
export type System<GS extends BloopSchema = BloopSchema> = {
|
|
11
11
|
label?: string;
|
|
12
12
|
|
|
13
13
|
update?: (context: Context<GS>) => void;
|
package/test/inputs.test.ts
CHANGED
|
@@ -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
|
+
});
|