unit.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. -- Copyright (c) 2012-2024 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. require "core/lua/class"
  4. UnitUtils = UnitUtils or {}
  5. function UnitUtils.freeze(world, unit_id)
  6. local pw = World.physics_world(world)
  7. local actor = PhysicsWorld.actor_instance(pw, unit_id)
  8. if (actor ~= nil) then
  9. PhysicsWorld.actor_set_kinematic(pw, actor, true)
  10. end
  11. end
  12. UnitBox = class(UnitBox)
  13. function UnitBox:init(world, id, unit_id, prefab)
  14. self._world = world
  15. self._rw = World.render_world(world)
  16. self._id = id
  17. self._unit_id = unit_id
  18. self._prefab = prefab
  19. self._sg = World.scene_graph(world)
  20. self._selected = false
  21. UnitUtils.freeze(world, unit_id)
  22. end
  23. function UnitBox:id()
  24. return self._id
  25. end
  26. function UnitBox:prefab()
  27. return self._prefab
  28. end
  29. function UnitBox:unit_id()
  30. return self._unit_id
  31. end
  32. function UnitBox:destroy()
  33. World.destroy_unit(self._world, self._unit_id)
  34. end
  35. function UnitBox:local_position()
  36. local tr = SceneGraph.instance(self._sg, self._unit_id)
  37. return tr and SceneGraph.local_position(self._sg, tr) or Vector3.zero()
  38. end
  39. function UnitBox:local_rotation()
  40. local tr = SceneGraph.instance(self._sg, self._unit_id)
  41. return tr and SceneGraph.local_rotation(self._sg, tr) or Quaternion.identity()
  42. end
  43. function UnitBox:local_scale()
  44. local tr = SceneGraph.instance(self._sg, self._unit_id)
  45. return tr and SceneGraph.local_scale(self._sg, tr) or Vector3(1, 1, 1)
  46. end
  47. function UnitBox:local_pose()
  48. local tr = SceneGraph.instance(self._sg, self._unit_id)
  49. return tr and SceneGraph.local_pose(self._sg, tr) or Matrix4x4.identity()
  50. end
  51. function UnitBox:world_position()
  52. local tr = SceneGraph.instance(self._sg, self._unit_id)
  53. return tr and SceneGraph.world_position(self._sg, tr) or Vector3.zero()
  54. end
  55. function UnitBox:world_rotation()
  56. local tr = SceneGraph.instance(self._sg, self._unit_id)
  57. return tr and SceneGraph.world_rotation(self._sg, tr) or Quaternion.identity()
  58. end
  59. function UnitBox:world_scale()
  60. return Vector3(1, 1, 1)
  61. end
  62. function UnitBox:world_pose()
  63. local tr = SceneGraph.instance(self._sg, self._unit_id)
  64. return tr and SceneGraph.world_pose(self._sg, tr) or Matrix4x4.identity()
  65. end
  66. function UnitBox:set_local_position(pos)
  67. local tr = SceneGraph.instance(self._sg, self._unit_id)
  68. if tr then SceneGraph.set_local_position(self._sg, tr, pos) end
  69. end
  70. function UnitBox:set_local_rotation(rot)
  71. local tr = SceneGraph.instance(self._sg, self._unit_id)
  72. if tr then SceneGraph.set_local_rotation(self._sg, tr, rot) end
  73. end
  74. function UnitBox:set_local_scale(scale)
  75. local tr = SceneGraph.instance(self._sg, self._unit_id)
  76. if tr then SceneGraph.set_local_scale(self._sg, tr, scale) end
  77. end
  78. function UnitBox:set_local_pose(pose)
  79. local tr = SceneGraph.instance(self._sg, self._unit_id)
  80. if tr then SceneGraph.set_local_pose(self._sg, tr, pose) end
  81. end
  82. function UnitBox:on_selected(selected)
  83. self._selected = selected
  84. RenderWorld.selection(self._rw, self._unit_id, selected);
  85. end
  86. -- Returns the Oriented Bounding-Box of the unit.
  87. function UnitBox:obb()
  88. local mesh = RenderWorld.mesh_instance(self._rw, self._unit_id)
  89. if mesh then
  90. return RenderWorld.mesh_obb(self._rw, mesh)
  91. end
  92. local sprite = RenderWorld.sprite_instance(self._rw, self._unit_id)
  93. if sprite then
  94. return RenderWorld.sprite_obb(self._rw, sprite)
  95. end
  96. return Matrix4x4.identity(), Vector3(1, 1, 1)
  97. end
  98. function UnitBox:raycast(pos, dir)
  99. local mesh = RenderWorld.mesh_instance(self._rw, self._unit_id)
  100. if mesh then
  101. return RenderWorld.mesh_cast_ray(self._rw, mesh, pos, dir)
  102. end
  103. local sprite = RenderWorld.sprite_instance(self._rw, self._unit_id)
  104. if sprite then
  105. return RenderWorld.sprite_cast_ray(self._rw, sprite, pos, dir)
  106. end
  107. return -1.0
  108. end
  109. function UnitBox:draw()
  110. if self._selected then
  111. local light = RenderWorld.light_instance(self._rw, self._unit_id)
  112. if light then
  113. RenderWorld.light_debug_draw(self._rw, light, LevelEditor._lines)
  114. end
  115. end
  116. end
  117. function UnitBox:set_light(type, range, intensity, angle, color)
  118. local light = RenderWorld.light_instance(self._rw, self._unit_id)
  119. RenderWorld.light_set_type(self._rw, light, type)
  120. RenderWorld.light_set_color(self._rw, light, color)
  121. RenderWorld.light_set_range(self._rw, light, range)
  122. RenderWorld.light_set_intensity(self._rw, light, intensity)
  123. RenderWorld.light_set_spot_angle(self._rw, light, angle)
  124. end
  125. function UnitBox:set_mesh(mesh_resource, geometry, material, visible)
  126. local mesh = RenderWorld.mesh_instance(self._rw, self._unit_id)
  127. RenderWorld.mesh_set_geometry(self._rw, mesh, mesh_resource, geometry)
  128. RenderWorld.mesh_set_material(self._rw, mesh, material)
  129. RenderWorld.mesh_set_visible(self._rw, mesh, visible)
  130. end
  131. function UnitBox:set_sprite(sprite_resource_name, material, layer, depth, visible)
  132. local sprite = RenderWorld.sprite_instance(self._rw, self._unit_id)
  133. RenderWorld.sprite_set_sprite(self._rw, sprite, sprite_resource_name)
  134. RenderWorld.sprite_set_material(self._rw, sprite, material)
  135. RenderWorld.sprite_set_layer(self._rw, sprite, layer)
  136. RenderWorld.sprite_set_depth(self._rw, sprite, depth)
  137. RenderWorld.sprite_set_visible(self._rw, sprite, visible)
  138. end
  139. function UnitBox:set_camera(projection, fov, near_range, far_range)
  140. local camera = World.camera_instance(self._world, self._unit_id)
  141. World.camera_set_projection_type(self._world, camera, projection)
  142. World.camera_set_fov(self._world, camera, fov)
  143. World.camera_set_near_clip_distance(self._world, camera, near_range)
  144. World.camera_set_far_clip_distance(self._world, camera, far_range)
  145. end