OpenAstra
 
Loading...
Searching...
No Matches
GameObject Class Reference

The GameObject class is at the core of OpenAstra. More...

#include <gameobject.h>

Inheritance diagram for GameObject:
Capability Command GameManager Module Player StarSystem StarsystemObject Capability_autotarget Capability_dock Capability_dock_transfer Capability_dockee Capability_dockee_transfer Capability_grow_quantity Capability_manage_hp Capability_module_aggregate Capability_move Capability_orbit Capability_transform_quantity Command_create_object Command_replace_from_list Command_replace_module Command_set_dataitems

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.
 
DataItemaddDataItem (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.
 
DataItemaccessDataItem (const std::string &dataItem)
 get a reference to the dataItem
 
const DataItemaccessDataItem (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< GameObjectgetParent () 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< GameObjectgetSibling (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< GameObjectgetChild (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.
 

Detailed Description

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:

  • can be automatically serialized to JSON on disk and deserialized back from disk (save/restore)
  • can contain any type of data (more or less) without requiring complex maintanance, see class DataItem.
  • can express an hierarchy of objects, with all the above advantages
  • offers a nice search interface to access child objects and data

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:

  • an unique ID (ID)
  • a category (to identify which object family it is)
  • a type (to identify which type inside the family it is)

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.

Constructor & Destructor Documentation

◆ GameObject()

GameObject::GameObject ( const std::string & category,
ObjectId id,
const std::string & type )
protected

Create a GameObject object.

Parameters
item_categoryobject category (header)
item_idobject ID (header)
item_typeobject type (header)

Member Function Documentation

◆ _finalizeDump()

virtual void GameObject::_finalizeDump ( )
inlineprotectedvirtual

Gives the object an opportunity to perform some additional stuff after the object had been serialized to disk.

◆ _finalizeUndump() [1/2]

virtual void GameObject::_finalizeUndump ( )
inlineprotectedvirtual

Gives the object an opportunity to perform some cleanup/inizialization after the entire object has been deserialized from disk.

Reimplemented in Player.

◆ _finalizeUndump() [2/2]

virtual void GameObject::_finalizeUndump ( std::shared_ptr< GameObject > )
inlineprotectedvirtual

Gives the object an opportunity to perform some cleanup/inizialization after the specific child has been deserialized from disk.

◆ accessDataItem() [1/2]

DataItem & GameObject::accessDataItem ( const std::string & dataItem)
inline

get a reference to the dataItem

Parameters
dataItem
Returns
reference to the dataItem

◆ accessDataItem() [2/2]

const DataItem & GameObject::accessDataItem ( const std::string & dataItem) const
inline

get a const reference to the dataItem

Parameters
dataItem
Returns
reference to the dataItem

◆ addDataItem()

DataItem & GameObject::addDataItem ( const std::string & dataItem)
inline

Add a dataItem, and return a reference to it.

Parameters
dataItem
Returns
pointer to the dataItem (like accessDataItem())

◆ category()

const std::string & GameObject::category ( ) const
inline

return the object category (starsystemobject, module, command...)

Returns
the object category

◆ childrenMap()

std::map< ObjectId, std::shared_ptr< GameObject > > & GameObject::childrenMap ( )
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.

Returns
a reference to the internal children map

◆ createFromJsonFile()

template<typename T>
static std::shared_ptr< T > GameObject::createFromJsonFile ( const std::string & category,
ObjectId id )
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.

Parameters
categorytype of the object
idunique id of the object
Returns
created object

◆ dataItemsFromTemplate()

void GameObject::dataItemsFromTemplate ( const nlohmann::json & template_data)

Initialize a list of dataitems from a JSON template.

Parameters
template_data
Returns
true if no error

◆ getChild() [1/2]

std::shared_ptr< GameObject > GameObject::getChild ( ObjectId id)
inline

return the child to match the id

Parameters
idthe ID to match, required
Returns
the first matching child

◆ getChild() [2/2]

template<typename T>
std::shared_ptr< T > GameObject::getChild ( ObjectId id)
inline

return the child to match the id

Parameters
idthe ID to match, required
Returns
the first matching child

◆ getParent() [1/2]

std::shared_ptr< GameObject > GameObject::getParent ( ) const
inline

return pointer to this object parent

Returns
the object parent

◆ getParent() [2/2]

template<typename T>
std::shared_ptr< T > GameObject::getParent ( ) const
inline

return pointer to this object parent

Returns
the object parent

◆ getSibling() [1/2]

std::shared_ptr< GameObject > GameObject::getSibling ( ObjectId id)
inline

get sibling with given ID

Parameters
id
Returns
pointer to sibling

◆ getSibling() [2/2]

template<typename T>
std::shared_ptr< T > GameObject::getSibling ( ObjectId id)
inline

get sibling with given ID

Parameters
id
Returns
pointer to sibling

◆ hasChild()

bool GameObject::hasChild ( ObjectId id)

check if current object has a child with this id

Parameters
ud
Returns
true if there is at least child

◆ hasDataItem()

bool GameObject::hasDataItem ( const std::string & dataItem) const
inline

Check if a specific data item exist.

Parameters
dataItemdata item to check
Returns
true if it exist

◆ id()

ObjectId GameObject::id ( ) const
inline

return the object unique ID

Returns
the id

◆ initialize()

bool GameObject::initialize ( const std::string & storage_path)
static

initialize the disk serialization process

Parameters
storage_pathpath that will contain the saved files. Will be created if missing.
Returns
true if the folder exist or was created. False in case of error.

◆ registerSubtype()

void GameObject::registerSubtype ( const std::string & category,
undumpFactory undumpCb )
static

Static function that needs to be called for each derived class to create types autmatically while undumping from JSON.

Parameters
categoryfamily of the object
undumpCbcallback that will be used to allocate a new instance of type category during deserialization

◆ reparent()

void GameObject::reparent ( std::shared_ptr< GameObject > new_parent)

reparent from the current parent, to a new one

Parameters
new_parentthe new parent

◆ toJson()

nlohmann::json GameObject::toJson ( )

serialize the current object to JSON object

Returns
JSON data struct

◆ type()

const std::string & GameObject::type ( ) const
inline

return the type (ship_xxx, station_xxx, planet_xxx...)

Returns
the object type

◆ writeToJsonFile()

bool GameObject::writeToJsonFile ( )

Write to the storage folder a file (see _create_filename() below) containing the object in JSON format.

Returns
true if no error

The documentation for this class was generated from the following files: