system.d 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. module scripts.lua.system;
  2. import bindbc.lua;
  3. import raylib;
  4. import variables;
  5. import graphics.effects;
  6. import std.conv;
  7. import system.debugwriteln;
  8. import system.config;
  9. import std.string;
  10. import std.algorithm;
  11. import system.init;
  12. import graphics.playback;
  13. import std.file;
  14. import graphics.collision;
  15. import audio.sfx;
  16. import scripts.hbs;
  17. import system.hpf;
  18. extern (C) nothrow int luaL_3dSwitcher(lua_State *L) {
  19. bool value = cast(bool)luaL_checkinteger(L, 1);
  20. is3dEnabled = value;
  21. return 0;
  22. }
  23. extern (C) nothrow int luaL_2dSwitcher(lua_State *L) {
  24. bool value = cast(bool)luaL_checkinteger(L, 1);
  25. is2dEnabled = value;
  26. return 0;
  27. }
  28. extern (C) nothrow int luaL_print(lua_State *L) {
  29. try {
  30. string lineToPrint = to!string(luaL_checkstring(L, 1));
  31. debugWriteln(lineToPrint);
  32. } catch (Exception e) {
  33. debugWriteln(e.msg);
  34. }
  35. return 0;
  36. }
  37. extern (C) nothrow int luaL_showCursor(lua_State *L) {
  38. ShowCursor();
  39. return 0;
  40. }
  41. extern (C) nothrow int luaL_hideCursor(lua_State *L) {
  42. HideCursor();
  43. return 0;
  44. }
  45. extern (C) nothrow int luaL_setMousePosition(lua_State *L) {
  46. SetMousePosition(cast(int)luaL_checkinteger(L, 1), cast(int)luaL_checkinteger(L, 2));
  47. return 0;
  48. }
  49. extern (C) nothrow int luaL_drawFPS(lua_State *L) {
  50. DrawFPS(cast(int)luaL_checkinteger(L, 1), cast(int)luaL_checkinteger(L, 2));
  51. return 0;
  52. }
  53. extern (C) nothrow int luaL_getFPS(lua_State *L) {
  54. lua_pushinteger(L, GetFPS());
  55. return 1;
  56. }
  57. extern (C) nothrow int luaL_getFrameTime(lua_State *L) {
  58. lua_pushnumber(L, GetFrameTime());
  59. return 1;
  60. }
  61. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  62. {
  63. lua_pushinteger(L, GetScreenWidth());
  64. return 1;
  65. }
  66. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  67. {
  68. lua_pushinteger(L, GetScreenHeight());
  69. return 1;
  70. }
  71. extern (C) nothrow int luaL_unloadFont(lua_State *L) {
  72. UnloadFont(textFont);
  73. return 0;
  74. }
  75. extern (C) nothrow int luaL_loadFont(lua_State* L)
  76. {
  77. const char* x = luaL_checkstring(L, 1);
  78. debugWriteln("Setting custom font: ", x.to!string);
  79. int[512] codepoints = 0;
  80. //configuring both cyrillic and latin fonts if available
  81. foreach (i; 0 .. 95)
  82. {
  83. codepoints[i] = 32 + i;
  84. }
  85. foreach (i; 0 .. 255)
  86. {
  87. codepoints[96 + i] = 0x400 + i;
  88. }
  89. int fontSize = max(10, cast(int)(40 * scale));
  90. textFont = LoadFontEx(x, fontSize, null, 0);
  91. return 0;
  92. }
  93. extern (C) nothrow int luaL_getTime(lua_State* L)
  94. {
  95. //getTime() returns current time.
  96. lua_pushnumber(L, GetTime());
  97. return 1;
  98. }
  99. extern (C) nothrow int luaL_getDeltaTime(lua_State* L)
  100. {
  101. //getDeltaTime
  102. lua_pushnumber(L, GetFrameTime());
  103. return 1;
  104. }
  105. extern (C) nothrow int luaL_loadScript(lua_State* L)
  106. {
  107. try
  108. {
  109. luaExec = luaL_checkstring(L, 1).to!string;
  110. }
  111. catch (Exception e)
  112. {
  113. debugWriteln(e.msg);
  114. }
  115. luaReload = true;
  116. return 0;
  117. }
  118. extern (C) nothrow int luaL_setGameState(lua_State *L) {
  119. currentGameState = cast(int)luaL_checkinteger(L, 1);
  120. return 0;
  121. }
  122. extern (C) nothrow int luaL_setCamera(lua_State *L) {
  123. camera.position = Vector3(
  124. luaL_checknumber(L, 1),
  125. luaL_checknumber(L, 2),
  126. luaL_checknumber(L, 3)
  127. );
  128. camera.target = Vector3(
  129. luaL_checknumber(L, 4),
  130. luaL_checknumber(L, 5),
  131. luaL_checknumber(L, 6)
  132. );
  133. return 0;
  134. }
  135. extern (C) nothrow int luaL_clearBackground(lua_State *L) {
  136. ClearBackground(Colors.BLACK);
  137. return 0;
  138. }
  139. extern (C) nothrow int luaL_loadArchive(lua_State *L) {
  140. try {
  141. int indexOfArchive = cast(int)luaL_checkinteger(L, 1);
  142. string archiveName = to!string(cast(char*)luaL_checkstring(L, 2));
  143. parsedChunks.length += 1;
  144. archivesNames.length += 1;
  145. parsedChunks[indexOfArchive] ~= parseArchive(archiveName);
  146. archivesNames[indexOfArchive] ~= archiveName;
  147. } catch (Exception e) {
  148. debugWriteln("exception in luaL_loadArchive");
  149. }
  150. return 0;
  151. }