在游戏中使用 Web Workers 有意义吗?
我正在开发一款具有人工智能逻辑、动作等和绘图的游戏。使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在游戏中使用 WebWorkers 确实有意义,但是,这意味着您必须保留一个游戏状态对象,该对象可以转换为有效的 JSON,您可以将其传递给 WebWorkers。好的一面是,您还可以将大量内在数据放入这些网络工作者中。
然后你设置一个 webworker 来进行巡逻和可能的事件(你也可以在 webworker 中调用其他 webworker 并处理巡逻事件)
现在必须清楚,使用 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.
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)
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.