Skip to content

Commit dd3a929

Browse files
committed
Better importing and exporting with game parameter
1 parent f096b98 commit dd3a929

18 files changed

+244
-284
lines changed

coffee/client/module/AssetLoader.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class AssetLoader
88
$.get "assets/assetLoader.json", (assets)->
99
_this.load assets,()->
1010
console.log "AssetLoader: done loading!"
11-
init _this
11+
if init isnt null
12+
init _this
1213
return
1314
return
1415
return

coffee/client/module/BlockBreak.coffee

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as THREE from './build/three.module.js'
22

33
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"
4+
constructor:(game)->
5+
console.log game
6+
@game=game
7+
@texture=@game.al.get "blocksAtlasSnap"
108
@texture.magFilter=THREE.NearestFilter
119
@cursor=new THREE.Mesh(
1210
new THREE.BoxBufferGeometry(1.001, 1.001, 1.001)
@@ -20,7 +18,7 @@ class BlockBreak
2018
new THREE.EdgesGeometry( @cursor.geometry )
2119
new THREE.LineBasicMaterial( { color: 0x000000 } )
2220
)
23-
@scene.add @cursor, @cursorOut
21+
@game.scene.add @cursor, @cursorOut
2422
@uv={}
2523
@isDigging=false
2624
@done=true
@@ -58,7 +56,7 @@ class BlockBreak
5856
@cursor.geometry.attributes.uv.array[i]=1-q*toxY
5957
@cursor.geometry.attributes.uv.needsUpdate = true
6058
updatePos:(cb)->
61-
rayBlock=@world.getRayBlock()
59+
rayBlock=@game.world.getRayBlock()
6260
if JSON.stringify(@lastPos) != JSON.stringify(rayBlock)
6361
@lastPos=rayBlock
6462
cb()
@@ -74,11 +72,11 @@ class BlockBreak
7472
digRequest:()->
7573
console.log "REQUESTING DIGGING..."
7674
_this=@
77-
pos=@world.getRayBlock().posBreak
75+
pos=@game.world.getRayBlock().posBreak
7876
if pos isnt undefined
79-
block=@world.cellTerrain.getBlock pos...
77+
block=@game.world.cellTerrain.getBlock pos...
8078
if block.diggable
81-
@socket.emit "dig", pos
79+
@game.socket.emit "dig", pos
8280
@done=false
8381
return
8482
startDigging:(time)->
@@ -101,7 +99,7 @@ class BlockBreak
10199
@done=true
102100
@isDigging=false
103101
console.log "Digging Stopped!"
104-
@socket.emit "stopDigging",(xd)->
102+
@game.socket.emit "stopDigging",(xd)->
105103
callback(xd)
106104
@setState 0
107105
clearInterval @int

coffee/client/module/Chat.coffee

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
class Chat
2-
constructor:(options)->
3-
_this=@
4-
@FPC=options.FPC
5-
@chatDiv = document.querySelector(".chat")
6-
@listen()
7-
return
8-
listen:()->
9-
_this=@
10-
window.addEventListener "wheel", (e)->
11-
if _this.FPC.gameState isnt "chat"
12-
e.preventDefault()
13-
return
14-
, {passive: false}
15-
return @
16-
isElementScrolledToBottom:(el)->
17-
if el.scrollTop >= (el.scrollHeight - el.offsetHeight)
18-
return true
19-
return false
20-
scrollToBottom:(el)->
21-
el.scrollTop = el.scrollHeight
22-
return
23-
log:(message)->
24-
atBottom = @isElementScrolledToBottom(@chatDiv)
25-
$(".chat").append(message+"<br>")
26-
if atBottom
27-
@scrollToBottom(@chatDiv)
28-
return
2+
constructor:(game)->
3+
@game=game
4+
@chatDiv = document.querySelector(".chat")
5+
@listen()
6+
@history=[]
7+
return
8+
listen:()->
9+
_this=@
10+
window.addEventListener "wheel", (e)->
11+
if _this.game.FPC.gameState isnt "chat"
12+
e.preventDefault()
13+
return
14+
, {passive: false}
15+
return @
16+
isElementScrolledToBottom:(el)->
17+
if el.scrollTop >= (el.scrollHeight - el.offsetHeight)
18+
return true
19+
return false
20+
scrollToBottom:(el)->
21+
el.scrollTop = el.scrollHeight
22+
return
23+
log:(message)->
24+
atBottom = @isElementScrolledToBottom(@chatDiv)
25+
$(".chat").append(message+"<br>")
26+
if atBottom
27+
@scrollToBottom(@chatDiv)
28+
return
29+
command:(com)->
30+
if com isnt ""
31+
@history.push com
32+
@game.socket.emit "command",com
2933
export {Chat}

coffee/client/module/Entities.coffee

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import * as THREE from './build/three.module.js'
22
class Entities
3-
constructor:(options)->
4-
@scene=options.scene
5-
@nick=options.nick
6-
@TWEEN=options.TWEEN
3+
constructor:(game)->
4+
@game=game
75
@mobMaterial = new THREE.MeshBasicMaterial {color: new THREE.Color "red" }
86
@mobGeometry = new THREE.BoxGeometry 1, 1, 1
97
@mobMaxCount=200
108
@mobMesh=new THREE.InstancedMesh @mobGeometry,@mobMaterial,@mobMaxCount
119
@mobMesh.instanceMatrix.setUsage THREE.DynamicDrawUsage
12-
@scene.add @mobMesh
10+
@game.scene.add @mobMesh
1311
@dummy = new THREE.Object3D()
1412
return
1513
update:(entities)->

coffee/client/module/FirstPersonControls.coffee

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as THREE from './build/three.module.js'
22

33
class FirstPersonControls
4-
constructor: (options)->
4+
constructor: (game)->
5+
@game=game
56
@kc={
67
87:"forward"
78
65:"right"
@@ -11,25 +12,19 @@ class FirstPersonControls
1112
16:"sneak"
1213
82:"sprint"
1314
}
14-
@pii=options.pii
15-
@fov=options.fov
1615
@keys={}
17-
@canvas=options.canvas
18-
@camera=options.camera
19-
@socket=options.socket
20-
@TWEEN=options.TWEEN
2116
@setState "menu"
2217
@listen()
2318
updatePosition: (e)->
2419
#Updatowanie kursora
2520
if @gameState is "gameLock"
26-
@camera.rotation.x -= THREE.MathUtils.degToRad e.movementY / 10
27-
@camera.rotation.y -= THREE.MathUtils.degToRad e.movementX / 10
28-
if THREE.MathUtils.radToDeg( @camera.rotation.x ) < -90
29-
@camera.rotation.x = THREE.MathUtils.degToRad -90
30-
if THREE.MathUtils.radToDeg( @camera.rotation.x ) > 90
31-
@camera.rotation.x = THREE.MathUtils.degToRad 90
32-
@socket.emit "rotate", [@camera.rotation.y,@camera.rotation.x]
21+
@game.camera.rotation.x -= THREE.MathUtils.degToRad e.movementY / 10
22+
@game.camera.rotation.y -= THREE.MathUtils.degToRad e.movementX / 10
23+
if THREE.MathUtils.radToDeg( @game.camera.rotation.x ) < -90
24+
@game.camera.rotation.x = THREE.MathUtils.degToRad -90
25+
if THREE.MathUtils.radToDeg( @game.camera.rotation.x ) > 90
26+
@game.camera.rotation.x = THREE.MathUtils.degToRad 90
27+
@game.socket.emit "rotate", [@game.camera.rotation.y,@game.camera.rotation.x]
3328
return
3429
listen: ->
3530
_this=@
@@ -43,7 +38,7 @@ class FirstPersonControls
4338

4439
#Klawisz Enter
4540
if z.keyCode is 13 and _this.gameState is "chat"
46-
_this.socket.emit "command",$(".com_i").val()
41+
_this.game.chat.command $(".com_i").val()
4742
$(".com_i").val("")
4843

4944
#Klawisz E
@@ -69,14 +64,14 @@ class FirstPersonControls
6964

7065
#Wysyłanie state'u do serwera
7166
if _this.kc[z.keyCode] isnt undefined and _this.gameState is "gameLock"
72-
_this.socket.emit "move",_this.kc[z.keyCode],true
67+
_this.game.socket.emit "move",_this.kc[z.keyCode],true
7368
if _this.kc[z.keyCode] is "sprint"
74-
to={fov:_this.fov+10}
75-
new _this.TWEEN.Tween _this.camera
69+
to={fov:_this.game.fov+10}
70+
new _this.game.TWEEN.Tween _this.game.camera
7671
.to to, 200
77-
.easing _this.TWEEN.Easing.Quadratic.Out
72+
.easing _this.game.TWEEN.Easing.Quadratic.Out
7873
.onUpdate ()->
79-
_this.camera.updateProjectionMatrix()
74+
_this.game.camera.updateProjectionMatrix()
8075
.start()
8176
return
8277
$(document).keyup (z) ->
@@ -85,22 +80,22 @@ class FirstPersonControls
8580

8681
#Wysyłanie state'u do serwera
8782
if _this.kc[z.keyCode] isnt undefined
88-
_this.socket.emit "move",_this.kc[z.keyCode],false
83+
_this.game.socket.emit "move",_this.kc[z.keyCode],false
8984
if _this.kc[z.keyCode] is "sprint"
90-
to={fov:_this.fov}
91-
new _this.TWEEN.Tween _this.camera
85+
to={fov:_this.game.fov}
86+
new _this.game.TWEEN.Tween _this.game.camera
9287
.to to, 200
93-
.easing _this.TWEEN.Easing.Quadratic.Out
88+
.easing _this.game.TWEEN.Easing.Quadratic.Out
9489
.onUpdate ()->
95-
_this.camera.updateProjectionMatrix()
90+
_this.game.camera.updateProjectionMatrix()
9691
.start()
9792

9893
return
9994
$(".gameOn").click ->
10095
_this.setState "game"
10196
return
10297
lockChangeAlert=()->
103-
if document.pointerLockElement is _this.canvas or document.mozPointerLockElement is _this.canvas
98+
if document.pointerLockElement is _this.game.canvas or document.mozPointerLockElement is _this.game.canvas
10499
#Lock
105100
if _this.gameState is "game"
106101
_this.setState "gameLock"
@@ -116,16 +111,16 @@ class FirstPersonControls
116111
, false
117112
return @
118113
reqLock:()->
119-
@canvas.requestPointerLock()
114+
@game.canvas.requestPointerLock()
120115
unLock:()->
121116
document.exitPointerLock = document.exitPointerLock or document.mozExitPointerLock
122117
document.exitPointerLock()
123118
state:(state)->
124119
@gameState=state
125120
if state is "inventory"
126-
@pii.show()
121+
@game.pii.show()
127122
else
128-
@pii.hide()
123+
@game.pii.hide()
129124
# console.log "Game state: "+state
130125
resetState:()->
131126
$(".chat").removeClass("focus")

coffee/client/module/PlayerInInventory.coffee

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import * as THREE from './build/three.module.js'
22
class PlayerInInventory
3-
constructor:(options)->
4-
@canvas=options.canvas
5-
@al=options.al
3+
constructor:(game)->
4+
@game=game
65
@renderer=new THREE.WebGLRenderer {
7-
canvas:@canvas
6+
canvas:@game.pcanvas
87
PixelRatio:window.devicePixelRatio
98
}
109
@scene=new THREE.Scene
1110
@scene.background = new THREE.Color "black"
1211
light = new THREE.AmbientLight( 0xffffff )
1312
@scene.add( light );
1413

15-
player=@al.get "player"
16-
playerTex=@al.get "playerTex"
14+
player=@game.al.get "player"
15+
playerTex=@game.al.get "playerTex"
1716

1817
playerTex.magFilter=THREE.NearestFilter;
1918
player.children[0].material.map=playerTex;
@@ -42,7 +41,7 @@ class PlayerInInventory
4241
render:()->
4342
@renderer.render @scene, @camera
4443
show:()->
45-
@canvas.style.display="block"
44+
@game.pcanvas.style.display="block"
4645
hide:()->
47-
@canvas.style.display="none"
46+
@game.pcanvas.style.display="none"
4847
export {PlayerInInventory}

coffee/client/module/World/World.coffee

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,18 @@ import {CellTerrain} from './CellTerrain.js'
33
import {AnimatedTextureAtlas} from './AnimatedTextureAtlas.js'
44

55
class World
6-
constructor: (options) ->
6+
constructor: (game) ->
77
_this=@
8+
@game=game
89
@cellBlackList={}
910
@cellMesh={}
1011
@cellNeedsUpdate={}
1112
@models={}
12-
@cellSize=options.cellSize
13-
@camera=options.camera
14-
@scene=options.scene
15-
@toxelSize=options.toxelSize
16-
@al=options.al
17-
@cellTerrain=new CellTerrain {cellSize:@cellSize}
18-
@ATA=new AnimatedTextureAtlas {al:@al}
13+
@cellTerrain=new CellTerrain {cellSize:@game.cellSize}
14+
@ATA=new AnimatedTextureAtlas {al:@game.al}
1915
@material=@ATA.material
2016
@cellUpdateTime=null
2117
@renderTime=100
22-
@renderer=options.renderer
2318
@neighbours=[[-1, 0, 0],[1, 0, 0],[0, -1, 0],[0, 1, 0],[0, 0, -1],[0, 0, 1]]
2419

2520
#Utworzenie Workera do obliczania geometrii chunków
@@ -29,10 +24,10 @@ class World
2924
@chunkWorker.postMessage {
3025
type:'init'
3126
data:{
32-
blocksMapping: @al.get "blocksMapping"
33-
toxelSize: @toxelSize
34-
cellSize: @cellSize
35-
blocksTex: @al.get "blocksTex"
27+
blocksMapping: @game.al.get "blocksMapping"
28+
toxelSize: @game.toxelSize
29+
cellSize: @game.cellSize
30+
blocksTex: @game.al.get "blocksTex"
3631
}
3732
}
3833

@@ -100,9 +95,9 @@ class World
10095
if cellBlackList[i] is true
10196
@cellMesh[i].geometry.dispose()
10297
# @cellMesh[i].material.dispose()
103-
@scene.remove @cellMesh[i]
98+
@game.scene.remove @cellMesh[i]
10499
@cellMesh[i]="disposed"
105-
@renderer.renderLists.dispose()
100+
@game.renderer.renderLists.dispose()
106101
updateCell: (data)->
107102
#Updatowanie komórki z już obliczoną geometrią
108103
cellId=@cellTerrain.vec3 data.info...
@@ -113,14 +108,16 @@ class World
113108
geometry.setAttribute 'normal',new THREE.BufferAttribute(new Float32Array(cell.normals), 3)
114109
geometry.setAttribute 'uv',new THREE.BufferAttribute(new Float32Array(cell.uvs), 2)
115110
geometry.setAttribute 'color',new THREE.BufferAttribute(new Float32Array(cell.colors), 3)
111+
geometry.matrixAutoUpdate=false
116112
if mesh is undefined or mesh is "disposedX"
117113
@cellMesh[cellId]=new THREE.Mesh geometry,@material
114+
@cellMesh[cellId].matrixAutoUpdate=false
118115
@cellMesh[cellId].frustumCulled = false
119116
_this=@
120117
@cellMesh[cellId].onAfterRender = ()->
121118
_this.cellMesh[cellId].frustumCulled = true
122119
_this.cellMesh[cellId].onAfterRender = ->
123-
@scene.add @cellMesh[cellId]
120+
@game.scene.add @cellMesh[cellId]
124121
else if mesh isnt "disposed"
125122
@cellMesh[cellId].geometry=geometry
126123
return
@@ -200,8 +197,8 @@ class World
200197
steppedIndex = 2
201198
return null
202199
getRayBlock: ->
203-
start = new THREE.Vector3().setFromMatrixPosition(@camera.matrixWorld)
204-
end = new THREE.Vector3().set(0,0, 1).unproject(@camera)
200+
start = new THREE.Vector3().setFromMatrixPosition(@game.camera.matrixWorld)
201+
end = new THREE.Vector3().set(0,0, 1).unproject(@game.camera)
205202
intersection = @intersectsRay start, end
206203
if intersection
207204
posPlace = intersection.position.map (v, ndx) ->

0 commit comments

Comments
 (0)