Skip to content

Commit f096b98

Browse files
committed
Block Destroying prototype
1 parent 0370504 commit f096b98

File tree

9 files changed

+365
-41
lines changed

9 files changed

+365
-41
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import * as THREE from './build/three.module.js'
2+
3+
class BlockBreak
4+
constructor:(options)->
5+
@scene=options.scene
6+
@al=options.al
7+
@world=options.world
8+
@socket=options.socket
9+
@texture=@al.get "blocksAtlasSnap"
10+
@texture.magFilter=THREE.NearestFilter
11+
@cursor=new THREE.Mesh(
12+
new THREE.BoxBufferGeometry(1.001, 1.001, 1.001)
13+
new THREE.MeshBasicMaterial({
14+
map: @texture
15+
transparent:true
16+
})
17+
)
18+
@lastPos=[]
19+
@cursorOut=new THREE.LineSegments(
20+
new THREE.EdgesGeometry( @cursor.geometry )
21+
new THREE.LineBasicMaterial( { color: 0x000000 } )
22+
)
23+
@scene.add @cursor, @cursorOut
24+
@uv={}
25+
@isDigging=false
26+
@done=true
27+
@setState 0
28+
setState:(state)->
29+
#od 0 do 9
30+
if state is 0
31+
@cursor.material.visible=false
32+
else
33+
@cursor.material.visible=true
34+
toxX=6+state
35+
toxY=8
36+
q=1/27
37+
for i in [0..@cursor.geometry.attributes.uv.array.length]
38+
if @uv[i] is undefined
39+
if i%2 is 0
40+
if @cursor.geometry.attributes.uv.array[i] is 0
41+
@uv[i]=0
42+
else
43+
@uv[i]=1
44+
else
45+
if @cursor.geometry.attributes.uv.array[i] is 0
46+
@uv[i]=0
47+
else
48+
@uv[i]=1
49+
if i%2 is 0
50+
if @uv[i] is 0
51+
@cursor.geometry.attributes.uv.array[i]=q*toxX
52+
else
53+
@cursor.geometry.attributes.uv.array[i]=q*toxX+q
54+
else
55+
if @uv[i] is 0
56+
@cursor.geometry.attributes.uv.array[i]=1-q*toxY-q
57+
else
58+
@cursor.geometry.attributes.uv.array[i]=1-q*toxY
59+
@cursor.geometry.attributes.uv.needsUpdate = true
60+
updatePos:(cb)->
61+
rayBlock=@world.getRayBlock()
62+
if JSON.stringify(@lastPos) != JSON.stringify(rayBlock)
63+
@lastPos=rayBlock
64+
cb()
65+
if rayBlock
66+
pos=rayBlock.posBreak
67+
@cursor.position.set pos...
68+
@cursor.visible=true
69+
@cursorOut.position.set pos...
70+
@cursorOut.visible=true
71+
else
72+
@cursor.visible=false
73+
@cursorOut.visible=false
74+
digRequest:()->
75+
console.log "REQUESTING DIGGING..."
76+
_this=@
77+
pos=@world.getRayBlock().posBreak
78+
if pos isnt undefined
79+
block=@world.cellTerrain.getBlock pos...
80+
if block.diggable
81+
@socket.emit "dig", pos
82+
@done=false
83+
return
84+
startDigging:(time)->
85+
_this=@
86+
ile=0
87+
if @isDigging is false
88+
@isDigging=true
89+
@int=setInterval ()->
90+
if ile is 11
91+
_this.setState 0
92+
clearInterval _this.int
93+
_this.isDigging=false
94+
else
95+
_this.setState ile
96+
ile++
97+
return
98+
,time/10
99+
return
100+
stopDigging:(callback)->
101+
@done=true
102+
@isDigging=false
103+
console.log "Digging Stopped!"
104+
@socket.emit "stopDigging",(xd)->
105+
callback(xd)
106+
@setState 0
107+
clearInterval @int
108+
return
109+
export {BlockBreak}

coffee/client/module/World/AnimatedTextureAtlas.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ class AnimatedTextureAtlas
118118
return
119119
,100)
120120

121-
export {AnimatedTextureAtlas}
121+
export {AnimatedTextureAtlas,TextureAtlasCreator}

coffee/client/module/index.coffee

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {GUI} from './jsm/libs/dat.gui.module.js'
1212
import {Chat} from './Chat.js'
1313
import {Entities} from './Entities.js'
1414
import {PlayerInInventory} from './PlayerInInventory.js'
15+
import {BlockBreak} from './BlockBreak.js'
1516

1617
class Game
1718
constructor:(options)->
@@ -131,20 +132,25 @@ class Game
131132
return
132133
"entities":(entities)->
133134
_this.ent.update entities
135+
return
136+
"diggingCompleted":(block)->
137+
_this.bb.done=true
138+
return
139+
"diggingAborted":(block)->
140+
return
141+
"digTime":(time,block)->
142+
_this.bb.startDigging time
143+
return
134144
}
135145
for i of eventMap
136146
@socket.on i,eventMap[i]
137147

138-
@cursor=new THREE.LineSegments(
139-
new THREE.EdgesGeometry(
140-
new THREE.BoxGeometry 1, 1, 1
141-
),
142-
new THREE.LineBasicMaterial {
143-
color: 0x000000,
144-
linewidth: 0.5
145-
}
146-
)
147-
@scene.add @cursor
148+
@bb=new BlockBreak {
149+
scene:@scene
150+
al:@al
151+
world:@world
152+
socket:@socket
153+
}
148154
gui = new GUI()
149155
@params={
150156
fog:false
@@ -161,10 +167,15 @@ class Game
161167
_this.scene.fog = null
162168
gui.add( @world.material, 'wireframe' ).name( 'Wireframe' ).listen()
163169
gui.add( @params, 'chunkdist',0,10,1).name( 'Render distance' ).listen()
170+
@mouse=false
164171
$(document).mousedown (e)->
172+
_this.mouse=true
165173
if _this.FPC.gameState is "gameLock"
166-
console.log _this.world.cellTerrain.getBlock _this.world.getRayBlock().posBreak...
174+
_this.bb.digRequest()
167175
return
176+
$(document).mouseup (e)->
177+
_this.mouse=false
178+
_this.bb.stopDigging()
168179
@animate()
169180
animate:()->
170181
_this=@
@@ -176,6 +187,7 @@ class Game
176187
_this.animate()
177188
return
178189
render:()->
190+
_this=@
179191
width=window.innerWidth
180192
height=window.innerHeight
181193
if @canvas.width isnt width or @canvas.height isnt height
@@ -185,13 +197,11 @@ class Game
185197
@camera.aspect = width / height
186198
@camera.updateProjectionMatrix()
187199

188-
rayBlock=@world.getRayBlock()
189-
if rayBlock
190-
pos=rayBlock.posBreak
191-
@cursor.position.set pos...
192-
@cursor.visible=true
193-
else
194-
@cursor.visible=false
200+
@bb.updatePos ()->
201+
if _this.bb.isDigging
202+
_this.bb.stopDigging()
203+
if _this.mouse and _this.bb.done
204+
_this.bb.digRequest()
195205

196206
@world.updateCellsAroundPlayer @camera.position,@params.chunkdist
197207

coffee/server.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ module.exports=(type)->
8686
"blockUpdate":(oldb,newb)->
8787
emit ["blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]]
8888
return
89+
"diggingCompleted":(block)->
90+
emit ["diggingCompleted",block]
91+
return
92+
"diggingAborted":(block)->
93+
emit ["diggingAborted",block]
94+
return
8995
}
9096
for i of botEventMap
9197
((i)->
@@ -120,6 +126,21 @@ module.exports=(type)->
120126
socketInfo[socket.id].bot.end()
121127
delete socketInfo[socket.id]
122128
return
129+
"dig":(pos)->
130+
block=bot().blockAt(vec3(pos[0],pos[1]-16,pos[2]))
131+
if block isnt null
132+
digTime=bot().digTime(block)
133+
if bot().targetDigBlock isnt null
134+
console.log "Already digging..."
135+
bot().stopDigging()
136+
emit ["digTime",digTime,block]
137+
bot().dig block,false,()->
138+
return
139+
"stopDigging":(callback)->
140+
bot().stopDigging()
141+
return
123142
}
124143
for i of socketEventMap
125144
socket.on i,socketEventMap[i]
145+
return
146+
return

src/client/assets/assetLoader.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@
2626
"playerTex":{
2727
"path":"assets/models/player.png",
2828
"type":"texture"
29+
},
30+
"blocksAtlasSnap":{
31+
"path":"assets/blocks/blocksAtlas-snap.png",
32+
"type":"texture"
2933
}
3034
}

src/client/module/BlockBreak.js

Lines changed: 140 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/module/World/AnimatedTextureAtlas.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)