tree.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -- Copyright (c) 2012-2026 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. -- Note: the following table must be global and uniquely named.
  4. Tree = Tree or {
  5. data = {}
  6. }
  7. local data = Tree.data
  8. -- Called after units are spawned into a world.
  9. function Tree.spawned(world, units)
  10. if data[world] == nil then
  11. data[world] = {}
  12. end
  13. local world_data = data[world]
  14. for _, unit in pairs(units) do
  15. -- Store instance-specific data.
  16. if world_data[unit] == nil then
  17. world_data[unit] = {}
  18. end
  19. -- Set sprite depth based on unit's position.
  20. local sg = World.scene_graph(world)
  21. local rw = World.render_world(world)
  22. local tr = SceneGraph.instance(sg, unit)
  23. local pos = SceneGraph.local_position(sg, tr)
  24. local depth = math.floor(1000 + (1000 - 32*pos.y))
  25. local sprite = RenderWorld.sprite_instance(rw, unit)
  26. RenderWorld.sprite_set_depth(rw, sprite, depth)
  27. end
  28. end
  29. -- Called once per frame.
  30. function Tree.update(world, dt)
  31. local world_data = data[world]
  32. for unit, unit_data in pairs(world_data) do
  33. -- Update unit.
  34. end
  35. end
  36. -- Called before units are unspawned from a world.
  37. function Tree.unspawned(world, units)
  38. local world_data = data[world]
  39. -- Cleanup.
  40. for _, unit in pairs(units) do
  41. if world_data[unit] then
  42. world_data[unit] = nil
  43. end
  44. end
  45. end
  46. return Tree