LuaWorld.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "LuaStack.h"
  24. #include "LuaEnvironment.h"
  25. #include "World.h"
  26. #include "Device.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. CE_EXPORT int world_spawn_unit(lua_State* L)
  31. {
  32. LuaStack stack(L);
  33. World* world = stack.get_world(1);
  34. const char* name = stack.get_string(2);
  35. const Vector3& pos = stack.num_args() > 2 ? stack.get_vector3(3) : Vector3::ZERO;
  36. const Quaternion& rot = stack.num_args() > 3 ? stack.get_quaternion(4) : Quaternion::IDENTITY;
  37. UnitId unit = world->spawn_unit(name, pos, rot);
  38. stack.push_unit(world->lookup_unit(unit));
  39. return 1;
  40. }
  41. //-----------------------------------------------------------------------------
  42. CE_EXPORT int world_destroy_unit(lua_State* L)
  43. {
  44. LuaStack stack(L);
  45. World* world = stack.get_world(1);
  46. Unit* unit = stack.get_unit(2);
  47. world->destroy_unit(unit->id());
  48. return 0;
  49. }
  50. //-----------------------------------------------------------------------------
  51. CE_EXPORT int world_num_units(lua_State* L)
  52. {
  53. LuaStack stack(L);
  54. World* world = stack.get_world(1);
  55. stack.push_uint32(world->num_units());
  56. return 1;
  57. }
  58. //-----------------------------------------------------------------------------
  59. CE_EXPORT int world_play_sound(lua_State* L)
  60. {
  61. LuaStack stack(L);
  62. World* world = stack.get_world(1);
  63. const char* name = stack.get_string(2);
  64. const bool loop = stack.num_args() > 2 ? stack.get_bool(3) : false;
  65. const float volume = stack.num_args() > 3 ? stack.get_float(4) : 1.0f; // test value
  66. const Vector3& pos = stack.num_args() > 4 ? stack.get_vector3(5) : Vector3::ZERO;
  67. const float range = stack.num_args() > 5 ? stack.get_float(6) : 1000.0f; // test value
  68. SoundInstanceId id = world->play_sound(name, loop, volume, pos, range);
  69. stack.push_sound_instance_id(id);
  70. return 1;
  71. }
  72. //-----------------------------------------------------------------------------
  73. CE_EXPORT int world_stop_sound(lua_State* L)
  74. {
  75. LuaStack stack(L);
  76. World* world = stack.get_world(1);
  77. const SoundInstanceId id = stack.get_sound_instance_id(2);
  78. world->stop_sound(id);
  79. return 0;
  80. }
  81. //-----------------------------------------------------------------------------
  82. CE_EXPORT int world_link_sound(lua_State* L)
  83. {
  84. LuaStack stack(L);
  85. World* world = stack.get_world(1);
  86. const SoundInstanceId id = stack.get_sound_instance_id(2);
  87. Unit* unit = stack.get_unit(3);
  88. const int32_t node = stack.get_int(4);
  89. world->link_sound(id, unit, node);
  90. return 0;
  91. }
  92. //-----------------------------------------------------------------------------
  93. CE_EXPORT int world_set_listener(lua_State* L)
  94. {
  95. LuaStack stack(L);
  96. World* world = stack.get_world(1);
  97. const Vector3& pos = stack.get_vector3(2);
  98. const Vector3& vel = stack.get_vector3(3);
  99. const Vector3& or_up = stack.get_vector3(4);
  100. const Vector3& or_at = stack.get_vector3(5);
  101. world->set_listener(pos, vel, or_up, or_at);
  102. return 0;
  103. }
  104. //-----------------------------------------------------------------------------
  105. CE_EXPORT int world_set_sound_position(lua_State* L)
  106. {
  107. LuaStack stack(L);
  108. World* world = stack.get_world(1);
  109. const SoundInstanceId id = stack.get_sound_instance_id(2);
  110. const Vector3& pos = stack.get_vector3(3);
  111. world->set_sound_position(id, pos);
  112. return 0;
  113. }
  114. //-----------------------------------------------------------------------------
  115. CE_EXPORT int world_set_sound_range(lua_State* L)
  116. {
  117. LuaStack stack(L);
  118. World* world = stack.get_world(1);
  119. const SoundInstanceId id = stack.get_sound_instance_id(2);
  120. float range = stack.get_float(3);
  121. world->set_sound_range(id, range);
  122. return 0;
  123. }
  124. //-----------------------------------------------------------------------------
  125. CE_EXPORT int world_set_sound_volume(lua_State* L)
  126. {
  127. LuaStack stack(L);
  128. World* world = stack.get_world(1);
  129. const SoundInstanceId id = stack.get_sound_instance_id(2);
  130. float vol = stack.get_float(3);
  131. world->set_sound_volume(id, vol);
  132. return 0;
  133. }
  134. //-----------------------------------------------------------------------------
  135. CE_EXPORT int world_physics_world(lua_State* L)
  136. {
  137. LuaStack stack(L);
  138. World* world = stack.get_world(1);
  139. stack.push_physics_world(world->physics_world());
  140. return 1;
  141. }
  142. //-----------------------------------------------------------------------------
  143. void load_world(LuaEnvironment& env)
  144. {
  145. env.load_module_function("World", "spawn_unit", world_spawn_unit);
  146. env.load_module_function("World", "destroy_unit", world_destroy_unit);
  147. env.load_module_function("World", "num_units", world_num_units);
  148. env.load_module_function("World", "play_sound", world_play_sound);
  149. env.load_module_function("World", "stop_sound", world_stop_sound);
  150. env.load_module_function("World", "link_sound", world_link_sound);
  151. env.load_module_function("World", "set_listener", world_set_listener);
  152. env.load_module_function("World", "set_sound_position", world_set_sound_position);
  153. env.load_module_function("World", "set_sound_range", world_set_sound_range);
  154. env.load_module_function("World", "set_sound_volume", world_set_sound_volume);
  155. env.load_module_function("World", "physics_world", world_physics_world);
  156. }
  157. } // namespace crown