@celljs/ta-voxel 0.0.0-stage → 4.31.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.en.md ADDED
@@ -0,0 +1,3 @@
1
+ # Cell - Ta Voxel
2
+
3
+ A small engine of blocks for Ta's extensions (a plain library, not a pi package), on the Three.js of `pi.ui.Game3D`: kinds of blocks (`VoxelBlocks`), chunks of 16 × 16 columns as high as their blocks (`VoxelStore`), meshes of the faces that show with ambient occlusion (`VoxelMesher`), a body that walks, runs, jumps, flies, swims, climbs a ladder (forward against its wall up, back down) and goes up a step by itself (`VoxelBody`), the block aimed at (`VoxelRay`), the meshes of Three.js (`VoxelMeshes`) and the controls of the first person for keys, a mouse and a touch screen (`VoxelControls`). Block Earth (`ta-earth`) is its first user.
package/README.md CHANGED
@@ -1,3 +1,12 @@
1
- # Temporary Holding Version
1
+ # Cell - Ta Voxel
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 给 Ta 的扩展用的方块小引擎(普通的库,不是 pi 包),在 `pi.ui.Game3D` 的 Three.js 上:
4
+
5
+ - **方块登记**(`VoxelBlocks`):每种方块的颜色、实心、透明、液体、自发光。
6
+ - **分块存储**(`VoxelStore`):16 × 16 列一块,高度按用到的取 8 的倍数,平地一块约 2 KB。
7
+ - **网格**(`VoxelMesher`):只画露出来的面,角落环境光遮蔽,分实心、发光、透明、水四部分;改一块只重做它所在的块。
8
+ - **身体**(`VoxelBody`):走、跑、跳、飞、游泳、爬梯子(顶着墙往前就往上,往后退就下去),按轴碰撞,一格台阶自动走上去。
9
+ - **射线**(`VoxelRay`):准星对着的方块和面。
10
+ - **Three.js 网格**(`VoxelMeshes`)与**第一人称操作**(`VoxelControls`):键鼠(WASD、指针锁定,锁定被拒时拖动看)和手机(左半屏摇杆、右半屏看,按钮由页面自己放)。
11
+
12
+ 第一个使用者是方块地球(`ta-earth`)。
package/esm/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './voxel-blocks.js';
2
+ export * from './voxel-body.js';
3
+ export * from './voxel-chunks.js';
4
+ export * from './voxel-controls.js';
5
+ export * from './voxel-mesher.js';
6
+ export * from './voxel-ray.js';
7
+ export * from './voxel-three.js';
package/esm/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from './voxel-blocks.js';
2
+ export * from './voxel-body.js';
3
+ export * from './voxel-chunks.js';
4
+ export * from './voxel-controls.js';
5
+ export * from './voxel-mesher.js';
6
+ export * from './voxel-ray.js';
7
+ export * from './voxel-three.js';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,44 @@
1
+ export interface BlockLook {
2
+ /** Its colour, `0xrrggbb`. */
3
+ color: number;
4
+ /** What stands in the way and is stood on. Water is not. */
5
+ solid: boolean;
6
+ /** Seen through: glass, leaves, water; the faces beside it are drawn. */
7
+ clear?: boolean;
8
+ /** Water: one swims in it, its top a little lower. */
9
+ liquid?: boolean;
10
+ /** Lit by itself (a lamp, a window at night): drawn by the light of the world's glow, not the sun. */
11
+ glow?: boolean;
12
+ /** How it is drawn: the grain of a block, or flat. */
13
+ flat?: boolean;
14
+ /** The tile of its faces (VoxelBlocks.tile), and of its top, bottom and sides if they differ; none, the grain. */
15
+ tile?: string;
16
+ top?: string;
17
+ bottom?: string;
18
+ side?: string;
19
+ /** The colour of its sides, if not its own (grass: the tile of its sides is coloured itself, so white). */
20
+ sideColor?: number;
21
+ }
22
+ /** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
23
+ export declare const TILE = 16;
24
+ export declare const TILE_PAD = 2;
25
+ export declare const TILE_CELL: number;
26
+ export declare const TILE_COLUMNS = 16;
27
+ export declare const AIR = 0;
28
+ export declare class VoxelBlocks {
29
+ readonly looks: BlockLook[];
30
+ protected readonly names: Map<string, number>;
31
+ /** By id, for the mesher: drawn as solid (hides what is behind it). */
32
+ opaque: Uint8Array<ArrayBuffer>;
33
+ solid: Uint8Array<ArrayBuffer>;
34
+ /** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
35
+ readonly tiles: Uint8Array[];
36
+ readonly faceTile: Uint8Array<ArrayBuffer>;
37
+ protected readonly tileNames: Map<string, number>;
38
+ /** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
39
+ tile(name: string, pixels: Uint8Array): number;
40
+ /** A kind of block, its id; the same name again is the same id. */
41
+ define(name: string, look: BlockLook): number;
42
+ id(name: string): number | undefined;
43
+ name(id: number): string | undefined;
44
+ }
@@ -0,0 +1,84 @@
1
+ // The kinds of blocks of a world (VoxelBlocks): what each looks like and how it is met. A world defines its own;
2
+ // the id of a kind is a byte of the chunks, 0 being air. The engine reads only these looks. Each face is drawn with a
3
+ // tile of 16 × 16 pixels the world paints (bark, bricks, blades of grass), mostly grey, tinted by the block's colour;
4
+ // one without, the grain.
5
+ /** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
6
+ export const TILE = 16;
7
+ export const TILE_PAD = 2;
8
+ export const TILE_CELL = TILE + TILE_PAD * 2;
9
+ export const TILE_COLUMNS = 16;
10
+ /** A grain of grey pixels, the same every time: a block of 16 × 16, RGBA. */
11
+ function grain() {
12
+ const data = new Uint8Array(TILE * TILE * 4);
13
+ let seed = 11;
14
+ const random = () => {
15
+ seed = (seed * 16807) % 2147483647;
16
+ return seed / 2147483647;
17
+ };
18
+ for (let i = 0; i < TILE * TILE; i++) {
19
+ const x = i % TILE;
20
+ const y = Math.floor(i / TILE);
21
+ const edge = x === 0 || y === 0 ? -14 : 0;
22
+ const value = Math.max(0, Math.min(255, 232 + Math.floor(random() * 22) + edge - (random() < 0.06 ? 22 : 0)));
23
+ data.set([value, value, value, 255], i * 4);
24
+ }
25
+ return data;
26
+ }
27
+ export const AIR = 0;
28
+ export class VoxelBlocks {
29
+ constructor() {
30
+ this.looks = [{ color: 0, solid: false, clear: true }];
31
+ this.names = new Map();
32
+ /** By id, for the mesher: drawn as solid (hides what is behind it). */
33
+ this.opaque = new Uint8Array(256);
34
+ this.solid = new Uint8Array(256);
35
+ /** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
36
+ this.tiles = [grain()];
37
+ this.faceTile = new Uint8Array(256 * 6);
38
+ this.tileNames = new Map([['grain', 0]]);
39
+ }
40
+ /** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
41
+ tile(name, pixels) {
42
+ const known = this.tileNames.get(name);
43
+ if (known !== undefined) {
44
+ return known;
45
+ }
46
+ this.tiles.push(pixels);
47
+ this.tileNames.set(name, this.tiles.length - 1);
48
+ return this.tiles.length - 1;
49
+ }
50
+ /** A kind of block, its id; the same name again is the same id. */
51
+ define(name, look) {
52
+ var _a, _b, _c, _d;
53
+ const known = this.names.get(name);
54
+ if (known !== undefined) {
55
+ return known;
56
+ }
57
+ const id = this.looks.length;
58
+ if (id > 255) {
59
+ throw new Error('At most 255 kinds of blocks');
60
+ }
61
+ this.looks.push(look);
62
+ this.names.set(name, id);
63
+ this.opaque[id] = look.solid && !look.clear ? 1 : 0;
64
+ this.solid[id] = look.solid ? 1 : 0;
65
+ // Faces in the mesher's order: up, down, then the four sides.
66
+ for (let f = 0; f < 6; f++) {
67
+ const name = f === 0 ? ((_a = look.top) !== null && _a !== void 0 ? _a : look.tile) : f === 1 ? ((_b = look.bottom) !== null && _b !== void 0 ? _b : look.tile) : ((_c = look.side) !== null && _c !== void 0 ? _c : look.tile);
68
+ this.faceTile[id * 6 + f] = name ? ((_d = this.tileNames.get(name)) !== null && _d !== void 0 ? _d : 0) : 0;
69
+ }
70
+ return id;
71
+ }
72
+ id(name) {
73
+ return this.names.get(name);
74
+ }
75
+ name(id) {
76
+ for (const [name, each] of this.names) {
77
+ if (each === id) {
78
+ return name;
79
+ }
80
+ }
81
+ return undefined;
82
+ }
83
+ }
84
+ //# sourceMappingURL=voxel-blocks.js.map
@@ -0,0 +1,45 @@
1
+ export interface BodyInput {
2
+ /** -1 to 1: forward is the way it looks; strafe, to its right. */
3
+ forward: number;
4
+ strafe: number;
5
+ /** Which way it looks, round the vertical, in radians (0 north, the -z of the world). */
6
+ yaw: number;
7
+ jump: boolean;
8
+ /** Down while flying (Shift). */
9
+ down: boolean;
10
+ run: boolean;
11
+ /** Flying, how many times as fast as it flies by itself (1 if none). */
12
+ boost?: number;
13
+ }
14
+ export interface BodyWorld {
15
+ solid(x: number, y: number, z: number): boolean;
16
+ liquid(x: number, y: number, z: number): boolean;
17
+ /** A ladder: what one climbs, not solid. */
18
+ climb?(x: number, y: number, z: number): boolean;
19
+ }
20
+ export declare class VoxelBody {
21
+ x: number;
22
+ y: number;
23
+ z: number;
24
+ vx: number;
25
+ vy: number;
26
+ vz: number;
27
+ onGround: boolean;
28
+ inWater: boolean;
29
+ climbing: boolean;
30
+ /** Walking into something last time: on a ladder, up it. */
31
+ protected pushing: boolean;
32
+ flying: boolean;
33
+ /** How far it just went up a step: the camera eases it. */
34
+ stepped: number;
35
+ constructor(x: number, y: number, z: number);
36
+ /** Moves it on by `seconds`, in small steps. */
37
+ step(seconds: number, input: BodyInput, world: BodyWorld): void;
38
+ protected tick(dt: number, input: BodyInput, world: BodyWorld): void;
39
+ /** Whether the box with its feet at (x, y, z) is in a solid block. */
40
+ hits(x: number, y: number, z: number, world: BodyWorld): boolean;
41
+ /** Moves along one axis (0 x, 1 y, 2 z) as far as it can; whether something stopped it. */
42
+ protected move(axis: 0 | 1 | 2, distance: number, world: BodyWorld): boolean;
43
+ /** The height of the eyes above the feet. */
44
+ static readonly EYES = 1.62;
45
+ }
@@ -0,0 +1,172 @@
1
+ // A body in a world of blocks (VoxelBody): a box of 0.6 × 1.8 that walks, runs, jumps, flies and swims, stopped by
2
+ // solid blocks one axis at a time; walking into a step of one block it goes up it by itself (on a phone one cannot
3
+ // jump and walk with one thumb); on a ladder it holds on, forward against the wall it is on (or jump) goes up and
4
+ // back goes down, walking past it does not; it speeds up and slows down in about a sixth of a second.
5
+ const HALF = 0.3;
6
+ const HEIGHT = 1.8;
7
+ const GRAVITY = 30;
8
+ const JUMP = 9.2;
9
+ const WALK = 4.4;
10
+ const RUN = 6.8;
11
+ const FLY = 12;
12
+ const SWIM = 2.6;
13
+ const CLIMB = 3.4;
14
+ /** How quickly it gets to the speed asked: a sixth of a second. */
15
+ const EASE = 6;
16
+ const STEP = 1 / 60;
17
+ export class VoxelBody {
18
+ constructor(x, y, z) {
19
+ this.x = x;
20
+ this.y = y;
21
+ this.z = z;
22
+ this.vx = 0;
23
+ this.vy = 0;
24
+ this.vz = 0;
25
+ this.onGround = false;
26
+ this.inWater = false;
27
+ this.climbing = false;
28
+ /** Walking into something last time: on a ladder, up it. */
29
+ this.pushing = false;
30
+ this.flying = false;
31
+ /** How far it just went up a step: the camera eases it. */
32
+ this.stepped = 0;
33
+ }
34
+ /** Moves it on by `seconds`, in small steps. */
35
+ step(seconds, input, world) {
36
+ let left = Math.min(seconds, 0.25);
37
+ while (left > 1e-6) {
38
+ const dt = Math.min(STEP, left);
39
+ this.tick(dt, input, world);
40
+ left -= dt;
41
+ }
42
+ }
43
+ tick(dt, input, world) {
44
+ var _a;
45
+ const bx = Math.floor(this.x);
46
+ const bz = Math.floor(this.z);
47
+ this.inWater = world.liquid(bx, Math.floor(this.y + 0.4), bz);
48
+ this.climbing = !this.flying && !!world.climb && (world.climb(bx, Math.floor(this.y + 0.2), bz) || world.climb(bx, Math.floor(this.y + 1.2), bz));
49
+ const speed = this.flying ? FLY * (input.run ? 1.6 : 1) * ((_a = input.boost) !== null && _a !== void 0 ? _a : 1) : this.inWater ? SWIM : input.run ? RUN : WALK;
50
+ const sin = Math.sin(input.yaw);
51
+ const cos = Math.cos(input.yaw);
52
+ const length = Math.min(1, Math.hypot(input.forward, input.strafe));
53
+ const scale = length > 0 ? length / Math.hypot(input.forward, input.strafe) : 0;
54
+ // As the camera turns by yaw: forward is (-sin, -cos), the right (cos, -sin).
55
+ const wantX = (input.strafe * cos - input.forward * sin) * scale * speed;
56
+ const wantZ = (-input.strafe * sin - input.forward * cos) * scale * speed;
57
+ const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater || this.climbing ? 3 : 1) * dt);
58
+ this.vx += (wantX - this.vx) * ease;
59
+ this.vz += (wantZ - this.vz) * ease;
60
+ if (this.flying) {
61
+ const wantY = (input.jump ? 1 : 0) - (input.down ? 1 : 0);
62
+ this.vy += (wantY * speed - this.vy) * ease;
63
+ }
64
+ else if (this.climbing) {
65
+ const up = input.jump || (input.forward > 0.3 && this.pushing);
66
+ const down = input.down || input.forward < -0.3;
67
+ this.vy = up ? CLIMB : down ? -CLIMB : 0;
68
+ }
69
+ else if (this.inWater) {
70
+ this.vy -= GRAVITY * 0.25 * dt;
71
+ this.vy = input.jump ? Math.min(this.vy + 20 * dt, 3.2) : Math.max(this.vy, -2.5);
72
+ }
73
+ else {
74
+ this.vy -= GRAVITY * dt;
75
+ if (input.jump && this.onGround) {
76
+ this.vy = JUMP;
77
+ }
78
+ this.vy = Math.max(this.vy, -50);
79
+ }
80
+ const wasOnGround = this.onGround;
81
+ this.onGround = false;
82
+ const blockedX = this.move(0, this.vx * dt, world);
83
+ const blockedZ = this.move(2, this.vz * dt, world);
84
+ // A step of one block in the way: up it, if there is room above.
85
+ this.pushing = blockedX || blockedZ;
86
+ if ((blockedX || blockedZ) && (wasOnGround || this.inWater || this.climbing) && !this.flying) {
87
+ const up = this.y + 1.01;
88
+ if (!this.hits(this.x + Math.sign(this.vx) * 0.05, up, this.z + Math.sign(this.vz) * 0.05, world) && !this.hits(this.x, up, this.z, world)) {
89
+ const before = this.y;
90
+ this.y = Math.floor(this.y) + 1;
91
+ this.stepped += this.y - before;
92
+ this.move(0, this.vx * dt, world);
93
+ this.move(2, this.vz * dt, world);
94
+ }
95
+ }
96
+ if (this.move(1, this.vy * dt, world)) {
97
+ if (this.vy < 0) {
98
+ this.onGround = true;
99
+ if (this.flying) {
100
+ this.flying = false;
101
+ }
102
+ }
103
+ this.vy = 0;
104
+ }
105
+ if (blockedX) {
106
+ this.vx = 0;
107
+ }
108
+ if (blockedZ) {
109
+ this.vz = 0;
110
+ }
111
+ }
112
+ /** Whether the box with its feet at (x, y, z) is in a solid block. */
113
+ hits(x, y, z, world) {
114
+ const x0 = Math.floor(x - HALF);
115
+ const x1 = Math.floor(x + HALF - 1e-6);
116
+ const y0 = Math.floor(y);
117
+ const y1 = Math.floor(y + HEIGHT - 1e-6);
118
+ const z0 = Math.floor(z - HALF);
119
+ const z1 = Math.floor(z + HALF - 1e-6);
120
+ for (let by = y0; by <= y1; by++) {
121
+ for (let bz = z0; bz <= z1; bz++) {
122
+ for (let bx = x0; bx <= x1; bx++) {
123
+ if (world.solid(bx, by, bz)) {
124
+ return true;
125
+ }
126
+ }
127
+ }
128
+ }
129
+ return false;
130
+ }
131
+ /** Moves along one axis (0 x, 1 y, 2 z) as far as it can; whether something stopped it. */
132
+ move(axis, distance, world) {
133
+ if (distance === 0) {
134
+ return false;
135
+ }
136
+ // Fast (flying boosted), in pieces of less than a block, so as not to go through a wall.
137
+ if (Math.abs(distance) > 0.9) {
138
+ const pieces = Math.ceil(Math.abs(distance) / 0.9);
139
+ for (let i = 0; i < pieces; i++) {
140
+ if (this.move(axis, distance / pieces, world)) {
141
+ return true;
142
+ }
143
+ }
144
+ return false;
145
+ }
146
+ const now = [this.x, this.y, this.z];
147
+ const wanted = [...now];
148
+ wanted[axis] += distance;
149
+ if (!this.hits(wanted[0], wanted[1], wanted[2], world)) {
150
+ [this.x, this.y, this.z] = wanted;
151
+ return false;
152
+ }
153
+ // Up against the face of the block in the way, no farther back than it was.
154
+ const at = wanted[axis];
155
+ let to;
156
+ if (axis === 1) {
157
+ to = distance > 0 ? Math.floor(at + HEIGHT) - HEIGHT - 1e-4 : Math.floor(at) + 1 + 1e-4;
158
+ }
159
+ else {
160
+ to = distance > 0 ? Math.floor(at + HALF) - HALF - 1e-4 : Math.floor(at - HALF) + 1 + HALF + 1e-4;
161
+ }
162
+ const placed = [...now];
163
+ placed[axis] = distance > 0 ? Math.min(Math.max(to, now[axis]), at) : Math.max(Math.min(to, now[axis]), at);
164
+ if (!this.hits(placed[0], placed[1], placed[2], world)) {
165
+ [this.x, this.y, this.z] = placed;
166
+ }
167
+ return true;
168
+ }
169
+ }
170
+ /** The height of the eyes above the feet. */
171
+ VoxelBody.EYES = 1.62;
172
+ //# sourceMappingURL=voxel-body.js.map
@@ -0,0 +1,37 @@
1
+ export declare const CHUNK = 16;
2
+ /** The highest a world goes. */
3
+ export declare const MAX_HEIGHT = 256;
4
+ export declare class VoxelChunk {
5
+ readonly cx: number;
6
+ readonly cz: number;
7
+ height: number;
8
+ data: Uint8Array;
9
+ /** One more each time a block changes: its mesh is made again. */
10
+ version: number;
11
+ constructor(cx: number, cz: number, height?: number);
12
+ get(x: number, y: number, z: number): number;
13
+ set(x: number, y: number, z: number, id: number): void;
14
+ /** Fills a column from `from` to `to` (not including). */
15
+ column(x: number, z: number, from: number, to: number, id: number): void;
16
+ protected grow(height: number): void;
17
+ }
18
+ export declare namespace VoxelKey {
19
+ /** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
20
+ function of(cx: number, cz: number): number;
21
+ function parts(key: number): {
22
+ cx: number;
23
+ cz: number;
24
+ };
25
+ }
26
+ export declare class VoxelStore {
27
+ readonly chunks: Map<number, VoxelChunk>;
28
+ chunk(cx: number, cz: number): VoxelChunk | undefined;
29
+ add(chunk: VoxelChunk): void;
30
+ remove(cx: number, cz: number): void;
31
+ /** The block at a point; undefined where nothing is loaded. */
32
+ get(x: number, y: number, z: number): number | undefined;
33
+ /** Sets a block; the chunks whose mesh it changes (its own, and a neighbour's when it is at the edge). */
34
+ set(x: number, y: number, z: number, id: number): VoxelChunk[];
35
+ /** The highest block of a column that is solid by `solid`, its y; -1 if none, undefined if not loaded. */
36
+ top(x: number, z: number, solid: Uint8Array): number | undefined;
37
+ }
@@ -0,0 +1,136 @@
1
+ // The blocks of a world (VoxelStore), in chunks of 16 × 16 columns, each as high as its highest block (to 8 more),
2
+ // so that the many chunks of flat ground take 2 KB and a tower only its own height. A block is a byte by global
3
+ // (x, y, z): x east, y up from 0, z south. What is not loaded is undefined, which the mesher takes as hidden.
4
+ export const CHUNK = 16;
5
+ const AREA = CHUNK * CHUNK;
6
+ /** The highest a world goes. */
7
+ export const MAX_HEIGHT = 256;
8
+ export class VoxelChunk {
9
+ constructor(cx, cz, height = 8) {
10
+ this.cx = cx;
11
+ this.cz = cz;
12
+ this.height = height;
13
+ /** One more each time a block changes: its mesh is made again. */
14
+ this.version = 0;
15
+ this.data = new Uint8Array(AREA * height);
16
+ }
17
+ get(x, y, z) {
18
+ return y >= 0 && y < this.height ? this.data[(y * CHUNK + z) * CHUNK + x] : 0;
19
+ }
20
+ set(x, y, z, id) {
21
+ if (y < 0 || y >= MAX_HEIGHT) {
22
+ return;
23
+ }
24
+ if (y >= this.height) {
25
+ if (id === 0) {
26
+ return;
27
+ }
28
+ this.grow(y + 1);
29
+ }
30
+ this.data[(y * CHUNK + z) * CHUNK + x] = id;
31
+ this.version++;
32
+ }
33
+ /** Fills a column from `from` to `to` (not including). */
34
+ column(x, z, from, to, id) {
35
+ if (to > this.height) {
36
+ this.grow(to);
37
+ }
38
+ for (let y = Math.max(0, from); y < to; y++) {
39
+ this.data[(y * CHUNK + z) * CHUNK + x] = id;
40
+ }
41
+ this.version++;
42
+ }
43
+ grow(height) {
44
+ const next = Math.min(MAX_HEIGHT, Math.ceil(height / 8) * 8);
45
+ const data = new Uint8Array(AREA * next);
46
+ data.set(this.data);
47
+ this.data = data;
48
+ this.height = next;
49
+ }
50
+ }
51
+ /** Chunks of the earth across: its 2^25 columns, 16 a chunk. */
52
+ const SPAN = 2097152;
53
+ export var VoxelKey;
54
+ (function (VoxelKey) {
55
+ /** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
56
+ function of(cx, cz) {
57
+ return cx * SPAN + cz;
58
+ }
59
+ VoxelKey.of = of;
60
+ function parts(key) {
61
+ const cz = key % SPAN;
62
+ return { cx: (key - cz) / SPAN, cz };
63
+ }
64
+ VoxelKey.parts = parts;
65
+ })(VoxelKey || (VoxelKey = {}));
66
+ export class VoxelStore {
67
+ constructor() {
68
+ this.chunks = new Map();
69
+ }
70
+ chunk(cx, cz) {
71
+ return this.chunks.get(VoxelKey.of(cx, cz));
72
+ }
73
+ add(chunk) {
74
+ this.chunks.set(VoxelKey.of(chunk.cx, chunk.cz), chunk);
75
+ }
76
+ remove(cx, cz) {
77
+ this.chunks.delete(VoxelKey.of(cx, cz));
78
+ }
79
+ /** The block at a point; undefined where nothing is loaded. */
80
+ get(x, y, z) {
81
+ const chunk = this.chunks.get(VoxelKey.of(Math.floor(x / CHUNK), Math.floor(z / CHUNK)));
82
+ if (!chunk) {
83
+ return undefined;
84
+ }
85
+ return chunk.get(x - chunk.cx * CHUNK, y, z - chunk.cz * CHUNK);
86
+ }
87
+ /** Sets a block; the chunks whose mesh it changes (its own, and a neighbour's when it is at the edge). */
88
+ set(x, y, z, id) {
89
+ const cx = Math.floor(x / CHUNK);
90
+ const cz = Math.floor(z / CHUNK);
91
+ const chunk = this.chunks.get(VoxelKey.of(cx, cz));
92
+ if (!chunk) {
93
+ return [];
94
+ }
95
+ const lx = x - cx * CHUNK;
96
+ const lz = z - cz * CHUNK;
97
+ chunk.set(lx, y, lz, id);
98
+ const touched = [chunk];
99
+ const near = (dx, dz) => {
100
+ const other = this.chunks.get(VoxelKey.of(cx + dx, cz + dz));
101
+ if (other) {
102
+ other.version++;
103
+ touched.push(other);
104
+ }
105
+ };
106
+ if (lx === 0) {
107
+ near(-1, 0);
108
+ }
109
+ else if (lx === CHUNK - 1) {
110
+ near(1, 0);
111
+ }
112
+ if (lz === 0) {
113
+ near(0, -1);
114
+ }
115
+ else if (lz === CHUNK - 1) {
116
+ near(0, 1);
117
+ }
118
+ return touched;
119
+ }
120
+ /** The highest block of a column that is solid by `solid`, its y; -1 if none, undefined if not loaded. */
121
+ top(x, z, solid) {
122
+ const chunk = this.chunks.get(VoxelKey.of(Math.floor(x / CHUNK), Math.floor(z / CHUNK)));
123
+ if (!chunk) {
124
+ return undefined;
125
+ }
126
+ const lx = x - chunk.cx * CHUNK;
127
+ const lz = z - chunk.cz * CHUNK;
128
+ for (let y = chunk.height - 1; y >= 0; y--) {
129
+ if (solid[chunk.get(lx, y, lz)]) {
130
+ return y;
131
+ }
132
+ }
133
+ return -1;
134
+ }
135
+ }
136
+ //# sourceMappingURL=voxel-chunks.js.map
@@ -0,0 +1,67 @@
1
+ export type ControlAction = 'primary' | 'secondary' | 'jump' | 'down' | 'interact';
2
+ export interface ControlHooks {
3
+ /** An action pressed or let go. */
4
+ action?(action: ControlAction, down: boolean): void;
5
+ /** The wheel, or a number key (1 to 9: `slot`). */
6
+ pick?(change: {
7
+ step?: number;
8
+ slot?: number;
9
+ }): void;
10
+ /** A key the controls do not use (M, Esc…). */
11
+ key?(key: string, event: KeyboardEvent): void;
12
+ }
13
+ export declare class VoxelControls {
14
+ protected readonly element: HTMLElement;
15
+ protected readonly hooks: ControlHooks;
16
+ yaw: number;
17
+ pitch: number;
18
+ readonly held: Set<ControlAction>;
19
+ /** The stick of a touch screen, -1 to 1 each way; where it started and is, for the page to draw. */
20
+ stick?: {
21
+ x: number;
22
+ y: number;
23
+ ox: number;
24
+ oy: number;
25
+ px: number;
26
+ py: number;
27
+ };
28
+ run: boolean;
29
+ protected readonly keys: Set<string>;
30
+ protected readonly touches: Map<number, {
31
+ kind: 'stick' | 'look';
32
+ x: number;
33
+ y: number;
34
+ }>;
35
+ /** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
36
+ protected lockRefused: boolean;
37
+ protected readonly off: (() => void)[];
38
+ constructor(element: HTMLElement, hooks?: ControlHooks);
39
+ get locked(): boolean;
40
+ /** Where to walk: forward and to the right, -1 to 1. */
41
+ movement(): {
42
+ forward: number;
43
+ strafe: number;
44
+ };
45
+ /** Whether running: Shift held (walking; flying it goes down), or the stick pushed all the way. */
46
+ running(): boolean;
47
+ /** A button of the page pressed or let go. */
48
+ press(action: ControlAction, down: boolean): void;
49
+ /** Lets go of everything: the page was left, a panel opened. */
50
+ release(): void;
51
+ turn(dx: number, dy: number, scale?: number): void;
52
+ /** The way it looks, as a vector (x east, y up, z south). */
53
+ direction(): {
54
+ x: number;
55
+ y: number;
56
+ z: number;
57
+ };
58
+ dispose(): void;
59
+ protected keyDown(event: KeyboardEvent): void;
60
+ protected keyUp(event: KeyboardEvent): void;
61
+ protected mouseDown(event: MouseEvent): void;
62
+ protected mouseUp(event: MouseEvent): void;
63
+ protected mouseMove(event: MouseEvent): void;
64
+ protected touchStart(event: TouchEvent): void;
65
+ protected touchMove(event: TouchEvent): void;
66
+ protected touchEnd(event: TouchEvent): void;
67
+ }