mirror of
https://gitee.com/Dcivan/LazyFighters.git
synced 2026-05-07 01:00:43 +08:00
改动:每格只能站一个人
This commit is contained in:
3
game/constants.js
Normal file
3
game/constants.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
fps: 10
|
||||
}
|
||||
61
game/map.js
61
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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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") 右
|
||||
|
||||
Reference in New Issue
Block a user