melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.91k stars 643 forks source link

[Question] Attachment / turrets #762

Closed yngndrw closed 8 years ago

yngndrw commented 8 years ago

Hello,

What support is there in MelonJS for entities which are made of multiple attached sprites. Examples might be entities which use Spline animation, or the turret in a top-down tower defence game, or a turret of a top-down tank ? In the tank example, you would want the body of the tank to be drawn separately from the turret, which should rotate about the body but should also move with the body.

It appears to me from reading issue #613 that the intention is for the tank to consist of the following objects: Container

When moving the tank you would move the container, when rotating the turret you would just rotate the sprite. Is this the best way of doing it or is there another suggested way ?

Thanks.

parasyte commented 8 years ago

me.Container is the class that supports entity/sprite composition. You can think of it as a branch node in the scene graph; a node which can support one or many other nodes (other branch nodes, or leaf nodes).

You can build a top-down perspective tank in the way you describe. In fact, someone already did (though I cannot find the link at the moment. It was on the forum.) The hierarchy will look like this:

The body is shown for illustration that the entity is a composition of a few different classes; me.Body stores the shapes for physics interactions, and renderable (me.Container) is what the entity will use for drawing. The me.Container can draw multiple layers with arbitrary attachment points, etc.

You have to put the attachment points together by hand, since we currently don't have a great interface for describing attachments semantically. Joints might be the answer for that, or even something else entirely.

yngndrw commented 8 years ago

Thank you very much, that clarifies it perfectly.