Soft little lights that bob about the Green and drift over to anyone standing still.
| 1 | --!strict |
| 2 | -- Glowbug: soft little lights that bob about the Green. They drift over |
| 3 | -- to anyone nearby, and scatter when bopped. Change the glow in Tweak. |
| 4 | |
| 5 | local world = require("@potluck/world") |
| 6 | local tweak = require("@potluck/tweak") |
| 7 | local sound = require("@potluck/sound@0.1") |
| 8 | |
| 9 | local knobs = tweak { |
| 10 | glow = { "#ffd76a", label = "Glow color", order = 1 }, |
| 11 | bob = { 0.5, min = 0, max = 1.5, label = "Bob height", order = 2 }, |
| 12 | swarm = { 8, min = 1, max = 16, label = "How many", order = 3 }, |
| 13 | } |
| 14 | |
| 15 | local Glowbug = world.creature { |
| 16 | name = "Glowbug", |
| 17 | speed = 1.4, |
| 18 | count = math.floor(knobs.swarm), |
| 19 | home = "green", |
| 20 | body = { |
| 21 | { "glow", size = { 0.26, 0.22, 0.32 }, at = { 0, 0.3, 0.04 }, color = knobs.glow, glow = true }, |
| 22 | { "head", size = { 0.16, 0.14, 0.14 }, at = { 0, 0.32, -0.18 }, color = "#2d2a24", head = true }, |
| 23 | { "wing_left", size = { 0.24, 0.03, 0.16 }, at = { -0.16, 0.44, 0 }, color = "#e9f2ff", leg = true }, |
| 24 | { "wing_right", size = { 0.24, 0.03, 0.16 }, at = { 0.16, 0.44, 0 }, color = "#e9f2ff", leg = true }, |
| 25 | }, |
| 26 | } |
| 27 | |
| 28 | function Glowbug:spawned() |
| 29 | self.bob_in = world.random(0, 2) |
| 30 | end |
| 31 | |
| 32 | function Glowbug:tick(dt: number) |
| 33 | -- a little hop now and then keeps it bobbing in the air |
| 34 | self.bob_in -= dt |
| 35 | if self.bob_in <= 0 then |
| 36 | self.bob_in = world.random(0.8, 2.2) |
| 37 | if knobs.bob > 0 then |
| 38 | self:hop(knobs.bob) |
| 39 | end |
| 40 | end |
| 41 | local p = world.nearest_player(self.position, 8) |
| 42 | if p and self:distance_to(p.position) > 2 then |
| 43 | self:walk_to(p.position + vector.create(world.random(-1, 1), 0, world.random(-1, 1)), 1.2) |
| 44 | else |
| 45 | self:wander(1) |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | function Glowbug:hit(player) |
| 50 | local away = self.position + (self.position - player.position) * 3 |
| 51 | self:walk_to(away, 4) |
| 52 | self:hop(1.2) |
| 53 | sound.play("sparkle", { at = self, volume = 0.7 }) |
| 54 | end |