A little robot on the Green that runs from anyone close. Catch it and tag it: the most tags go on the board.
| 1 | --!strict |
| 2 | -- Tag Bot: a mini game. A little robot runs from anyone who comes close; |
| 3 | -- catch it and hit it to tag it. Each player's tags go on the board, and |
| 4 | -- the world keeps count (in the store) even after it restarts. A tag shows |
| 5 | -- your count on your screen for a while, and Interact with it shows its |
| 6 | -- board (both from @potluck/ui). |
| 7 | |
| 8 | local world = require("@potluck/world@0.2") |
| 9 | local tweak = require("@potluck/tweak") |
| 10 | local game = require("@potluck/game@0.1") |
| 11 | local store = require("@potluck/store@0.1") |
| 12 | local ui = require("@potluck/ui@0.1") |
| 13 | local sound = require("@potluck/sound@0.1") |
| 14 | |
| 15 | local knobs = tweak { |
| 16 | paint = { "#d9dde3", label = "Paint", order = 1 }, |
| 17 | light = { "#5ad1ff", label = "Light color", order = 2 }, |
| 18 | speed = { 4.2, min = 1, max = 8, label = "Running speed", order = 3 }, |
| 19 | wary = { 7, min = 2, max = 16, label = "How close before it runs", order = 4 }, |
| 20 | } |
| 21 | |
| 22 | local Bot = world.creature { |
| 23 | name = "Tag Bot", |
| 24 | speed = 4.5, |
| 25 | count = 1, |
| 26 | home = "green", |
| 27 | body = { |
| 28 | { "body", size = { 0.6, 0.6, 0.5 }, at = { 0, 0.6, 0 }, color = knobs.paint }, |
| 29 | { "head", size = { 0.5, 0.36, 0.44 }, at = { 0, 1.1, 0 }, color = knobs.paint, head = true }, |
| 30 | { "eyes", size = { 0.36, 0.1, 0.04 }, at = { 0, 1.12, -0.23 }, color = knobs.light, head = true, glow = true }, |
| 31 | { "antenna", size = { 0.05, 0.24, 0.05 }, at = { 0, 1.4, 0 }, color = "#6b7078", head = true }, |
| 32 | { "bulb", size = { 0.12, 0.12, 0.12 }, at = { 0, 1.56, 0 }, color = knobs.light, head = true, glow = true }, |
| 33 | { "wheel_left", size = { 0.12, 0.3, 0.3 }, at = { -0.34, 0.15, 0 }, color = "#2b2f36", leg = true }, |
| 34 | { "wheel_right", size = { 0.12, 0.3, 0.3 }, at = { 0.34, 0.15, 0 }, color = "#2b2f36", leg = true }, |
| 35 | }, |
| 36 | } |
| 37 | |
| 38 | -- tags this world has seen, by player name, kept with the world |
| 39 | local tags: { [string]: number } = store.get("tags") or {} |
| 40 | |
| 41 | world.on("player_joined", function(player) |
| 42 | local n = tags[player.name] |
| 43 | if n then |
| 44 | world.say("Tag Bot remembers " .. player.name .. ": " .. n .. " tags so far.") |
| 45 | end |
| 46 | end) |
| 47 | |
| 48 | -- your count on your screen, for a few seconds after each tag |
| 49 | local hiding: { [number]: number } = {} |
| 50 | local function show_tags(player, n: number) |
| 51 | ui.show(player, "tags", "Tag Bot: " .. n .. (if n == 1 then " tag" else " tags")) |
| 52 | if hiding[player.id] then |
| 53 | world.cancel(hiding[player.id]) |
| 54 | end |
| 55 | hiding[player.id] = world.after(8, function() |
| 56 | ui.hide(player, "tags") |
| 57 | hiding[player.id] = nil |
| 58 | end) |
| 59 | end |
| 60 | |
| 61 | function Bot:spawned() |
| 62 | self.dizzy = 0 |
| 63 | end |
| 64 | |
| 65 | function Bot:tick(dt: number) |
| 66 | if self.dizzy > 0 then |
| 67 | self.dizzy -= dt |
| 68 | self:stop() |
| 69 | return |
| 70 | end |
| 71 | local p = world.nearest_player(self.position, knobs.wary) |
| 72 | if p then |
| 73 | -- away from whoever's closest, but never too far from the Green |
| 74 | local away = self.position + (self.position - p.position) * 1.5 |
| 75 | local green = world.spot("green") |
| 76 | if green and (away - green).x ^ 2 + (away - green).z ^ 2 > 400 then |
| 77 | away = green + vector.create(world.random(-6, 6), 0, world.random(-6, 6)) |
| 78 | end |
| 79 | self:walk_to(away, knobs.speed) |
| 80 | else |
| 81 | self:wander() |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | function Bot:hit(player) |
| 86 | if self.dizzy > 0 then |
| 87 | return |
| 88 | end |
| 89 | local n = (tags[player.name] or 0) + 1 |
| 90 | tags[player.name] = n |
| 91 | store.set("tags", tags) |
| 92 | game.score("tags", player, n) |
| 93 | show_tags(player, n) |
| 94 | self.dizzy = 1.5 |
| 95 | self:hop(0.8) |
| 96 | self:say("Tagged! That's " .. n .. " for " .. player.name .. ".") |
| 97 | sound.play("beep", { at = self }) |
| 98 | end |
| 99 | |
| 100 | function Bot:used(player) |
| 101 | local top = game.board("tags", 5) |
| 102 | if #top == 0 then |
| 103 | self:say("Beep! Nobody's caught me yet.") |
| 104 | sound.play("beep", { at = self, pitch = 1.2 }) |
| 105 | return |
| 106 | end |
| 107 | local lines = {} |
| 108 | for i, s in top do |
| 109 | table.insert(lines, i .. ". " .. s.name .. ": " .. math.floor(s.value)) |
| 110 | end |
| 111 | self:say("Beep! Here's my board.") |
| 112 | sound.play("beep", { at = self, pitch = 1.2 }) |
| 113 | ui.ask(player, { title = "Who's tagged Tag Bot most", text = table.concat(lines, "\n"), choices = { "OK" } }, function() end) |
| 114 | end |