Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Suggestion 1</strong></p> <p>How about introducing an EnemyCreator?</p> <pre><code>class EnemyCreator { protected: Enemy* createImpl() const = 0; public: // ctors Enemy* create() const { return(createImpl()); }; // eo create }; // eo class EnemyCreator // a concrete creator class EnemyType1Creator : public EnemyCreator { protected: Enemy* createImpl() const { Properties p; // ... // fill in properties here // return new Enemy(p); // Enemy will populate }; }; // eo class EnemyType1Creator </code></pre> <p>Then modify your EnemyFactor to manage a bunch of EnemyCreator objects.</p> <p><strong>Suggestion 2</strong></p> <p>You say that the property-set number and types are the same, they just vary from enemy to enemy. Another way would be to have a base-class that sets up enemy properties in a generic fashion (assumes <code>_property</code> is a member of <code>Properties</code> and accessible):</p> <pre><code>class EnemyProperties : public Properties { protected: virtual void initialiseImpl() = 0; public: void initialise() { // set up the properties all enemies have _property["healh"] = 5; _property["skill"] = 0.5f; _property["skillWithABanana"] = 1.0f; // now let derived classes specialise initialiseImpl(); }; }; // eo class EnemyProperties </code></pre> <p>Now we can make property sets for any number of types and only change what we want:</p> <pre><code>class BadBananaEnemy : public EnemyProperties { protected: void initialiseImpl() { _property["skillWithABanana"] = 0.0f; // he's bad at this. }; // eo initialiseImpl() }; // eo class BadBananaEnemy </code></pre> <p>etceteras.</p> <p>You can then put these in a factory that can dish these out by name:</p> <pre><code>class EnemyFactory { private: typedef std::pair&lt;std::string, EnemyProperties&gt; EnemyPair; typedef std::map&lt;std::string, EnemyProperties&gt; EnemyMap; EnemyMap m_EnemyTypes; public: // register a type void registerType(const std::string&amp; _name, const EnemyProperties&amp; _prop) { m_EnemyTypes.insert(EnemyPair(_name, _prop)); }; // eo registerType // create an enemy Enemy* createEnemy(const std::string&amp; _type) { EnemyMap::iterator it(m_EnemyTypes.find(_type)); if(it != m_EnemyTypes.end()) return(NULL); else return(new Enemey(it-&gt;second)); }; // eo createEnemy }; // eo class EnemyFactory </code></pre>
    singulars
    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. 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