OpenAstra
 
Loading...
Searching...
No Matches
gameobject.h
1#ifndef GameObject_H
2#define GameObject_H
3
4#include <stdint.h>
5
6#include <map>
7#include <memory>
8#include <mutex>
9#include <set>
10#include <string>
11
12#include "../utils/debugprint.hpp"
13#include "../utils/json.hpp"
14#include "../utils/dataitem.hpp"
15
46 : public std::enable_shared_from_this<GameObject>
47{
48public:
49 using undumpFactory = std::function<std::shared_ptr<GameObject>(ObjectId, const std::string &)>;
50
56 static bool initialize(const std::string &storage_path);
57
64 static void registerSubtype(const std::string &category,
65 undumpFactory undumpCb );
66
67
68public: // Disk serialization stuff
69
81 template<typename T>
82 static std::shared_ptr<T> createFromJsonFile(const std::string &category, ObjectId id) {
83 return std::dynamic_pointer_cast<T>( _createFromJsonFile( category, id ) );
84 }
85
91 nlohmann::json toJson();
92
98 bool writeToJsonFile();
99
100protected:
105 virtual void _finalizeDump() {};
106
112 virtual void _finalizeUndump(std::shared_ptr<GameObject> /* ptr */) {};
113
119 virtual void _finalizeUndump() {};
120
121private: // serialize part, private data
122 inline static std::string _storage_folder;
123
130 static std::string _create_filename(const std::string &category, ObjectId id);
131 static std::shared_ptr<GameObject> _createFromJson(std::shared_ptr<GameObject> parent,
132 const nlohmann::json &json_data);
133 static std::shared_ptr<GameObject> _createFromJsonFile(const std::string &category, ObjectId id);
134
135public: // dataitems
136 inline static DataItem invalidDataItem = DataItem();
137
143 DataItem& addDataItem(const std::string &dataItem) {
144 DataItem new_item( dataItem );
145 if ( new_item.isValid() ) {
146 const auto& i = _dataitems_map.find( new_item.name() );
147 if ( i == _dataitems_map.end() )
148 return _dataitems_map[ new_item.name() ] = new_item;
149 else
150 DebugPrint() << "dataItem multiple initialization for: '" << dataItem << "' (" << _object_category << "/" << _object_type << ").";
151 return i->second;
152 }
153 return invalidDataItem;
154 }
155
161 bool hasDataItem(const std::string &dataItem) const {
162 return _dataitems_map.find(dataItem) != _dataitems_map.end();
163 }
164
170 DataItem& accessDataItem(const std::string &dataItem) {
171 auto var = _dataitems_map.find(dataItem);
172 if (var != _dataitems_map.end()) {
173 return var->second;
174 }
175 return invalidDataItem;
176 }
177
183 const DataItem& accessDataItem(const std::string &dataItem) const {
184 auto var = _dataitems_map.find(dataItem);
185 if (var != _dataitems_map.end()) {
186 return var->second;
187 }
188 return invalidDataItem;
189 }
190
196 void dataItemsFromTemplate(const nlohmann::json& template_data);
197
198private:
199 std::map<std::string,DataItem> _dataitems_map;
200
201
202public: // access to object header (category, type, id, father)
207 inline const std::string &category() const { return _object_category; }
208
213 inline const std::string &type() const { return _object_type; }
214
219 ObjectId id() const { return _object_id; }
220
225 void reparent(std::shared_ptr<GameObject> new_parent);
226
227private: // private header stuff
228 std::string _object_category;
229 std::string _object_type;
230 ObjectId _object_id;
231
232public: // multi-threading support (locks)
233 void lock();
234 void unlock();
235
236private:
237 std::mutex _object_access_mutex;
238
239public: // Chldren access
244 std::shared_ptr<GameObject> getParent() const {
245 return _parent;
246 }
247
252 template <typename T>
253 std::shared_ptr<T> getParent() const {
254 return std::dynamic_pointer_cast<T>(_parent);
255 }
256
262 std::shared_ptr<GameObject> getSibling(ObjectId id) {
263 return _parent != nullptr ? _parent->getChild(id) : nullptr;
264 }
265
271 template <typename T>
272 std::shared_ptr<T> getSibling(ObjectId id) {
273 return _parent != nullptr ? _parent->getChild<T>(id) : nullptr;
274 }
275
281 std::shared_ptr<GameObject> getChild(ObjectId id) {
282 auto d = _children_id_map.find(id);
283 return d != _children_id_map.end() ? d->second : nullptr;
284 }
285
291 template <typename T>
292 std::shared_ptr<T> getChild(ObjectId id) {
293 auto d = _children_id_map.find(id);
294 return d != _children_id_map.end() ? std::dynamic_pointer_cast<T>(d->second)
295 : nullptr;
296 }
297
303 bool hasChild(ObjectId id);
304
314 inline std::map<ObjectId, std::shared_ptr<GameObject>> &childrenMap() {
315 return _children_id_map;
316 }
317
318private: // parent and children management
319 std::shared_ptr<GameObject> _parent;
320
321 std::map<ObjectId, std::shared_ptr<GameObject>> _children_id_map;
322 std::map<std::string, std::set<std::shared_ptr<GameObject>>> _children_category_map;
323
324 static inline std::map<std::string, GameObject::undumpFactory> _register_functions;
325
326protected: // Constructor, destructor and internal functions
333 GameObject(const std::string &category,
334 ObjectId id,
335 const std::string &type);
336 virtual ~GameObject();
337
338public: // operations functions
339 virtual void tick(double /*delta_time_s*/, double /*total_time_s*/) {};
340};
341
342
343#endif // GameObject_H
Definition dataitem.hpp:18
const std::string & name() const
return name of dataitem
Definition dataitem.hpp:145
bool isValid() const
return true if the data item contains a valid type&value
Definition dataitem.hpp:137
Definition debugprint.hpp:10
The GameObject class is at the core of OpenAstra.
Definition gameobject.h:47
bool hasChild(ObjectId id)
check if current object has a child with this id
Definition gameobject.cpp:213
GameObject(const std::string &category, ObjectId id, const std::string &type)
Create a GameObject object.
Definition gameobject.cpp:38
const std::string & type() const
return the type (ship_xxx, station_xxx, planet_xxx...)
Definition gameobject.h:213
std::shared_ptr< GameObject > getParent() const
return pointer to this object parent
Definition gameobject.h:244
std::shared_ptr< GameObject > getSibling(ObjectId id)
get sibling with given ID
Definition gameobject.h:262
bool writeToJsonFile()
Write to the storage folder a file (see _create_filename() below) containing the object in JSON forma...
Definition gameobject.cpp:150
void dataItemsFromTemplate(const nlohmann::json &template_data)
Initialize a list of dataitems from a JSON template.
Definition gameobject.cpp:163
static bool initialize(const std::string &storage_path)
initialize the disk serialization process
Definition gameobject.cpp:23
bool hasDataItem(const std::string &dataItem) const
Check if a specific data item exist.
Definition gameobject.h:161
std::shared_ptr< GameObject > getChild(ObjectId id)
return the child to match the id
Definition gameobject.h:281
static std::shared_ptr< T > createFromJsonFile(const std::string &category, ObjectId id)
create a GameObject object from a JSON file
Definition gameobject.h:82
const DataItem & accessDataItem(const std::string &dataItem) const
get a const reference to the dataItem
Definition gameobject.h:183
const std::string & category() const
return the object category (starsystemobject, module, command...)
Definition gameobject.h:207
virtual void _finalizeUndump()
Gives the object an opportunity to perform some cleanup/inizialization after the entire object has be...
Definition gameobject.h:119
void reparent(std::shared_ptr< GameObject > new_parent)
reparent from the current parent, to a new one
Definition gameobject.cpp:185
std::shared_ptr< T > getParent() const
return pointer to this object parent
Definition gameobject.h:253
virtual void _finalizeUndump(std::shared_ptr< GameObject >)
Gives the object an opportunity to perform some cleanup/inizialization after the specific child has b...
Definition gameobject.h:112
DataItem & addDataItem(const std::string &dataItem)
Add a dataItem, and return a reference to it.
Definition gameobject.h:143
std::map< ObjectId, std::shared_ptr< GameObject > > & childrenMap()
get a direct reference to the children map
Definition gameobject.h:314
std::shared_ptr< T > getSibling(ObjectId id)
get sibling with given ID
Definition gameobject.h:272
static void registerSubtype(const std::string &category, undumpFactory undumpCb)
Static function that needs to be called for each derived class to create types autmatically while und...
Definition gameobject.cpp:179
nlohmann::json toJson()
serialize the current object to JSON object
Definition gameobject.cpp:52
ObjectId id() const
return the object unique ID
Definition gameobject.h:219
virtual void _finalizeDump()
Gives the object an opportunity to perform some additional stuff after the object had been serialized...
Definition gameobject.h:105
std::shared_ptr< T > getChild(ObjectId id)
return the child to match the id
Definition gameobject.h:292
DataItem & accessDataItem(const std::string &dataItem)
get a reference to the dataItem
Definition gameobject.h:170
Definition gameid.hpp:9