TDME2  1.9.200
SceneLibrary.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <unordered_map>
5 #include <string>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
13 #include <tdme/math/fwd-tdme.h>
15 
16 using std::make_unique;
17 using std::unordered_map;
18 using std::string;
19 using std::unique_ptr;
20 using std::vector;
21 
26 
27 /**
28  * Scene prototype library definition
29  * @author Andreas Drewke
30  */
32 {
33 public:
34  static constexpr int ID_ALLOCATE { -1 };
35 
36 private:
37  Scene* scene { nullptr };
38  unordered_map<int, Prototype*> prototypesById;
39  vector<unique_ptr<Prototype>> prototypes;
41 
42 public:
43  // forbid class copy
45 
46  /**
47  * Public constructor
48  * @param scene scene
49  */
51 
52  /**
53  * Destructor
54  */
55  ~SceneLibrary();
56 
57  /**
58  * Allocata a unique prototype index
59  * @return index
60  */
61  inline int allocatePrototypeId() {
62  return prototypeIdx++;
63  }
64 
65  /**
66  * Clears this scene prototype library
67  */
68  void clear();
69 
70  /**
71  * @return prototypes iterator
72  */
74  return UniquePtrSequenceIterator<Prototype>(&(*prototypes.begin()), &(*prototypes.end()));
75  }
76 
77  /**
78  * @return prototype count
79  */
80  inline int getPrototypeCount() {
81  return prototypes.size();
82  }
83 
84  /**
85  * Get prototype at given index
86  * @param idx index
87  * @return prototype
88  */
89  inline Prototype* getPrototypeAt(int idx) {
90  return prototypes[idx].get();
91  }
92 
93  /**
94  * Get a prototype by given id
95  * @param id id
96  * @return prototype
97  */
98  inline Prototype* getPrototype(int id) {
99  auto prototypeByIdIt = prototypesById.find(id);
100  if (prototypeByIdIt != prototypesById.end()) {
101  return prototypeByIdIt->second;
102  }
103  return nullptr;
104  }
105 
106  /**
107  * Get a prototype by given name
108  * @param name name
109  * @return prototype
110  */
111  inline Prototype* getPrototypeByName(const string& name) {
112  for (const auto& prototype: prototypes) {
113  if (prototype->getName() == name) return prototype.get();
114  }
115  return nullptr;
116  }
117 
118  /**
119  * Get a terrain prototype
120  * @return terrain prototype
121  */
123  for (const auto& prototype: prototypes) {
124  if (prototype->getType() == Prototype_Type::TERRAIN) return prototype.get();
125  }
126  return nullptr;
127  }
128 
129  /**
130  * Add a prototype
131  * @param prototype prototype
132  */
133  void addPrototype(Prototype* prototype);
134 
135  /**
136  * Remove a prototype
137  * @param id id
138  */
139  void removePrototype(int id);
140 
141 };
Prototype definition.
Definition: Prototype.h:55
Scene prototype library definition.
Definition: SceneLibrary.h:32
vector< unique_ptr< Prototype > > prototypes
Definition: SceneLibrary.h:39
void removePrototype(int id)
Remove a prototype.
Prototype * getPrototypeByName(const string &name)
Get a prototype by given name.
Definition: SceneLibrary.h:111
Prototype * getTerrainPrototype()
Get a terrain prototype.
Definition: SceneLibrary.h:122
Prototype * getPrototype(int id)
Get a prototype by given id.
Definition: SceneLibrary.h:98
UniquePtrSequenceIterator< Prototype > getPrototypes()
Definition: SceneLibrary.h:73
Prototype * getPrototypeAt(int idx)
Get prototype at given index.
Definition: SceneLibrary.h:89
int allocatePrototypeId()
Allocata a unique prototype index.
Definition: SceneLibrary.h:61
SceneLibrary(Scene *scene)
Public constructor.
unordered_map< int, Prototype * > prototypesById
Definition: SceneLibrary.h:38
void clear()
Clears this scene prototype library.
void addPrototype(Prototype *prototype)
Add a prototype.
static constexpr int ID_ALLOCATE
Definition: SceneLibrary.h:34
Scene definition.
Definition: Scene.h:50
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6