sound.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. -- Copyright (c) 2012-2026 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:is_spatial()
  29. return true
  30. end
  31. function SoundObject:local_position()
  32. local tr = SceneGraph.instance(self._sg, self._unit_id)
  33. return tr and SceneGraph.local_position(self._sg, tr) or Vector3.zero()
  34. end
  35. function SoundObject:local_rotation()
  36. local tr = SceneGraph.instance(self._sg, self._unit_id)
  37. return tr and SceneGraph.local_rotation(self._sg, tr) or Quaternion.identity()
  38. end
  39. function SoundObject:local_scale()
  40. local tr = SceneGraph.instance(self._sg, self._unit_id)
  41. return tr and SceneGraph.local_scale(self._sg, tr) or Vector3(1, 1, 1)
  42. end
  43. function SoundObject:local_pose()
  44. local tr = SceneGraph.instance(self._sg, self._unit_id)
  45. return tr and SceneGraph.local_pose(self._sg, tr) or Matrix4x4.identity()
  46. end
  47. function SoundObject:world_position()
  48. local tr = SceneGraph.instance(self._sg, self._unit_id)
  49. return tr and SceneGraph.world_position(self._sg, tr) or Vector3.zero()
  50. end
  51. function SoundObject:world_rotation()
  52. local tr = SceneGraph.instance(self._sg, self._unit_id)
  53. return tr and SceneGraph.world_rotation(self._sg, tr) or Quaternion.identity()
  54. end
  55. function SoundObject:world_scale()
  56. return Vector3(1, 1, 1)
  57. end
  58. function SoundObject:world_pose()
  59. local tr = SceneGraph.instance(self._sg, self._unit_id)
  60. return tr and SceneGraph.world_pose(self._sg, tr) or Matrix4x4.identity()
  61. end
  62. function SoundObject:set_local_position(pos)
  63. local tr = SceneGraph.instance(self._sg, self._unit_id)
  64. if tr then SceneGraph.set_local_position(self._sg, tr, pos) end
  65. end
  66. function SoundObject:set_local_rotation(rot)
  67. local tr = SceneGraph.instance(self._sg, self._unit_id)
  68. if tr then SceneGraph.set_local_rotation(self._sg, tr, rot) end
  69. end
  70. function SoundObject:set_local_scale(scale)
  71. local tr = SceneGraph.instance(self._sg, self._unit_id)
  72. if tr then SceneGraph.set_local_scale(self._sg, tr, scale) end
  73. end
  74. function SoundObject:set_local_pose(pose)
  75. local tr = SceneGraph.instance(self._sg, self._unit_id)
  76. if tr then SceneGraph.set_local_pose(self._sg, tr, pose) end
  77. end
  78. function SoundObject:on_selected(selected)
  79. self._selected = selected
  80. RenderWorld.selection(LevelEditor._rw, self._unit_id, selected);
  81. end
  82. function SoundObject:obb()
  83. local rw = LevelEditor._rw
  84. return RenderWorld.mesh_obb(rw, RenderWorld.mesh_instance(rw, self._unit_id))
  85. end
  86. function SoundObject:raycast(pos, dir)
  87. local rw = LevelEditor._rw
  88. local mesh = RenderWorld.mesh_instance(rw, self._unit_id)
  89. local tm, hext = RenderWorld.mesh_obb(rw, mesh)
  90. return RenderWorld.mesh_cast_ray(rw, mesh, pos, dir)
  91. end
  92. function SoundObject:draw()
  93. if self._selected then
  94. DebugLine.add_sphere(LevelEditor._lines
  95. , self:local_position()
  96. , self._range
  97. , Color4.yellow()
  98. )
  99. end
  100. end
  101. function SoundObject:set_range(range)
  102. self._range = range
  103. end
  104. function SoundObject:send()
  105. Device.console_send { type = "sound_spawned"
  106. , id = self._id
  107. , name = self:name()
  108. , position = self:local_position()
  109. , rotation = self:local_rotation()
  110. , scale = self:local_scale()
  111. , range = self._range
  112. , volume = self._volume
  113. , loop = self._loop
  114. }
  115. end