1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <string> #include <map>
class Color; class Texture; class Canvas;
class TreeType { private: std::string name; Color color; Texture texture; public: TreeType(const std::string & name, const Color & color, const Texture & texture): name(name), color(color), texture(texture) {} void draw(Canvas & convas, float x, float y) {} };
class Tree { private: float x, y; const TreeType & treeType; public: Tree(float x, float y, TreeType & treeType): x(x), y(y), treeType(treeType) {} virtual void draw(Canvas & canvas) { this.treeType->draw(canvas, x, y); } };
class TreeTypeFactory { private: std::map<std::string, TreeType> treeTypes; TreeTypeFactory() = default; static TreeTypeFactory _instance; public: static TreeTypeFactory & shared(); TreeType & getTreeType(const std::string & name, const Color & color, const Texture & texture) { } };
class Forest { private: std::vector<Tree *> trees; public: planeTree(float x, float y, const std::string & treeTypeName, const Color & color, const Texture & texture) { TreeType & treeType = TreeTypeFactory.shared().getTreeType(treeTypeName, color, texture); Tree * tree = new Tree(x, y, treeType); this->trees.push_back(tree); } virtual void draw(Canvas & canvas) { for (auto tree : trees) { tree->draw(); } } };
|