Hen*_*ryz 13 javascript canvas node.js socket.io
我有一个问题,我无法找到答案.
我正在尝试使用Node.JS和Socket.IO构建多人游戏.我已经建立了一个聊天室作为我的第一个实验,所以我有广播工作等等.现在我正处于想要使用Canvas的地方.
我遇到的问题是围绕多个独立玩家.我知道每个玩家都会将他们的x,y线发送到服务器,服务器会广播那些,但是客户端如何知道要显示多少玩家,我猜他们必须存储在某个地方的数组中.
rez*_*ner 39
我的实现将非常幼稚和简化,没有滞后补偿,外推等等,但它应该指出与节点"多人游戏"的一般概念.
我认为最简单的方法是在客户端和服务器上都有一个包含播放器(实体)的关联数组.然后从客户端发送命令,{action: "move", target:[32, 100]}并使用服务器逻辑处理此命令(真实游戏正在运行).对于每个套接字,on connection 您应该分配一个播放器对象或ID,以便您可以访问它:
var lastPlayerID = 0;
var players = {};
server.on("connection", function(socket) {
var newcommer = new Player({id: lastPlayerID});
players[lastPlayerID] = newcommer;
socket.player = newcommer; // or lastPlayerID
lastPlayerID++;
socket.onMessage = function(message) {
this.player.doSomething();
}
});
Run Code Online (Sandbox Code Playgroud)
然后每个让我们说100毫秒你可以发送快照给所有连接的玩家:
{
timestamp: game.delta,
players: {
1: {x: 32, y: 100},
2: {x: 14, y: 11}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在客户端接收数据并从旧值插值到新值.
// duration in this simplified example is snapshot sending interval in [ms]
Player.prototype.interpolateTo = function(data, duration) {
if(typeof data.x != "undefined") {
// step needed to get `destination x` within `duration` miliseconds
this.stepValues.x = Math.abs(data.x - this.x) / duration;
this.target.x = data.x;
}
// ...
}
// step you call for each game loop iteration
Player.prototype.step = function(delta) {
if(this.x < this.target.x) {
this.x += delta * this.stepValues.x
}
}
Run Code Online (Sandbox Code Playgroud)
对于具有最多20个对象的半街机游戏来说,这是一个足够的算法.减少快照的间隔使其几乎适用于具有更多对象的策略游戏.你的主要敌人是带宽使用,你可以减少数据包的大小.例如,阅读有关BiSON,LZW并且不发送自上次快照以来未更改的数据.
我的声誉不允许我发布所有链接,所以我在这里附上:http:
//pastebin.com/Kh3wvF1D
Glenn Fiedler对多人概念的一般介绍:
Quake的一些多人游戏技术:这将给出关于插值和外推(预测)的线索
http://fabiensanglard.net/quakeSource/quakeSourcePrediction.php
Valve关于延迟补偿和一般优化的文章:
帝国时代的多人游戏技巧:
您还可以阅读我关于优化带宽使用的文章
http://rezoner.net/minimizing-bandwidth-usage-in-html5-games-using-websocket,299
为Ivo的Wetzel Mapple.js +1,这是一大堆知识.
| 归档时间: |
|
| 查看次数: |
12752 次 |
| 最近记录: |