The GameObject class is at the core of OpenAstra. More...
#include <gameobject.h>
Public Types | |
| using | undumpFactory = std::function<std::shared_ptr<GameObject>(ObjectId, const std::string &)> |
Public Member Functions | |
| nlohmann::json | toJson () |
| serialize the current object to JSON object | |
| bool | writeToJsonFile () |
| Write to the storage folder a file (see _create_filename() below) containing the object in JSON format. | |
| DataItem & | addDataItem (const std::string &dataItem) |
| Add a dataItem, and return a reference to it. | |
| bool | hasDataItem (const std::string &dataItem) const |
| Check if a specific data item exist. | |
| DataItem & | accessDataItem (const std::string &dataItem) |
| get a reference to the dataItem | |
| const DataItem & | accessDataItem (const std::string &dataItem) const |
| get a const reference to the dataItem | |
| void | dataItemsFromTemplate (const nlohmann::json &template_data) |
| Initialize a list of dataitems from a JSON template. | |
| const std::string & | category () const |
| return the object category (starsystemobject, module, command...) | |
| const std::string & | type () const |
| return the type (ship_xxx, station_xxx, planet_xxx...) | |
| ObjectId | id () const |
| return the object unique ID | |
| void | reparent (std::shared_ptr< GameObject > new_parent) |
| reparent from the current parent, to a new one | |
| void | lock () |
| void | unlock () |
| std::shared_ptr< GameObject > | getParent () const |
| return pointer to this object parent | |
| template<typename T> | |
| std::shared_ptr< T > | getParent () const |
| return pointer to this object parent | |
| std::shared_ptr< GameObject > | getSibling (ObjectId id) |
| get sibling with given ID | |
| template<typename T> | |
| std::shared_ptr< T > | getSibling (ObjectId id) |
| get sibling with given ID | |
| std::shared_ptr< GameObject > | getChild (ObjectId id) |
| return the child to match the id | |
| template<typename T> | |
| std::shared_ptr< T > | getChild (ObjectId id) |
| return the child to match the id | |
| bool | hasChild (ObjectId id) |
| check if current object has a child with this id | |
| std::map< ObjectId, std::shared_ptr< GameObject > > & | childrenMap () |
| get a direct reference to the children map | |
| virtual void | tick (double, double) |
Static Public Member Functions | |
| static bool | initialize (const std::string &storage_path) |
| initialize the disk serialization process | |
| 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 undumping from JSON. | |
| template<typename T> | |
| static std::shared_ptr< T > | createFromJsonFile (const std::string &category, ObjectId id) |
| create a GameObject object from a JSON file | |
Static Public Attributes | |
| static DataItem | invalidDataItem = DataItem() |
Protected Member Functions | |
| virtual void | _finalizeDump () |
| Gives the object an opportunity to perform some additional stuff after the object had been serialized to disk. | |
| virtual void | _finalizeUndump (std::shared_ptr< GameObject >) |
| Gives the object an opportunity to perform some cleanup/inizialization after the specific child has been deserialized from disk. | |
| virtual void | _finalizeUndump () |
| Gives the object an opportunity to perform some cleanup/inizialization after the entire object has been deserialized from disk. | |
| GameObject (const std::string &category, ObjectId id, const std::string &type) | |
| Create a GameObject object. | |
The GameObject class is at the core of OpenAstra.
This class implements the entire object management of the game.
The basic class contains data and child objects, allowing for an advanced and stackable object management.
Some of the features of GameObject objects:
Each object has "data" items, which are labels/value couples. Each object has "children", which are other GameObject instances.
Each GameObject instance is identified by three pieces of information:
Each object can have a list of data items. Each data item is defined by it's string label, which is unique for the object, and it's stored by the GameObject itself. You can access those references.
|
protected |
Create a GameObject object.
| item_category | object category (header) |
| item_id | object ID (header) |
| item_type | object type (header) |
|
inlineprotectedvirtual |
Gives the object an opportunity to perform some additional stuff after the object had been serialized to disk.
|
inlineprotectedvirtual |
Gives the object an opportunity to perform some cleanup/inizialization after the entire object has been deserialized from disk.
Reimplemented in Player.
|
inlineprotectedvirtual |
Gives the object an opportunity to perform some cleanup/inizialization after the specific child has been deserialized from disk.
|
inline |
get a reference to the dataItem
| dataItem |
|
inline |
get a const reference to the dataItem
| dataItem |
|
inline |
Add a dataItem, and return a reference to it.
| dataItem |
|
inline |
return the object category (starsystemobject, module, command...)
|
inline |
get a direct reference to the children map
Use this method where speed is of the essence. You will need to iterate the set yourself, but it will be faster than performing multiple calls to the getChildren() call.
|
inlinestatic |
create a GameObject object from a JSON file
This method will load a specific file (see _create_filename() below) and create a from it. it will use the registered types to call the respective factory functions.
| category | type of the object |
| id | unique id of the object |
| void GameObject::dataItemsFromTemplate | ( | const nlohmann::json & | template_data | ) |
Initialize a list of dataitems from a JSON template.
| template_data |
|
inline |
return the child to match the id
| id | the ID to match, required |
|
inline |
return the child to match the id
| id | the ID to match, required |
|
inline |
return pointer to this object parent
|
inline |
return pointer to this object parent
|
inline |
get sibling with given ID
| id |
|
inline |
get sibling with given ID
| id |
| bool GameObject::hasChild | ( | ObjectId | id | ) |
check if current object has a child with this id
| ud |
|
inline |
Check if a specific data item exist.
| dataItem | data item to check |
|
inline |
return the object unique ID
|
static |
initialize the disk serialization process
| storage_path | path that will contain the saved files. Will be created if missing. |
|
static |
Static function that needs to be called for each derived class to create types autmatically while undumping from JSON.
| category | family of the object |
| undumpCb | callback that will be used to allocate a new instance of type category during deserialization |
| void GameObject::reparent | ( | std::shared_ptr< GameObject > | new_parent | ) |
reparent from the current parent, to a new one
| new_parent | the new parent |
| nlohmann::json GameObject::toJson | ( | ) |
serialize the current object to JSON object
|
inline |
return the type (ship_xxx, station_xxx, planet_xxx...)
| bool GameObject::writeToJsonFile | ( | ) |
Write to the storage folder a file (see _create_filename() below) containing the object in JSON format.