1 Star 0 Fork 36

张文轩/LazyFighters

forked from yk/LazyFighters 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
socket-server.js 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
yk 提交于 2015-02-26 21:30 . 新增:血量回复
var fps = 10
var distance = 100
var map = require('./game/map')(10, 10)
var onlineNumber = 0
var socketServer = function(server) {
var io = require('socket.io')(server)
io.on('connection', function(socket) {
onlineNumber++
var player = require('./game/character')(map)
var gameOn = true
var sendAttr = function(attr) {
socket.emit('attr', attr)
}
sendAttr(player.attr)
var sendPosition = function(position) {
socket.emit('position', position.toJSON())
}
sendPosition(player.position)
// character event
player.on('log', function(msg) {
socket.emit('log', msg)
})
player.on('positionChange', function(position) {
sendPosition(position)
})
player.on('attrChange', function(attr) {
sendAttr(attr)
})
// client event
socket.on('disconnect', function() {
onlineNumber--
gameOn = false
})
socket.on('move', function(direction) {
if(!direction) {
console.err('invalid parameter')
}
player.move(direction, distance)
})
socket.on('target', function(data) {
if(!data) {
console.err('invalid parameter')
}
player.setTarget(data.x, data.y)
})
socket.on('attack', function() {
player.attack()
})
// send network data
var updateNetwork = function() {
if(!gameOn) {
return
}
data = {
sendTime: new Date().getTime(),
onlineNumber: onlineNumber
}
socket.emit('network', data)
setTimeout(updateNetwork, 1000)
}
updateNetwork()
// send game data
var updateGame = function() {
if(!gameOn) {
return
}
data = {
hp: Math.floor(player.hp)
}
if(player.status != 'idle' && player.status != 'dead') {
data.process = player.process / player.processMax * 100
}
socket.emit('game', data)
player.update()
if(gameOn) {
setTimeout(updateGame, 1000/fps)
}
}
updateGame()
})
}
module.exports = socketServer
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/menuhin/LazyFighters.git
git@gitee.com:menuhin/LazyFighters.git
menuhin
LazyFighters
LazyFighters
master

搜索帮助