forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockBreak.js
More file actions
140 lines (133 loc) · 4.25 KB
/
BlockBreak.js
File metadata and controls
140 lines (133 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
NearestFilter,
Mesh,
BoxBufferGeometry,
MeshBasicMaterial,
LineSegments,
EdgesGeometry,
LineBasicMaterial,
} from "three";
class BlockBreak {
constructor(game) {
this.game = game;
this.texture = this.game.al.get("blocksAtlasSnap");
this.texture.magFilter = NearestFilter;
this.cursor = new Mesh(
new BoxBufferGeometry(1.001, 1.001, 1.001),
new MeshBasicMaterial({
map: this.texture,
transparent: true,
})
);
this.lastPos = [];
this.cursorOut = new LineSegments(
new EdgesGeometry(this.cursor.geometry),
new LineBasicMaterial({
color: 0x000000,
})
);
this.game.scene.add(this.cursor, this.cursorOut);
this.uv = {};
this.isDigging = false;
this.done = true;
this.setState(0);
}
setState(state) {
if (state === 0) {
return (this.cursor.material.visible = false);
} else {
this.cursor.material.visible = true;
const toxX = 6 + state;
const toxY = 8;
const q = 1 / 27;
for (
let i = 0;
i <= this.cursor.geometry.attributes.uv.array.length;
i++
) {
if (this.uv[i] === void 0) {
if (i % 2 === 0) {
if (this.cursor.geometry.attributes.uv.array[i] === 0) {
this.uv[i] = 0;
} else {
this.uv[i] = 1;
}
} else {
if (this.cursor.geometry.attributes.uv.array[i] === 0) {
this.uv[i] = 0;
} else {
this.uv[i] = 1;
}
}
}
if (i % 2 === 0) {
if (this.uv[i] === 0) {
this.cursor.geometry.attributes.uv.array[i] = q * toxX;
} else {
this.cursor.geometry.attributes.uv.array[i] =
q * toxX + q;
}
} else {
if (this.uv[i] === 0) {
this.cursor.geometry.attributes.uv.array[i] =
1 - q * toxY - q;
} else {
this.cursor.geometry.attributes.uv.array[i] =
1 - q * toxY;
}
}
}
return (this.cursor.geometry.attributes.uv.needsUpdate = true);
}
}
updatePos(cb) {
const rayBlock = this.game.world.getRayBlock();
if (JSON.stringify(this.lastPos) !== JSON.stringify(rayBlock)) {
this.lastPos = rayBlock;
cb();
}
if (rayBlock) {
const pos = rayBlock.posBreak;
this.cursor.position.set(...pos);
this.cursor.visible = true;
this.cursorOut.position.set(...pos);
return (this.cursorOut.visible = true);
} else {
this.cursor.visible = false;
return (this.cursorOut.visible = false);
}
}
digRequest() {
// console.log("REQUESTING DIGGING...");
const pos = this.game.world.getRayBlock().posBreak;
if (pos !== void 0) {
this.game.socket.emit("dig", pos);
this.done = false;
}
}
startDigging(time) {
let ile = 0;
if (this.isDigging === false) {
this.isDigging = true;
this.int = setInterval(() => {
if (ile === 11) {
this.setState(0);
clearInterval(this.int);
this.isDigging = false;
} else {
this.setState(ile);
}
ile++;
}, time / 10);
}
}
stopDigging() {
this.done = true;
this.isDigging = false;
// console.log("Digging Stopped!");
this.game.socket.emit("stopDigging");
this.setState(0);
clearInterval(this.int);
}
}
export { BlockBreak };