成分和射弹
我在当前的游戏中使用了复合设计,效果非常好。所有游戏对象要么是 GameObject 的实例,要么是它的直接后代......某些对象实现特定的接口(例如武器),以便它们可以被其他对象“使用”,或者只是在它们需要额外的共享数据/方法时“使用”。然而,我现在开始将 box2d 集成到游戏中,但在这种情况下我遇到了组合问题。例如,精灵在精灵渲染组件中渲染,并且在渲染期间应用诸如材质之类的对象属性,但在诸如射弹之类的情况下,
我发现我基本上需要对相同的东西进行两次编码:基础射弹具有'onFire'、'onExpire' 等方法,因为武器总是拥有射弹,因此直接调用这些方法。然而,尝试将这些数据与弹丸物理组件同步,对我来说似乎是浪费时间。我认为在这种情况下,我可能应该让射弹本身“拥有”物理主体,作为射弹物理组件,将处理 onCollision 事件,并更新位置.. 这对我来说似乎更干净。现在我当然意识到,这样做时,我显式地将射弹对象与 box2d 耦合,但对于我的需要来说,这样就可以了。
我真的很想对此提供一些意见,因为到目前为止,我只是遇到了很多问题,试图做出与其他对象使用的相同的复合设计,以满足我对射弹的需求。请注意,我并不担心成为最纯粹的复合体,我的目标是创建一个游戏,而不是源引擎的 2D 等效项;)
I'm using a composite design in my current game which works pretty well. All game objects are either instances of GameObject or are direct descendants of it... some objects implement a particular interface such as weapons, so that they can be "used" by other objects or simply whenever they need additional shared data/methods. However, I'm beginning to integrate box2d into the game at the moment, but I'm having an issue with composition in this case. For example, a sprite gets rendered in the sprite render component and the objects properties such as material, get applied during rendering, but in cases such as projectiles,
I'm finding I need to essentially code the same stuff twice: the base projectile has an 'onFire','onExpire', etc method because weapons always own projectiles and as such, call those methods directly. However, trying to sync that data with the projectile physics component, just seems like a waste of time to me. I think in this case, I should possibly make the projectiles themselves "own" the physics body, where as the projectile physics component, would handle the onCollision events, and updating the positions.. That would just seem cleaner to me. Now I of course realize that in doing so, I'm explicitly coupling the projectile object with box2d, but for my needs, that would be fine.
I just really would like some input on this because as of right now, I'm just having a lot of issues trying to make the same composite design that other objects use, fit my needs with projectiles. Note that I'm not worried about being a composite purest, my aim is to create a game, not the 2D equivalent of the source engine ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对于 2d 来说是不典型的。在 3d 材质中,影响反射和光照,但在 2d 中,这种情况很少见。
是的,任何模拟对象都应该有一个带有任何固定装置的主体。对你的射弹使用 isBullte = true 标志。
如果您的游戏逻辑需要在射弹上调用某些方法,只需保留引用列表,但您不需要为此进行组合。
It is untipical for 2d. In 3d Material affects reflection and lighting, but in 2d it's a rarely case.
Yes, any simulated object should have a body with any fixture attached. Use isBullte = true flagfor your projectiles.
If your game logic needs call some methods on your projectiles, just keep list of references, but you don't need composition for that.