-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
172 lines (153 loc) · 4.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var url = require('url')
var websocket = require('websocket-stream')
var engine = require('voxel-engine')
var duplexEmitter = require('duplex-emitter')
var toolbar = require('toolbar')
var randomName = require('./randomname')
var crunch = require('voxel-crunch')
var emitChat = require('./chat')
var highlight = require('voxel-highlight')
var skin = require('minecraft-skin')
var player = require('voxel-player')
var texturePath = "/textures/"
//var game
module.exports = Client
function Client(server, game) {
if(!(this instanceof Client)) {
return new Client(server, game)
}
// this.blockSelector = toolbar({el: '#tools'})
this.playerID
this.lastProcessedSeq = 0
this.localInputs = []
this.connected = false
this.currentMaterial = 1
this.lerpPercent = 0.1
this.server = server || 'ws://' + url.parse(window.location.href).host
this.others = {}
this.connect(server, game)
this.game
window.others = this.others
}
Client.prototype.connect = function(server, game) {
var self = this
var socket = websocket(server)
socket.on('end', function() { self.connected = false })
this.socket = socket
this.bindEvents(socket, game)
}
Client.prototype.bindEvents = function(socket, game) {
var self = this
this.emitter = duplexEmitter(socket)
var emitter = this.emitter
this.connected = true
emitter.on('id', function(id) {
console.log('got id', id)
self.playerID = id
if (game != null) {
self.game = game
console.log("Sending local settings to the server.")
emitter.emit('clientSettings', self.game.settings)
} else {
emitter.emit('clientSettings', null)
}
})
emitter.on('settings', function(settings) {
settings.texturePath = texturePath
settings.generateChunks = false
//deserialise the voxel.generator function.
if (settings.generatorToString != null) {
settings.generate = eval("(" + settings.generatorToString + ")")
}
self.game = self.createGame(settings, game)
emitter.emit('created')
emitter.on('chunk', function(encoded, chunk) {
var voxels = crunch.decode(encoded, new Uint32Array(chunk.length))
chunk.voxels = voxels
self.game.showChunk(chunk)
})
})
// fires when server sends us voxel edits
emitter.on('set', function(pos, val) {
self.game.setBlock(pos, val)
})
}
Client.prototype.createGame = function(settings, game) {
var self = this
var emitter = this.emitter
settings.controlsDisabled = false
self.game = engine(settings)
self.game.settings = settings
function sendState() {
if (!self.connected) return
var player = self.game.controls.target()
var state = {
position: player.yaw.position,
rotation: {
y: player.yaw.rotation.y,
x: player.pitch.rotation.x
}
}
emitter.emit('state', state)
}
var name = localStorage.getItem('name')
if (!name) {
name = randomName()
localStorage.setItem('name', name)
}
self.game.controls.on('data', function(state) {
var interacting = false
Object.keys(state).map(function(control) {
if (state[control] > 0) interacting = true
})
if (interacting) sendState()
})
emitChat(name, emitter)
// setTimeout is because three.js seems to throw errors if you add stuff too soon
setTimeout(function() {
emitter.on('update', function(updates) {
Object.keys(updates.positions).map(function(player) {
var update = updates.positions[player]
if (player === self.playerID) return self.onServerUpdate(update) // local player
self.updatePlayerPosition(player, update) // other players
})
})
}, 1000)
emitter.on('leave', function(id) {
if (!self.others[id]) return
self.game.scene.remove(self.others[id].mesh)
delete self.others[id]
})
return self.game
}
Client.prototype.onServerUpdate = function(update) {
// todo use server sent location
}
Client.prototype.lerpMe = function(position) {
var to = new this.game.THREE.Vector3()
to.copy(position)
var from = this.game.controls.target().yaw.position
from.copy(from.lerp(to, this.lerpPercent))
}
Client.prototype.updatePlayerPosition = function(id, update) {
var pos = update.position
var player = this.others[id]
if (!player) {
var playerSkin = skin(this.game.THREE, 'player.png', {
scale: new this.game.THREE.Vector3(0.04, 0.04, 0.04)
})
var playerMesh = playerSkin.mesh
this.others[id] = playerSkin
playerMesh.children[0].position.y = 10
this.game.scene.add(playerMesh)
}
var playerSkin = this.others[id]
var playerMesh = playerSkin.mesh
playerMesh.position.copy(playerMesh.position.lerp(pos, this.lerpPercent))
// playerMesh.position.y += 17
playerMesh.children[0].rotation.y = update.rotation.y + (Math.PI / 2)
playerSkin.head.rotation.z = scale(update.rotation.x, -1.5, 1.5, -0.75, 0.75)
}
function scale( x, fromLow, fromHigh, toLow, toHigh ) {
return ( x - fromLow ) * ( toHigh - toLow ) / ( fromHigh - fromLow ) + toLow
}