lua_unit.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. static int unit_has_key(lua_State* L)
  150. {
  151. LuaStack stack(L);
  152. stack.push_bool(stack.get_unit(1)->has_key(stack.get_string(2)));
  153. return 1;
  154. }
  155. static int unit_get_key(lua_State* L)
  156. {
  157. LuaStack stack(L);
  158. Unit* unit = stack.get_unit(1);
  159. const char* key = stack.get_string(2);
  160. switch (unit->value_type(key))
  161. {
  162. case ValueType::BOOL:
  163. {
  164. bool val;
  165. unit->get_key(key, val);
  166. stack.push_bool(val);
  167. return 1;
  168. }
  169. case ValueType::FLOAT:
  170. {
  171. float val;
  172. unit->get_key(key, val);
  173. stack.push_float(val);
  174. return 1;
  175. }
  176. case ValueType::STRING:
  177. {
  178. StringId32 val;
  179. unit->get_key(key, val);
  180. stack.push_string_id(val);
  181. return 1;
  182. }
  183. case ValueType::VECTOR3:
  184. {
  185. Vector3 val;
  186. unit->get_key(key, val);
  187. stack.push_vector3(val);
  188. return 1;
  189. }
  190. default: CE_FATAL("Unknown value type"); break;
  191. }
  192. return 0;
  193. }
  194. static int unit_set_key(lua_State* L)
  195. {
  196. LuaStack stack(L);
  197. Unit* unit = stack.get_unit(1);
  198. const char* key = stack.get_string(2);
  199. switch (stack.value_type(3))
  200. {
  201. case LUA_TBOOLEAN: unit->set_key(key, stack.get_bool(3)); break;
  202. case LUA_TNUMBER: unit->set_key(key, stack.get_float(3)); break;
  203. case LUA_TSTRING: unit->set_key(key, stack.get_string(3)); break;
  204. case LUA_TLIGHTUSERDATA: unit->set_key(key, stack.get_vector3(3)); break;
  205. default: CE_FATAL("Unsupported value type"); break; // FIXME use LUA_ASSERT
  206. }
  207. return 0;
  208. }
  209. void load_unit(LuaEnvironment& env)
  210. {
  211. env.load_module_function("Unit", "local_position", unit_local_position);
  212. env.load_module_function("Unit", "local_rotation", unit_local_rotation);
  213. env.load_module_function("Unit", "local_scale", unit_local_scale);
  214. env.load_module_function("Unit", "local_pose", unit_local_pose);
  215. env.load_module_function("Unit", "world_position", unit_world_position);
  216. env.load_module_function("Unit", "world_rotation", unit_world_rotation);
  217. env.load_module_function("Unit", "world_pose", unit_world_pose);
  218. env.load_module_function("Unit", "set_local_position", unit_set_local_position);
  219. env.load_module_function("Unit", "set_local_rotation", unit_set_local_rotation);
  220. env.load_module_function("Unit", "set_local_scale", unit_set_local_scale);
  221. env.load_module_function("Unit", "set_local_pose", unit_set_local_pose);
  222. env.load_module_function("Unit", "camera", unit_camera);
  223. env.load_module_function("Unit", "material", unit_material);
  224. env.load_module_function("Unit", "sprite", unit_sprite);
  225. env.load_module_function("Unit", "actor", unit_actor);
  226. env.load_module_function("Unit", "controller", unit_controller);
  227. env.load_module_function("Unit", "is_a", unit_is_a);
  228. env.load_module_function("Unit", "play_sprite_animation", unit_play_sprite_animation);
  229. env.load_module_function("Unit", "stop_sprite_animation", unit_stop_sprite_animation);
  230. env.load_module_function("Unit", "has_key", unit_has_key);
  231. env.load_module_function("Unit", "get_key", unit_get_key);
  232. env.load_module_function("Unit", "set_key", unit_set_key);
  233. }
  234. } // namespace crown