Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/system/ComponentManager.h
Show All 18 Lines | |||||
#define INCLUDED_COMPONENTMANAGER | #define INCLUDED_COMPONENTMANAGER | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "simulation2/helpers/Player.h" | #include "simulation2/helpers/Player.h" | ||||
#include "simulation2/system/Components.h" | #include "simulation2/system/Components.h" | ||||
#include "simulation2/system/Entity.h" | #include "simulation2/system/Entity.h" | ||||
#include "simulation2/system/IComponent.h" | #include "simulation2/system/IComponent.h" | ||||
#include "simulation2/system/ComponentDataHolder.h" | |||||
#include <boost/random/linear_congruential.hpp> | #include <boost/random/linear_congruential.hpp> | ||||
#include <map> | #include <map> | ||||
#include <set> | #include <set> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
class IComponent; | class IComponent; | ||||
class CParamNode; | class CParamNode; | ||||
Show All 26 Lines | enum EComponentTypeType | ||||
CT_Script | CT_Script | ||||
}; | }; | ||||
// Representation of a component type, to be used when instantiating components | // Representation of a component type, to be used when instantiating components | ||||
struct ComponentType | struct ComponentType | ||||
{ | { | ||||
EComponentTypeType type; | EComponentTypeType type; | ||||
InterfaceId iid; | InterfaceId iid; | ||||
AllocFunc alloc; | AllocFunc alloc; | ||||
jprahman: nit: Should AllocFunc -> ConstructFunc for symmetry and precision (since allocation is not… | |||||
DeallocFunc dealloc; | DeallocFunc dealloc; | ||||
size_t size; | |||||
size_t align; | |||||
std::string name; | std::string name; | ||||
std::string schema; // RelaxNG fragment | std::string schema; // RelaxNG fragment | ||||
std::unique_ptr<JS::PersistentRootedValue> ctor; // only valid if type == CT_Script | std::unique_ptr<JS::PersistentRootedValue> ctor; // only valid if type == CT_Script | ||||
}; | }; | ||||
std::unordered_map<ComponentTypeId, CComponentDataHolder> m_ComponentData; | |||||
Done Inline ActionsThese are owning pointer right? Why not storing the actual objects? phosit: These are owning pointer right? Why not storing the actual objects? | |||||
public: | public: | ||||
CComponentManager(CSimContext&, std::shared_ptr<ScriptContext> cx, bool skipScriptFunctions = false); | CComponentManager(CSimContext&, std::shared_ptr<ScriptContext> cx, bool skipScriptFunctions = false); | ||||
~CComponentManager(); | ~CComponentManager(); | ||||
void LoadComponentTypes(); | void LoadComponentTypes(); | ||||
/** | /** | ||||
* Load a script and execute it in a new function scope. | * Load a script and execute it in a new function scope. | ||||
* @param filename VFS path to load | * @param filename VFS path to load | ||||
* @param hotload set to true if this script has been loaded before, and redefinitions of | * @param hotload set to true if this script has been loaded before, and redefinitions of | ||||
* existing components should not be considered errors | * existing components should not be considered errors | ||||
*/ | */ | ||||
bool LoadScript(const VfsPath& filename, bool hotload = false); | bool LoadScript(const VfsPath& filename, bool hotload = false); | ||||
void RegisterMessageType(MessageTypeId mtid, const char* name); | void RegisterMessageType(MessageTypeId mtid, const char* name); | ||||
void RegisterComponentType(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema); | void RegisterComponentType(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, size_t size, size_t align, const char*, const std::string& schema); | ||||
void RegisterComponentTypeScriptWrapper(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema); | void RegisterComponentTypeScriptWrapper(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, size_t size, size_t align, const char*, const std::string& schema); | ||||
void MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid); | void MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid); | ||||
/** | /** | ||||
* Subscribe the current component type to the given message type. | * Subscribe the current component type to the given message type. | ||||
* Each component's HandleMessage will be called on any BroadcastMessage of this message type, | * Each component's HandleMessage will be called on any BroadcastMessage of this message type, | ||||
* or on any PostMessage of this type targeted at the component's entity. | * or on any PostMessage of this type targeted at the component's entity. | ||||
* Must only be called by a component type's ClassInit. | * Must only be called by a component type's ClassInit. | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | public: | ||||
IComponent* QueryInterface(entity_id_t ent, InterfaceId iid) const; | IComponent* QueryInterface(entity_id_t ent, InterfaceId iid) const; | ||||
using InterfacePair = std::pair<entity_id_t, IComponent*>; | using InterfacePair = std::pair<entity_id_t, IComponent*>; | ||||
using InterfaceList = std::vector<InterfacePair>; | using InterfaceList = std::vector<InterfacePair>; | ||||
using InterfaceListUnordered = std::unordered_map<entity_id_t, IComponent*>; | using InterfaceListUnordered = std::unordered_map<entity_id_t, IComponent*>; | ||||
InterfaceList GetEntitiesWithInterface(InterfaceId iid) const; | InterfaceList GetEntitiesWithInterface(InterfaceId iid) const; | ||||
const InterfaceListUnordered& GetEntitiesWithInterfaceUnordered(InterfaceId iid) const; | SComponentDataGenerator GetEntitiesWithInterfaceUnordered(InterfaceId iid) const; | ||||
/** | /** | ||||
* Send a message, targeted at a particular entity. The message will be received by any | * Send a message, targeted at a particular entity. The message will be received by any | ||||
* components of that entity which subscribed to the message type, and by any other components | * components of that entity which subscribed to the message type, and by any other components | ||||
* that subscribed globally to the message type. | * that subscribed globally to the message type. | ||||
*/ | */ | ||||
void PostMessage(entity_id_t ent, const CMessage& msg); | void PostMessage(entity_id_t ent, const CMessage& msg); | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | private: | ||||
ComponentTypeId m_CurrentComponent; // used when loading component types | ComponentTypeId m_CurrentComponent; // used when loading component types | ||||
bool m_CurrentlyHotloading; | bool m_CurrentlyHotloading; | ||||
// TODO: some of these should be vectors | // TODO: some of these should be vectors | ||||
std::map<ComponentTypeId, ComponentType> m_ComponentTypesById; | std::map<ComponentTypeId, ComponentType> m_ComponentTypesById; | ||||
std::vector<CComponentManager::ComponentTypeId> m_ScriptedSystemComponents; | std::vector<CComponentManager::ComponentTypeId> m_ScriptedSystemComponents; | ||||
std::vector<std::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId | std::vector<std::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId | ||||
std::vector<CComponentDataHolder*> m_ComponentDataByInterface; // indexed by InterfaceId | |||||
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId; | std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId; | ||||
std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions; | std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions; | ||||
std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions; | std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions; | ||||
std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName; | std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName; | ||||
std::map<std::string, MessageTypeId> m_MessageTypeIdsByName; | std::map<std::string, MessageTypeId> m_MessageTypeIdsByName; | ||||
std::map<MessageTypeId, std::string> m_MessageTypeNamesById; | std::map<MessageTypeId, std::string> m_MessageTypeNamesById; | ||||
std::map<std::string, InterfaceId> m_InterfaceIdsByName; | std::map<std::string, InterfaceId> m_InterfaceIdsByName; | ||||
Show All 20 Lines |
Wildfire Games · Phabricator
nit: Should AllocFunc -> ConstructFunc for symmetry and precision (since allocation is not handled by the pool allocator)?