Skip to content

Commit 6550930

Browse files
committed
Convert more to ES6, up to refresh textures
1 parent 8daf520 commit 6550930

File tree

1 file changed

+103
-80
lines changed

1 file changed

+103
-80
lines changed

mine.js

Lines changed: 103 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -70,111 +70,134 @@ class Mine extends EventEmitter {
7070
this.enable();
7171
}
7272

73-
// TODO
74-
Mine::timeToMine = (target) ->
75-
return this.opts.timeToMine(target) if this.opts.timeToMine? # custom callback
76-
77-
# if no registry, can't lookup per-block hardness, use same for all
78-
return 9 if not this.registry
79-
80-
# from registry, get the innate difficulty of mining this block
81-
blockID = game.getBlock(target.voxel)
82-
blockName = this.registry.getBlockName(blockID)
83-
hardness = this.registry.getProp(blockName, 'hardness') ? 1.0 # seconds
73+
timeToMine(target) {
74+
if (this.opts.timeToMine !== undefined) { // custom callback
75+
return this.opts.timeToMine(target);
76+
}
8477

85-
effectiveTool = this.registry.getProp(blockName, 'effectiveTool') ? 'pickaxe'
78+
// if no registry, can't lookup per-block hardness, use same for all
79+
if (!this.registry) return 9;
8680

87-
# if no held item concept, just use registry hardness
88-
return hardness if not this.hotbar
81+
// from registry, get the innate difficulty of mining this block
82+
const blockID = game.getBlock(target.voxel);
83+
const blockName = this.registry.getBlockName(blockID);
84+
let hardness = this.registry.getProp(blockName, 'hardness')
85+
if (hardness === undefined) hardness = 1.0; // seconds
8986

90-
# if hotbar is available - factor in effectiveness of currently held tool, shortens mining time
91-
heldItem = this.hotbar.held()
92-
toolClass = this.registry.getProp(heldItem?.item, 'toolClass')
87+
let effectiveTool = this.registry.getProp(blockName, 'effectiveTool');
88+
if (effectiveTool === undefined) effectiveTool = 'pickaxe';
9389

94-
speed = 1.0
90+
// if no held item concept, just use registry hardness
91+
if (!this.hotbar) return hardness;
9592

96-
if toolClass == effectiveTool
97-
# this tool is effective against this block, so it mines faster
98-
speed = this.registry.getProp(heldItem?.item, 'speed') ? 1.0
99-
# TODO: if wrong tool, deal double damage?
93+
// if hotbar is available - factor in effectiveness of currently held tool, shortens mining time
94+
const heldItem = this.hotbar.held();
95+
const toolClass = this.registry.getProp(heldItem !== undefined ? heldItem.item : heldItem, 'toolClass');
10096

97+
let speed = 1.0;
10198

102-
finalTimeToMine = Math.max(hardness / speed, 0)
103-
# TODO: more complex mining 'classes', e.g. shovel against dirt, axe against wood
99+
if (toolClass === effectiveTool) {
100+
// this tool is effective against this block, so it mines faster
101+
speed = this.registry.getProp(heldItem !== undefined ? heldItem.item : heldItem, 'speed');
102+
if (speed === undefined) speed = 1.0;
103+
// TODO: if wrong tool, deal double damage?
104+
}
104105

105-
return finalTimeToMine
106+
const finalTimeToMine = Math.max(hardness / speed, 0);
107+
// TODO: more complex mining 'classes', e.g. shovel against dirt, axe against wood
106108

107-
Mine::enable = ->
108-
this.reach.on 'mining', this.onMining = (target) =>
109-
if not target
110-
console.log("no block mined")
111-
return
109+
return finalTimeToMine;
110+
}
112111

113-
this.progress += 1 # incremented each fire (this.secondsPerFire)
114-
progressSeconds = this.progress * this.secondsPerFire # how long they've been mining
112+
enable() {
113+
this.reach.on('mining', this.onMining = (target) => {
114+
if (!target) {
115+
console.log('no block mined');
116+
return;
117+
}
115118

116-
hardness = this.timeToMine(target)
117-
if this.instaMine || progressSeconds >= hardness
118-
this.progress = 0
119-
this.reach.emit 'stop mining', target
120-
this.emit 'break', target
119+
this.progress += 1; // incremented each fire (this.secondsPerFire)
120+
const progressSeconds = this.progress * this.secondsPerFire; // how long they've been mining
121121

122-
this.updateForStage(progressSeconds, hardness)
122+
const hardness = this.timeToMine(target);
123+
if (this.instaMine || progressSeconds >= hardness) {
124+
this.progress = 0;
125+
this.reach.emit('stop mining', target);
126+
this.emit('break', target);
127+
}
123128

124-
this.reach.on 'start mining', this.onStartMining = (target) =>
125-
if not target
126-
return
129+
this.updateForStage(progressSeconds, hardness);
130+
});
127131

128-
this.createOverlay(target)
132+
this.reach.on('start mining', this.onStartMining = (target) => {
133+
if (!target) {
134+
return;
135+
}
129136

130-
this.reach.on 'stop mining', this.onStopMining = (target) =>
131-
if not target
132-
return
137+
this.createOverlay(target);
138+
});
133139

134-
# Reset this.progress if mouse released
135-
this.destroyOverlay()
136-
this.progress = 0
140+
this.reach.on('stop mining', this.onStopMining = (target) => {
141+
if (!target) {
142+
return;
143+
}
137144

138-
Mine::disable = ->
139-
this.reach.removeListener 'mining', this.onMining
140-
this.reach.removeListener 'start mining', this.onStartMining
141-
this.reach.removeListener 'stop mining', this.onStopMining
145+
// Reset this.progress if mouse released
146+
this.destroyOverlay();
147+
this.progress = 0;
148+
});
142149

143-
Mine::setupTextures = ->
144-
if not this.texturesEnabled
145-
return
150+
disable() {
151+
this.reach.removeListener('mining', this.onMining);
152+
this.reach.removeListener('start mining', this.onStartMining);
153+
this.reach.removeListener('stop mining', this.onStopMining);
154+
}
146155

147-
this.progressTextures = [] # TODO: placeholders until loaded?
156+
setupTextures() {
157+
if (!this.texturesEnabled) {
158+
return;
159+
}
148160

149-
this.registry.onTexturesReady () => this.refreshTextures()
150-
if this.game.materials?.artPacks?
151-
this.game.materials.artPacks.on 'refresh', () => this.refreshTextures()
161+
this.progressTextures = []; // TODO: placeholders until loaded?
152162

153-
if this.decals
154-
# add to atlas
155-
for i in [0..this.opts.progressTexturesCount]
156-
name = this.opts.progressTexturesPrefix + i
163+
this.registry.onTexturesReady(() => this.refreshTextures());
164+
if (this.game.materials.artPacks) {
165+
this.game.materials.artPacks.on('refresh', () => this.refreshTextures());
166+
}
157167

158-
this.stitch.preloadTexture name
168+
if (this.decals) {
169+
// add to atlas
170+
for (let i = 0; i < this.opts.progressTexturesCount; ++i) {
171+
const name = this.opts.progressTexturesPrefix + i;
159172

160-
this.progressTextures.push name
173+
this.stitch.preloadTexture(name);
161174

162-
Mine::refreshTextures = ->
163-
if this.decals
164-
#
165-
else
166-
this.progressTextures = []
167-
for i in [0..this.opts.progressTexturesCount]
168-
path = this.registry.getTextureURL this.opts.progressTexturesPrefix + i
169-
if not path?
170-
# fallback to default texture if missing
171-
if this.defaultTextureURL.indexOf('data:') == 0
172-
# for some reason, data: URLs are not allowed with crossOrigin, see https://github.com/mrdoob/three.js/issues/687
173-
# warning: this might break other stuff
174-
delete this.game.THREE.ImageUtils.crossOrigin
175-
path = this.defaultTextureURL
176-
this.progressTextures.push(this.game.THREE.ImageUtils.loadTexture(path))
175+
this.progressTextures.push(name);
176+
}
177+
}
178+
}
177179

180+
refreshTextures() {
181+
if (this.decals) {
182+
183+
} else {
184+
this.progressTextures = [];
185+
for (let i = 0; i < this.opts.progressTexturesCount; ++i) {
186+
let path = this.registry.getTextureURL(this.opts.progressTexturesPrefix + i);
187+
if (path === undefined) {
188+
// fallback to default texture if missing
189+
if (this.defaultTextureURL.indexOf('data:') === 0) {
190+
// for some reason, data: URLs are not allowed with crossOrigin, see https://github.com/mrdoob/three.js/issues/687
191+
// warning: this might break other stuff
192+
delete this.game.THREE.ImageUtils.crossOrigin;
193+
}
194+
path = this.defaultTextureURL;
195+
}
196+
this.progressTextures.push(this.game.THREE.ImageUtils.loadTexture(path));
197+
}
198+
}
199+
}
200+
//TODO
178201
Mine::createOverlay = (target) ->
179202
if this.instaMine or not this.texturesEnabled
180203
return

0 commit comments

Comments
 (0)