Skip to content

Commit

Permalink
tweak some stuff, still not working
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Jan 12, 2013
1 parent 513081a commit 99889b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
24 changes: 15 additions & 9 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var AverageLatency = require('./latency')

window.socket = websocket('ws://' + url.parse(window.location.href).host)
window.emitter = duplexEmitter(socket)
window.horses = {}
var playerID

function createGame(options) {
options.generateVoxelChunk = simplex({
Expand All @@ -21,12 +21,15 @@ function createGame(options) {
})
var game = engine(options)
game.on('tick', function() {
game.controls.enabled = false
})
game.controls.on('command', function() {
emitter.emit('state', {
moveForward: game.controls.moveForward,
moveBackward: game.controls.moveBackward,
moveLeft: game.controls.moveLeft,
moveRight: game.controls.moveRight,
enabled: game.controls.enabled
enabled: true
})
})
game.appendTo('#container')
Expand All @@ -46,20 +49,28 @@ function createGame(options) {
return game
}

emitter.on('id', function(id) {
console.log('id', id)
playerID = id
})

emitter.on('settings', function(settings) {
window.game = createGame(settings)
emitter.emit('generated', Date.now())
})

emitter.on('update', function(update) {
emitter.on('update', function(updates) {
if (!playerID) return
var update = updates.positions[playerID]
if (!update) return
game.controls.velocity.copy(update.velocity)
var to = new game.THREE.Vector3()
to.copy(update.position)
var from = game.controls.yawObject.position
var distance = from.distanceTo(to)
if (distance > 20) {
from.copy(update.position)
} else if (distance > 0.1){
} else if (distance > 0.01){
from.x += to.x * 0.1
from.y += to.y * 0.1
from.z += to.z * 0.1
Expand Down Expand Up @@ -126,11 +137,6 @@ window.addEventListener('keydown', ctrlToggle)
// if (Object.keys(horses).length === 0) return
// emitter.emit('position', JSON.parse(JSON.stringify(game.controls.yawObject.position.clone())))
// }, 10)

// emitter.on('join', function(id) {
// console.log('new horse!', id)
// horses[id] = new Horse()
// })
//
// emitter.on('leave', function(id) {
// game.scene.remove(horses[id])
Expand Down
25 changes: 19 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ function broadcast(id, cmd, arg1, arg2) {
})
}

setInterval(function() {
var clientKeys = Object.keys(clients)
if (clientKeys.length === 0) return
var update = {positions:{}}
clientKeys.map(function(key) {
var emitter = clients[key]
if (!emitter.player.enabled) return
update.positions[key] = {
position: emitter.player.yawObject.position,
velocity: emitter.player.velocity
}
})
update.time = Date.now()
broadcast(false, 'update', update)
}, 1000/22)

wss.on('connection', function(ws) {
var stream = websocket(ws)
var emitter = duplexEmitter(stream)
Expand All @@ -53,6 +69,7 @@ wss.on('connection', function(ws) {
emitter.player = playerPhysics()
emitter.player.yawObject.position.copy(settings.startingPosition)
console.log(id, 'joined')
emitter.emit('id', id)
broadcast(id, 'join', id)
stream.once('end', leave)
stream.once('error', leave)
Expand All @@ -66,12 +83,8 @@ wss.on('connection', function(ws) {
Object.keys(state).map(function(key) {
emitter.player[key] = state[key]
})
emitter.player.tick(Date.now() - emitter.lastUpdate, game.updatePlayerPhysics.bind(game))
var newPosition = {
position: emitter.player.yawObject.position,
velocity: emitter.player.velocity
}
emitter.emit('update', newPosition)
var delta = Date.now() - emitter.lastUpdate
emitter.player.tick(delta, game.updatePlayerPhysics.bind(game))
emitter.lastUpdate = Date.now()
})
})
Expand Down

0 comments on commit 99889b3

Please sign in to comment.