Composition and projectiles
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 ph开发者_C百科ysics 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 ;)
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,
It is untipical for 2d. In 3d Material affects reflection and lighting, but in 2d it's a rarely case.
I think in this case, I should possibly make the projectiles themselves "own" the physics body
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.
精彩评论