Sign in
creature

Duck

by Potluck · version 1 · Checked for all ages

A mother duck and her ducklings, who follow her everywhere along the creek.

1--!strict
2-- Duck: a mother duck and her ducklings along the creek. The ducklings
3-- follow her in a line. Two kinds of creature from one creation: they
4-- share this file, so the ducklings can always see where she is.
5
6local world = require("@potluck/world@0.10")
7local tweak = require("@potluck/tweak")
8
9local knobs = tweak {
10 ducklings = { 3, min = 0, max = 6, label = "Ducklings", order = 1 },
11 feathers = { "#8a6a4a", label = "Mother's feathers", order = 2 },
12 quack = { true, label = "Quacks", order = 3 },
13}
14
15local Duck = world.creature {
16 name = "Duck",
17 speed = 1.8,
18 count = 1,
19 home = "creek",
20 -- quacks now and then by itself, and hops when bopped
21 behaviors = {
22 chatter = if knobs.quack then { "Quack!", "Quack quack." } else nil,
23 sound = if knobs.quack then "quack" else nil,
24 hop_when_hit = 0.6,
25 },
26 body = {
27 { "body", size = { 0.5, 0.36, 0.7 }, at = { 0, 0.34, 0.04 }, color = knobs.feathers },
28 { "tail", size = { 0.26, 0.16, 0.16 }, at = { 0, 0.5, 0.42 }, color = knobs.feathers },
29 { "neck", size = { 0.18, 0.26, 0.16 }, at = { 0, 0.6, -0.26 }, color = knobs.feathers, head = true },
30 { "head", size = { 0.26, 0.24, 0.28 }, at = { 0, 0.82, -0.3 }, color = "#6b5038", head = true },
31 { "bill", size = { 0.16, 0.08, 0.18 }, at = { 0, 0.78, -0.5 }, color = "#e8a13a", head = true },
32 { "eye_left", size = { 0.09, 0.095, 0.06 }, at = { -0.13, 0.86, -0.36 }, color = "#f6f2e6", head = true },
33 { "pupil_left", size = { 0.05, 0.063, 0.06 }, at = { -0.13, 0.857, -0.401 }, color = "#1b1b1b", head = true },
34 { "eye_right", size = { 0.09, 0.095, 0.06 }, at = { 0.13, 0.86, -0.36 }, color = "#f6f2e6", head = true },
35 { "pupil_right", size = { 0.05, 0.063, 0.06 }, at = { 0.13, 0.857, -0.401 }, color = "#1b1b1b", head = true },
36 { "foot_left", size = { 0.12, 0.16, 0.14 }, at = { -0.12, 0.08, 0 }, color = "#e8a13a", leg = true },
37 { "foot_right", size = { 0.12, 0.16, 0.14 }, at = { 0.12, 0.08, 0 }, color = "#e8a13a", leg = true },
38 },
39}
40
41local Duckling = world.creature {
42 name = "Duckling",
43 speed = 2.2,
44 count = math.floor(knobs.ducklings),
45 home = "creek",
46 behaviors = { chatter = { "Peep!" }, sound = "chirp", hop_when_hit = 0.4 },
47 body = {
48 { "body", size = { 0.28, 0.22, 0.34 }, at = { 0, 0.2, 0 }, color = "#f2d14b" },
49 { "head", size = { 0.18, 0.18, 0.18 }, at = { 0, 0.4, -0.14 }, color = "#f2d14b", head = true },
50 { "bill", size = { 0.1, 0.05, 0.1 }, at = { 0, 0.38, -0.27 }, color = "#e8a13a", head = true },
51 { "foot_left", size = { 0.07, 0.1, 0.08 }, at = { -0.07, 0.05, 0 }, color = "#e8a13a", leg = true },
52 { "foot_right", size = { 0.07, 0.1, 0.08 }, at = { 0.07, 0.05, 0 }, color = "#e8a13a", leg = true },
53 },
54}
55
56-- where she is and which way she went, for the ducklings to follow
57local mom: vector? = nil
58local trail: { vector } = {}
59
60function Duck:tick(dt: number)
61 self:wander()
62 mom = self.position
63 local last = trail[1]
64 if not last or self:distance_to(last) > 0.8 then
65 table.insert(trail, 1, self.position)
66 if #trail > 12 then
67 table.remove(trail)
68 end
69 end
70end
71
72function Duck:hit(player)
73 self:say("QUACK!")
74end
75
76function Duckling:spawned()
77 -- each one knows its place in the line
78 self.place = #world.creatures(Duckling)
79end
80
81function Duckling:tick(dt: number)
82 if not mom then
83 self:wander()
84 return
85 end
86 local spot = trail[math.min(#trail, 2 + self.place * 2)] or mom
87 if self:distance_to(spot) > 0.6 then
88 self:walk_to(spot, 2.4)
89 else
90 self:stop()
91 end
92end
93
94function Duckling:used(player)
95 self:hop(0.4)
96end