Old music boxes turn up in the island's lost places: a gnome's in the diggings, a brass one in the workshops' vaults, one of moonsilver behind the moon seal and a little wooden one in old chests. Hold one and Interact looking at the sky to wind it, and it plays its tune for everyone near.
| 1 | --!strict |
| 2 | -- Music boxes: old music boxes turn up in the island's lost places (a |
| 3 | -- gnome's in the Gnome Diggings' crates, a brass one in the Lost Workshops' |
| 4 | -- vaults, one of moonsilver in the Moonsilver Vaults' coffers and a little |
| 5 | -- wooden one in Mining's old chests, each listed among their prizes). Each |
| 6 | -- is an item of this creation's. Hold one and Interact looking at the sky |
| 7 | -- to wind it, and it plays its tune where you stand for everyone near: two |
| 8 | -- halves, a sound each in potluck.toml, so a remix can write its own. |
| 9 | |
| 10 | local world = require("@potluck/world@0.27") |
| 11 | local ui = require("@potluck/ui@0.2") |
| 12 | local sound = require("@potluck/sound@0.2") |
| 13 | |
| 14 | -- each box: its tune's name, and the sounds that play its two halves and how |
| 15 | -- long each is |
| 16 | type Box = { tune: string, halves: { string }, each: number } |
| 17 | local BOXES: { [string]: Box } = { |
| 18 | ["Brass music box"] = { tune = "Island waltz", halves = { "brass_a", "brass_b" }, each = 3.0 }, |
| 19 | ["Gnome music box"] = { tune = "Digger's jig", halves = { "gnome_a", "gnome_b" }, each = 3.0 }, |
| 20 | ["Moonsilver music box"] = { tune = "Lullaby of the deep", halves = { "moonsilver_a", "moonsilver_b" }, each = 3.0 }, |
| 21 | ["Wooden music box"] = { tune = "Potluck polka", halves = { "wooden_a", "wooden_b" }, each = 3.0 }, |
| 22 | } |
| 23 | |
| 24 | -- who's playing one now, so winding it again waits for the tune to end |
| 25 | local playing: { [number]: boolean } = {} |
| 26 | |
| 27 | local function tell(player, text: string) |
| 28 | ui.show(player, "music", text, { at = "bottom" }) |
| 29 | world.after(4, function() |
| 30 | ui.hide(player, "music") |
| 31 | end) |
| 32 | end |
| 33 | |
| 34 | world.on("item_used", function(player, item: string?) |
| 35 | local box = item and BOXES[item] |
| 36 | if not box then |
| 37 | return |
| 38 | end |
| 39 | if playing[player.id] then |
| 40 | return |
| 41 | end |
| 42 | playing[player.id] = true |
| 43 | tell(player, "The " .. string.lower(item :: string) .. " plays " .. box.tune .. ".") |
| 44 | for i, half in box.halves do |
| 45 | world.after((i - 1) * box.each, function() |
| 46 | -- where the player is now, if they're still here |
| 47 | for _, p in world.players() do |
| 48 | if p.id == player.id then |
| 49 | sound.play(half, { at = p.position, volume = 0.7 }) |
| 50 | end |
| 51 | end |
| 52 | end) |
| 53 | end |
| 54 | world.after(#box.halves * box.each, function() |
| 55 | playing[player.id] = nil |
| 56 | end) |
| 57 | end) |
| 58 | |
| 59 | world.on("player_left", function(p) |
| 60 | playing[p.id] = nil |
| 61 | end) |