harvest/game/scripts/toast_feed.gd
Forrest aaf5a81b4e Task #21: animals roam the whole farm, never lost on their own
Loose animals now roam the whole playable map, day and night. Each walk
goes to a goal 4-8 m away that is walkable, in a clear straight line,
at least 2 m inside FarmZones.playable (passed into AnimalSystem.setup),
and neither inside the pen nor across it. Every morning a loose animal
is back at its loose spot (no save change).

Animals no longer escape on their own:
- _stray and LootSystem.animal_escaped are gone.
- A new LootSystem.animal_loose makes an animal LOOSE where it stands
  and unpens it. It covers a carrier killed or scared (drop), a broken
  follow leash, and an animal getting out of an open pen (animal_strays,
  which still records animals_strayed).
- An animal dropped inside the pen is home again.
- An animal let out of an open pen walks out along the escape field,
  then roams. If the pen shuts first, it stays home.
- return_dropped still pens followers at dawn's end, but leaves loose
  animals loose.
- The only loss paths left are a carrier leaving the farm
  (thief escape, or removal with reason LEFT).

Loose spots at x = -12 move inward to (6|9|12, 0.5). The toast,
review-panel, dropped-animal toast, RULES.md and header text no longer
promise animals wander off or run for the edge.

test_targets: _loose_start has a 90 s day + 90 s night roam run (every
animal >4 m, never off the map or into the pen, none lost), a morning
reset to the spots, a killed carrier leaving the animal loose, and an
unopposed thief carrying one off. _animal_theft, _leading, _breach and
_make_loose follow the new rules. AWAY moves off the map so roaming
animals can't start following the parked scarecrow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 13:07:13 -04:00

95 lines
3.4 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name ToastFeed
extends VBoxContainer
## Short event messages stacked under the zone banner, each fading after a few seconds. A fixed
## pool of labels is reused (the oldest message gives way). A message that's already showing
## isn't repeated: it counts up instead ("A crow flew in from the east ×4") and stays longer,
## so a flock arriving is one line.
const LIFE := 3.2
const FADE := 0.6
const SLOTS := 5
## Loot events per category (LootSystem.Category): the text ("%s" is the item's noun) and color.
const MESSAGES := {
&"stolen": [["A crop was snatched!", "A %s was grabbed!", "Aliens took %s from the shed!"], Color(1.0, 0.6, 0.15)],
&"dropped": [["The alien dropped the crop. Grab it!", "The alien dropped the %s. Lead it back to the pen!",
"The alien dropped %s. Grab it!"], Color(1.0, 0.9, 0.3)],
&"recovered": [["Crop recovered", "The %s is back in the pen", "%s back in the shed"], Color(0.5, 1.0, 0.45)],
&"returned": [["A dropped crop went back to its plot", "The %s went back to the pen", "%s went back to the shed"],
Color(0.85, 0.95, 1.0)],
&"lost": [["A crop was carried off the farm", "A %s was lost", "%s was carried off the farm"], Color(1.0, 0.3, 0.25)],
&"following": [["", "The %s follows you: lead it back to the pen", ""], Color(0.85, 1.0, 0.7)],
&"follow_full": [["", "You can only lead %d animals at a time", ""], Color(1.0, 0.8, 0.5)],
&"strayed": [["", "A %s wandered out of the open pen!", ""], Color(1.0, 0.6, 0.15)],
}
## Set by FarmSim: what an event's item is.
var loot: LootSystem
var _labels: Array[Label] = []
var _left := PackedFloat32Array()
var _base: Array[String] = [] # each slot's message without its count
var _count := PackedInt32Array()
func _ready() -> void:
set_anchors_preset(Control.PRESET_TOP_WIDE)
offset_top = 152.0
mouse_filter = Control.MOUSE_FILTER_IGNORE
alignment = BoxContainer.ALIGNMENT_BEGIN
_left.resize(SLOTS)
_count.resize(SLOTS)
_base.resize(SLOTS)
for i in SLOTS:
var l := Label.new()
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
l.add_theme_font_size_override("font_size", 26)
l.add_theme_color_override("font_outline_color", Color.BLACK)
l.add_theme_constant_override("outline_size", 7)
l.visible = false
add_child(l)
_labels.append(l)
func push(text: String, color: Color) -> void:
for i in SLOTS:
if _left[i] > FADE and _base[i] == text:
_count[i] += 1
_labels[i].text = "%s ×%d" % [text, _count[i]]
_left[i] = LIFE
return
var slot := 0
for i in SLOTS:
if _left[i] < _left[slot]:
slot = i
var l := _labels[slot]
_base[slot] = text
_count[slot] = 1
l.text = text
l.add_theme_color_override("font_color", color)
l.visible = true
_left[slot] = LIFE
move_child(l, get_child_count() - 1) # newest at the bottom
func on_loot_event(kind: StringName, id: int, _position: Vector3) -> void:
if not MESSAGES.has(kind):
return
var m: Array = MESSAGES[kind]
var c := int(loot.category[id]) if loot != null else LootSystem.Category.CROP
var text: String = m[0][c]
if text == "":
return
if text.contains("%d"):
text = text % loot.config.max_following_animals
elif text.contains("%s"):
text = text % (loot.noun(id) if loot != null else "item")
push(text[0].to_upper() + text.substr(1), m[1])
func _process(delta: float) -> void:
for i in SLOTS:
if _left[i] <= 0.0:
continue
_left[i] = maxf(_left[i] - delta, 0.0)
_labels[i].visible = _left[i] > 0.0
_labels[i].modulate.a = clampf(_left[i] / FADE, 0.0, 1.0)