ScriptManager.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef SCRIPT_MANAGER_H
  2. #define SCRIPT_MANAGER_H
  3. #include <OgreSingleton.h>
  4. #include <OgreString.h>
  5. #include "Event.h"
  6. extern "C"
  7. {
  8. #include <lua/lua.h>
  9. }
  10. #include <luabind/luabind.hpp>
  11. class Entity;
  12. struct ScriptId
  13. {
  14. ScriptId(): entity( "" ), function( "" ){}
  15. Ogre::String entity;
  16. Ogre::String function;
  17. };
  18. struct ScriptEntity;
  19. struct QueueScript
  20. {
  21. QueueScript():
  22. function( "" ),
  23. argument1( "" ),
  24. argument2( "" ),
  25. priority( 0 ),
  26. state( NULL ),
  27. seconds_to_wait( 0 ),
  28. wait( false ),
  29. yield( false )
  30. {}
  31. Ogre::String function;
  32. Ogre::String argument1;
  33. Ogre::String argument2;
  34. int priority;
  35. lua_State* state;
  36. int state_id; // for storing and deleating thread
  37. float seconds_to_wait;
  38. bool wait;
  39. bool yield;
  40. ScriptId paused_script_start; // script paused by call of this script.
  41. ScriptId paused_script_end; // script paused by call of this script.
  42. };
  43. class ScriptManager : public Ogre::Singleton< ScriptManager >
  44. {
  45. public:
  46. enum Type
  47. {
  48. SYSTEM,
  49. ENTITY,
  50. UI
  51. };
  52. ScriptManager();
  53. virtual ~ScriptManager();
  54. void Input( const Event& event );
  55. void Update( const Type type );
  56. void RunString( const Ogre::String& lua );
  57. void RunFile( const Ogre::String& file );
  58. // binds
  59. void InitBinds();
  60. void InitCmd();
  61. void AddEntity( const Type type, const Ogre::String& entity_name, Entity* entity );
  62. void RemoveEntity( const Type type, const Ogre::String& entity_name );
  63. void AddEntityScript( const Ogre::String& entity_name, const Ogre::String& function_name, int priority );
  64. void RemoveEntityTopScript( ScriptEntity& entity );
  65. luabind::object GetTableByEntityName( const ScriptManager::Type type, const Ogre::String& name, lua_State* state ) const;
  66. QueueScript* GetScriptByScriptId( const ScriptId& script ) const;
  67. ScriptEntity* GetScriptEntityByName( const Type type, const Ogre::String& entity_name ) const;
  68. const ScriptId GetCurrentScriptId() const;
  69. void ContinueScriptExecution( const ScriptId& script );
  70. int ScriptWait( const float seconds );
  71. void ScriptRequest( const Type type, const char* entity, const char* function, const int priority );
  72. int ScriptRequestStartSync( const Type type, const char* entity, const char* function, const int priority );
  73. int ScriptRequestEndSync( const Type type, const char* entity, const char* function, const int priority );
  74. bool ScriptRequest( ScriptEntity* script_entity, const Ogre::String& function, const int priority, const Ogre::String& argument1, const Ogre::String& argument2, bool start_sync, bool end_sync );
  75. void AddValueToStack( const float value );
  76. private:
  77. lua_State* m_LuaState;
  78. Ogre::String m_SystemTableName;
  79. Ogre::String m_EntityTableName;
  80. Ogre::String m_UiTableName;
  81. std::vector< ScriptEntity > m_ScriptEntity;
  82. ScriptId m_CurrentScriptId;
  83. };
  84. struct ScriptEntity
  85. {
  86. ScriptEntity():
  87. name( "" ),
  88. type( ScriptManager::SYSTEM ),
  89. resort( false )
  90. {
  91. }
  92. Ogre::String name;
  93. ScriptManager::Type type;
  94. std::vector< QueueScript > queue;
  95. bool resort;
  96. };
  97. #endif // SCRIPT_MANAGER_H