sound.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. -- Copyright (c) 2012-2025 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. require "core/lua/class"
  4. SoundObject = class(SoundObject)
  5. function SoundObject:init(world, id, name, range, volume, loop)
  6. self._world = world
  7. self._id = id
  8. self._name = name
  9. self._range = range
  10. self._volume = volume
  11. self._loop = loop
  12. self._unit_id = World.spawn_unit(world, "core/units/sound")
  13. self._sg = World.scene_graph(world)
  14. self._selected = false
  15. end
  16. function SoundObject:id()
  17. return self._id
  18. end
  19. function SoundObject:unit_id()
  20. return self._unit_id
  21. end
  22. function SoundObject:destroy()
  23. UnitUtils.destroy_tree(self._world, self._unit_id)
  24. end
  25. function SoundObject:name()
  26. return self._name
  27. end
  28. function SoundObject:local_position()
  29. local tr = SceneGraph.instance(self._sg, self._unit_id)
  30. return tr and SceneGraph.local_position(self._sg, tr) or Vector3.zero()
  31. end
  32. function SoundObject:local_rotation()
  33. local tr = SceneGraph.instance(self._sg, self._unit_id)
  34. return tr and SceneGraph.local_rotation(self._sg, tr) or Quaternion.identity()
  35. end
  36. function SoundObject:local_scale()
  37. local tr = SceneGraph.instance(self._sg, self._unit_id)
  38. return tr and SceneGraph.local_scale(self._sg, tr) or Vector3(1, 1, 1)
  39. end
  40. function SoundObject:local_pose()
  41. local tr = SceneGraph.instance(self._sg, self._unit_id)
  42. return tr and SceneGraph.local_pose(self._sg, tr) or Matrix4x4.identity()
  43. end
  44. function SoundObject:world_position()
  45. local tr = SceneGraph.instance(self._sg, self._unit_id)
  46. return tr and SceneGraph.world_position(self._sg, tr) or Vector3.zero()
  47. end
  48. function SoundObject:world_rotation()
  49. local tr = SceneGraph.instance(self._sg, self._unit_id)
  50. return tr and SceneGraph.world_rotation(self._sg, tr) or Quaternion.identity()
  51. end
  52. function SoundObject:world_scale()
  53. return Vector3(1, 1, 1)
  54. end
  55. function SoundObject:world_pose()
  56. local tr = SceneGraph.instance(self._sg, self._unit_id)
  57. return tr and SceneGraph.world_pose(self._sg, tr) or Matrix4x4.identity()
  58. end
  59. function SoundObject:set_local_position(pos)
  60. local tr = SceneGraph.instance(self._sg, self._unit_id)
  61. if tr then SceneGraph.set_local_position(self._sg, tr, pos) end
  62. end
  63. function SoundObject:set_local_rotation(rot)
  64. local tr = SceneGraph.instance(self._sg, self._unit_id)
  65. if tr then SceneGraph.set_local_rotation(self._sg, tr, rot) end
  66. end
  67. function SoundObject:set_local_scale(scale)
  68. local tr = SceneGraph.instance(self._sg, self._unit_id)
  69. if tr then SceneGraph.set_local_scale(self._sg, tr, scale) end
  70. end
  71. function SoundObject:set_local_pose(pose)
  72. local tr = SceneGraph.instance(self._sg, self._unit_id)
  73. if tr then SceneGraph.set_local_pose(self._sg, tr, pose) end
  74. end
  75. function SoundObject:on_selected(selected)
  76. self._selected = selected
  77. RenderWorld.selection(LevelEditor._rw, self._unit_id, selected);
  78. end
  79. function SoundObject:obb()
  80. local rw = LevelEditor._rw
  81. return RenderWorld.mesh_obb(rw, RenderWorld.mesh_instance(rw, self._unit_id))
  82. end
  83. function SoundObject:raycast(pos, dir)
  84. local rw = LevelEditor._rw
  85. local mesh = RenderWorld.mesh_instance(rw, self._unit_id)
  86. local tm, hext = RenderWorld.mesh_obb(rw, mesh)
  87. return RenderWorld.mesh_cast_ray(rw, mesh, pos, dir)
  88. end
  89. function SoundObject:draw()
  90. if self._selected then
  91. DebugLine.add_sphere(LevelEditor._lines
  92. , self:local_position()
  93. , self._range
  94. , Color4.yellow()
  95. )
  96. end
  97. end
  98. function SoundObject:set_range(range)
  99. self._range = range
  100. end
  101. function SoundObject:send()
  102. Device.console_send { type = "sound_spawned"
  103. , id = self._id
  104. , name = self:name()
  105. , position = self:local_position()
  106. , rotation = self:local_rotation()
  107. , scale = self:local_scale()
  108. , range = self._range
  109. , volume = self._volume
  110. , loop = self._loop
  111. }
  112. end