diff --git a/game/constants.js b/game/constants.js new file mode 100644 index 0000000..99b68dc --- /dev/null +++ b/game/constants.js @@ -0,0 +1,3 @@ +module.exports = { + fps: 10 +} diff --git a/game/map.js b/game/map.js index c3e7031..8068091 100644 --- a/game/map.js +++ b/game/map.js @@ -1,44 +1,43 @@ -var map = function() { +/* + map + 记录地图上的玩家 + 参数 + xmax + x坐标最大值 + ymax + y坐标最大值 +*/ +module.exports = function(xmax, ymax) { + return new map(xmax, ymax) +} +var map = function(xmax, ymax) { + if(!xmax || !ymax) { + console.log("map.js : Alert - not enough parameters") + } this.data = [] + this.xmax = xmax + this.ymax = ymax } -map.prototype.getCount = function(position) { +map.prototype.get = function(position) { var key = position.toString() - this.check(key) - return this.data[key].length + return this.data[key] } -map.prototype.add = function(position, player) { +map.prototype.set = function(position, player) { var key = position.toString() - this.check(key) - this.data[key].push(player) - - console.log("map.js: player added to ", key) - console.log("map.js: " + key + " now has " + this.getCount(position) + " people") -} - -map.prototype.remove = function(position, player) { - var key = position.toString() - this.check(key) - for(var i = 0; i < this.data[key].length; i++) { - if(this.data[key][i] === player) { - this.data[key].splice(i, 1) - break - } - } - - console.log("map.js: player removed from ", position.toString()) - console.log("map.js: " + key + " now has " + this.getCount(position) + " people") -} - -map.prototype.check = function(key) { if(!this.data[key]) { - this.data[key] = [] + this.data[key] = player } } -var createMap = function() { - return new map() +map.prototype.remove = function(position) { + var key = position.toString() + this.data[key] = null } -module.exports = createMap +map.prototype.generatePosition = function() { + var x = Math.floor(Math.random() * this.xmax) + var y = Math.floor(Math.random() * this.ymax) + return require('./model/position')(x, y) +} diff --git a/game/player.js b/game/player.js index ddaecef..508a49b 100644 --- a/game/player.js +++ b/game/player.js @@ -1,49 +1,75 @@ -var player = function() { - this.position = require('./model/position')(10, 10) - this.speed = 10 - this.next = null - this.moveCallback = null +module.exports = function(map, fn) { + return new player(map, fn) } -player.prototype.nextMove = function(direction, distance, moveCallback) { +var constants = require('./constants') + +var player = function(map, fn) { + // public + this.position = map.generatePosition() + this.speed = 20 + this.next = null + this.status = "idle" + this.remaining = null + + // private + this.map = map + this.fn = fn +} + +player.prototype.nextMove = function(direction, distance) { switch(direction) { case 'left': - this.remaining = distance this.next = this.position.left() break case 'right': - this.remaining = distance this.next = this.position.right() break case 'up': - this.remaining = distance this.next = this.position.up() break case 'down': - this.remaining = distance this.next = this.position.down() break } - this.moveCallback = moveCallback ? moveCallback : function(){} + if(this.map.get(this.next)) { + this.fn("该位置已被人占领") + return + } + this.setStatus("moving") + this.remaining = distance } player.prototype.isMoving = function() { - return this.next != null + return this.status == "moving" } -player.prototype.move = function(distance) { - if(this.isMoving()) { - this.remaining -= distance - if(this.remaining <= 0) { - this.moveCallback(this.position, this.next) - this.position = this.next - this.next = null - } +player.prototype.move = function(shift) { + if(this.map.get(this.next)) { + this.setStatus("idle") + this.fn("该位置已被人占领") + return + } + + this.remaining -= shift + if(this.remaining <= 0) { + this.map.remove(this.position) + this.map.set(this.next, this) + + this.position = this.next + this.next = null + this.setStatus("idle") + this.fn("player " + this.id + " has moved to position : " + this.position.toString()) } } -var createPlayer = function() { - return new player() +player.prototype.setStatus = function(status) { + this.status = status + this.fn("status set to `" + status + "`") } -module.exports = createPlayer +player.prototype.update = function() { + if(this.isMoving()) { + this.move(this.speed / constants.fps) + } +} diff --git a/public/js/game.js b/public/js/game.js index 4e83218..e05e7a5 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -29,7 +29,7 @@ $(function() { $('#online-number').html(data.onlineNumber) }) - socket.on('ui', function(data) { + socket.on('game', function(data) { $('#x').html(data.position.x) $('#y').html(data.position.y) if(data.remaining) { @@ -41,4 +41,8 @@ $(function() { } $('#block-number').html(data.blockNumber) }) + + socket.on('log', function(msg) { + console.log(msg) + }) }) diff --git a/socket-server.js b/socket-server.js index 5f5dd8f..9f6a576 100644 --- a/socket-server.js +++ b/socket-server.js @@ -1,6 +1,6 @@ var fps = 10 var distance = 100 -var map = require('./game/map')() +var map = require('./game/map')(10, 10) var onlineNumber = 0 @@ -8,21 +8,19 @@ var socketServer = function(server) { var io = require('socket.io')(server) io.on('connection', function(socket) { onlineNumber++ - var player = require('./game/player')() - map.add(player.position, player) + var player = require('./game/player')(map, function(msg) { + socket.emit('log', msg) + }) var gameOn = true - + // event disconnect socket.on('disconnect', function() { onlineNumber-- gameOn = false }) - socket.on('move', function(msg) { - player.nextMove(msg, distance, function(position1, position2) { - map.remove(position1, player) - map.add(position2, player) - }) + socket.on('move', function(direction) { + player.nextMove(direction, distance) }) // send network data @@ -51,15 +49,15 @@ var socketServer = function(server) { position: { x: player.position.x, y: player.position.y - }, - blockNumber: map.getCount(player.position) + } } if(player.isMoving()) { data.remaining = player.remaining - player.move(player.speed / fps) } + socket.emit('game', data) + + player.update() - socket.emit('ui', data) if(gameOn) { setTimeout(function() { updateGame() diff --git a/views/index.jade b/views/index.jade index 202156f..42eedf6 100644 --- a/views/index.jade +++ b/views/index.jade @@ -26,9 +26,6 @@ block content | 剩余进度: span(id='process-data') | % - p - | 该位置人数: - span(id='block-number') div(id = 'btn-panel') button(id="btn-left") 左 button(id="btn-right") 右