Skip to content

Commit 4b1c20b

Browse files
committed
Complete conversion to ES6
1 parent 6550930 commit 4b1c20b

File tree

1 file changed

+70
-145
lines changed

1 file changed

+70
-145
lines changed

mine.js

+70-145
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class Mine extends EventEmitter {
146146
this.destroyOverlay();
147147
this.progress = 0;
148148
});
149+
}
149150

150151
disable() {
151152
this.reach.removeListener('mining', this.onMining);
@@ -197,148 +198,72 @@ class Mine extends EventEmitter {
197198
}
198199
}
199200
}
200-
//TODO
201-
Mine::createOverlay = (target) ->
202-
if this.instaMine or not this.texturesEnabled
203-
return
204-
205-
this.destroyOverlay()
206-
207-
if this.decals
208-
this.decalPosition = target.voxel.slice(0)
209-
this.decalNormal = target.normal.slice(0)
210-
211-
this.decals.add
212-
position: this.decalPosition
213-
normal: this.decalNormal
214-
texture: this.progressTextures[0]
215-
216-
this.decals.update()
217-
else
218-
this.createOverlayThreejs(target)
219-
220-
Mine::createOverlayThreejs = (target) ->
221-
geometry = new this.game.THREE.Geometry()
222-
# TODO: actually compute this
223-
if target.normal[2] == 1
224-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 0))
225-
geometry.vertices.push(new this.game.THREE.Vector3(1, 0, 0))
226-
geometry.vertices.push(new this.game.THREE.Vector3(1, 1, 0))
227-
geometry.vertices.push(new this.game.THREE.Vector3(0, 1, 0))
228-
offset = [0, 0, 1]
229-
else if target.normal[1] == 1
230-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 0))
231-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 1))
232-
geometry.vertices.push(new this.game.THREE.Vector3(1, 0, 1))
233-
geometry.vertices.push(new this.game.THREE.Vector3(1, 0, 0))
234-
offset = [0, 1, 0]
235-
else if target.normal[0] == 1
236-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 0))
237-
geometry.vertices.push(new this.game.THREE.Vector3(0, 1, 0))
238-
geometry.vertices.push(new this.game.THREE.Vector3(0, 1, 1))
239-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 1))
240-
offset = [1, 0, 0]
241-
else if target.normal[0] == -1
242-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 0))
243-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 1))
244-
geometry.vertices.push(new this.game.THREE.Vector3(0, 1, 1))
245-
geometry.vertices.push(new this.game.THREE.Vector3(0, 1, 0))
246-
offset = [0, 0, 0]
247-
else if target.normal[1] == -1
248-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 0))
249-
geometry.vertices.push(new this.game.THREE.Vector3(1, 0, 0))
250-
geometry.vertices.push(new this.game.THREE.Vector3(1, 0, 1))
251-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 1))
252-
offset = [0, 0, 0]
253-
else if target.normal[2] == -1
254-
geometry.vertices.push(new this.game.THREE.Vector3(0, 0, 0))
255-
geometry.vertices.push(new this.game.THREE.Vector3(0, 1, 0))
256-
geometry.vertices.push(new this.game.THREE.Vector3(1, 1, 0))
257-
geometry.vertices.push(new this.game.THREE.Vector3(1, 0, 0))
258-
offset = [0, 0, 0]
259-
else
260-
console.log "unknown face", target.normal
261-
return
262-
263-
# rectangle geometry, see http://stackoverflow.com/questions/19085369/rendering-custom-geometry-in-three-js
264-
geometry.faces.push(new this.game.THREE.Face3(0, 1, 2)) # counter-clockwise winding order
265-
geometry.faces.push(new this.game.THREE.Face3(0, 2, 3))
266-
267-
geometry.computeCentroids()
268-
geometry.computeFaceNormals()
269-
geometry.computeVertexNormals()
270-
geometry.faceVertexUvs = [
271-
[
272-
[
273-
{x:0, y:0},
274-
{x:1, y:0},
275-
{x:1, y:1},
276-
{x:0, y:1}
277-
],
278-
[
279-
{x:0, y:0},
280-
{x:1, y:1},
281-
{x:0, y:1},
282-
{x:0, y:1},
283-
],
284-
]
285-
]
286-
287-
material = new this.game.THREE.MeshLambertMaterial()
288-
289-
material.map = this.progressTextures[0]
290-
this.opts.applyTextureParams(material.map)
291-
292-
material.side = this.game.THREE.FrontSide
293-
material.transparent = true
294-
material.polygonOffset = true
295-
material.polygonOffsetFactor = -1.0
296-
material.polygonOffsetUnits = -1.0
297-
mesh = new this.game.THREE.Mesh(geometry, material)
298-
this.overlay = new this.game.THREE.Object3D()
299-
300-
this.overlay.add(mesh)
301-
this.overlay.position.set(target.voxel[0] + offset[0],
302-
target.voxel[1] + offset[1],
303-
target.voxel[2] + offset[2])
304-
305-
this.game.scene.add(this.overlay)
306-
307-
return this.overlay
308-
309-
# Set overlay texture based on mining progress stage
310-
Mine::updateForStage = (progress, hardness) ->
311-
if not this.texturesEnabled
312-
return
313-
314-
index = Math.floor((progress / hardness) * (this.progressTextures.length - 1))
315-
texture = this.progressTextures[index]
316-
317-
this.setOverlayTexture(texture)
318-
319-
Mine::setOverlayTexture = (texture) ->
320-
if not this.texturesEnabled or (not this.overlay and not this.decalPosition)
321-
return
322-
323-
if this.decals
324-
this.decals.change
325-
position: this.decalPosition
326-
normal: this.decalNormal
327-
texture: texture
328-
this.decals.update()
329-
else
330-
this.opts.applyTextureParams(texture)
331-
this.overlay.children[0].material.map = texture
332-
this.overlay.children[0].material.needsUpdate = true
333-
334-
Mine::destroyOverlay = () ->
335-
if not this.texturesEnabled or (not this.overlay and not this.decalPosition)
336-
return
337-
338-
if this.decals
339-
this.decals.remove(this.decalPosition) if this.decalPosition?
340-
this.decals.update()
341-
this.decalPosition = undefined
342-
else
343-
this.game.scene.remove(this.overlay)
344-
this.overlay = null
201+
202+
createOverlay(target) {
203+
if (this.instaMine || !this.texturesEnabled) {
204+
return;
205+
}
206+
207+
this.destroyOverlay();
208+
209+
if (this.decals) {
210+
this.decalPosition = target.voxel.slice(0);
211+
this.decalNormal = target.normal.slice(0);
212+
213+
this.decals.add({
214+
position: this.decalPosition,
215+
normal: this.decalNormal,
216+
texture: this.progressTextures[0]});
217+
218+
this.decals.update();
219+
} else {
220+
throw new Error('voxel-mine three.js support removed');
221+
}
222+
}
223+
224+
// Set overlay texture based on mining progress stage
225+
updateForStage(progress, hardness) {
226+
if (!this.texturesEnabled) {
227+
return;
228+
}
229+
230+
const index = Math.floor((progress / hardness) * (this.progressTextures.length - 1));
231+
const texture = this.progressTextures[index];
232+
233+
this.setOverlayTexture(texture);
234+
}
235+
236+
setOverlayTexture(texture) {
237+
if (!this.texturesEnabled || (!this.overlay && !this.decalPosition)) {
238+
return;
239+
}
240+
241+
if (this.decals) {
242+
this.decals.change({
243+
position: this.decalPosition,
244+
normal: this.decalNormal,
245+
texture: texture});
246+
this.decals.update();
247+
} else {
248+
this.opts.applyTextureParams(texture);
249+
this.overlay.children[0].material.map = texture;
250+
this.overlay.children[0].material.needsUpdate = true;
251+
}
252+
}
253+
254+
destroyOverlay() {
255+
if (!this.texturesEnabled || (!this.overlay && !this.decalPosition)) {
256+
return;
257+
}
258+
259+
if (this.decals) {
260+
if (this.decalPosition !== undefined) this.decals.remove(this.decalPosition);
261+
this.decals.update();
262+
this.decalPosition = undefined;
263+
} else {
264+
this.game.scene.remove(this.overlay);
265+
}
266+
267+
this.overlay = null;
268+
}
269+
}

0 commit comments

Comments
 (0)