我正在尝试运行一个在服务器上运行物理模拟的服务器,并让客户端通过 websockets/socket.io 连接到该服务器。我知道我可以使用 Matter.js 与渲染分开计算引擎。所以我的问题是,如何将引擎数据发送给客户端?
我有一个Game类,在套接字连接上,我想将其发送World给客户端进行渲染。
var g = new Game()
g.initEngine()
io.sockets.on('connection', function(socket) {
io.emit('gamestate', g.getGameState())
});
Run Code Online (Sandbox Code Playgroud)
在游戏状态下,我不太确定要传递什么:
var Game = function(Player1, Player2) {
var self = this
this.gameID = 22
this.engine = null
this.world = null
// Get game state - WHAT DO I SEND HERE!??
this.getGameState = function() {
return self.engine
}
// Create the engine
this.initEngine = function() {
self.engine = M.Engine.create();
self.world = self.engine.world;
self.world.gravity.x = 0;
self.world.gravity.y = 0;
M.Engine.update(self.engine, …Run Code Online (Sandbox Code Playgroud)