Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are still one step behing "composition over inheritance" mindset. The base class should know what to composite. To change image, you should change sprite instance, you shouldn't provide interface of composed instances. For example:</p> <pre><code>class GameObject { public: // you can choose public access or getters and setters, it's your choice Sprite sprite; PhysicalBody body; }; object = GameObject(); object.sprite = graphicalSystem.getSpriteFromImage("image.png"); // or if you prefer setters and getters object.setSprite(sprite); </code></pre> <p>More generally GameObject should contain instances (or pointers to instances, depends on your implementation) of base class Component. It makes sense to use inheritance in this case, because this way they can be in one storage like std::map. For example:</p> <pre><code>class Component { // ... }; class Sprite : public Component { //... }; class PhysicalBody : public Component { //... }; class GameObject { protected: std::map&lt;std::string, Component*&gt; components; //... public: Component* getComponent(const std::string&amp; name) const; void setComponent(const std::string&amp; name, Component* component); //... }; </code></pre> <p>For component creation and rendering in main loop use <strong>Systems</strong>. For example GraphicalSystem knows all instances of Sprite it has created and while rendering it renders only sprites attached to some GameObject instance. Detached component can be garbage collected. Information about position and size might be part of the GameObject or it might be a component "physical".</p> <p>The best way to understand it is to write your own prototype or to check existing implementations (<a href="http://gamadu.com/artemis/" rel="nofollow">Artemis</a>, <a href="http://docs.unity3d.com/Documentation/Manual/GameObjects.html" rel="nofollow">Unity 3D</a> and many others). For more information see <a href="http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/" rel="nofollow">Cowboy programming: Evolve Your Hierarchy</a> or try to find Entity/component system.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload