18#ifndef FOOLSENGINE_SIMSYSTEM_H
19#define FOOLSENGINE_SIMSYSTEM_H
28#include <nlohmann/json.hpp>
71 inline void from_json(
const nlohmann::json& json,
SimCore& simCore) {
78 inline void to_json(nlohmann::json& json,
const SimCore& simCore) {
92 class SimSystem:
public System<SimSystem, std::tuple<>, std::tuple<SimCore>> {
94 explicit SimSystem(std::weak_ptr<ECSWorld> world):
115 virtual std::shared_ptr<BaseSystem> instantiate(std::weak_ptr<ECSWorld> world)
override;
124 template <
typename TSimObjectAspect>
133 std::shared_ptr<BaseSimObjectAspect>
constructAspect(
const nlohmann::json& jsonAspectProperties);
148 void onVariableStep(
float simulationProgress, uint32_t variableStepMillis)
override;
154 std::unordered_map<std::string, std::shared_ptr<BaseSimObjectAspect> (*)(
const nlohmann::json& jsonAspectProperties)>
mAspectConstructors {};
245 template <
typename ...TComponents>
246 static std::shared_ptr<SimObject>
create(
const Placement& placement,
const std::string& name, TComponents...components);
254 static std::shared_ptr<SimObject>
create(
const nlohmann::json& jsonSimObject);
262 static std::shared_ptr<SimObject>
copy(
const std::shared_ptr<const SimObject> simObject);
269 void addAspect(
const nlohmann::json& jsonAspectProperties);
285 template <
typename TSimObjectAspect>
295 bool hasAspect(
const std::string& aspectType)
const;
304 template <
typename TInterface>
327 template <
typename TSimObjectAspect>
344 template <
typename TInterface>
352 template <
typename TSimObjectAspect>
364 template <
typename ...TComponents>
365 SimObject(
const Placement& placement,
const std::string& name, TComponents...components);
366 SimObject(
const nlohmann::json& jsonSimObject);
421 std::shared_ptr<SceneNodeCore>
clone()
const override;
454 return mHandler(actionData, actionDefinition);
489 friend class BaseSimObjectAspect;
523 virtual void variableUpdate(uint32_t variableStepMillis) {(void)variableStepMillis; }
563 template <
typename TSimObjectAspectDerived>
567 auto& simSystemRegistrator {
570 simSystemRegistrator.emptyFunc();
589 template <
typename TComponent>
599 template <
typename TComponent>
608 template <
typename TComponent>
618 template <
typename TComponent>
619 TComponent
getComponent(
const float simulationProgress=1.f)
const;
626 template <
typename TComponent>
635 void addAspect(
const nlohmann::json& jsonAspectProperties);
651 template <
typename TSimObjectAspect>
662 bool hasAspect(
const std::string& aspectType)
const;
670 template <
typename TSimObjectAspect>
686 template <
typename TSimObjectAspect>
754 std::weak_ptr<ECSWorld>
getWorld()
const;
779 void onDeactivated_();
839 virtual std::shared_ptr<BaseSimObjectAspect>
clone()
const = 0;
847 std::pair<std::string, std::string>,
848 std::shared_ptr<FixedActionBinding>,
849 std::less<std::pair<std::string, std::string>>
955 template <
typename TSimObjectAspectDerived>
956 class SimObjectAspect:
public BaseSimObjectAspect {
958 SimObjectAspect(
int explicitlyInitializeMe){
959 (void)explicitlyInitializeMe;
969 return TSimObjectAspectDerived::getSimObjectAspectTypeName();
985 friend class Registrator<SimObjectAspect<TSimObjectAspectDerived>>;
986 friend class SimObject;
989 template<
typename TSimObjectAspectDerived>
994 template <
typename TSimObjectAspect>
996 assert((std::is_base_of<BaseSimObjectAspect, TSimObjectAspect>::value) &&
"Type being registered must be a subclass of BaseSimObjectAspect");
998 TSimObjectAspect::getSimObjectAspectTypeName(),
999 &(TSimObjectAspect::create)
1003 template <
typename ...TComponents>
1008 template <
typename ...TComponents>
1009 SimObject::SimObject(
const Placement& placement,
const std::string& name, TComponents ... components) :
1014 template <
typename TComponent>
1016 mSimObject->addComponent<TComponent>(component);
1018 template <
typename TComponent>
1020 return mSimObject->hasComponent<TComponent>();
1022 template <
typename TComponent>
1024 mSimObject->updateComponent<TComponent>(component);
1026 template <
typename TComponent>
1028 return mSimObject->getComponent<TComponent>(simulationProgress);
1030 template <
typename TComponent>
1032 return mSimObject->removeComponent<TComponent>();
1035 template <
typename TSimObjectAspect>
1040 template <
typename TInterface>
1043 if(
auto interfaceReference = std::dynamic_pointer_cast<TInterface>(aspect.second)){
1050 template <
typename TSimObjectAspect>
1052 return *(
static_cast<TSimObjectAspect*
>(
mSimObjectAspects.at(TSimObjectAspect::getSimObjectAspectTypeName()).get()));
1055 template <
typename TInterface>
1057 std::vector<std::reference_wrapper<TInterface>> interfaceImplementations {};
1059 if(
auto interfaceReference = std::dynamic_pointer_cast<TInterface>(aspect.second)) {
1060 interfaceImplementations.push_back(*interfaceReference);
1063 return interfaceImplementations;
1066 template <
typename TSimObjectAspect>
1071 template <
typename TSimObjectAspect>
1073 mSimObject->removeAspect<TSimObjectAspect>();
1076 template <
typename TSimObjectAspect>
1078 return mSimObject->getAspect<TSimObjectAspect>();
1081 template <
typename TSimObjectAspect>
1083 return mSimObject->hasAspect<TSimObjectAspect>();
1088 static BaseSimObjectAspect& get(std::shared_ptr<SceneNodeCore> rootNode,
const std::string& where) {
1089 std::string::const_iterator div {std::find(where.begin(), where.end(),
'@')};
1090 assert(div != where.end() &&
"Must contain @ to be a valid path to a scene node's aspect");
1093 const std::string nodePath { where.substr(0, div - where.begin()) };
1094 const std::string aspectName { where.substr(1 + (div - where.begin())) };
1095 assert(rootNode->getWorld().lock()->getSystem<
SimSystem>()->
aspectRegistered(aspectName) &&
"No aspect of this type has been registered with the Sim System");
1097 std::shared_ptr<SimObject> node { rootNode->getByPath<std::shared_ptr<SimObject>>(nodePath) };
1098 return node->getAspect(aspectName);
1101 static constexpr bool s_valid {
true };
1104 template <
typename TAspect>
1106 static TAspect& get(std::shared_ptr<SceneNodeCore> rootNode,
const std::string& where) {
1107 return static_cast<TAspect&
>(SceneNodeCore::getByPath_Helper<BaseSimObjectAspect&>::get(rootNode, where));
1109 static constexpr bool s_valid {
true };
1122 assert(
false &&
"Cannot remove a sim object's sim core component.");
A CRTP template for all the scene node types present in the project.
Definition scene_system.hpp:699
BaseSceneNode(const Key &key, const Placement &placement, const std::string &name, TComponents...components)
Definition scene_system.hpp:762
static std::shared_ptr< TSceneNode > create(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.
Definition scene_system.hpp:1771
The base class for all aspects, providing an interface to its attached SimObject, and consequently,...
Definition sim_system.hpp:499
void addComponent(const TComponent &component)
Adds a component of some type to the underlying entity.
Definition sim_system.hpp:1015
virtual ~BaseSimObjectAspect()=default
Destroys this object.
void removeComponent()
Removes a component of some type belonging to the underlying SimObject.
Definition sim_system.hpp:1031
ViewportNode & getLocalViewport()
Returns the closest ancestor viewport to this node, if one exists (which it should,...
Definition sim_system.cpp:210
virtual void onActivated()
Callback for when the aspect is activated (after it is attached to an active SimObject,...
Definition sim_system.hpp:797
void activateFixedActionBindings()
Called when an aspect has just been activated to bind its handler method to its associated action.
Definition sim_system.cpp:234
virtual void onDetached()
Callback for when an aspect is about to be removed from an object (after it has been deactivated).
Definition sim_system.hpp:791
TSimObjectAspect & getAspect()
Gets an aspect of a particular type belonging to the underlying SimObject.
Definition sim_system.hpp:1077
uint8_t mState
Value representing the readiness of this aspect.
Definition sim_system.hpp:873
bool hasAspect() const
Tests whether an aspect of a particular type is attached to the underlying SimObject.
Definition sim_system.hpp:1082
virtual std::string getAspectTypeName() const =0
Overridable function for fetching the aspect type string of an aspect.
bool isAttached()
Tests whether this aspect is attached to a SimObject.
Definition sim_system.hpp:811
void addOrReplaceAspect(const BaseSimObjectAspect &aspect)
Adds or replaces an aspect on the underlying SimObject with a new aspect constructed as a copy of ano...
Definition sim_system.cpp:195
std::map< std::pair< std::string, std::string >, std::shared_ptr< FixedActionBinding >, std::less< std::pair< std::string, std::string > > > mFixedActionBindings
The set of action bindings owned by this aspect.
Definition sim_system.hpp:850
std::weak_ptr< FixedActionBinding > declareFixedActionBinding(const std::string &context, const std::string &action, std::function< bool(const ActionData &, const ActionDefinition &)>)
Binds some method (or any function) present on this object to an action generated by the InputManager...
Definition sim_system.cpp:219
static void registerAspect()
Registers an implementation of an aspect with the SimSystem.
Definition sim_system.hpp:564
bool isActive()
Tests whether the SimObject this aspect is attached to is active on the SceneSystem and the SimSystem...
Definition sim_system.hpp:819
virtual void simulationUpdate(uint32_t simStepMillis)
Overriding this allows an aspect to respond to simulation updates.
Definition sim_system.hpp:514
TComponent getComponent(const float simulationProgress=1.f) const
Gets the value of a component belonging to this object.
Definition sim_system.hpp:1027
void deactivateFixedActionBindings()
Called when an aspect's SimObject has been deactivated, retiring all currently active action bindings...
Definition sim_system.cpp:251
std::weak_ptr< ECSWorld > getWorld() const
Gets a weak reference to the ECSWorld to which our SimObject's entity belongs.
Definition sim_system.cpp:262
void detach()
Causes this aspect to be disconnected from its current SimObject.
Definition sim_system.cpp:177
EntityID getEntityID() const
Gets the ID of the ECSWorld Entity belonging to our SimObject.
Definition sim_system.cpp:120
SimObject & getSimObject()
Returns the sim object that this aspect is attached to.
Definition sim_system.cpp:214
void addAspect(const nlohmann::json &jsonAspectProperties)
Adds a new aspect to the underlying SimObject constructed based on its properties in JSON.
Definition sim_system.cpp:191
virtual void onDeactivated()
Callback for when the aspect is deactivated (just prior to being detached, or when its SimObject has ...
Definition sim_system.hpp:803
virtual void onAttached()
Callback for when an aspect has just been attached to an object (but which hasn't yet been activated)...
Definition sim_system.hpp:785
SimObject * mSimObject
The SimObject underlying this aspect.
Definition sim_system.hpp:856
AspectState
Enum for mask values representing the readiness of an aspect.
Definition sim_system.hpp:862
virtual void variableUpdate(uint32_t variableStepMillis)
Overriding this allows an aspect to respond to variable updates.
Definition sim_system.hpp:523
void attach(SimObject *owner)
Causes this aspect to be detached from its previous owner and to be attached to a new one.
Definition sim_system.cpp:167
virtual std::shared_ptr< BaseSimObjectAspect > clone() const =0
A method which must be overridden to specify how a new aspect should be constructed as a copy of this...
void updateComponent(const TComponent &component)
Updates the value of a component belonging to this object to a new one.
Definition sim_system.hpp:1023
void removeAspect()
Removes an aspect from the underlying SimObject.
Definition sim_system.hpp:1072
bool handleAction(const ActionData &actionData, const ActionDefinition &actionDefinition) override final
Pipes an action received from the InputManager via our SimObject to all that action's handler methods...
Definition sim_system.cpp:245
bool hasComponent()
Tests whether a component of some specific type is present on the object.
Definition sim_system.hpp:1019
static std::shared_ptr< TSystem > getSystemPrototype()
Get the system object of a specific type belonging to the prototype ECSWorld.
Definition ecs_world.hpp:2777
FixedActionBinding(const std::string &context, const std::string &name, std::function< bool(const ActionData &, const ActionDefinition &)> handler)
Constructs a new binding object with the given action name and context, and a reference to the functi...
Definition sim_system.hpp:464
std::string mContext
The name of the context owning the action.
Definition sim_system.hpp:474
std::string mName
The name of the action itself.
Definition sim_system.hpp:480
std::function< bool(const ActionData &, const ActionDefinition &)> mHandler
A reference to the handler interested in receiving the action.
Definition sim_system.hpp:488
bool call(const ActionData &actionData, const ActionDefinition &actionDefinition)
Calls the handler this binding holds a reference to with some newly received action data generated by...
Definition sim_system.hpp:453
Class interface for systems that wish to be notified when action events occur in an action context.
Definition input_system.hpp:301
Helper class for registering a class at program startup.
Definition registrator.hpp:64
The base class for any type whose creation and storage should be managed by the ResourceDatabase.
Definition resource_database.hpp:372
Resource(int explicitlyInitializeMe)
Definition resource_database.hpp:389
void removeComponent()
Removes a component present on this node.
Definition scene_system.hpp:1911
SignalTracker()
Constructs a new SignalTracker object.
std::string getAspectTypeName() const override
Returns the aspect type string associated with TSimObjectAspectDerived.
Definition sim_system.hpp:968
static Registrator< SimObjectAspect< TSimObjectAspectDerived > > & s_registrator
The static helper class instance, responsible for making sure registerSelf() is called during the sta...
Definition sim_system.hpp:984
static void registerSelf()
Registers the new aspect class and its constructor with the SimSystem.
Definition sim_system.hpp:976
A wrapper on entity that allows objects in the Scene to be scriptable.
Definition sim_system.hpp:219
static std::shared_ptr< SimObject > copy(const std::shared_ptr< const SimObject > simObject)
Creates a new SimObject as a copy of another.
Definition sim_system.cpp:19
void copyAspects(const SimObject &other)
Copies all aspects present on another SimObject onto this one.
Definition sim_system.cpp:112
~SimObject() override
Detaches all SimObjectAspects from this SimObject before allowing destruction to proceed.
Definition sim_system.cpp:5
void variableUpdate(uint32_t variableStepMillis)
Calls aspect variable update hooks for attached aspects.
Definition sim_system.cpp:106
void removeAspect()
Removes an aspect of a certain type from this SimObject.
Definition sim_system.hpp:1067
void onDeactivated() override
Notifies all attached aspects that this scene node has stopped being an active part of the scene.
Definition sim_system.cpp:53
std::shared_ptr< SceneNodeCore > clone() const override
Copies this scene object returning one containing all the same components and aspects as it.
Definition sim_system.cpp:23
void onActivated() override
Notifies all attached aspects that this scene node has become an active part of the scene.
Definition sim_system.cpp:48
bool hasAspect() const
Tests whether an aspect of a particular type is attached to this node.
Definition sim_system.hpp:1036
static std::shared_ptr< SimObject > create(const Placement &placement, const std::string &name, TComponents...components)
Creates a new SimObject scene node initialized with some placement and component values.
Definition sim_system.hpp:1004
bool hasAspectWithInterface() const
Tests whether any aspects implementing some base class are present on this node.
Definition sim_system.hpp:1041
void simulationUpdate(uint32_t simStepMillis)
Copy assignment operator.
Definition sim_system.cpp:100
static std::string getResourceTypeName()
Gets the resource type string for the SimObject class.
Definition sim_system.hpp:233
void addAspect(const nlohmann::json &jsonAspectProperties)
Constructs and attaches a new SimObjectAspect to this node based on the aspect's description in JSON.
Definition sim_system.cpp:138
TSimObjectAspect & getAspect()
Gets a reference to a specific aspect present on this node.
Definition sim_system.hpp:1051
std::unordered_map< std::string, std::shared_ptr< BaseSimObjectAspect > > mSimObjectAspects
Aspect name and pointer pairs for all aspects attached to this SimObject.
Definition sim_system.hpp:427
std::vector< std::reference_wrapper< TInterface > > getAspectsWithInterface()
Gets a list of aspects which subclass some base class.
Definition sim_system.hpp:1056
void addOrReplaceAspect(const BaseSimObjectAspect &simObjectAspect)
Adds or replaces an aspect for this node.
Definition sim_system.cpp:130
The SimSystem is a system responsible for providing scriptability via SimObjects and SimObjectAspects...
Definition sim_system.hpp:92
std::shared_ptr< BaseSimObjectAspect > constructAspect(const nlohmann::json &jsonAspectProperties)
Constructs a SimObjectAspect based on its description in JSON.
Definition sim_system.cpp:84
void registerAspect()
Registers a new aspect (a derived class of SimObjectAspect) as an aspect known by the SimSystem.
Definition sim_system.hpp:995
bool aspectRegistered(const std::string &aspectName) const
Tests whether an aspect with a certain name is a valid SimObjectAspect type known by this application...
Definition sim_system.cpp:74
std::unordered_map< std::string, std::shared_ptr< BaseSimObjectAspect >(*)(const nlohmann::json &jsonAspectProperties)> mAspectConstructors
A database for all constructors of SimObjectAspects, provided by the implementation of the aspects th...
Definition sim_system.hpp:154
void onSimulationStep(uint32_t simulationStepMillis) override
The method responsible for forwarding engine simulation step events to SimObjects and their SimObject...
Definition sim_system.cpp:88
static std::string getSystemTypeName()
Gets the system type string associated with this system.
Definition sim_system.hpp:103
void onVariableStep(float simulationProgress, uint32_t variableStepMillis) override
The method responsible for forwarding engine variable step events to SimObjects and their SimObjectAs...
Definition sim_system.cpp:93
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1062
A type of node capable of and responsible for interacting sensibly with the engine's RenderSystem and...
Definition scene_system.hpp:844
ToyMaker Engine's implementation of an ECS system.
T operator()(const T &previousState, const T &nextState, float simulationProgress=1.f) const
Returns an interpolated value for a component between two given states.
Definition ecs_world.hpp:2346
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
Contains the definition for the Registrator<T> utility class, used anywhere that automatic registrati...
Headers relating to resources and their management for a given project.
System classes relating to the SceneSystem, which in some ways lies at the heart of the engine.
Classes relating to this engine's implementation of signals. Contains template classes used to define...
The file containing classes related to the spatial query system, which uses simplified geometrical da...
The definition of a single action, including whether it represents state or change,...
Definition input_data.hpp:511
A component representing the position, rotation, and scale of an entity.
Definition scene_components.hpp:30
A helper intended to get scene nodes and related objects attached to the scene tree.
Definition scene_system.hpp:548
The component associated with the SimSystem.
Definition sim_system.hpp:54
SimObject * mSimObject
Unmanaged pointer to the SimObject this is a component of.
Definition sim_system.hpp:66
static std::string getComponentTypeName()
Gets the component type string for this class.
Definition sim_system.hpp:60
A union that may contain any one of SimpleActionData, OneAxisActionData, TwoAxisActionData,...
Definition input_data.hpp:721