lua_unit.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "lua_stack.h"
  6. #include "lua_environment.h"
  7. #include "unit.h"
  8. namespace crown
  9. {
  10. static int unit_local_position(lua_State* L)
  11. {
  12. LuaStack stack(L);
  13. stack.push_vector3(stack.get_unit(1)->local_position());
  14. return 1;
  15. }
  16. static int unit_local_rotation(lua_State* L)
  17. {
  18. LuaStack stack(L);
  19. stack.push_quaternion(stack.get_unit(1)->local_rotation());
  20. return 1;
  21. }
  22. static int unit_local_scale(lua_State* L)
  23. {
  24. LuaStack stack(L);
  25. stack.push_vector3(stack.get_unit(1)->local_scale());
  26. return 1;
  27. }
  28. static int unit_local_pose(lua_State* L)
  29. {
  30. LuaStack stack(L);
  31. stack.push_matrix4x4(stack.get_unit(1)->local_pose());
  32. return 1;
  33. }
  34. static int unit_world_position(lua_State* L)
  35. {
  36. LuaStack stack(L);
  37. stack.push_vector3(stack.get_unit(1)->world_position());
  38. return 1;
  39. }
  40. static int unit_world_rotation(lua_State* L)
  41. {
  42. LuaStack stack(L);
  43. stack.push_quaternion(stack.get_unit(1)->world_rotation());
  44. return 1;
  45. }
  46. static int unit_world_pose(lua_State* L)
  47. {
  48. LuaStack stack(L);
  49. stack.push_matrix4x4(stack.get_unit(1)->world_pose());
  50. return 1;
  51. }
  52. static int unit_set_local_position(lua_State* L)
  53. {
  54. LuaStack stack(L);
  55. stack.get_unit(1)->set_local_position(stack.get_vector3(2));
  56. return 0;
  57. }
  58. static int unit_set_local_rotation(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. stack.get_unit(1)->set_local_rotation(stack.get_quaternion(2));
  62. return 0;
  63. }
  64. static int unit_set_local_scale(lua_State* L)
  65. {
  66. LuaStack stack(L);
  67. stack.get_unit(1)->set_local_scale(stack.get_vector3(2));
  68. return 0;
  69. }
  70. static int unit_set_local_pose(lua_State* L)
  71. {
  72. LuaStack stack(L);
  73. stack.get_unit(1)->set_local_pose(stack.get_matrix4x4(2));
  74. return 0;
  75. }
  76. static int unit_camera(lua_State* L)
  77. {
  78. LuaStack stack(L);
  79. Unit* unit = stack.get_unit(1);
  80. if (stack.is_number(2))
  81. {
  82. stack.push_camera(unit->camera((uint32_t) stack.get_int(2)));
  83. return 1;
  84. }
  85. stack.push_camera(unit->camera(stack.get_string(2)));
  86. return 1;
  87. }
  88. static int unit_material(lua_State* L)
  89. {
  90. LuaStack stack(L);
  91. Unit* unit = stack.get_unit(1);
  92. if (stack.is_number(2))
  93. {
  94. stack.push_material(unit->material((uint32_t) stack.get_int(2)));
  95. return 1;
  96. }
  97. stack.push_material(unit->material(stack.get_string(2)));
  98. return 1;
  99. }
  100. static int unit_sprite(lua_State* L)
  101. {
  102. LuaStack stack(L);
  103. Unit* unit = stack.get_unit(1);
  104. if (stack.is_number(2))
  105. {
  106. stack.push_sprite(unit->sprite((uint32_t) stack.get_int(2)));
  107. return 1;
  108. }
  109. stack.push_sprite(unit->sprite(stack.get_string(2)));
  110. return 1;
  111. }
  112. static int unit_actor(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. Unit* unit = stack.get_unit(1);
  116. if (stack.is_number(2))
  117. {
  118. stack.push_actor(unit->actor((uint32_t) stack.get_int(2)));
  119. return 1;
  120. }
  121. stack.push_actor(unit->actor(stack.get_string(2)));
  122. return 1;
  123. }
  124. static int unit_controller(lua_State* L)
  125. {
  126. LuaStack stack(L);
  127. Controller* ctl = stack.get_unit(1)->controller();
  128. ctl ? stack.push_controller(ctl) : stack.push_nil();
  129. return 1;
  130. }
  131. static int unit_is_a(lua_State* L)
  132. {
  133. LuaStack stack(L);
  134. stack.push_bool(stack.get_unit(1)->is_a(stack.get_resource_id(2)));
  135. return 1;
  136. }
  137. static int unit_play_sprite_animation(lua_State* L)
  138. {
  139. LuaStack stack(L);
  140. stack.get_unit(1)->play_sprite_animation(stack.get_string(2), stack.get_bool(3));
  141. return 0;
  142. }
  143. static int unit_stop_sprite_animation(lua_State* L)
  144. {
  145. LuaStack stack(L);
  146. stack.get_unit(1)->stop_sprite_animation();
  147. return 0;
  148. }
  149. void load_unit(LuaEnvironment& env)
  150. {
  151. env.load_module_function("Unit", "local_position", unit_local_position);
  152. env.load_module_function("Unit", "local_rotation", unit_local_rotation);
  153. env.load_module_function("Unit", "local_scale", unit_local_scale);
  154. env.load_module_function("Unit", "local_pose", unit_local_pose);
  155. env.load_module_function("Unit", "world_position", unit_world_position);
  156. env.load_module_function("Unit", "world_rotation", unit_world_rotation);
  157. env.load_module_function("Unit", "world_pose", unit_world_pose);
  158. env.load_module_function("Unit", "set_local_position", unit_set_local_position);
  159. env.load_module_function("Unit", "set_local_rotation", unit_set_local_rotation);
  160. env.load_module_function("Unit", "set_local_scale", unit_set_local_scale);
  161. env.load_module_function("Unit", "set_local_pose", unit_set_local_pose);
  162. env.load_module_function("Unit", "camera", unit_camera);
  163. env.load_module_function("Unit", "material", unit_material);
  164. env.load_module_function("Unit", "sprite", unit_sprite);
  165. env.load_module_function("Unit", "actor", unit_actor);
  166. env.load_module_function("Unit", "controller", unit_controller);
  167. env.load_module_function("Unit", "is_a", unit_is_a);
  168. env.load_module_function("Unit", "play_sprite_animation", unit_play_sprite_animation);
  169. env.load_module_function("Unit", "stop_sprite_animation", unit_stop_sprite_animation);
  170. }
  171. } // namespace crown