@celljs/ta-voxel 0.0.0-stage → 4.30.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 +3 -0
- package/README.md +11 -2
- package/esm/index.d.ts +7 -0
- package/esm/index.js +8 -0
- package/esm/package.json +3 -0
- package/esm/voxel-blocks.d.ts +26 -0
- package/esm/voxel-blocks.js +40 -0
- package/esm/voxel-body.d.ts +38 -0
- package/esm/voxel-body.js +147 -0
- package/esm/voxel-chunks.d.ts +37 -0
- package/esm/voxel-chunks.js +134 -0
- package/esm/voxel-controls.d.ts +71 -0
- package/esm/voxel-controls.js +244 -0
- package/esm/voxel-mesher.d.ts +20 -0
- package/esm/voxel-mesher.js +128 -0
- package/esm/voxel-ray.d.ts +21 -0
- package/esm/voxel-ray.js +30 -0
- package/esm/voxel-three.d.ts +28 -0
- package/esm/voxel-three.js +104 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +24 -0
- package/lib/voxel-blocks.d.ts +27 -0
- package/lib/voxel-blocks.js +44 -0
- package/lib/voxel-body.d.ts +39 -0
- package/lib/voxel-body.js +151 -0
- package/lib/voxel-chunks.d.ts +38 -0
- package/lib/voxel-chunks.js +139 -0
- package/lib/voxel-controls.d.ts +72 -0
- package/lib/voxel-controls.js +248 -0
- package/lib/voxel-mesher.d.ts +21 -0
- package/lib/voxel-mesher.js +131 -0
- package/lib/voxel-ray.d.ts +22 -0
- package/lib/voxel-ray.js +33 -0
- package/lib/voxel-three.d.ts +29 -0
- package/lib/voxel-three.js +109 -0
- package/package.json +48 -4
|
@@ -0,0 +1,44 @@
|
|
|
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.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.VoxelBlocks = exports.AIR = void 0;
|
|
6
|
+
exports.AIR = 0;
|
|
7
|
+
class VoxelBlocks {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.looks = [{ color: 0, solid: false, clear: true }];
|
|
10
|
+
this.names = new Map();
|
|
11
|
+
/** By id, for the mesher: drawn as solid (hides what is behind it). */
|
|
12
|
+
this.opaque = new Uint8Array(256);
|
|
13
|
+
this.solid = new Uint8Array(256);
|
|
14
|
+
}
|
|
15
|
+
/** A kind of block, its id; the same name again is the same id. */
|
|
16
|
+
define(name, look) {
|
|
17
|
+
const known = this.names.get(name);
|
|
18
|
+
if (known !== undefined) {
|
|
19
|
+
return known;
|
|
20
|
+
}
|
|
21
|
+
const id = this.looks.length;
|
|
22
|
+
if (id > 255) {
|
|
23
|
+
throw new Error('At most 255 kinds of blocks');
|
|
24
|
+
}
|
|
25
|
+
this.looks.push(look);
|
|
26
|
+
this.names.set(name, id);
|
|
27
|
+
this.opaque[id] = look.solid && !look.clear ? 1 : 0;
|
|
28
|
+
this.solid[id] = look.solid ? 1 : 0;
|
|
29
|
+
return id;
|
|
30
|
+
}
|
|
31
|
+
id(name) {
|
|
32
|
+
return this.names.get(name);
|
|
33
|
+
}
|
|
34
|
+
name(id) {
|
|
35
|
+
for (const [name, each] of this.names) {
|
|
36
|
+
if (each === id) {
|
|
37
|
+
return name;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.VoxelBlocks = VoxelBlocks;
|
|
44
|
+
//# sourceMappingURL=voxel-blocks.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
12
|
+
export interface BodyWorld {
|
|
13
|
+
solid(x: number, y: number, z: number): boolean;
|
|
14
|
+
liquid(x: number, y: number, z: number): boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare class VoxelBody {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
z: number;
|
|
20
|
+
vx: number;
|
|
21
|
+
vy: number;
|
|
22
|
+
vz: number;
|
|
23
|
+
onGround: boolean;
|
|
24
|
+
inWater: boolean;
|
|
25
|
+
flying: boolean;
|
|
26
|
+
/** How far it just went up a step: the camera eases it. */
|
|
27
|
+
stepped: number;
|
|
28
|
+
constructor(x: number, y: number, z: number);
|
|
29
|
+
/** Moves it on by `seconds`, in small steps. */
|
|
30
|
+
step(seconds: number, input: BodyInput, world: BodyWorld): void;
|
|
31
|
+
protected tick(dt: number, input: BodyInput, world: BodyWorld): void;
|
|
32
|
+
/** Whether the box with its feet at (x, y, z) is in a solid block. */
|
|
33
|
+
hits(x: number, y: number, z: number, world: BodyWorld): boolean;
|
|
34
|
+
/** Moves along one axis (0 x, 1 y, 2 z) as far as it can; whether something stopped it. */
|
|
35
|
+
protected move(axis: 0 | 1 | 2, distance: number, world: BodyWorld): boolean;
|
|
36
|
+
/** The height of the eyes above the feet. */
|
|
37
|
+
static readonly EYES = 1.62;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=voxel-body.d.ts.map
|
|
@@ -0,0 +1,151 @@
|
|
|
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); it speeds up and slows down in about a sixth of a second.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.VoxelBody = void 0;
|
|
7
|
+
const HALF = 0.3;
|
|
8
|
+
const HEIGHT = 1.8;
|
|
9
|
+
const GRAVITY = 30;
|
|
10
|
+
const JUMP = 9.2;
|
|
11
|
+
const WALK = 4.4;
|
|
12
|
+
const RUN = 6.8;
|
|
13
|
+
const FLY = 12;
|
|
14
|
+
const SWIM = 2.6;
|
|
15
|
+
/** How quickly it gets to the speed asked: a sixth of a second. */
|
|
16
|
+
const EASE = 6;
|
|
17
|
+
const STEP = 1 / 60;
|
|
18
|
+
class VoxelBody {
|
|
19
|
+
constructor(x, y, z) {
|
|
20
|
+
this.x = x;
|
|
21
|
+
this.y = y;
|
|
22
|
+
this.z = z;
|
|
23
|
+
this.vx = 0;
|
|
24
|
+
this.vy = 0;
|
|
25
|
+
this.vz = 0;
|
|
26
|
+
this.onGround = false;
|
|
27
|
+
this.inWater = false;
|
|
28
|
+
this.flying = false;
|
|
29
|
+
/** How far it just went up a step: the camera eases it. */
|
|
30
|
+
this.stepped = 0;
|
|
31
|
+
}
|
|
32
|
+
/** Moves it on by `seconds`, in small steps. */
|
|
33
|
+
step(seconds, input, world) {
|
|
34
|
+
let left = Math.min(seconds, 0.25);
|
|
35
|
+
while (left > 1e-6) {
|
|
36
|
+
const dt = Math.min(STEP, left);
|
|
37
|
+
this.tick(dt, input, world);
|
|
38
|
+
left -= dt;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
tick(dt, input, world) {
|
|
42
|
+
this.inWater = world.liquid(Math.floor(this.x), Math.floor(this.y + 0.4), Math.floor(this.z));
|
|
43
|
+
const speed = this.flying ? FLY * (input.run ? 1.6 : 1) : this.inWater ? SWIM : input.run ? RUN : WALK;
|
|
44
|
+
const sin = Math.sin(input.yaw);
|
|
45
|
+
const cos = Math.cos(input.yaw);
|
|
46
|
+
const length = Math.min(1, Math.hypot(input.forward, input.strafe));
|
|
47
|
+
const scale = length > 0 ? length / Math.hypot(input.forward, input.strafe) : 0;
|
|
48
|
+
// Forward is -z at yaw 0; the right is +x.
|
|
49
|
+
const wantX = (input.strafe * cos - input.forward * sin) * scale * speed;
|
|
50
|
+
const wantZ = (input.strafe * sin + input.forward * -cos) * scale * speed;
|
|
51
|
+
const ease = 1 - Math.exp(-EASE * (this.onGround || this.flying || this.inWater ? 3 : 1) * dt);
|
|
52
|
+
this.vx += (wantX - this.vx) * ease;
|
|
53
|
+
this.vz += (wantZ - this.vz) * ease;
|
|
54
|
+
if (this.flying) {
|
|
55
|
+
const wantY = (input.jump ? 1 : 0) - (input.down ? 1 : 0);
|
|
56
|
+
this.vy += (wantY * speed * 0.7 - this.vy) * ease;
|
|
57
|
+
}
|
|
58
|
+
else if (this.inWater) {
|
|
59
|
+
this.vy -= GRAVITY * 0.25 * dt;
|
|
60
|
+
this.vy = input.jump ? Math.min(this.vy + 20 * dt, 3.2) : Math.max(this.vy, -2.5);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.vy -= GRAVITY * dt;
|
|
64
|
+
if (input.jump && this.onGround) {
|
|
65
|
+
this.vy = JUMP;
|
|
66
|
+
}
|
|
67
|
+
this.vy = Math.max(this.vy, -50);
|
|
68
|
+
}
|
|
69
|
+
const wasOnGround = this.onGround;
|
|
70
|
+
this.onGround = false;
|
|
71
|
+
const blockedX = this.move(0, this.vx * dt, world);
|
|
72
|
+
const blockedZ = this.move(2, this.vz * dt, world);
|
|
73
|
+
// A step of one block in the way: up it, if there is room above.
|
|
74
|
+
if ((blockedX || blockedZ) && (wasOnGround || this.inWater) && !this.flying) {
|
|
75
|
+
const up = this.y + 1.01;
|
|
76
|
+
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)) {
|
|
77
|
+
const before = this.y;
|
|
78
|
+
this.y = Math.floor(this.y) + 1;
|
|
79
|
+
this.stepped += this.y - before;
|
|
80
|
+
this.move(0, this.vx * dt, world);
|
|
81
|
+
this.move(2, this.vz * dt, world);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (this.move(1, this.vy * dt, world)) {
|
|
85
|
+
if (this.vy < 0) {
|
|
86
|
+
this.onGround = true;
|
|
87
|
+
if (this.flying) {
|
|
88
|
+
this.flying = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.vy = 0;
|
|
92
|
+
}
|
|
93
|
+
if (blockedX) {
|
|
94
|
+
this.vx = 0;
|
|
95
|
+
}
|
|
96
|
+
if (blockedZ) {
|
|
97
|
+
this.vz = 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Whether the box with its feet at (x, y, z) is in a solid block. */
|
|
101
|
+
hits(x, y, z, world) {
|
|
102
|
+
const x0 = Math.floor(x - HALF);
|
|
103
|
+
const x1 = Math.floor(x + HALF - 1e-6);
|
|
104
|
+
const y0 = Math.floor(y);
|
|
105
|
+
const y1 = Math.floor(y + HEIGHT - 1e-6);
|
|
106
|
+
const z0 = Math.floor(z - HALF);
|
|
107
|
+
const z1 = Math.floor(z + HALF - 1e-6);
|
|
108
|
+
for (let by = y0; by <= y1; by++) {
|
|
109
|
+
for (let bz = z0; bz <= z1; bz++) {
|
|
110
|
+
for (let bx = x0; bx <= x1; bx++) {
|
|
111
|
+
if (world.solid(bx, by, bz)) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
/** Moves along one axis (0 x, 1 y, 2 z) as far as it can; whether something stopped it. */
|
|
120
|
+
move(axis, distance, world) {
|
|
121
|
+
if (distance === 0) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const now = [this.x, this.y, this.z];
|
|
125
|
+
const wanted = [...now];
|
|
126
|
+
wanted[axis] += distance;
|
|
127
|
+
if (!this.hits(wanted[0], wanted[1], wanted[2], world)) {
|
|
128
|
+
[this.x, this.y, this.z] = wanted;
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
// Up against the face of the block in the way, no farther back than it was.
|
|
132
|
+
const at = wanted[axis];
|
|
133
|
+
let to;
|
|
134
|
+
if (axis === 1) {
|
|
135
|
+
to = distance > 0 ? Math.floor(at + HEIGHT) - HEIGHT - 1e-4 : Math.floor(at) + 1 + 1e-4;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
to = distance > 0 ? Math.floor(at + HALF) - HALF - 1e-4 : Math.floor(at - HALF) + 1 + HALF + 1e-4;
|
|
139
|
+
}
|
|
140
|
+
const placed = [...now];
|
|
141
|
+
placed[axis] = distance > 0 ? Math.min(Math.max(to, now[axis]), at) : Math.max(Math.min(to, now[axis]), at);
|
|
142
|
+
if (!this.hits(placed[0], placed[1], placed[2], world)) {
|
|
143
|
+
[this.x, this.y, this.z] = placed;
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.VoxelBody = VoxelBody;
|
|
149
|
+
/** The height of the eyes above the feet. */
|
|
150
|
+
VoxelBody.EYES = 1.62;
|
|
151
|
+
//# 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^20, as the columns 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,139 @@
|
|
|
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
|
+
var VoxelKey;
|
|
56
|
+
(function (VoxelKey) {
|
|
57
|
+
/** A chunk by its coordinates, as one number (cz below 2^20, as the columns of the earth's grid are). */
|
|
58
|
+
function of(cx, cz) {
|
|
59
|
+
return cx * 1048576 + cz;
|
|
60
|
+
}
|
|
61
|
+
VoxelKey.of = of;
|
|
62
|
+
function parts(key) {
|
|
63
|
+
const cz = key % 1048576;
|
|
64
|
+
return { cx: (key - cz) / 1048576, cz };
|
|
65
|
+
}
|
|
66
|
+
VoxelKey.parts = parts;
|
|
67
|
+
})(VoxelKey || (exports.VoxelKey = VoxelKey = {}));
|
|
68
|
+
class VoxelStore {
|
|
69
|
+
constructor() {
|
|
70
|
+
this.chunks = new Map();
|
|
71
|
+
}
|
|
72
|
+
chunk(cx, cz) {
|
|
73
|
+
return this.chunks.get(VoxelKey.of(cx, cz));
|
|
74
|
+
}
|
|
75
|
+
add(chunk) {
|
|
76
|
+
this.chunks.set(VoxelKey.of(chunk.cx, chunk.cz), chunk);
|
|
77
|
+
}
|
|
78
|
+
remove(cx, cz) {
|
|
79
|
+
this.chunks.delete(VoxelKey.of(cx, cz));
|
|
80
|
+
}
|
|
81
|
+
/** The block at a point; undefined where nothing is loaded. */
|
|
82
|
+
get(x, y, z) {
|
|
83
|
+
const chunk = this.chunks.get(VoxelKey.of(Math.floor(x / exports.CHUNK), Math.floor(z / exports.CHUNK)));
|
|
84
|
+
if (!chunk) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
return chunk.get(x - chunk.cx * exports.CHUNK, y, z - chunk.cz * exports.CHUNK);
|
|
88
|
+
}
|
|
89
|
+
/** Sets a block; the chunks whose mesh it changes (its own, and a neighbour's when it is at the edge). */
|
|
90
|
+
set(x, y, z, id) {
|
|
91
|
+
const cx = Math.floor(x / exports.CHUNK);
|
|
92
|
+
const cz = Math.floor(z / exports.CHUNK);
|
|
93
|
+
const chunk = this.chunks.get(VoxelKey.of(cx, cz));
|
|
94
|
+
if (!chunk) {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
const lx = x - cx * exports.CHUNK;
|
|
98
|
+
const lz = z - cz * exports.CHUNK;
|
|
99
|
+
chunk.set(lx, y, lz, id);
|
|
100
|
+
const touched = [chunk];
|
|
101
|
+
const near = (dx, dz) => {
|
|
102
|
+
const other = this.chunks.get(VoxelKey.of(cx + dx, cz + dz));
|
|
103
|
+
if (other) {
|
|
104
|
+
other.version++;
|
|
105
|
+
touched.push(other);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
if (lx === 0) {
|
|
109
|
+
near(-1, 0);
|
|
110
|
+
}
|
|
111
|
+
else if (lx === exports.CHUNK - 1) {
|
|
112
|
+
near(1, 0);
|
|
113
|
+
}
|
|
114
|
+
if (lz === 0) {
|
|
115
|
+
near(0, -1);
|
|
116
|
+
}
|
|
117
|
+
else if (lz === exports.CHUNK - 1) {
|
|
118
|
+
near(0, 1);
|
|
119
|
+
}
|
|
120
|
+
return touched;
|
|
121
|
+
}
|
|
122
|
+
/** The highest block of a column that is solid by `solid`, its y; -1 if none, undefined if not loaded. */
|
|
123
|
+
top(x, z, solid) {
|
|
124
|
+
const chunk = this.chunks.get(VoxelKey.of(Math.floor(x / exports.CHUNK), Math.floor(z / exports.CHUNK)));
|
|
125
|
+
if (!chunk) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const lx = x - chunk.cx * exports.CHUNK;
|
|
129
|
+
const lz = z - chunk.cz * exports.CHUNK;
|
|
130
|
+
for (let y = chunk.height - 1; y >= 0; y--) {
|
|
131
|
+
if (solid[chunk.get(lx, y, lz)]) {
|
|
132
|
+
return y;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return -1;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.VoxelStore = VoxelStore;
|
|
139
|
+
//# sourceMappingURL=voxel-chunks.js.map
|
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
protected lastJump: number;
|
|
36
|
+
/** The page would not lock the mouse (a desktop app without it): a click acts, a drag looks. */
|
|
37
|
+
protected lockRefused: boolean;
|
|
38
|
+
/** Twice the jump: the game flies, or lands. */
|
|
39
|
+
onDoubleJump?: () => void;
|
|
40
|
+
protected readonly off: (() => void)[];
|
|
41
|
+
constructor(element: HTMLElement, hooks?: ControlHooks);
|
|
42
|
+
get locked(): boolean;
|
|
43
|
+
/** Where to walk: forward and to the right, -1 to 1. */
|
|
44
|
+
movement(): {
|
|
45
|
+
forward: number;
|
|
46
|
+
strafe: number;
|
|
47
|
+
};
|
|
48
|
+
/** Whether running: Shift held (walking; flying it goes down), or the stick pushed all the way. */
|
|
49
|
+
running(): boolean;
|
|
50
|
+
/** A button of the page pressed or let go. */
|
|
51
|
+
press(action: ControlAction, down: boolean): void;
|
|
52
|
+
/** Lets go of everything: the page was left, a panel opened. */
|
|
53
|
+
release(): void;
|
|
54
|
+
turn(dx: number, dy: number, scale?: number): void;
|
|
55
|
+
/** The way it looks, as a vector (x east, y up, z south). */
|
|
56
|
+
direction(): {
|
|
57
|
+
x: number;
|
|
58
|
+
y: number;
|
|
59
|
+
z: number;
|
|
60
|
+
};
|
|
61
|
+
dispose(): void;
|
|
62
|
+
protected jumped(): void;
|
|
63
|
+
protected keyDown(event: KeyboardEvent): void;
|
|
64
|
+
protected keyUp(event: KeyboardEvent): void;
|
|
65
|
+
protected mouseDown(event: MouseEvent): void;
|
|
66
|
+
protected mouseUp(event: MouseEvent): void;
|
|
67
|
+
protected mouseMove(event: MouseEvent): void;
|
|
68
|
+
protected touchStart(event: TouchEvent): void;
|
|
69
|
+
protected touchMove(event: TouchEvent): void;
|
|
70
|
+
protected touchEnd(event: TouchEvent): void;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=voxel-controls.d.ts.map
|