@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.
@@ -0,0 +1,45 @@
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
+ }
45
+ //# sourceMappingURL=voxel-blocks.d.ts.map
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ // The kinds of blocks of a world (VoxelBlocks): what each looks like and how it is met. A world defines its own;
3
+ // 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
4
+ // tile of 16 × 16 pixels the world paints (bark, bricks, blades of grass), mostly grey, tinted by the block's colour;
5
+ // one without, the grain.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.VoxelBlocks = exports.AIR = exports.TILE_COLUMNS = exports.TILE_CELL = exports.TILE_PAD = exports.TILE = void 0;
8
+ /** Pixels of a tile across; a tile in the atlas with `PAD` of its edge repeated round it, so that it does not bleed. */
9
+ exports.TILE = 16;
10
+ exports.TILE_PAD = 2;
11
+ exports.TILE_CELL = exports.TILE + exports.TILE_PAD * 2;
12
+ exports.TILE_COLUMNS = 16;
13
+ /** A grain of grey pixels, the same every time: a block of 16 × 16, RGBA. */
14
+ function grain() {
15
+ const data = new Uint8Array(exports.TILE * exports.TILE * 4);
16
+ let seed = 11;
17
+ const random = () => {
18
+ seed = (seed * 16807) % 2147483647;
19
+ return seed / 2147483647;
20
+ };
21
+ for (let i = 0; i < exports.TILE * exports.TILE; i++) {
22
+ const x = i % exports.TILE;
23
+ const y = Math.floor(i / exports.TILE);
24
+ const edge = x === 0 || y === 0 ? -14 : 0;
25
+ const value = Math.max(0, Math.min(255, 232 + Math.floor(random() * 22) + edge - (random() < 0.06 ? 22 : 0)));
26
+ data.set([value, value, value, 255], i * 4);
27
+ }
28
+ return data;
29
+ }
30
+ exports.AIR = 0;
31
+ class VoxelBlocks {
32
+ constructor() {
33
+ this.looks = [{ color: 0, solid: false, clear: true }];
34
+ this.names = new Map();
35
+ /** By id, for the mesher: drawn as solid (hides what is behind it). */
36
+ this.opaque = new Uint8Array(256);
37
+ this.solid = new Uint8Array(256);
38
+ /** The tiles (RGBA, `TILE` × `TILE`, the first the grain), and of each face of each kind its tile (id × 6 + face). */
39
+ this.tiles = [grain()];
40
+ this.faceTile = new Uint8Array(256 * 6);
41
+ this.tileNames = new Map([['grain', 0]]);
42
+ }
43
+ /** A tile of 16 × 16 pixels (RGBA, rows from the top), before the kinds that use it; its index. */
44
+ tile(name, pixels) {
45
+ const known = this.tileNames.get(name);
46
+ if (known !== undefined) {
47
+ return known;
48
+ }
49
+ this.tiles.push(pixels);
50
+ this.tileNames.set(name, this.tiles.length - 1);
51
+ return this.tiles.length - 1;
52
+ }
53
+ /** A kind of block, its id; the same name again is the same id. */
54
+ define(name, look) {
55
+ var _a, _b, _c, _d;
56
+ const known = this.names.get(name);
57
+ if (known !== undefined) {
58
+ return known;
59
+ }
60
+ const id = this.looks.length;
61
+ if (id > 255) {
62
+ throw new Error('At most 255 kinds of blocks');
63
+ }
64
+ this.looks.push(look);
65
+ this.names.set(name, id);
66
+ this.opaque[id] = look.solid && !look.clear ? 1 : 0;
67
+ this.solid[id] = look.solid ? 1 : 0;
68
+ // Faces in the mesher's order: up, down, then the four sides.
69
+ for (let f = 0; f < 6; f++) {
70
+ 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);
71
+ this.faceTile[id * 6 + f] = name ? ((_d = this.tileNames.get(name)) !== null && _d !== void 0 ? _d : 0) : 0;
72
+ }
73
+ return id;
74
+ }
75
+ id(name) {
76
+ return this.names.get(name);
77
+ }
78
+ name(id) {
79
+ for (const [name, each] of this.names) {
80
+ if (each === id) {
81
+ return name;
82
+ }
83
+ }
84
+ return undefined;
85
+ }
86
+ }
87
+ exports.VoxelBlocks = VoxelBlocks;
88
+ //# sourceMappingURL=voxel-blocks.js.map
@@ -0,0 +1,46 @@
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
+ }
46
+ //# sourceMappingURL=voxel-body.d.ts.map
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ // A body in a world of blocks (VoxelBody): a box of 0.6 × 1.8 that walks, runs, jumps, flies and swims, stopped by
3
+ // 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
4
+ // jump and walk with one thumb); on a ladder it holds on, forward against the wall it is on (or jump) goes up and
5
+ // back goes down, walking past it does not; it speeds up and slows down in about a sixth of a second.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.VoxelBody = void 0;
8
+ const HALF = 0.3;
9
+ const HEIGHT = 1.8;
10
+ const GRAVITY = 30;
11
+ const JUMP = 9.2;
12
+ const WALK = 4.4;
13
+ const RUN = 6.8;
14
+ const FLY = 12;
15
+ const SWIM = 2.6;
16
+ const CLIMB = 3.4;
17
+ /** How quickly it gets to the speed asked: a sixth of a second. */
18
+ const EASE = 6;
19
+ const STEP = 1 / 60;
20
+ class VoxelBody {
21
+ constructor(x, y, z) {
22
+ this.x = x;
23
+ this.y = y;
24
+ this.z = z;
25
+ this.vx = 0;
26
+ this.vy = 0;
27
+ this.vz = 0;
28
+ this.onGround = false;
29
+ this.inWater = false;
30
+ this.climbing = false;
31
+ /** Walking into something last time: on a ladder, up it. */
32
+ this.pushing = false;
33
+ this.flying = false;
34
+ /** How far it just went up a step: the camera eases it. */
35
+ this.stepped = 0;
36
+ }
37
+ /** Moves it on by `seconds`, in small steps. */
38
+ step(seconds, input, world) {
39
+ let left = Math.min(seconds, 0.25);
40
+ while (left > 1e-6) {
41
+ const dt = Math.min(STEP, left);
42
+ this.tick(dt, input, world);
43
+ left -= dt;
44
+ }
45
+ }
46
+ tick(dt, input, world) {
47
+ var _a;
48
+ const bx = Math.floor(this.x);
49
+ const bz = Math.floor(this.z);
50
+ this.inWater = world.liquid(bx, Math.floor(this.y + 0.4), bz);
51
+ 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));
52
+ 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;
53
+ const sin = Math.sin(input.yaw);
54
+ const cos = Math.cos(input.yaw);
55
+ const length = Math.min(1, Math.hypot(input.forward, input.strafe));
56
+ const scale = length > 0 ? length / Math.hypot(input.forward, input.strafe) : 0;
57
+ // As the camera turns by yaw: forward is (-sin, -cos), the right (cos, -sin).
58
+ const wantX = (input.strafe * cos - input.forward * sin) * scale * speed;
59
+ const wantZ = (-input.strafe * sin - input.forward * cos) * scale * speed;
60
+ const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater || this.climbing ? 3 : 1) * dt);
61
+ this.vx += (wantX - this.vx) * ease;
62
+ this.vz += (wantZ - this.vz) * ease;
63
+ if (this.flying) {
64
+ const wantY = (input.jump ? 1 : 0) - (input.down ? 1 : 0);
65
+ this.vy += (wantY * speed - this.vy) * ease;
66
+ }
67
+ else if (this.climbing) {
68
+ const up = input.jump || (input.forward > 0.3 && this.pushing);
69
+ const down = input.down || input.forward < -0.3;
70
+ this.vy = up ? CLIMB : down ? -CLIMB : 0;
71
+ }
72
+ else if (this.inWater) {
73
+ this.vy -= GRAVITY * 0.25 * dt;
74
+ this.vy = input.jump ? Math.min(this.vy + 20 * dt, 3.2) : Math.max(this.vy, -2.5);
75
+ }
76
+ else {
77
+ this.vy -= GRAVITY * dt;
78
+ if (input.jump && this.onGround) {
79
+ this.vy = JUMP;
80
+ }
81
+ this.vy = Math.max(this.vy, -50);
82
+ }
83
+ const wasOnGround = this.onGround;
84
+ this.onGround = false;
85
+ const blockedX = this.move(0, this.vx * dt, world);
86
+ const blockedZ = this.move(2, this.vz * dt, world);
87
+ // A step of one block in the way: up it, if there is room above.
88
+ this.pushing = blockedX || blockedZ;
89
+ if ((blockedX || blockedZ) && (wasOnGround || this.inWater || this.climbing) && !this.flying) {
90
+ const up = this.y + 1.01;
91
+ 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)) {
92
+ const before = this.y;
93
+ this.y = Math.floor(this.y) + 1;
94
+ this.stepped += this.y - before;
95
+ this.move(0, this.vx * dt, world);
96
+ this.move(2, this.vz * dt, world);
97
+ }
98
+ }
99
+ if (this.move(1, this.vy * dt, world)) {
100
+ if (this.vy < 0) {
101
+ this.onGround = true;
102
+ if (this.flying) {
103
+ this.flying = false;
104
+ }
105
+ }
106
+ this.vy = 0;
107
+ }
108
+ if (blockedX) {
109
+ this.vx = 0;
110
+ }
111
+ if (blockedZ) {
112
+ this.vz = 0;
113
+ }
114
+ }
115
+ /** Whether the box with its feet at (x, y, z) is in a solid block. */
116
+ hits(x, y, z, world) {
117
+ const x0 = Math.floor(x - HALF);
118
+ const x1 = Math.floor(x + HALF - 1e-6);
119
+ const y0 = Math.floor(y);
120
+ const y1 = Math.floor(y + HEIGHT - 1e-6);
121
+ const z0 = Math.floor(z - HALF);
122
+ const z1 = Math.floor(z + HALF - 1e-6);
123
+ for (let by = y0; by <= y1; by++) {
124
+ for (let bz = z0; bz <= z1; bz++) {
125
+ for (let bx = x0; bx <= x1; bx++) {
126
+ if (world.solid(bx, by, bz)) {
127
+ return true;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ return false;
133
+ }
134
+ /** Moves along one axis (0 x, 1 y, 2 z) as far as it can; whether something stopped it. */
135
+ move(axis, distance, world) {
136
+ if (distance === 0) {
137
+ return false;
138
+ }
139
+ // Fast (flying boosted), in pieces of less than a block, so as not to go through a wall.
140
+ if (Math.abs(distance) > 0.9) {
141
+ const pieces = Math.ceil(Math.abs(distance) / 0.9);
142
+ for (let i = 0; i < pieces; i++) {
143
+ if (this.move(axis, distance / pieces, world)) {
144
+ return true;
145
+ }
146
+ }
147
+ return false;
148
+ }
149
+ const now = [this.x, this.y, this.z];
150
+ const wanted = [...now];
151
+ wanted[axis] += distance;
152
+ if (!this.hits(wanted[0], wanted[1], wanted[2], world)) {
153
+ [this.x, this.y, this.z] = wanted;
154
+ return false;
155
+ }
156
+ // Up against the face of the block in the way, no farther back than it was.
157
+ const at = wanted[axis];
158
+ let to;
159
+ if (axis === 1) {
160
+ to = distance > 0 ? Math.floor(at + HEIGHT) - HEIGHT - 1e-4 : Math.floor(at) + 1 + 1e-4;
161
+ }
162
+ else {
163
+ to = distance > 0 ? Math.floor(at + HALF) - HALF - 1e-4 : Math.floor(at - HALF) + 1 + HALF + 1e-4;
164
+ }
165
+ const placed = [...now];
166
+ placed[axis] = distance > 0 ? Math.min(Math.max(to, now[axis]), at) : Math.max(Math.min(to, now[axis]), at);
167
+ if (!this.hits(placed[0], placed[1], placed[2], world)) {
168
+ [this.x, this.y, this.z] = placed;
169
+ }
170
+ return true;
171
+ }
172
+ }
173
+ exports.VoxelBody = VoxelBody;
174
+ /** The height of the eyes above the feet. */
175
+ VoxelBody.EYES = 1.62;
176
+ //# sourceMappingURL=voxel-body.js.map
@@ -0,0 +1,38 @@
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
+ }
38
+ //# sourceMappingURL=voxel-chunks.d.ts.map
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ // The blocks of a world (VoxelStore), in chunks of 16 × 16 columns, each as high as its highest block (to 8 more),
3
+ // 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
4
+ // (x, y, z): x east, y up from 0, z south. What is not loaded is undefined, which the mesher takes as hidden.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.VoxelStore = exports.VoxelKey = exports.VoxelChunk = exports.MAX_HEIGHT = exports.CHUNK = void 0;
7
+ exports.CHUNK = 16;
8
+ const AREA = exports.CHUNK * exports.CHUNK;
9
+ /** The highest a world goes. */
10
+ exports.MAX_HEIGHT = 256;
11
+ class VoxelChunk {
12
+ constructor(cx, cz, height = 8) {
13
+ this.cx = cx;
14
+ this.cz = cz;
15
+ this.height = height;
16
+ /** One more each time a block changes: its mesh is made again. */
17
+ this.version = 0;
18
+ this.data = new Uint8Array(AREA * height);
19
+ }
20
+ get(x, y, z) {
21
+ return y >= 0 && y < this.height ? this.data[(y * exports.CHUNK + z) * exports.CHUNK + x] : 0;
22
+ }
23
+ set(x, y, z, id) {
24
+ if (y < 0 || y >= exports.MAX_HEIGHT) {
25
+ return;
26
+ }
27
+ if (y >= this.height) {
28
+ if (id === 0) {
29
+ return;
30
+ }
31
+ this.grow(y + 1);
32
+ }
33
+ this.data[(y * exports.CHUNK + z) * exports.CHUNK + x] = id;
34
+ this.version++;
35
+ }
36
+ /** Fills a column from `from` to `to` (not including). */
37
+ column(x, z, from, to, id) {
38
+ if (to > this.height) {
39
+ this.grow(to);
40
+ }
41
+ for (let y = Math.max(0, from); y < to; y++) {
42
+ this.data[(y * exports.CHUNK + z) * exports.CHUNK + x] = id;
43
+ }
44
+ this.version++;
45
+ }
46
+ grow(height) {
47
+ const next = Math.min(exports.MAX_HEIGHT, Math.ceil(height / 8) * 8);
48
+ const data = new Uint8Array(AREA * next);
49
+ data.set(this.data);
50
+ this.data = data;
51
+ this.height = next;
52
+ }
53
+ }
54
+ exports.VoxelChunk = VoxelChunk;
55
+ /** Chunks of the earth across: its 2^25 columns, 16 a chunk. */
56
+ const SPAN = 2097152;
57
+ var VoxelKey;
58
+ (function (VoxelKey) {
59
+ /** A chunk by its coordinates, as one number (cz below 2^21, as the chunks of the earth's grid are). */
60
+ function of(cx, cz) {
61
+ return cx * SPAN + cz;
62
+ }
63
+ VoxelKey.of = of;
64
+ function parts(key) {
65
+ const cz = key % SPAN;
66
+ return { cx: (key - cz) / SPAN, cz };
67
+ }
68
+ VoxelKey.parts = parts;
69
+ })(VoxelKey || (exports.VoxelKey = VoxelKey = {}));
70
+ class VoxelStore {
71
+ constructor() {
72
+ this.chunks = new Map();
73
+ }
74
+ chunk(cx, cz) {
75
+ return this.chunks.get(VoxelKey.of(cx, cz));
76
+ }
77
+ add(chunk) {
78
+ this.chunks.set(VoxelKey.of(chunk.cx, chunk.cz), chunk);
79
+ }
80
+ remove(cx, cz) {
81
+ this.chunks.delete(VoxelKey.of(cx, cz));
82
+ }
83
+ /** The block at a point; undefined where nothing is loaded. */
84
+ get(x, y, z) {
85
+ const chunk = this.chunks.get(VoxelKey.of(Math.floor(x / exports.CHUNK), Math.floor(z / exports.CHUNK)));
86
+ if (!chunk) {
87
+ return undefined;
88
+ }
89
+ return chunk.get(x - chunk.cx * exports.CHUNK, y, z - chunk.cz * exports.CHUNK);
90
+ }
91
+ /** Sets a block; the chunks whose mesh it changes (its own, and a neighbour's when it is at the edge). */
92
+ set(x, y, z, id) {
93
+ const cx = Math.floor(x / exports.CHUNK);
94
+ const cz = Math.floor(z / exports.CHUNK);
95
+ const chunk = this.chunks.get(VoxelKey.of(cx, cz));
96
+ if (!chunk) {
97
+ return [];
98
+ }
99
+ const lx = x - cx * exports.CHUNK;
100
+ const lz = z - cz * exports.CHUNK;
101
+ chunk.set(lx, y, lz, id);
102
+ const touched = [chunk];
103
+ const near = (dx, dz) => {
104
+ const other = this.chunks.get(VoxelKey.of(cx + dx, cz + dz));
105
+ if (other) {
106
+ other.version++;
107
+ touched.push(other);
108
+ }
109
+ };
110
+ if (lx === 0) {
111
+ near(-1, 0);
112
+ }
113
+ else if (lx === exports.CHUNK - 1) {
114
+ near(1, 0);
115
+ }
116
+ if (lz === 0) {
117
+ near(0, -1);
118
+ }
119
+ else if (lz === exports.CHUNK - 1) {
120
+ near(0, 1);
121
+ }
122
+ return touched;
123
+ }
124
+ /** The highest block of a column that is solid by `solid`, its y; -1 if none, undefined if not loaded. */
125
+ top(x, z, solid) {
126
+ const chunk = this.chunks.get(VoxelKey.of(Math.floor(x / exports.CHUNK), Math.floor(z / exports.CHUNK)));
127
+ if (!chunk) {
128
+ return undefined;
129
+ }
130
+ const lx = x - chunk.cx * exports.CHUNK;
131
+ const lz = z - chunk.cz * exports.CHUNK;
132
+ for (let y = chunk.height - 1; y >= 0; y--) {
133
+ if (solid[chunk.get(lx, y, lz)]) {
134
+ return y;
135
+ }
136
+ }
137
+ return -1;
138
+ }
139
+ }
140
+ exports.VoxelStore = VoxelStore;
141
+ //# sourceMappingURL=voxel-chunks.js.map
@@ -0,0 +1,68 @@
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
+ }
68
+ //# sourceMappingURL=voxel-controls.d.ts.map