12#include "../utils/debugprint.hpp"
13#include "../utils/json.hpp"
14#include "../utils/dataitem.hpp"
46 :
public std::enable_shared_from_this<GameObject>
49 using undumpFactory = std::function<std::shared_ptr<GameObject>(
ObjectId,
const std::string &)>;
56 static bool initialize(
const std::string &storage_path);
65 undumpFactory undumpCb );
83 return std::dynamic_pointer_cast<T>( _createFromJsonFile(
category,
id ) );
122 inline static std::string _storage_folder;
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);
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;
150 DebugPrint() <<
"dataItem multiple initialization for: '" << dataItem <<
"' (" << _object_category <<
"/" << _object_type <<
").";
153 return invalidDataItem;
162 return _dataitems_map.find(dataItem) != _dataitems_map.end();
171 auto var = _dataitems_map.find(dataItem);
172 if (var != _dataitems_map.end()) {
175 return invalidDataItem;
184 auto var = _dataitems_map.find(dataItem);
185 if (var != _dataitems_map.end()) {
188 return invalidDataItem;
199 std::map<std::string,DataItem> _dataitems_map;
207 inline const std::string &
category()
const {
return _object_category; }
213 inline const std::string &
type()
const {
return _object_type; }
225 void reparent(std::shared_ptr<GameObject> new_parent);
228 std::string _object_category;
229 std::string _object_type;
237 std::mutex _object_access_mutex;
252 template <
typename T>
254 return std::dynamic_pointer_cast<T>(_parent);
263 return _parent !=
nullptr ? _parent->getChild(
id) :
nullptr;
271 template <
typename T>
273 return _parent !=
nullptr ? _parent->getChild<T>(
id) :
nullptr;
282 auto d = _children_id_map.find(
id);
283 return d != _children_id_map.end() ? d->second :
nullptr;
291 template <
typename T>
293 auto d = _children_id_map.find(
id);
294 return d != _children_id_map.end() ? std::dynamic_pointer_cast<T>(d->second)
314 inline std::map<ObjectId, std::shared_ptr<GameObject>> &
childrenMap() {
315 return _children_id_map;
319 std::shared_ptr<GameObject> _parent;
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;
324 static inline std::map<std::string, GameObject::undumpFactory> _register_functions;
335 const std::string &
type);
339 virtual void tick(
double ,
double ) {};
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