ScriptManagerBinds.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "Console.h"
  2. #include "Logger.h"
  3. #include "Entity.h"
  4. #include "EntityManager.h"
  5. #include "Timer.h"
  6. #include "UiManager.h"
  7. #include "UiWidget.h"
  8. #include "XmlMapFile.h"
  9. #include "XmlMapsFile.h"
  10. #include "modules/worldmap/WorldMapModule.h"
  11. void
  12. ScriptPrint( const char* text )
  13. {
  14. Console::getSingleton().AddTextToOutput( text );
  15. }
  16. void
  17. ScriptMap( const char* text )
  18. {
  19. EntityManager::getSingleton().Clear();
  20. XmlMapsFile xml( "./data/maps.xml" );
  21. Ogre::String file_name = xml.GetMapFileNameByName( text );
  22. XmlMapFile xml_map( "./data/" + file_name );
  23. xml_map.LoadMap();
  24. }
  25. void
  26. ScriptConsole( const char* text )
  27. {
  28. Console::getSingleton().ExecuteCommand( text );
  29. }
  30. void
  31. ScriptManager::InitBinds()
  32. {
  33. // globals
  34. luabind::module( m_LuaState )
  35. [
  36. luabind::def( "print", ( void( * )( const char* ) ) &ScriptPrint ),
  37. luabind::def( "map", ( void( * )( const char* ) ) &ScriptMap ),
  38. luabind::def( "console", ( void( * )( const char* ) ) &ScriptConsole )
  39. ];
  40. // entity access
  41. luabind::module( m_LuaState )
  42. [
  43. luabind::class_< Entity >( "Entity" )
  44. .def( "set_position", ( void( Entity::* )( const float, const float, const float ) ) &Entity::ScriptSetPosition )
  45. .def( "get_position", ( void( Entity::* )() ) &Entity::ScriptGetPosition ) // return 3 values internaly
  46. .def( "set_rotation", ( void( Entity::* )( const float ) ) &Entity::ScriptSetRotation )
  47. .def( "get_rotation", ( float( Entity::* )() ) &Entity::ScriptGetRotation )
  48. .def( "set_solid_radius", ( void( Entity::* )( const float ) ) &Entity::SetSolidRadius )
  49. .def( "get_solid_radius", ( float( Entity::* )() ) &Entity::GetSolidRadius )
  50. .def( "set_solid", ( void( Entity::* )( const bool ) ) &Entity::SetSolid )
  51. .def( "is_solid", ( bool( Entity::* )() ) &Entity::IsSolid )
  52. .def( "set_talk_radius", ( void( Entity::* )( const float ) ) &Entity::SetTalkRadius )
  53. .def( "get_talk_radius", ( float( Entity::* )() ) &Entity::GetTalkRadius )
  54. .def( "set_talkable", ( void( Entity::* )( const bool ) ) &Entity::SetTalkable )
  55. .def( "is_talkable", ( bool( Entity::* )() ) &Entity::IsTalkable )
  56. .def( "set_visible", ( void( Entity::* )( const bool ) ) &Entity::SetVisible )
  57. .def( "is_visible", ( bool( Entity::* )() ) &Entity::IsVisible )
  58. .def( "set_move_auto_speed", ( void( Entity::* )( const float ) ) &Entity::SetMoveAutoSpeed )
  59. .def( "get_move_auto_speed", ( float( Entity::* )() ) &Entity::GetMoveAutoSpeed )
  60. .def( "get_move_triangle_id", ( int( Entity::* )() ) &Entity::GetMoveTriangleId )
  61. .def( "move_auto_rotation", ( void( Entity::* )( const bool ) ) &Entity::SetMoveAutoRotation )
  62. .def( "move_auto_animation", ( void( Entity::* )( const bool ) ) &Entity::SetMoveAutoAnimation )
  63. .def( "move_to_position", ( void( Entity::* )( const float, const float ) ) &Entity::ScriptMoveToPosition )
  64. .def( "move_to_entity", ( void( Entity::* )( Entity* ) ) &Entity::ScriptMoveToEntity )
  65. .def( "move_sync", ( int( Entity::* )() ) &Entity::ScriptMoveSync, luabind::yield )
  66. .def( "linear_to_position", ( void( Entity::* )( const float, const float, const float, const LinearMovement, const char* ) ) &Entity::ScriptLinearToPosition )
  67. .def( "linear_sync", ( int( Entity::* )() ) &Entity::ScriptLinearSync, luabind::yield )
  68. .def( "jump_to_position", ( void( Entity::* )( const float, const float, const float, const float ) )&Entity::ScriptJumpToPosition)
  69. .def( "jump_sync", ( int( Entity::* )() ) &Entity::ScriptJumpSync, luabind::yield )
  70. .def( "offset_to_position", ( void( Entity::* )( const float, const float, const float, const ActionType, const float ) ) &Entity::ScriptOffsetToPosition )
  71. .def( "offset_sync", ( int( Entity::* )() ) &Entity::ScriptOffsetSync, luabind::yield )
  72. .def( "turn_to_entity", ( void( Entity::* )( Entity*, const TurnDirection, const float ) )&Entity::ScriptTurnToEntity )
  73. .def( "turn_to_direction", ( void( Entity::* )( const float, const TurnDirection, const ActionType, const float ) )&Entity::ScriptTurnToDirection )
  74. .def( "turn_sync", ( int( Entity::* )() ) &Entity::ScriptTurnSync, luabind::yield )
  75. .def( "set_animation_speed", ( void( Entity::* )( const float ) ) &Entity::ScriptSetAnimationSpeed )
  76. .def( "play_animation", ( void( Entity::* )( const char* ) ) &Entity::ScriptPlayAnimation )
  77. .def( "play_animation_stop", ( void( Entity::* )( const char* ) ) &Entity::ScriptPlayAnimationStop )
  78. .def( "play_animation", ( void( Entity::* )( const char*, const float, const float ) ) &Entity::ScriptPlayAnimation )
  79. .def( "play_animation_stop", ( void( Entity::* )( const char*, const float, const float ) ) &Entity::ScriptPlayAnimationStop )
  80. .def( "set_default_animation", ( void( Entity::* )( const char* ) ) &Entity::ScriptSetDefaultAnimation )
  81. .def( "animation_sync", ( int( Entity::* )()) &Entity::ScriptAnimationSync, luabind::yield )
  82. .enum_( "constants" )
  83. [
  84. luabind::value( "NONE", AT_NONE ),
  85. luabind::value( "LINEAR", AT_LINEAR ),
  86. luabind::value( "SMOOTH", AT_SMOOTH ),
  87. luabind::value( "CLOCKWISE", TD_CLOCKWISE ),
  88. luabind::value( "ANTICLOCKWISE", TD_ANTICLOCKWISE ),
  89. luabind::value( "CLOSEST", TD_CLOSEST ),
  90. luabind::value( "UP_TO_DOWN", LM_UP_TO_DOWN ),
  91. luabind::value( "DOWN_TO_UP", LM_DOWN_TO_UP ),
  92. luabind::value( "LEFT_TO_RIGHT", LM_LEFT_TO_RIGHT ),
  93. luabind::value( "RIGHT_TO_LEFT", LM_RIGHT_TO_LEFT )
  94. ]
  95. ];
  96. // game access
  97. luabind::module( m_LuaState )
  98. [
  99. luabind::class_< EntityPoint >( "EntityPoint" )
  100. .def( "get_position", ( void( EntityPoint::* )() ) &EntityPoint::ScriptGetPosition ) // return 3 values internaly
  101. .def( "get_rotation", ( float( EntityPoint::* )() ) &EntityPoint::ScriptGetRotation )
  102. ];
  103. // game access
  104. luabind::module( m_LuaState )
  105. [
  106. luabind::class_< EntityManager >( "EntityManager" )
  107. .def( "set_paused", ( void( EntityManager::* )( const bool ) ) &EntityManager::ScriptSetPaused )
  108. .def( "add_entity", ( void( EntityManager::* )( const char*, const char*, const float, const float, const float, const float ) ) &EntityManager::ScriptAddEntity )
  109. .def( "add_entity_script", ( void( EntityManager::* )( const char* ) ) &EntityManager::ScriptAddEntityScript )
  110. .def( "get_entity", ( Entity*( EntityManager::* )( const char* ) ) &EntityManager::ScriptGetEntity )
  111. .def( "get_entity_point", ( EntityPoint*( EntityManager::* )( const char* ) ) &EntityManager::ScriptGetEntityPoint )
  112. .def( "set_player_entity", ( void( EntityManager::* )( const char* ) ) &EntityManager::ScriptSetPlayerEntity )
  113. .def( "unset_player_entity", ( void( EntityManager::* )() ) &EntityManager::ScriptUnsetPlayerEntity )
  114. .def( "player_lock", ( void( EntityManager::* )( const bool ) ) &EntityManager::ScriptPlayerLock )
  115. ];
  116. // 2d background access
  117. luabind::module( m_LuaState )
  118. [
  119. luabind::class_< Background2D >( "Background2D" )
  120. .def( "autoscroll_to_entity", ( void( Background2D::* )( Entity* ) ) &Background2D::ScriptAutoScrollToEntity )
  121. .def( "scroll_to_position", ( void( Background2D::* )( const float, const float, const Background2D::ScrollType, const float ) ) &Background2D::ScriptScrollToPosition )
  122. .def( "scroll_sync", ( int( Background2D::* )() ) &Background2D::ScriptScrollSync, luabind::yield )
  123. .def( "play_animation_looped", ( void( Background2D::* )( const char* ) ) &Background2D::ScriptPlayAnimationLooped )
  124. .def( "play_animation_once", ( void( Background2D::* )( const char* ) ) &Background2D::ScriptPlayAnimationOnce )
  125. .def( "animation_sync", ( int( Background2D::* )( const char* ) ) &Background2D::ScriptAnimationSync, luabind::yield )
  126. .enum_( "constants" )
  127. [
  128. luabind::value( "NONE", AT_NONE ),
  129. luabind::value( "LINEAR", AT_LINEAR ),
  130. luabind::value( "SMOOTH", AT_SMOOTH )
  131. ]
  132. ];
  133. // ui widget access
  134. luabind::module( m_LuaState )
  135. [
  136. luabind::class_< UiWidget >( "UiWidget" )
  137. .def( "set_visible", ( void( UiWidget::* )( const bool ) ) &UiWidget::SetVisible )
  138. .def( "is_visible", ( bool( UiWidget::* )() ) &UiWidget::IsVisible )
  139. .def( "play_animation", ( void( UiWidget::* )( const char* ) ) &UiWidget::ScriptPlayAnimation )
  140. .def( "play_animation_stop", ( void( UiWidget::* )( const char* ) ) &UiWidget::ScriptPlayAnimationStop )
  141. .def( "play_animation", ( void( UiWidget::* )( const char*, const float, const float ) ) &UiWidget::ScriptPlayAnimation )
  142. .def( "play_animation_stop", ( void( UiWidget::* )( const char*, const float, const float ) ) &UiWidget::ScriptPlayAnimationStop )
  143. .def( "set_default_animation", ( void( UiWidget::* )( const char* ) ) &UiWidget::ScriptSetDefaultAnimation )
  144. .def( "animation_sync", ( int( UiWidget::* )() ) &UiWidget::ScriptAnimationSync, luabind::yield )
  145. .def( "set_colour", ( void( UiWidget::* )( const float, const float, const float ) ) &UiWidget::SetColour )
  146. .def( "set_alpha", ( void( UiWidget::* )( const float ) ) &UiWidget::SetAlpha )
  147. .def( "set_z", ( void( UiWidget::* )( const float ) ) &UiWidget::SetZ )
  148. ];
  149. // ui access
  150. luabind::module( m_LuaState )
  151. [
  152. luabind::class_< UiManager >( "UiManager" )
  153. .def( "get_widget", ( UiWidget*( UiManager::* )( const char* ) ) &UiManager::ScriptGetWidget )
  154. ];
  155. // timer access
  156. luabind::module( m_LuaState )
  157. [
  158. luabind::class_< Timer >( "Timer" )
  159. .def( "get_game_time_total", ( float( Timer::* )() ) &Timer::GetGameTimeTotal )
  160. ];
  161. // script access
  162. luabind::module( m_LuaState )
  163. [
  164. luabind::class_< ScriptManager >( "Script" )
  165. .def( "wait", ( int( ScriptManager::* )( const float ) ) &ScriptManager::ScriptWait, luabind::yield )
  166. .def( "request", ( void( ScriptManager::* )( const ScriptManager::Type, const char*, const char*, const int ) ) &ScriptManager::ScriptRequest )
  167. .def( "request_start_sync", ( int( ScriptManager::* )( const ScriptManager::Type, const char*, const char*, const int ) ) &ScriptManager::ScriptRequestStartSync, luabind::yield )
  168. .def( "request_end_sync", ( int( ScriptManager::* )( const ScriptManager::Type, const char*, const char*, const int ) ) &ScriptManager::ScriptRequestEndSync, luabind::yield )
  169. .enum_( "constants" )
  170. [
  171. luabind::value( "SYSTEM", ScriptManager::SYSTEM ),
  172. luabind::value( "ENTITY", ScriptManager::ENTITY ),
  173. luabind::value( "UI", ScriptManager::UI )
  174. ]
  175. ];
  176. luabind::module( m_LuaState )
  177. [
  178. luabind::class_< QGears::WorldMapModule >( "world_map_module" )
  179. .def( "init", ( void( QGears::WorldMapModule::* )() ) &QGears::WorldMapModule::Init )
  180. ];
  181. luabind::globals( m_LuaState )[ "entity_manager" ] = boost::ref( *( EntityManager::getSingletonPtr() ) );
  182. luabind::globals( m_LuaState )[ "background2d" ] = boost::ref( *( EntityManager::getSingletonPtr()->GetBackground2D() ) );
  183. luabind::globals( m_LuaState )[ "ui_manager" ] = boost::ref( *( UiManager::getSingletonPtr() ) );
  184. luabind::globals( m_LuaState )[ "world_map_module" ] = boost::ref( *( QGears::WorldMapModule::getSingletonPtr() ) );
  185. luabind::globals( m_LuaState )[ "timer" ] = boost::ref( *( Timer::getSingletonPtr() ) );
  186. luabind::globals( m_LuaState )[ "script" ] = boost::ref( *this );
  187. }