Sign in
creature

Glimmerflies

by Potluck · version 1 · Checked for all ages

A glimmer cocoon turns up now and then in a gloom nest or a crystal bud. Hold it and Interact looking at the sky, and a glimmerfly comes out: a little moth that glows, follows you around and lights the dark around you, so gloomlings keep away. Use it to have it stay and light a spot, or follow again.

1--!strict
2-- Glimmerflies: a glimmer cocoon turns up now and then in a gloom nest
3-- (Gloom Hollow) or a crystal bud (Crystal Grottos). Hold it and Interact
4-- looking at the sky, and a glimmerfly comes out: a little moth whose
5-- glowing tail lights what's around it (a part's `light`, world 0.27), so
6-- world.find_light finds it and gloomlings and crawlers keep away. It
7-- follows whoever hatched it, a little over their head, and comes back
8-- to them if they get far ahead. Use it to have it stay where it is and
9-- light the spot, or to follow again. Each player has one, kept by name
10-- in the store, and it comes back with them when they come back.
11
12local world = require("@potluck/world@0.27")
13local tweak = require("@potluck/tweak")
14local store = require("@potluck/store")
15local ui = require("@potluck/ui@0.2")
16local sound = require("@potluck/sound@0.2")
17
18local knobs = tweak {
19 bright = { 0.55, min = 0.1, max = 1, label = "How brightly a glimmerfly glows", order = 1 },
20 glow = { "#b8ffd0", label = "Its glow's color", order = 2 },
21}
22
23local COCOON = "Glimmer cocoon"
24local FAR = 20 -- further from its player than this, it comes back to them
25local ABOVE = 2.1 -- how high over its player's feet it flies
26
27local Glimmerfly = world.creature {
28 name = "Glimmerfly",
29 flies = true,
30 speed = 4,
31 count = 0,
32 home = "here",
33 size = { 0.4, 0.4, 0.4 },
34 body = {
35 { "body", size = { 0.12, 0.12, 0.26 }, at = { 0, 0.2, 0 }, color = "#3a4a40" },
36 { "head", size = { 0.12, 0.11, 0.1 }, at = { 0, 0.22, -0.17 }, color = "#2a3530", head = true },
37 { "eye", size = { 0.03, 0.03, 0.02 }, at = { -0.04, 0.24, -0.225 }, color = "#101010" },
38 { "eye", size = { 0.03, 0.03, 0.02 }, at = { 0.04, 0.24, -0.225 }, color = "#101010" },
39 { "wing", size = { 0.3, 0.02, 0.2 }, at = { -0.2, 0.27, -0.02 }, color = "#dceef4", leg = true },
40 { "wing", size = { 0.3, 0.02, 0.2 }, at = { 0.2, 0.27, -0.02 }, color = "#dceef4", leg = true },
41 { "tail", size = { 0.14, 0.14, 0.16 }, at = { 0, 0.19, 0.19 }, color = knobs.glow, light = knobs.bright },
42 },
43}
44
45-- ---- what's kept ----
46
47-- each player's glimmerfly, by their name: where it stays, if it's been
48-- told to stay
49type Pet = { stay: vector? }
50local pets: { [string]: Pet } = store.get("pets") or {}
51
52local function save()
53 store.set("pets", pets)
54end
55
56-- whose each glimmerfly is, by its id, until its first tick keeps it
57local owner_of: { [number]: string } = {}
58
59local function tell(player, text: string)
60 ui.show(player, "glimmer", text, { at = "bottom" })
61 world.after(4, function()
62 ui.hide(player, "glimmer")
63 end)
64end
65
66local function player_named(name: string)
67 for _, p in world.players() do
68 if p.name == name then
69 return p
70 end
71 end
72 return nil
73end
74
75-- the glimmerfly that's this player's, if it's out
76local function fly_of(name: string): number?
77 for _, id in world.creatures(Glimmerfly) do
78 if owner_of[id] == name then
79 return id
80 end
81 end
82 return nil
83end
84
85-- what a glimmerfly flies through
86local function open(at: vector): boolean
87 local b = world.block(at)
88 return b == "Air" or b == "Water"
89end
90
91-- where it flies over a player: over their head, or lower where the roof
92-- is low (in a tunnel)
93local function over(p, x: number, z: number): vector
94 for _, h in { ABOVE, 1.5, 1.0 } do
95 local at = p.position + vector.create(x, h, z)
96 if open(at) then
97 return at
98 end
99 end
100 return p.position + vector.create(0, 1.0, 0)
101end
102
103local function bring(name: string, at: vector)
104 owner_of[world.spawn(Glimmerfly, at)] = name
105end
106
107-- out around each player who has one, where it stays or by them
108local function bring_out(p)
109 local pet = pets[p.name]
110 if pet and not fly_of(p.name) then
111 bring(p.name, pet.stay or over(p, 0.8, 0.6))
112 end
113end
114
115-- ---- flying ----
116
117function Glimmerfly:tick(dt: number)
118 if not self.owner then
119 self.owner = owner_of[self.id]
120 end
121 local name = self.owner :: string?
122 if not name then
123 self:remove()
124 return
125 end
126 owner_of[self.id] = name
127 local p = player_named(name)
128 local pet = pets[name]
129 if not p or not pet then
130 owner_of[self.id] = nil
131 self:remove()
132 return
133 end
134 local want: vector
135 if pet.stay then
136 -- hovering where it was told, rising and falling a little
137 want = pet.stay + vector.create(0, math.sin(world.time() * 1.1 + self.id) * 0.12, 0)
138 else
139 -- a slow circle over its player's head
140 local a = world.time() * 0.35 + self.id
141 want = over(p, math.cos(a) * 1.1, math.sin(a) * 1.1) + vector.create(0, math.sin(world.time() * 1.3) * 0.12, 0)
142 if vector.magnitude(want - self.position) > FAR then
143 -- left behind: it finds them again
144 owner_of[self.id] = nil
145 self:remove()
146 bring(name, want)
147 return
148 end
149 end
150 local off = want - self.position
151 local far = vector.magnitude(vector.create(off.x, 0, off.z))
152 if far > 0.4 then
153 self:walk_to(want, if far > 5 then 7 else 3)
154 end
155 self:climb(math.clamp(off.y * 1.5, -3, 3))
156end
157
158function Glimmerfly:used(player)
159 local name = self.owner :: string?
160 if name ~= player.name then
161 tell(player, if name then name .. "'s glimmerfly." else "A glimmerfly.")
162 return
163 end
164 local pet = pets[name]
165 if not pet then
166 return
167 end
168 if pet.stay then
169 pet.stay = nil
170 tell(player, "Your glimmerfly follows you again.")
171 else
172 pet.stay = self.position
173 tell(player, "Your glimmerfly stays here. Use it again to bring it.")
174 end
175 save()
176 sound.play("flutter", { at = self.position, volume = 0.5 })
177end
178
179-- ---- hatching ----
180
181world.on("item_used", function(player, item: string?)
182 if item ~= COCOON then
183 return
184 end
185 if pets[player.name] then
186 tell(player, "Your glimmerfly is with you already.")
187 return
188 end
189 if not world.take(player, COCOON, 1) then
190 return
191 end
192 pets[player.name] = {}
193 save()
194 bring(player.name, over(player, 0, -0.6))
195 sound.play("flutter", { at = player.position, volume = 0.6 })
196 sound.play("chime", { at = player.position, volume = 0.3, pitch = 1.5, caption = "A cocoon opens" })
197 tell(player, "A glimmerfly comes out of the cocoon to light your way.")
198end)
199
200-- it goes when its player does (on its next tick), and comes back with them
201world.on("player_joined", bring_out)
202
203-- after a reload, the glimmerflies still out keep their owners (each
204-- keeps its own as a field); anyone here whose isn't out gets it back
205world.after(0.2, function()
206 for _, p in world.players() do
207 bring_out(p)
208 end
209end)