instancing.script 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. local ANIMATIONS = {
  2. "Dance_Loop",
  3. "Jog_Fwd_Loop",
  4. "Idle_Loop",
  5. "Walk_Loop",
  6. }
  7. local WIDTH = 20
  8. local function spawn(self, amount)
  9. for i=1,amount do
  10. local count = #self.models + 1
  11. local div = count / (WIDTH * WIDTH)
  12. local mod = count % (WIDTH * WIDTH)
  13. local x = mod % WIDTH
  14. local z = math.floor(mod / WIDTH)
  15. local y = math.floor(div)
  16. local pos = vmath.vector3(x * 1, y * 3, z * -1) + vmath.vector3(-WIDTH / 2, 0, 0)
  17. local id = factory.create("#factory", pos)
  18. if not id then
  19. break
  20. end
  21. local model_url = msg.url(nil, id, "model")
  22. local animation_id = math.random(1, #ANIMATIONS)
  23. model.play_anim(model_url, ANIMATIONS[animation_id], go.PLAYBACK_LOOP_FORWARD)
  24. self.models[#self.models + 1] = id
  25. end
  26. end
  27. local function remove(self, amount)
  28. for i=1, amount do
  29. local id = self.models[#self.models]
  30. if id then
  31. go.delete(id)
  32. self.models[#self.models] = nil
  33. end
  34. end
  35. end
  36. function init(self)
  37. msg.post(".", "acquire_input_focus")
  38. math.randomseed(os.clock())
  39. self.models = {}
  40. local debug_ui = false
  41. if debug_ui then
  42. spawn(self, 1)
  43. else
  44. go.delete("controls")
  45. spawn(self, 1000)
  46. end
  47. end
  48. function on_input(self, action_id, action)
  49. if action_id == hash("EXAMPLE_1") and action.pressed then
  50. spawn(self, 100)
  51. end
  52. end
  53. function on_message(self, message_id, message, sender)
  54. if message_id == hash("add") then
  55. spawn(self, message.amount)
  56. elseif message_id == hash("remove") then
  57. remove(self, message.amount)
  58. end
  59. end