LuaWorld.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 = (World*) stack.get_lightdata(1);
  34. const char* name = stack.get_string(2);
  35. const Vec3& pos = stack.num_args() > 2 ? stack.get_vec3(3) : Vec3::ZERO;
  36. const Quat& rot = stack.num_args() > 3 ? stack.get_quat(4) : Quat::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_play_sound(lua_State* L)
  43. {
  44. LuaStack stack(L);
  45. World* world = (World*) stack.get_lightdata(1);
  46. const char* name = stack.get_string(2);
  47. const bool loop = stack.num_args() > 2 ? stack.get_bool(3) : false;
  48. const float volume = stack.num_args() > 3 ? stack.get_float(4) : 1.0f; // test value
  49. const Vec3& pos = stack.num_args() > 4 ? stack.get_vec3(5) : Vec3::ZERO;
  50. const float range = stack.num_args() > 5 ? stack.get_float(6) : 1000.0f; // test value
  51. SoundInstanceId id = world->play_sound(name, loop, volume, pos, range);
  52. stack.push_int32(id.encode());
  53. return 1;
  54. }
  55. //-----------------------------------------------------------------------------
  56. CE_EXPORT int world_pause_sound(lua_State* L)
  57. {
  58. LuaStack stack(L);
  59. World* world = (World*) stack.get_lightdata(1);
  60. SoundInstanceId id;
  61. id.decode(stack.get_int(2));
  62. world->pause_sound(id);
  63. return 0;
  64. }
  65. //-----------------------------------------------------------------------------
  66. CE_EXPORT int world_link_sound(lua_State* L)
  67. {
  68. LuaStack stack(L);
  69. World* world = (World*) stack.get_lightdata(1);
  70. SoundInstanceId sound;
  71. sound.decode(stack.get_int(2));
  72. UnitId unit;
  73. unit.decode(stack.get_int(3));
  74. world->link_sound(sound, unit);
  75. return 0;
  76. }
  77. //-----------------------------------------------------------------------------
  78. CE_EXPORT int world_set_listener(lua_State* L)
  79. {
  80. LuaStack stack(L);
  81. World* world = (World*) stack.get_lightdata(1);
  82. const Vec3& pos = stack.get_vec3(2);
  83. const Vec3& vel = stack.get_vec3(3);
  84. const Vec3& or_up = stack.get_vec3(4);
  85. const Vec3& or_at = stack.get_vec3(5);
  86. world->set_listener(pos, vel, or_up, or_at);
  87. return 0;
  88. }
  89. //-----------------------------------------------------------------------------
  90. CE_EXPORT int world_set_sound_position(lua_State* L)
  91. {
  92. LuaStack stack(L);
  93. World* world = (World*) stack.get_lightdata(1);
  94. SoundInstanceId id;
  95. id.decode(stack.get_int(2));
  96. const Vec3& pos = stack.get_vec3(3);
  97. world->set_sound_position(id, pos);
  98. return 0;
  99. }
  100. //-----------------------------------------------------------------------------
  101. CE_EXPORT int world_set_sound_range(lua_State* L)
  102. {
  103. LuaStack stack(L);
  104. World* world = (World*) stack.get_lightdata(1);
  105. SoundInstanceId id;
  106. id.decode(stack.get_int(2));
  107. float range = stack.get_float(3);
  108. world->set_sound_range(id, range);
  109. return 0;
  110. }
  111. //-----------------------------------------------------------------------------
  112. CE_EXPORT int world_set_sound_volume(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. World* world = (World*) stack.get_lightdata(1);
  116. SoundInstanceId id;
  117. id.decode(stack.get_int(2));
  118. float vol = stack.get_float(3);
  119. world->set_sound_volume(id, vol);
  120. return 0;
  121. }
  122. //-----------------------------------------------------------------------------
  123. void load_world(LuaEnvironment& env)
  124. {
  125. env.load_module_function("World", "spawn_unit", world_spawn_unit);
  126. env.load_module_function("World", "play_sound", world_play_sound);
  127. env.load_module_function("World", "pause_sound", world_pause_sound);
  128. env.load_module_function("World", "link_sound", world_link_sound);
  129. env.load_module_function("World", "set_listener", world_set_listener);
  130. env.load_module_function("World", "set_sound_position", world_set_sound_position);
  131. env.load_module_function("World", "set_sound_range", world_set_sound_range);
  132. env.load_module_function("World", "set_sound_volume", world_set_sound_volume);
  133. }
  134. } // namespace crown