local ANIMATIONS = { "Dance_Loop", "Jog_Fwd_Loop", "Idle_Loop", "Walk_Loop", } local WIDTH = 20 local function spawn(self, amount) for i=1,amount do local count = #self.models + 1 local div = count / (WIDTH * WIDTH) local mod = count % (WIDTH * WIDTH) local x = mod % WIDTH local z = math.floor(mod / WIDTH) local y = math.floor(div) local pos = vmath.vector3(x * 1, y * 3, z * -1) + vmath.vector3(-WIDTH / 2, 0, 0) local id = factory.create("#factory", pos) if not id then break end local model_url = msg.url(nil, id, "model") local animation_id = math.random(1, #ANIMATIONS) model.play_anim(model_url, ANIMATIONS[animation_id], go.PLAYBACK_LOOP_FORWARD) self.models[#self.models + 1] = id end end local function remove(self, amount) for i=1, amount do local id = self.models[#self.models] if id then go.delete(id) self.models[#self.models] = nil end end end function init(self) msg.post(".", "acquire_input_focus") math.randomseed(os.clock()) self.models = {} local debug_ui = false if debug_ui then spawn(self, 1) else go.delete("controls") spawn(self, 1000) end end function on_input(self, action_id, action) if action_id == hash("EXAMPLE_1") and action.pressed then spawn(self, 100) end end function on_message(self, message_id, message, sender) if message_id == hash("add") then spawn(self, message.amount) elseif message_id == hash("remove") then remove(self, message.amount) end end