使用Node.JS构建的多人游戏JavaScript游戏 - 分离玩家 [英] Multiplayer JavaScript game built with Node.JS - Separating players

查看:116
本文介绍了使用Node.JS构建的多人游戏JavaScript游戏 - 分离玩家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我找不到答案。

I have a question which I cannot find an answer to.

我正在尝试使用Node.JS和Socket.IO构建一个多人游戏。我已经建立了一个聊天室作为我的第一个实验,所以我有广播工作等。现在我在那里,我想得到一些工作与Canvas。

I'm experimenting with building a multiplayer game with Node.JS and Socket.IO. I have built a chat room as my first experiment, so I have broadcasts working etc. Now I'm at the point where I want to get something working with Canvas.

我所遇到的问题是让我的头脑围绕着多个独立的球员。我知道每个播放器会发送他们的x,y线给服务器,服务器会广播这些,但客户端如何知道有多少播放器显示,我猜他们必须存储在某个阵列。 p>

The problem I'm having is getting my head around multiple and independent players. I know that each player will send their x,y cords to the server and the server will broadcast those, but how does the client know how many players to display, I'm guessing they have to be stored in an array somewhere.

推荐答案

我的实现将非常天真和简化,没有滞后补偿,外推等,但它应该指出一个多人游戏。

My implementation will be very naive and simplified, no lag compensation, extrapolation and such, but it should point out a general conception of "multiplayering" with node.

我认为最简单的方法是在客户端和服务器上都有一个包含玩家(实体)的关联数组。然后从客户端发送像 {action:move,target:[32,100]} 的命令,并使用服务器逻辑)。对于连接上的每个套接字,您应该分配一个播放器对象或ID,以便您可以访问它:

I think the simplest approach is to have an associative array containing players(entities) on both client and server. Then from client side you send commands like {action: "move", target:[32, 100]} and process this command with server logic (where the real game is running). To each socket on connection you should assign a player object or id so you can access it like:

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(); 
  }

});

然后每个人说100ms就可以向所有连接的玩家发送快照:

Then each let's say 100ms you could send snapshots to all connected players:

{
  timestamp: game.delta,
  players: {
    1: {x: 32, y: 100},
    2: {x: 14, y: 11}
  }
}

然后在客户端接收数据并从旧值插入到新值。

And then at client side receive data and interpolate from old to new values.

// 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
  }
}

这是一个足够的算法,用于半街机游戏最多有20个对象。减少快照的间隔使它几乎适合与更多的对象的策略游戏。你的主要敌人是带宽使用,你可以减少最小化数据包的大小。例如阅读关于BiSON,LZW和不发送自上次快照后没有改变的数据。

This is a sufficient algorithm for a semi-arcade game with 20 objects at maximum. Decreasing snapshot's interval makes it almost suitable for strategy game with more objects. Your main enemy is bandwidth usage which you can decrease minimizing packet's size. For instance read about BiSON, LZW and don't send data which haven't changed since last snapshot.

我的名誉不允许我发布所有链接,因此我已将它们附加到这里:
http://pastebin.com/Kh3wvF1D

My reputation doesn't allow me to post all the links, so I have attached them here: http://pastebin.com/Kh3wvF1D

Glenn Fiedler对多人观念的一般介绍:

General introduction to multiplayer conceptions by Glenn Fiedler:


http://gafferongames.com/networking-for-game-programmers/

http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/

来自Quake的一些多人游戏技巧(每个程序员需要知道的游戏网络/ :
这将给予ua关于插值和外推(预测)的线索

Some multiplayer techniques from Quake: This will give u a clue about interpolation and extrapolation(prediction)


http://fabiensanglard.net/quakeSource/quakeSourcePrediction.php

Valve's关于延迟补偿和一般优化的文章:

Valve's article about latency compensation and general optimisations:


https://developer.valvesoftware.com/wiki/Latency_Compensating_Methods_in_Client/Server_In-game_Protocol_Design_and_Optimization

多人技巧在帝国时代:


http://zoo.cs.yale.edu/classes/cs538/readings/papers/terrano_1500arch.pdf#search=%22立即%20time%20strategy%20networking%20lockstep%22

您还可以阅读我的关于优化带宽使用的文章

You can also read my article about optimizing bandwidth usage


http://rezoner.net/minimizing-bandwidth-usage-in-html5-games-using-websocket,299

+1 for Ivo的Wetzel Mapple.js这是一大堆知识。

+1 for Ivo's Wetzel Mapple.js it's a big pile of knowledge.


https://github.com/BonsaiDen/Maple.js

这篇关于使用Node.JS构建的多人游戏JavaScript游戏 - 分离玩家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆