Skip to content

Commit 7f1c1e3

Browse files
committed
FirstPersonControls real player
1 parent a99d466 commit 7f1c1e3

File tree

11 files changed

+905
-101
lines changed

11 files changed

+905
-101
lines changed

coffee/client/module/FirstPersonControls.coffee

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,19 @@ import * as THREE from './build/three.module.js'
33
class FirstPersonControls
44
constructor: (options)->
55
@kc={
6-
"w": 87,
7-
"s": 83,
8-
"a": 65,
9-
"d": 68,
10-
"space": 32,
11-
"shift": 16
6+
87:"forward"
7+
65:"right"
8+
83:"back"
9+
68:"left"
10+
32:"jump"
1211
}
1312
@keys={}
1413
@canvas=options.canvas
1514
@camera=options.camera
1615
@micromove=options.micromove
16+
@socket=options.socket
1717
@gameState="menu"
1818
@listen()
19-
ac: (qx, qy, qa, qf)->
20-
m_x = -Math.sin(qa) * qf;
21-
m_y = -Math.cos(qa) * qf;
22-
r_x = qx - m_x;
23-
r_y = qy - m_y;
24-
return {
25-
x: r_x,
26-
y: r_y
27-
}
28-
camMicroMove: ->
29-
if @keys[@kc["w"]]
30-
@camera.position.x = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y + THREE.MathUtils.degToRad(180), @micromove).x
31-
@camera.position.z = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y + THREE.MathUtils.degToRad(180), @micromove).y
32-
if @keys[@kc["s"]]
33-
@camera.position.x = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y, @micromove).x
34-
@camera.position.z = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y, @micromove).y
35-
if @keys[@kc["a"]]
36-
@camera.position.x = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y - THREE.MathUtils.degToRad(90), @micromove).x
37-
@camera.position.z = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y - THREE.MathUtils.degToRad(90), @micromove).y
38-
if @keys[@kc["d"]]
39-
@camera.position.x = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y + THREE.MathUtils.degToRad(90), @micromove).x
40-
@camera.position.z = @ac(@camera.position.x, @camera.position.z, @camera.rotation.y + THREE.MathUtils.degToRad(90), @micromove).y
41-
if @keys[@kc["space"]]
42-
@camera.position.y += @micromove
43-
if @keys[@kc["shift"]]
44-
@camera.position.y -= @micromove
4519
updatePosition: (e)->
4620
if @gameState is "game"
4721
@camera.rotation.x -= THREE.MathUtils.degToRad e.movementY / 10
@@ -50,6 +24,7 @@ class FirstPersonControls
5024
@camera.rotation.x = THREE.MathUtils.degToRad -90
5125
if THREE.MathUtils.radToDeg( @camera.rotation.x ) > 90
5226
@camera.rotation.x = THREE.MathUtils.degToRad 90
27+
@socket.emit "playerRotate", [@camera.rotation.y,@camera.rotation.x]
5328
return
5429
listen: ->
5530
_this=@
@@ -62,8 +37,12 @@ class FirstPersonControls
6237
else
6338
document.exitPointerLock = document.exitPointerLock or document.mozExitPointerLock;
6439
document.exitPointerLock();
40+
if _this.kc[z.keyCode] isnt undefined
41+
_this.socket.emit "move",_this.kc[z.keyCode],true
6542
return
6643
$(document).keyup (z) ->
44+
if _this.kc[z.keyCode] isnt undefined
45+
_this.socket.emit "move",_this.kc[z.keyCode],false
6746
delete _this.keys[z.keyCode]
6847
return
6948
$(".gameOn").click ->

coffee/client/module/World/ChunkWorker.coffee

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,11 @@ handlers={
155155
terrain.setVoxel data...
156156
genCellGeo: (data)->
157157
if ((terrain.cellTerrain.vec3 data...) of terrain.cellTerrain.cells) is true
158-
t0 = performance.now()
159158
geo=terrain.genCellGeo data...
160-
t1 = performance.now()
161-
time+=(t1 - t0)
162-
console.log(time)
163159
postMessage {
164160
cell:geo
165161
info:data
166162
}
167163
setCell: (data)->
168-
console.log("Otrzymano komórkę")
169164
terrain.cellTerrain.cells["#{data[0]}:#{data[1]}:#{data[2]}"]=data[3]
170165
}

coffee/client/module/index.coffee

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ init = ()->
2020
PixelRatio:window.devicePixelRatio
2121
}
2222
scene=new THREE.Scene
23-
camera = new THREE.PerspectiveCamera 70, 2, 0.1, 1000
23+
camera = new THREE.PerspectiveCamera 90, 2, 0.1, 64
2424
camera.rotation.order = "YXZ"
2525
camera.position.set 26, 26, 26
2626
color = new THREE.Color("#adc8ff")
27-
near = 64
28-
far = 128
27+
near = 32
28+
far = 64
2929
scene.fog = new THREE.Fog(color, near, far)
3030
#skybox
3131
loader = new THREE.TextureLoader();
@@ -78,6 +78,12 @@ init = ()->
7878
return
7979
socket.on "mapChunk", (sections,x,z)->
8080
world._computeSections sections,x,z
81+
socket.on "botPosition", (pos)->
82+
to={x:pos.x-0.5,y:pos.y+17,z:pos.z-0.5}
83+
new TWEEN.Tween camera.position
84+
.to to, 40
85+
.easing TWEEN.Easing.Quadratic.Out
86+
.start()
8187
players=new Players {socket,scene,al}
8288
socket.on "playerUpdate",(data)->
8389
players.update data
@@ -113,6 +119,7 @@ init = ()->
113119
canvas
114120
camera
115121
micromove: 0.3
122+
socket
116123
}
117124

118125
#Raycast cursor
@@ -166,7 +173,6 @@ render = ->
166173
xyaw:-camera.rotation.x
167174
zyaw:camera.rotation.y+Math.PI
168175
}
169-
FPC.camMicroMove()
170176

171177
#Update cursor
172178
rayBlock=world.getRayBlock()
@@ -182,8 +188,10 @@ render = ->
182188

183189
#Rendering
184190
world.updateCells()
191+
TWEEN.update();
185192
renderer.render scene, camera
186193
return
194+
187195
animate = ->
188196
try
189197
stats.begin()

coffee/server.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,18 @@ module.exports=(config)->
6464
return
6565
socketInfo[socket.id].bot.chat message
6666
return
67+
socketInfo[socket.id].bot.on 'move',()->
68+
try
69+
console.log socketInfo[socket.id].bot.entity.position
70+
io.to(socket.id).emit "botPosition",socketInfo[socket.id].bot.entity.position
71+
return
6772
#first world load
6873
io.to(socket.id).emit "firstLoad",world
6974
return
75+
socket.on "move",(state,toggle)->
76+
socketInfo[socket.id].bot.setControlState(state,toggle);
77+
socket.on "playerRotate",(data)->
78+
socketInfo[socket.id].bot.look data...
7079
socket.on "playerUpdate",(data)->
7180
players[socket.id]=data
7281
io.sockets.emit "playerUpdate", players

0 commit comments

Comments
 (0)