ScriptObject.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. #include "InheritanceObjects.h"
  3. #include "LuaComponent.h"
  4. #include "System.h"
  5. enum ScriptComponentType : std::size_t
  6. {
  7. ScriptComponentType_LUA = 0,
  8. ScriptComponentType_NumOfComponents
  9. };
  10. class ScriptObject : public SystemObject, public SpatialDataManagerObject, public LoadableGraphicsObject
  11. {
  12. public:
  13. ScriptObject(SystemScene *p_systemScene, const std::string &p_name)
  14. : SystemObject(p_systemScene, p_name, Properties::ScriptObject)
  15. {
  16. m_luaComponent = nullptr;
  17. }
  18. ~ScriptObject()
  19. {
  20. // Iterate over all component types and delete components if they have been created
  21. for(std::size_t i = 0; i < ScriptComponentType::ScriptComponentType_NumOfComponents; i++)
  22. removeComponent(static_cast<ScriptComponentType>(i));
  23. }
  24. ErrorCode init() { return ErrorCode::Success; }
  25. void loadToMemory()
  26. {
  27. if(luaComponentPresent())
  28. m_luaComponent->loadToMemory();
  29. setActive(true);
  30. setLoadedToMemory(true);
  31. setLoadedToVideoMemory(true);
  32. }
  33. ErrorCode importObject(const PropertySet &p_properties)
  34. {
  35. ErrorCode returnError = ErrorCode::Success;
  36. // Check if the property set is valid and the script object hasn't been loaded already
  37. if(p_properties)
  38. {
  39. if(!isLoadedToMemory())
  40. {
  41. // Check if there is a property set for camera and load the camera component if there is
  42. auto const &luaComponentProperty = p_properties.getPropertySetByID(Properties::Lua);
  43. if(luaComponentProperty)
  44. {
  45. // Create the lua component
  46. addComponent(new LuaComponent(m_systemScene, m_name + Config::componentVar().lua_component_name));
  47. // Try to initialize the lua component
  48. auto componentInitError = m_luaComponent->init();
  49. if(componentInitError == ErrorCode::Success)
  50. {
  51. // Try to import the component
  52. auto const &componentImportError = m_luaComponent->importObject(luaComponentProperty);
  53. // Remove the component if it failed to import
  54. if(componentImportError != ErrorCode::Success)
  55. {
  56. removeComponent(ScriptComponentType::ScriptComponentType_LUA);
  57. ErrHandlerLoc().get().log(componentImportError, ErrorSource::Source_CameraComponent, m_name);
  58. }
  59. }
  60. else // Remove the component if it failed to initialize
  61. {
  62. removeComponent(ScriptComponentType::ScriptComponentType_LUA);
  63. ErrHandlerLoc().get().log(componentInitError, ErrorSource::Source_CameraComponent, m_name);
  64. }
  65. }
  66. }
  67. }
  68. else
  69. {
  70. returnError = ErrorCode::Failure;
  71. }
  72. return returnError;
  73. }
  74. PropertySet exportObject()
  75. {
  76. // If there are components present, export each one; if there are no components, return an empty propertySet
  77. //if(containsComponents())
  78. //{
  79. // PropertySet exportPropertySet(Properties::Rendering);
  80. // //if(cameraComponentPresent())
  81. // // exportPropertySet.addPropertySet(m_cameraComponent->exportObject());
  82. // //if(lightComponentPresent())
  83. // // exportPropertySet.addPropertySet(m_lightComponent->exportObject());
  84. // //if(modelComponentPresent())
  85. // // exportPropertySet.addPropertySet(m_modelComponent->exportObject());
  86. // //if(shaderComponentPresent())
  87. // // exportPropertySet.addPropertySet(m_shaderComponent->exportObject());
  88. // return exportPropertySet;
  89. //}
  90. //else
  91. return PropertySet();
  92. }
  93. void update(const float p_deltaTime)
  94. {
  95. if(!isLoadedToVideoMemory())
  96. {
  97. performCheckIsLoadedToVideoMemory();
  98. if(!isLoadedToVideoMemory())
  99. return;
  100. }
  101. if(luaComponentPresent())
  102. m_luaComponent->update(p_deltaTime);
  103. if(hasSpatialDataUpdated())
  104. {
  105. }
  106. if(isUpdateNeeded())
  107. {
  108. // Calculate model matrix
  109. //m_worldSpace.m_transformMat = Math::createTransformMat(m_worldSpace.m_spatialData.m_position, m_worldSpace.m_spatialData.m_rotationEuler, m_worldSpace.m_spatialData.m_scale);
  110. // Update components
  111. //if(modelComponentPresent())
  112. // m_modelComponent->update(p_deltaTime);
  113. //if(shaderComponentPresent())
  114. // m_shaderComponent->update(p_deltaTime);
  115. //if(lightComponentPresent())
  116. // m_lightComponent->update(p_deltaTime);
  117. // Mark as updated
  118. updatePerformed();
  119. }
  120. }
  121. // Assign a pointer to a const SpatialDataChangeManager, so the object can use it for its spatial data
  122. // Also assigns the pointer to every component that needs it
  123. virtual void setSpatialDataManagerReference(const SpatialDataManager &p_spatialData)
  124. {
  125. SpatialDataManagerObject::setSpatialDataManagerReference(p_spatialData);
  126. if(luaComponentPresent())
  127. m_luaComponent->setSpatialDataManagerReference(*m_spatialData);
  128. }
  129. // System type is Graphics
  130. BitMask getSystemType() { return Systems::Script; }
  131. virtual BitMask getDesiredSystemChanges() { return Systems::Changes::Graphics::All; }
  132. virtual BitMask getPotentialSystemChanges() { return Systems::Changes::Graphics::All; }
  133. inline LuaComponent *getLuaComponent() { return m_luaComponent; }
  134. inline const bool luaComponentPresent() const { return (m_luaComponent == nullptr) ? false : true; }
  135. virtual void changeOccurred(ObservedSubject *p_subject, BitMask p_changeType)
  136. {
  137. // Track what data has been modified
  138. BitMask newChanges = Systems::Changes::None;
  139. // If any data has been updated, post the changes to listeners
  140. if(newChanges != Systems::Changes::None)
  141. {
  142. postChanges(newChanges);
  143. }
  144. }
  145. void addComponent(LuaComponent *p_component)
  146. {
  147. // Make sure that this component isn't assigned already
  148. removeComponent(ScriptComponentType::ScriptComponentType_LUA);
  149. m_luaComponent = p_component;
  150. // Share the ScriptObjects spatial data with the component
  151. m_luaComponent->setSpatialDataManagerReference(*m_spatialData);
  152. // Set the flag for the lua component, so it is known from the flag that there is one currently present
  153. m_componentsFlag |= Systems::ScriptObjectComponents::Lua;
  154. }
  155. void removeComponent(const ScriptComponentType p_compType)
  156. {
  157. switch(p_compType)
  158. {
  159. case ScriptComponentType::ScriptComponentType_LUA:
  160. {
  161. if(m_luaComponent != nullptr)
  162. {
  163. // Delete the actual component
  164. delete m_luaComponent;
  165. // Assign the component pointer as nullptr to denote that it has been removed
  166. m_luaComponent = nullptr;
  167. // Remove the bit corresponding to lua component from the componentsFlag bitmask
  168. m_componentsFlag &= ~Systems::ScriptObjectComponents::Lua;
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. // Returns true if the graphics object contains any components
  175. inline const bool containsComponents()
  176. {
  177. if(luaComponentPresent())
  178. return true;
  179. return false;
  180. }
  181. private:
  182. // Components
  183. LuaComponent *m_luaComponent;
  184. // Stores a separate flag for each component currently present
  185. BitMask m_componentsFlag;
  186. };