Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches

A wrapper on entity that allows objects in the Scene to be scriptable. More...

#include <sim_system.hpp>

Inheritance diagram for ToyMaker::SimObject:
ToyMaker::BaseSceneNode< SimObject > ToyMaker::Resource< SimObject > ToyMaker::SceneNodeCore ToyMaker::IResource

Public Member Functions

 ~SimObject () override
 Detaches all SimObjectAspects from this SimObject before allowing destruction to proceed.
 
void addAspect (const nlohmann::json &jsonAspectProperties)
 Constructs and attaches a new SimObjectAspect to this node based on the aspect's description in JSON.
 
void addAspect (const BaseSimObjectAspect &simObjectAspect)
 Constructs and attaches a new SimObjectAspect to this node which is a copy of the aspect passed as argument.
 
template<typename TSimObjectAspect>
bool hasAspect () const
 Tests whether an aspect of a particular type is attached to this node.
 
bool hasAspect (const std::string &aspectType) const
 Tests whether an aspect of a particular type is attached to this node.
 
template<typename TInterface>
bool hasAspectWithInterface () const
 Tests whether any aspects implementing some base class are present on this node.
 
void addOrReplaceAspect (const BaseSimObjectAspect &simObjectAspect)
 Adds or replaces an aspect for this node.
 
void addOrReplaceAspect (const nlohmann::json &jsonAspectProperties)
 Adds or replaces an aspect for this node.
 
template<typename TSimObjectAspect>
TSimObjectAspect & getAspect ()
 Gets a reference to a specific aspect present on this node.
 
BaseSimObjectAspectgetAspect (const std::string &aspectType)
 Gets (a base class reference to) an aspect present on this node.
 
template<typename TInterface>
std::vector< std::reference_wrapper< TInterface > > getAspectsWithInterface ()
 Gets a list of aspects which subclass some base class.
 
template<typename TSimObjectAspect>
void removeAspect ()
 Removes an aspect of a certain type from this SimObject.
 
void removeAspect (const std::string &aspectType)
 Removes an aspect of a certain type from this SimObject.
 
template<typename ... TComponents>
 SimObject (const Placement &placement, const std::string &name, TComponents ... components)
 
- Public Member Functions inherited from ToyMaker::SceneNodeCore
virtual ~SceneNodeCore ()=default
 Destroys SceneNodeCore.
 
template<typename TComponent>
void addComponent (const TComponent &component, const bool bypassSceneActivityCheck=false)
 Adds a component of type TComponent to the node.
 
void addComponent (const nlohmann::json &jsonComponent, const bool bypassSceneActivityCheck=false)
 Adds a component to the node.
 
template<typename TComponent>
TComponent getComponent (const float simulationProgress=1.f) const
 Retrieves a component belonging to this node.
 
template<typename TComponent>
bool hasComponent () const
 Tests whether this node has a component of a specific type.
 
bool hasComponent (const std::string &type) const
 Tests whether this node has a component of a specific type.
 
template<typename TComponent>
void updateComponent (const TComponent &component)
 Updates the value of a component of this node (to what it should be at the start of the next simulation step).
 
void updateComponent (const nlohmann::json &component)
 Updates the value of a component of this node (to what it should be at the start of the next simulation step).
 
template<typename TComponent>
void addOrUpdateComponent (const TComponent &component, const bool bypassSceneActivityCheck=false)
 A method for adding a component or updating a component if that component is already present on this node.
 
void addOrUpdateComponent (const nlohmann::json &component, const bool bypassSceneActivityCheck=false)
 A method for adding a component, or updating a component if the same type of component is already present on this node.
 
template<typename TComponent>
void removeComponent ()
 Removes a component present on this node.
 
template<typename TSystem>
void setEnabled (bool state)
 Sets whether or not a given system should be able to influence this scene object.
 
template<typename TSystem>
bool getEnabled () const
 Returns whether a particular system has been enabled for this node.
 
EntityID getEntityID () const
 Returns the entity id associated with these scene node.
 
WorldID getWorldID () const
 Returns the ID of the ECSWorld this node belongs to.
 
UniversalEntityID getUniversalEntityID () const
 Gets the UniversalEntityID aka the world-entity-id pair associated with this node.
 
std::weak_ptr< ECSWorldgetWorld () const
 Gets a reference to the ECSWorld this node belongs to.
 
bool inScene () const
 Returns whether this node is present as part of the SceneSystem's scene tree.
 
bool isActive () const
 Returns whether this node is present as part of the SceneSystem's scene tree, AND is active there as well.
 
bool isAncestorOf (std::shared_ptr< const SceneNodeCore > sceneNode) const
 Tests whether a particular scene node is the ancestor of this one.
 
bool hasNode (const std::string &pathToChild) const
 Tests whether a node specified by some path relative to this node is a real descendant of this node.
 
void addNode (std::shared_ptr< SceneNodeCore > node, const std::string &where)
 Adds a node (or a tree of them) as a child of the node specified by the path in the argument.
 
std::vector< std::shared_ptr< SceneNodeCore > > getChildren ()
 Returns a list of all of this node's immediate children scene nodes.
 
std::vector< std::shared_ptr< const SceneNodeCore > > getChildren () const
 Returns a list of all of this node's immediate children scene nodes.
 
std::vector< std::shared_ptr< SceneNodeCore > > getDescendants ()
 Gets all of the descendant nodes belonging to this scene node.
 
template<typename TObject = std::shared_ptr<SceneNode>>
TObject getByPath (const std::string &where)
 Gets a reference to a node or related object by its path.
 
template<typename TSceneNode = SceneNode>
std::shared_ptr< TSceneNode > getNodeByID (EntityID entityID)
 Gets a pointer to a node by its EntityID, assuming that node and this one belong to the same ECSWorld.
 
std::string getPathFromAncestor (std::shared_ptr< const SceneNodeCore > ancestor) const
 Gets the path from a node (assumed to be an ancestor) to this node.
 
virtual std::shared_ptr< ViewportNodegetLocalViewport ()
 Returns the viewport node which is in the same ECSWorld as and is the closest ancestor of (or the same as) this node.
 
virtual std::shared_ptr< const ViewportNodegetLocalViewport () const
 Returns (a constant reference to) the viewport node which is in the same ECSWorld as and is the closes ancestor of (or the same as) this node.
 
std::shared_ptr< SceneNodeCoregetNode (const std::string &where)
 Gets a reference to a scene node (of any valid type) based on its path relative to this node.
 
std::shared_ptr< SceneNodeCoregetParentNode ()
 Gets the parent node of this node, if one is present.
 
std::shared_ptr< const SceneNodeCoregetParentNode () const
 Gets (a constant reference to) the parent node of this node, if one is present.
 
std::shared_ptr< SceneNodeCoreremoveNode (const std::string &where)
 Removes a node from the tree present at the path specified.
 
std::vector< std::shared_ptr< SceneNodeCore > > removeChildren ()
 Disconnects and removes all the child nodes attached to this node.
 
std::string getName () const
 Returns the name string for this node.
 
void setName (const std::string &name)
 Sets the name of this node.
 
std::string getViewportLocalPath () const
 Gets the path of this node relative to its local viewport node.
 
void setPrototype_ (std::shared_ptr< SceneNodeCore > prototype)
 A reference to the node which was used in order to construct this one.
 
template<>
void setEnabled (bool state)
 
template<>
void removeComponent ()
 
template<>
void removeComponent ()
 
template<>
void removeComponent ()
 
template<>
void setEnabled (bool)
 
template<>
void updateComponent (const AxisAlignedBounds &axisAlignedBoxBounds)
 
template<>
void updateComponent (const ObjectBounds &objectBounds)
 
template<>
void removeComponent ()
 
template<>
void removeComponent ()
 
- Public Member Functions inherited from ToyMaker::Resource< SimObject >
std::string getResourceTypeName_ () const override
 Get the resource type string for this resource.
 
- Public Member Functions inherited from ToyMaker::IResource
virtual ~IResource ()=default
 Destroy the IResource object.
 

Static Public Member Functions

static std::string getResourceTypeName ()
 Gets the resource type string for the SimObject class.
 
template<typename ... TComponents>
static std::shared_ptr< SimObjectcreate (const Placement &placement, const std::string &name, TComponents...components)
 Creates a new SimObject scene node initialized with some placement and component values.
 
static std::shared_ptr< SimObjectcreate (const nlohmann::json &jsonSimObject)
 Creates a SimObject based on its description in JSON.
 
static std::shared_ptr< SimObjectcopy (const std::shared_ptr< const SimObject > simObject)
 Creates a new SimObject as a copy of another.
 
- Static Public Member Functions inherited from ToyMaker::SceneNodeCore
static void SceneNodeCore_del_ (SceneNodeCore *sceneNode)
 Deleter for a managed pointer to a scene node which ensures its onDestroyed virtual function gets called.
 

Protected Member Functions

template<typename ... TComponents>
 SimObject (const Placement &placement, const std::string &name, TComponents...components)
 
 SimObject (const nlohmann::json &jsonSimObject)
 
 SimObject (const SimObject &other)
 
- Protected Member Functions inherited from ToyMaker::BaseSceneNode< SimObject >
 BaseSceneNode (const Key &key, const Placement &placement, const std::string &name, TComponents...components)
 Constructor for a single node of a subclass.
 
 BaseSceneNode (const Placement &placement, const std::string &name, TComponents...components)
 General constructor for a single node of a subclass.
 
 BaseSceneNode (const nlohmann::json &nodeDescription)
 Constructs a new node of a certain type based on its json description.
 
 BaseSceneNode (const SceneNodeCore &other)
 Constructs a new node of a certain type as a copy of another node.
 
- Protected Member Functions inherited from ToyMaker::SceneNodeCore
virtual void joinWorld (ECSWorld &world)
 Removes this node's entity from its current ECSWorld and adds it to a new ECSWorld.
 
template<typename ... TComponents>
 SceneNodeCore (const Placement &placement, const std::string &name, TComponents...components)
 Constructs a scene node object from essential and extra components.
 
 SceneNodeCore (const nlohmann::json &jsonSceneNode)
 Constructs a node based on its description of JSON, later verifying that essential components are present.
 
 SceneNodeCore (const SceneNodeCore &sceneObject)
 Constructs a new node as a copy of another node.
 
virtual void onCreated ()
 Copy assignment operator.
 
virtual void onDestroyed ()
 Scene node lifecycle hook for when a node (and possibly its descendants) are about to be destroyed.
 
- Protected Member Functions inherited from ToyMaker::Resource< SimObject >
 Resource (int explicitlyInitializeMe)
 Construct a new resource object.
 
- Protected Member Functions inherited from ToyMaker::IResource
 IResource ()=default
 Construct a new IResource object.
 

Private Member Functions

void simulationUpdate (uint32_t simStepMillis)
 Copy assignment operator.
 
void variableUpdate (uint32_t variableStepMillis)
 Calls aspect variable update hooks for attached aspects.
 
void onActivated () override
 Notifies all attached aspects that this scene node has become an active part of the scene.
 
void onDeactivated () override
 Notifies all attached aspects that this scene node has stopped being an active part of the scene.
 
void copyAspects (const SimObject &other)
 Copies all aspects present on another SimObject onto this one.
 
std::shared_ptr< SceneNodeCoreclone () const override
 Copies this scene object returning one containing all the same components and aspects as it.
 

Private Attributes

std::unordered_map< std::string, std::shared_ptr< BaseSimObjectAspect > > mSimObjectAspects {}
 Aspect name and pointer pairs for all aspects attached to this SimObject.
 

Friends

class SimSystem
 
class BaseSimObjectAspect
 
class BaseSceneNode< SimObject >
 

Additional Inherited Members

- Static Protected Member Functions inherited from ToyMaker::BaseSceneNode< SimObject >
static std::shared_ptr< SimObjectcreate (const Key &, const Placement &placement, const std::string &name, TComponents...components)
 A (private) method for the creation of a new scene node for a particular type.
 
static std::shared_ptr< SimObjectcreate (const Placement &placement, const std::string &name, TComponents...components)
 A method for creating a scene node of a specific type (TSceneNode)
 
static std::shared_ptr< SimObjectcreate (const nlohmann::json &sceneNodeDescription)
 Creates a scene node of a type based on its description in JSON.
 
static std::shared_ptr< SimObjectcopy (const std::shared_ptr< const SimObject > sceneNode)
 Creates a scene node of a specific type based on another node of that type.
 
- Static Protected Member Functions inherited from ToyMaker::SceneNodeCore
static std::shared_ptr< SceneNodeCorecopy (const std::shared_ptr< const SceneNodeCore > other)
 Creates a new scene tree by copying another scene node and its descendants.
 
static void validateName (const std::string &nodeName)
 Tests whether a given name is actually valid, throwing an error when it is not.
 
- Static Protected Member Functions inherited from ToyMaker::IResource
template<typename TResource>
static void RegisterResource ()
 Registers this resource as a Resource type with the ResourceDatabase.
 

Detailed Description

A wrapper on entity that allows objects in the Scene to be scriptable.

This node will track (in addition to components owned by SceneNodeCore) any SimObjectAspects attached to it. It will also ensure that such aspects receive scene object lifecycle and engine related events, which it itself receives from the SimSystem.

The purpose of this is to facilitate the same sort of object-oriented, component-oriented interfaces game developers may be accustomed to in other engines (like Monobehaviours in Unity, GDScript in Godot).

Usage:

An example JSON description of a SimObject, as would be seen in a scene file:

{
"aspects": [
{ "type": "QueryClick" },
{ "type": "UrLookAtBoard", "offset": [0.0, 1.0, 7.0] }
],
"components": [
{
"fov": 55.5,
"aspect": 1.77778,
"orthographic_dimensions": {"horizontal": 0, "vertical": 0},
"near_far_planes": {"near": 0.5, "far": 100},
"projection_mode": "frustum",
"type": "CameraProperties"
},
{
"orientation": [
0.310491145,
-0.0720598251,
-0.923300385,
-0.214287385
],
"position": [
-6.0,
8.4,
6.0,
1.0
],
"scale": [
1.0,
1.0,
1.0
],
"type": "Placement"
}
],
"name": "camera",
"parent": "/viewport_3D/",
"type": "SimObject"
}
See also
SimObjectAspect
SimSystem

Member Function Documentation

◆ addAspect() [1/2]

void SimObject::addAspect ( const BaseSimObjectAspect & simObjectAspect)

Constructs and attaches a new SimObjectAspect to this node which is a copy of the aspect passed as argument.

Parameters
simObjectAspectThe aspect being copied.

◆ addAspect() [2/2]

void SimObject::addAspect ( const nlohmann::json & jsonAspectProperties)

Constructs and attaches a new SimObjectAspect to this node based on the aspect's description in JSON.

Parameters
jsonAspectPropertiesThe SimObjectAspect's JSON description.

◆ addOrReplaceAspect() [1/2]

void SimObject::addOrReplaceAspect ( const BaseSimObjectAspect & simObjectAspect)

Adds or replaces an aspect for this node.

Parameters
simObjectAspectThe aspect copied to construct a new aspect.

◆ addOrReplaceAspect() [2/2]

void SimObject::addOrReplaceAspect ( const nlohmann::json & jsonAspectProperties)

Adds or replaces an aspect for this node.

Parameters
jsonAspectPropertiesA JSON description for the aspect added to this node.

◆ clone()

std::shared_ptr< SceneNodeCore > SimObject::clone ( ) const
overrideprivatevirtual

Copies this scene object returning one containing all the same components and aspects as it.

Returns
std::shared_ptr<SceneNodeCore> The newly constructed SimObject, as a pointer to its base class.

Reimplemented from ToyMaker::SceneNodeCore.

◆ copy()

std::shared_ptr< SimObject > SimObject::copy ( const std::shared_ptr< const SimObject > simObject)
static

Creates a new SimObject as a copy of another.

Parameters
simObjectThe SimObject being copied.
Returns
std::shared_ptr<SimObject> The newly constructed SimObject.

◆ copyAspects()

void SimObject::copyAspects ( const SimObject & other)
private

Copies all aspects present on another SimObject onto this one.

Parameters
otherAn object whose aspects are being copied.

◆ create() [1/2]

std::shared_ptr< SimObject > SimObject::create ( const nlohmann::json & jsonSimObject)
static

Creates a SimObject based on its description in JSON.

Parameters
jsonSimObjectA JSON description for this SimObject, including initial component values.
Returns
std::shared_ptr<SimObject> The newly constructed SimObject value.

◆ create() [2/2]

template<typename ... TComponents>
std::shared_ptr< SimObject > ToyMaker::SimObject::create ( const Placement & placement,
const std::string & name,
TComponents... components )
static

Creates a new SimObject scene node initialized with some placement and component values.

Template Parameters
TComponentsThe types of components being initialized for this node's entity.
Parameters
placementThis node's initial Placement value.
nameThe name for this node in the scene hierarchy.
componentsThe values of other components being initialized for this node.
Returns
std::shared_ptr<SimObject> The newly created SimObjectAspect.

◆ getAspect() [1/2]

template<typename TSimObjectAspect>
TSimObjectAspect & ToyMaker::SimObject::getAspect ( )

Gets a reference to a specific aspect present on this node.

Template Parameters
TSimObjectAspectThe type of the aspect being fetched.
Returns
TSimObjectAspect& The fetched aspect.

◆ getAspect() [2/2]

BaseSimObjectAspect & SimObject::getAspect ( const std::string & aspectType)

Gets (a base class reference to) an aspect present on this node.

Parameters
aspectTypeThe aspect type string for the asset being fetched.
Returns
BaseSimObjectAspect& The fetched aspect.

◆ getAspectsWithInterface()

template<typename TInterface>
std::vector< std::reference_wrapper< TInterface > > ToyMaker::SimObject::getAspectsWithInterface ( )

Gets a list of aspects which subclass some base class.

Template Parameters
TInterfaceThe class implemented by fetched aspects.
Returns
std::vector<std::reference_wrapper<TInterface>> A list of aspects implementing TInterface, by their base class references.

◆ getResourceTypeName()

static std::string ToyMaker::SimObject::getResourceTypeName ( )
inlinestatic

Gets the resource type string for the SimObject class.

Returns
std::string The resource type string for this class.

◆ hasAspect() [1/2]

template<typename TSimObjectAspect>
bool ToyMaker::SimObject::hasAspect ( ) const

Tests whether an aspect of a particular type is attached to this node.

Template Parameters
TSimObjectAspectThe type of aspect whose presence is being tested.
Return values
trueThe aspect is present;
falseThe aspect is absent;

◆ hasAspect() [2/2]

bool SimObject::hasAspect ( const std::string & aspectType) const

Tests whether an aspect of a particular type is attached to this node.

Parameters
aspectTypeThe aspect type string of the aspect whose presence is being tested.
Return values
trueThe aspect is present;
falseThe aspect is absent;

◆ hasAspectWithInterface()

template<typename TInterface>
bool ToyMaker::SimObject::hasAspectWithInterface ( ) const

Tests whether any aspects implementing some base class are present on this node.

Template Parameters
TInterfaceThe base class an aspect of this class, possibly.
Return values
trueAn aspect implementing TInterface is present;
falseNo aspect implementing TInterface is present;

◆ onActivated()

void SimObject::onActivated ( )
overrideprivatevirtual

Notifies all attached aspects that this scene node has become an active part of the scene.

See also
BaseSimObjectAspect::onActivated()

Reimplemented from ToyMaker::SceneNodeCore.

◆ onDeactivated()

void SimObject::onDeactivated ( )
overrideprivatevirtual

Notifies all attached aspects that this scene node has stopped being an active part of the scene.

See also
BaseSimObjectAspect::onDeactivated()

Reimplemented from ToyMaker::SceneNodeCore.

◆ removeAspect() [1/2]

template<typename TSimObjectAspect>
void ToyMaker::SimObject::removeAspect ( )

Removes an aspect of a certain type from this SimObject.

Template Parameters
TSimObjectAspectThe type of aspect being removed.

◆ removeAspect() [2/2]

void SimObject::removeAspect ( const std::string & aspectType)

Removes an aspect of a certain type from this SimObject.

Parameters
aspectTypeThe aspect type string of the asset being removed.

◆ simulationUpdate()

void SimObject::simulationUpdate ( uint32_t simStepMillis)
private

Copy assignment operator.

Todo
Figure out whether this operator will actually ever be useful.

Calls aspect simulation update hooks for attached aspects.

Parameters
simStepMillisThe time by which the simulation is to be advanced.

◆ variableUpdate()

void SimObject::variableUpdate ( uint32_t variableStepMillis)
private

Calls aspect variable update hooks for attached aspects.

Parameters
variableStepMillisThe time since the computation of the last frame.

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