在游戏中使用 Web Workers 有意义吗?

发布于 2024-09-13 12:51:57 字数 266 浏览 9 评论 0原文

我正在开发一款具有人工智能逻辑、动作等和绘图的游戏。使用 Web Workers 计算移动和 AI 逻辑有意义吗?但是我该怎么做——因为工作人员需要了解很多关于主线程的信息,比如某些物体的碰撞位置、子弹数量等。这感觉像是不可能的,因为工作人员与主线程完全分开根本无法访问。我确实知道有一个 postMessage() 系统,但感觉很……嗯,痛苦?

例如,我有一个步枪手对象,它代表一个具有精灵、位置、生命值等的步枪手。我希望他巡逻。那么,如何在工人身上执行巡逻代码呢?它几乎需要对该对象的全部访问权限。

I am working on a game that has AI logic, movement, etc and drawing. Does it make sense to calculate moving and AI logic using Web Workers? But how do I do that -- because the workers need to know so much about the main thread like the positions of certain objects for collisions, amount of bullets, etc. It feels like impossible because the worker is completely separate from the main thread with no access what so ever. I do know that there a postMessage() system, but that feels so ... umm, painful?

For example, I have a rifleman object that represents a rifleman with a sprite, position, health, etc. I want him to patrol. So, how do I do that patrolling code on a worker? It would need pretty much the whole access to that object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-09-20 12:51:57

我认为在游戏中使用 WebWorkers 确实有意义,但是,这意味着您必须保留一个游戏状态对象,该对象可以转换为有效的 JSON,您可以将其传递给 WebWorkers。好的一面是,您还可以将大量内在数据放入这些网络工作者中。

var gameState = {
   sprites: {
      {
         type: 'rifleman',  // damage, accuracy, speed etc set inside appropriate webworker.
         owner: 'playerA', 
         x: 100,
         y: 100,
         z: 0,
         level: 1, // used to fetch modifiers to dmg, acc, speed etc.

      },
      {
         // each sprite it's own state obj.
      }
   }
}

然后你设置一个 webworker 来进行巡逻和可能的事件(你也可以在 webworker 中调用其他 webworker 并处理巡逻事件)

var patrolWorker = new WebWorker('patrolWorker');
patrolWorker.onmessage = function(e){
   render(e.data); // your render function, can ALSO be a webworker if you like ;)
}
patrolWorker.postMessage(gameState.sprites);

现在必须清楚,使用 WebWorkers 实际上是一个架构决策,如果你想使用对他们来说,这将涉及大量的重构。如果没有重构,我怀疑它对你根本没有用。

I think it does make sense to use WebWorkers for a game, but yeah, it will mean you will have to keep a game state object which can be converted to valid JSON which you can pass to webworkers. On the brightside, you can also put a lot of intrinsic data inside those webworkers as well.

var gameState = {
   sprites: {
      {
         type: 'rifleman',  // damage, accuracy, speed etc set inside appropriate webworker.
         owner: 'playerA', 
         x: 100,
         y: 100,
         z: 0,
         level: 1, // used to fetch modifiers to dmg, acc, speed etc.

      },
      {
         // each sprite it's own state obj.
      }
   }
}

then you set up a webworker for patrolling and possible events (you can call other webworkers inside a webworker as well and process patrol events)

var patrolWorker = new WebWorker('patrolWorker');
patrolWorker.onmessage = function(e){
   render(e.data); // your render function, can ALSO be a webworker if you like ;)
}
patrolWorker.postMessage(gameState.sprites);

It must become clear by now, that using WebWorkers is actually very much an architectural decision, if you want to use them, it will involve a lot of refactoring. Without the refactoring, I doubt it's useful for you at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文