Sign in
creature

Moon jellies

by Potluck · version 1 · Checked for all ages

Pale jellyfish that drift and pulse in deep water and glow softly, so the sea fills with little lights after dark. Don't swim into one in survival: it stings a little, though never too much. Bop one and it drifts away.

1--!strict
2-- Moon jellies: pale jellyfish that drift in deep water (swims, world
3-- 0.31), pulsing slowly up and sinking back, and glowing softly (a part's
4-- light, world 0.27), so the sea is full of little lights after dark. More
5-- come at night. Swim into one in survival and it stings a little, never
6-- below a few hearts (world.hurt's down_to). Bop one and it drifts off.
7
8local world = require("@potluck/world@0.33")
9local tweak = require("@potluck/tweak")
10local sound = require("@potluck/sound@0.2")
11
12local knobs = tweak {
13 how_many = { 3, min = 0, max = 8, label = "How many drift near you by day (0 for none)", order = 1 },
14 night = { 6, min = 0, max = 12, label = "And by night", order = 2 },
15 sting = { 1, min = 0, max = 4, label = "How hard one stings", order = 3 },
16 glow = { 0.35, min = 0, max = 1, label = "How brightly they glow", order = 4 },
17 color = { "#c8d8f0", label = "Jelly color", order = 5 },
18}
19
20local Jelly = world.creature {
21 name = "Moon jelly",
22 speed = 0.8,
23 count = 0,
24 home = "here",
25 swims = true,
26 size = { 0.5, 0.6, 0.5 },
27 behaviors = { wander = false },
28 body = {
29 { "bell", size = { 0.6, 0.3, 0.6 }, at = { 0, 0.5, 0 }, color = knobs.color, light = knobs.glow },
30 { "bell_top", size = { 0.4, 0.1, 0.4 }, at = { 0, 0.7, 0 }, color = knobs.color, glow = true },
31 { "ring_1", size = { 0.1, 0.04, 0.1 }, at = { -0.1, 0.66, -0.1 }, color = "#e8b8d8", glow = true },
32 { "ring_2", size = { 0.1, 0.04, 0.1 }, at = { 0.1, 0.66, -0.1 }, color = "#e8b8d8", glow = true },
33 { "ring_3", size = { 0.1, 0.04, 0.1 }, at = { -0.1, 0.66, 0.1 }, color = "#e8b8d8", glow = true },
34 { "ring_4", size = { 0.1, 0.04, 0.1 }, at = { 0.1, 0.66, 0.1 }, color = "#e8b8d8", glow = true },
35 { "arms", size = { 0.24, 0.2, 0.24 }, at = { 0, 0.28, 0 }, color = "#e0e8f8", leg = true },
36 { "trail_1", size = { 0.03, 0.34, 0.03 }, at = { -0.2, 0.18, -0.2 }, color = knobs.color, leg = true },
37 { "trail_2", size = { 0.03, 0.34, 0.03 }, at = { 0.2, 0.18, -0.2 }, color = knobs.color, leg = true },
38 { "trail_3", size = { 0.03, 0.34, 0.03 }, at = { -0.2, 0.18, 0.2 }, color = knobs.color, leg = true },
39 { "trail_4", size = { 0.03, 0.34, 0.03 }, at = { 0.2, 0.18, 0.2 }, color = knobs.color, leg = true },
40 },
41}
42
43local MOST = 32
44local NEAR, FAR = 6, 20
45local LONELY = 44
46local DEEP = 4
47local STING_EVERY = 2
48local UP = vector.create(0, 1, 0)
49
50local where: { [number]: vector } = {}
51local stung: { [number]: number } = {} -- when each player was last stung
52
53local function water(at: vector): boolean
54 return world.block(at) == "Water"
55end
56
57local function gone(self)
58 where[self.id] = nil
59 self:remove()
60end
61
62function Jelly:spawned()
63 self.pulse = world.random(0, 3)
64 self.think = world.random(0, 1)
65end
66
67function Jelly:tick(dt: number)
68 where[self.id] = self.position
69 -- a slow pulse up, then a long sink
70 self.pulse -= dt
71 if self.pulse <= 0 then
72 self.pulse = world.random(2.5, 4)
73 self.rising = 0.8
74 end
75 if (self.rising or 0) > 0 then
76 self.rising -= dt
77 self:climb(if water(self.position + UP * 1.2) then 0.9 else 0)
78 else
79 self:climb(-0.25)
80 end
81 self.think -= dt
82 if self.think > 0 then
83 return
84 end
85 self.think = world.random(0.4, 0.6)
86 if knobs.how_many + knobs.night < 1 or not world.nearest_player(self.position, LONELY) then
87 gone(self)
88 return
89 end
90 -- drifts, and stings whoever swims into it
91 if not self.drifting then
92 self:wander(0.5)
93 end
94 self.drifting = nil
95 if knobs.sting <= 0 then
96 return
97 end
98 local now = world.time()
99 for _, p in world.players() do
100 local d = vector.magnitude(p.position + UP * 0.8 - (self.position + UP * 0.4))
101 if d < 1.1 and now - (stung[p.id] or -10) > STING_EVERY then
102 if world.hurt(p, knobs.sting, { down_to = 6 }) then
103 stung[p.id] = now
104 sound.play_for(p, "sting", { caption = "A jelly stings you" })
105 end
106 end
107 end
108end
109
110function Jelly:hit(player)
111 local away = self.position - player.position
112 local flat = vector.create(away.x, 0, away.z)
113 if vector.magnitude(flat) > 0.1 then
114 self:walk_to(self.position + vector.normalize(flat) * 3, 1.5)
115 self.drifting = true
116 end
117end
118
119-- More drift in near each player by deep water, most at night.
120world.every(3, function()
121 local night = world.daylight() < 0.3
122 local want = math.floor(if night then knobs.night else knobs.how_many)
123 if want < 1 then
124 return
125 end
126 local live = {}
127 for _, id in world.creatures() do
128 live[id] = true
129 end
130 local total = 0
131 for id in where do
132 if live[id] then
133 total += 1
134 else
135 where[id] = nil
136 end
137 end
138 for _, p in world.players() do
139 -- the Long Dusk has creatures of its own
140 if world.place(p.position) == "dusk" then
141 continue
142 end
143 local near = 0
144 for _, at in where do
145 if vector.magnitude(at - p.position) < FAR + 6 then
146 near += 1
147 end
148 end
149 if total >= MOST or near >= want then
150 continue
151 end
152 for _ = 1, 3 do
153 local a = world.random(0, 2 * math.pi)
154 local r = world.random(NEAR, FAR)
155 local x, z = math.floor(p.position.x + math.cos(a) * r) + 0.5, math.floor(p.position.z + math.sin(a) * r) + 0.5
156 local floor = world.top(x, z) + 1
157 local n = 0
158 while n < 10 and water(vector.create(x, floor + n, z)) do
159 n += 1
160 end
161 if n >= DEEP then
162 local at = vector.create(x, floor + world.random(1, n - 1.5), z)
163 where[world.spawn(Jelly, at)] = at
164 total += 1
165 break
166 end
167 end
168 end
169end)
170
171world.on("player_left", function(p)
172 stung[p.id] = nil
173end)