models.d 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module scripts.lua.models;
  2. import bindbc.lua;
  3. import raylib;
  4. import variables;
  5. import graphics.effects;
  6. import std.conv;
  7. import system.abstraction;
  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. extern (C) nothrow int luaL_loadModel(lua_State *L) {
  17. int modelIndex = cast(int)luaL_checkinteger(L, 1);
  18. if (modelIndex >= models.length) {
  19. models.length = modelIndex + 1;
  20. }
  21. if (models.length < models.length) {
  22. models.length = models.length;
  23. }
  24. if (modelIndex < models.length && models[modelIndex].meshCount != 0) {
  25. UnloadModel(models[modelIndex]);
  26. }
  27. char* modelPath = cast(char*)luaL_checkstring(L, 2);
  28. models[modelIndex] = LoadModel(modelPath);
  29. return 1;
  30. }
  31. extern (C) nothrow int luaL_unloadModel(lua_State *L) {
  32. UnloadModel(models[cast(int)luaL_checkinteger(L, 1)]);
  33. return 0;
  34. }
  35. extern (C) nothrow int luaL_drawModel(lua_State *L) {
  36. int modelIndex = cast(int)luaL_checkinteger(L, 1);
  37. float x = luaL_checknumber(L, 2);
  38. float y = luaL_checknumber(L, 3);
  39. float z = luaL_checknumber(L, 4);
  40. float rotation = luaL_optnumber(L, 5, 0);
  41. float scale = luaL_optnumber(L, 6, 1);
  42. Color color = Colors.WHITE;
  43. if (lua_istable(L, 6)) {
  44. lua_getfield(L, 6, "r");
  45. color.r = cast(ubyte)lua_tointeger(L, -1);
  46. lua_pop(L, 1);
  47. lua_getfield(L, 6, "g");
  48. color.g = cast(ubyte)lua_tointeger(L, -1);
  49. lua_pop(L, 1);
  50. lua_getfield(L, 6, "b");
  51. color.b = cast(ubyte)lua_tointeger(L, -1);
  52. lua_pop(L, 1);
  53. lua_getfield(L, 6, "a");
  54. color.a = cast(ubyte)lua_tointeger(L, -1);
  55. lua_pop(L, 1);
  56. }
  57. DrawModelEx(
  58. models[modelIndex],
  59. Vector3(x, y, z),
  60. Vector3(0.0f, 1.0f, 0.0f),
  61. rotation,
  62. Vector3(scale, scale, scale),
  63. color
  64. );
  65. return 0;
  66. }
  67. extern (C) nothrow int luaL_loadModelAnimations(lua_State *L) {
  68. modelAnimations = LoadModelAnimations(luaL_checkstring(L, 1), &animsCount);
  69. return 0;
  70. }
  71. extern (C) nothrow int luaL_updateModelAnimation(lua_State *L) {
  72. ModelAnimation anim = modelAnimations[cast(int)luaL_checkinteger(L, 2)];
  73. int speedMultipler;
  74. if (lua_gettop(L) == 3) {
  75. speedMultipler = cast(int)luaL_checkinteger(L, 3);
  76. } else {
  77. speedMultipler = 1;
  78. }
  79. animationCurrentFrame = (animationCurrentFrame + speedMultipler)%anim.frameCount;
  80. int modelIndex = cast(int)luaL_checkinteger(L, 1);
  81. UpdateModelAnimation(models[modelIndex], anim, animationCurrentFrame);
  82. return 0;
  83. }
  84. extern (C) nothrow int luaL_resetModelAnimation(lua_State *L) {
  85. animationCurrentFrame = 0;
  86. return 0;
  87. }