LuaInterface.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "LuaInterface.h"
  2. #include <RmlUi/Core/Lua/LuaType.h>
  3. #include <RmlUi/Core/Lua/lua.hpp>
  4. #include "Game.h"
  5. #include "GameDetails.h"
  6. #include <RmlUi/Core/Log.h>
  7. #include <Shell.h>
  8. #include <RmlUi/Core/Factory.h>
  9. #include "HighScores.h"
  10. #include <RmlUi/Core/Types.h>
  11. #include "ElementGameInstancer.h"
  12. //we have to create the binding ourselves, and these are the functions that will be
  13. //called. It has to match the function signature of int (*ftnptr)(lua_State*)
  14. int GameShutdown(lua_State* L);
  15. int GameSetPaused(lua_State* L);
  16. int GameSetDifficulty(lua_State* L);
  17. int GameSetDefenderColour(lua_State* L);
  18. int GameSubmitHighScore(lua_State* L);
  19. int GameSetHighScoreName(lua_State* L);
  20. static ElementGameInstancer game_instancer;
  21. void LuaInterface::Initialise(lua_State* L)
  22. {
  23. InitGame(L);
  24. Rml::Core::Factory::RegisterElementInstancer("game", &game_instancer);
  25. }
  26. void LuaInterface::InitGame(lua_State *L)
  27. {
  28. luaL_dostring(L,"Game = Game or {}"); //doing this in Lua because it would be 10+ lines in C++
  29. lua_getglobal(L,"Game");
  30. int game = lua_gettop(L);
  31. if(lua_isnil(L,game))
  32. {
  33. Rml::Core::Log::Message(Rml::Core::Log::LT_ERROR, "Error creating the Game table from C++ in LuaInterface::InitGame");
  34. return;
  35. }
  36. //this can be done in a loop similar to the implementation of LuaType::_regfunctions, but I am
  37. //defining them explicitly so that there is no black magic
  38. lua_pushcfunction(L,GameShutdown);
  39. lua_setfield(L, game, "Shutdown");
  40. lua_pushcfunction(L,GameSetPaused);
  41. lua_setfield(L,game,"SetPaused");
  42. lua_pushcfunction(L,GameSetDifficulty);
  43. lua_setfield(L,game,"SetDifficulty");
  44. lua_pushcfunction(L,GameSetDefenderColour);
  45. lua_setfield(L,game,"SetDefenderColour");
  46. lua_pushcfunction(L,GameSubmitHighScore);
  47. lua_setfield(L,game,"SubmitHighScore");
  48. lua_pushcfunction(L,GameSetHighScoreName);
  49. lua_setfield(L,game,"SetHighScoreName");
  50. lua_newtable(L); //table, Game
  51. lua_pushinteger(L,GameDetails::HARD); //int,table,Game
  52. lua_setfield(L,-2,"HARD");//table,Game
  53. lua_pushinteger(L,GameDetails::EASY); //int,table,Game
  54. lua_setfield(L,-2,"EASY"); //table,Game
  55. lua_setfield(L,game,"difficulty"); //Game
  56. lua_pop(L,1); //pop Game
  57. }
  58. int GameShutdown(lua_State* L)
  59. {
  60. Shell::RequestExit();
  61. return 0;
  62. }
  63. int GameSetPaused(lua_State* L)
  64. {
  65. bool paused = CHECK_BOOL(L,1); //CHECK_BOOL defined in LuaType.h
  66. GameDetails::SetPaused(paused);
  67. return 0;
  68. }
  69. int GameSetDifficulty(lua_State* L)
  70. {
  71. int difficulty = (int)luaL_checkinteger(L,1);
  72. GameDetails::SetDifficulty((GameDetails::Difficulty)difficulty);
  73. return 0;
  74. }
  75. int GameSetDefenderColour(lua_State* L)
  76. {
  77. Rml::Core::Colourb* colour = Rml::Core::Lua::LuaType<Rml::Core::Colourb>::check(L,1);
  78. GameDetails::SetDefenderColour(*colour);
  79. return 0;
  80. }
  81. int GameSubmitHighScore(lua_State* L)
  82. {
  83. int score = GameDetails::GetScore();
  84. if(score > 0)
  85. {
  86. // Submit the score the player just got to the high scores chart.
  87. HighScores::SubmitScore(GameDetails::GetDefenderColour(),GameDetails::GetWave(), GameDetails::GetScore());
  88. // Reset the score so the chart won't get confused next time we enter.
  89. GameDetails::ResetScore();
  90. }
  91. return 0;
  92. }
  93. int GameSetHighScoreName(lua_State* L)
  94. {
  95. const char* name = luaL_checkstring(L,1);
  96. HighScores::SubmitName(name);
  97. return 0;
  98. }