LuaInterface.cpp 3.3 KB

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