lua_unit.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_node(lua_State* L)
  11. {
  12. LuaStack stack(L);
  13. stack.push_int32(stack.get_unit(1)->node(stack.get_string(2)));
  14. return 1;
  15. }
  16. static int unit_has_node(lua_State* L)
  17. {
  18. LuaStack stack(L);
  19. stack.push_bool(stack.get_unit(1)->has_node(stack.get_string(2)));
  20. return 1;
  21. }
  22. static int unit_num_nodes(lua_State* L)
  23. {
  24. LuaStack stack(L);
  25. stack.push_int32(stack.get_unit(1)->num_nodes());
  26. return 1;
  27. }
  28. static int unit_local_position(lua_State* L)
  29. {
  30. LuaStack stack(L);
  31. stack.push_vector3(stack.get_unit(1)->local_position(stack.get_int(2)));
  32. return 1;
  33. }
  34. static int unit_local_rotation(lua_State* L)
  35. {
  36. LuaStack stack(L);
  37. stack.push_quaternion(stack.get_unit(1)->local_rotation(stack.get_int(2)));
  38. return 1;
  39. }
  40. static int unit_local_pose(lua_State* L)
  41. {
  42. LuaStack stack(L);
  43. stack.push_matrix4x4(stack.get_unit(1)->local_pose(stack.get_int(2)));
  44. return 1;
  45. }
  46. static int unit_world_position(lua_State* L)
  47. {
  48. LuaStack stack(L);
  49. stack.push_vector3(stack.get_unit(1)->world_position(stack.get_int(2)));
  50. return 1;
  51. }
  52. static int unit_world_rotation(lua_State* L)
  53. {
  54. LuaStack stack(L);
  55. stack.push_quaternion(stack.get_unit(1)->world_rotation(stack.get_int(2)));
  56. return 1;
  57. }
  58. static int unit_world_pose(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. stack.push_matrix4x4(stack.get_unit(1)->world_pose(stack.get_int(2)));
  62. return 1;
  63. }
  64. static int unit_set_local_position(lua_State* L)
  65. {
  66. LuaStack stack(L);
  67. stack.get_unit(1)->set_local_position(stack.get_int(2), stack.get_vector3(3));
  68. return 0;
  69. }
  70. static int unit_set_local_rotation(lua_State* L)
  71. {
  72. LuaStack stack(L);
  73. stack.get_unit(1)->set_local_rotation(stack.get_int(2), stack.get_quaternion(3));
  74. return 0;
  75. }
  76. static int unit_set_local_pose(lua_State* L)
  77. {
  78. LuaStack stack(L);
  79. stack.get_unit(1)->set_local_pose(stack.get_int(2), stack.get_matrix4x4(3));
  80. return 0;
  81. }
  82. static int unit_link_node(lua_State* L)
  83. {
  84. LuaStack stack(L);
  85. stack.get_unit(1)->link_node(stack.get_int(2), stack.get_int(3));
  86. return 0;
  87. }
  88. static int unit_unlink_node(lua_State* L)
  89. {
  90. LuaStack stack(L);
  91. stack.get_unit(1)->unlink_node(stack.get_int(2));
  92. return 0;
  93. }
  94. static int unit_camera(lua_State* L)
  95. {
  96. LuaStack stack(L);
  97. Unit* unit = stack.get_unit(1);
  98. if (stack.is_number(2))
  99. {
  100. stack.push_camera(unit->camera((uint32_t) stack.get_int(2)));
  101. return 1;
  102. }
  103. stack.push_camera(unit->camera(stack.get_string(2)));
  104. return 1;
  105. }
  106. static int unit_material(lua_State* L)
  107. {
  108. LuaStack stack(L);
  109. Unit* unit = stack.get_unit(1);
  110. if (stack.is_number(2))
  111. {
  112. stack.push_material(unit->material((uint32_t) stack.get_int(2)));
  113. return 1;
  114. }
  115. stack.push_material(unit->material(stack.get_string(2)));
  116. return 1;
  117. }
  118. static int unit_sprite(lua_State* L)
  119. {
  120. LuaStack stack(L);
  121. Unit* unit = stack.get_unit(1);
  122. if (stack.is_number(2))
  123. {
  124. stack.push_sprite(unit->sprite((uint32_t) stack.get_int(2)));
  125. return 1;
  126. }
  127. stack.push_sprite(unit->sprite(stack.get_string(2)));
  128. return 1;
  129. }
  130. static int unit_actor(lua_State* L)
  131. {
  132. LuaStack stack(L);
  133. Unit* unit = stack.get_unit(1);
  134. if (stack.is_number(2))
  135. {
  136. stack.push_actor(unit->actor((uint32_t) stack.get_int(2)));
  137. return 1;
  138. }
  139. stack.push_actor(unit->actor(stack.get_string(2)));
  140. return 1;
  141. }
  142. static int unit_controller(lua_State* L)
  143. {
  144. LuaStack stack(L);
  145. Controller* ctl = stack.get_unit(1)->controller();
  146. ctl ? stack.push_controller(ctl) : stack.push_nil();
  147. return 1;
  148. }
  149. static int unit_is_a(lua_State* L)
  150. {
  151. LuaStack stack(L);
  152. stack.push_bool(stack.get_unit(1)->is_a(stack.get_string(2)));
  153. return 1;
  154. }
  155. static int unit_play_sprite_animation(lua_State* L)
  156. {
  157. LuaStack stack(L);
  158. stack.get_unit(1)->play_sprite_animation(stack.get_string(2), stack.get_bool(3));
  159. return 0;
  160. }
  161. static int unit_stop_sprite_animation(lua_State* L)
  162. {
  163. LuaStack stack(L);
  164. stack.get_unit(1)->stop_sprite_animation();
  165. return 0;
  166. }
  167. static int unit_has_key(lua_State* L)
  168. {
  169. LuaStack stack(L);
  170. stack.push_bool(stack.get_unit(1)->has_key(stack.get_string(2)));
  171. return 1;
  172. }
  173. static int unit_get_key(lua_State* L)
  174. {
  175. LuaStack stack(L);
  176. Unit* unit = stack.get_unit(1);
  177. const char* key = stack.get_string(2);
  178. switch (unit->value_type(key))
  179. {
  180. case ValueType::BOOL:
  181. {
  182. bool val;
  183. unit->get_key(key, val);
  184. stack.push_bool(val);
  185. return 1;
  186. }
  187. case ValueType::FLOAT:
  188. {
  189. float val;
  190. unit->get_key(key, val);
  191. stack.push_float(val);
  192. return 1;
  193. }
  194. case ValueType::STRING:
  195. {
  196. StringId32 val;
  197. unit->get_key(key, val);
  198. stack.push_string_id(val);
  199. return 1;
  200. }
  201. case ValueType::VECTOR3:
  202. {
  203. Vector3 val;
  204. unit->get_key(key, val);
  205. stack.push_vector3(val);
  206. return 1;
  207. }
  208. default: CE_FATAL("Unknown value type"); break;
  209. }
  210. return 0;
  211. }
  212. static int unit_set_key(lua_State* L)
  213. {
  214. LuaStack stack(L);
  215. Unit* unit = stack.get_unit(1);
  216. const char* key = stack.get_string(2);
  217. switch (stack.value_type(3))
  218. {
  219. case LUA_TBOOLEAN: unit->set_key(key, stack.get_bool(3)); break;
  220. case LUA_TNUMBER: unit->set_key(key, stack.get_float(3)); break;
  221. case LUA_TSTRING: unit->set_key(key, stack.get_string(3)); break;
  222. case LUA_TLIGHTUSERDATA: unit->set_key(key, stack.get_vector3(3)); break;
  223. default: CE_FATAL("Unsupported value type"); break; // FIXME use LUA_ASSERT
  224. }
  225. return 0;
  226. }
  227. void load_unit(LuaEnvironment& env)
  228. {
  229. env.load_module_function("Unit", "node", unit_node);
  230. env.load_module_function("Unit", "has_node", unit_has_node);
  231. env.load_module_function("Unit", "num_nodes", unit_num_nodes);
  232. env.load_module_function("Unit", "local_position", unit_local_position);
  233. env.load_module_function("Unit", "local_rotation", unit_local_rotation);
  234. env.load_module_function("Unit", "local_pose", unit_local_pose);
  235. env.load_module_function("Unit", "world_position", unit_world_position);
  236. env.load_module_function("Unit", "world_rotation", unit_world_rotation);
  237. env.load_module_function("Unit", "world_pose", unit_world_pose);
  238. env.load_module_function("Unit", "set_local_position", unit_set_local_position);
  239. env.load_module_function("Unit", "set_local_rotation", unit_set_local_rotation);
  240. env.load_module_function("Unit", "set_local_pose", unit_set_local_pose);
  241. env.load_module_function("Unit", "link_node", unit_link_node);
  242. env.load_module_function("Unit", "unlink_node", unit_unlink_node);
  243. env.load_module_function("Unit", "camera", unit_camera);
  244. env.load_module_function("Unit", "material", unit_material);
  245. env.load_module_function("Unit", "sprite", unit_sprite);
  246. env.load_module_function("Unit", "actor", unit_actor);
  247. env.load_module_function("Unit", "controller", unit_controller);
  248. env.load_module_function("Unit", "is_a", unit_is_a);
  249. env.load_module_function("Unit", "play_sprite_animation", unit_play_sprite_animation);
  250. env.load_module_function("Unit", "stop_sprite_animation", unit_stop_sprite_animation);
  251. env.load_module_function("Unit", "has_key", unit_has_key);
  252. env.load_module_function("Unit", "get_key", unit_get_key);
  253. env.load_module_function("Unit", "set_key", unit_set_key);
  254. }
  255. } // namespace crown