95 lines
3.4 KiB
GDScript
95 lines
3.4 KiB
GDScript
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!", "Thieves took %s from the shed!"], Color(1.0, 0.6, 0.15)],
|
||
&"dropped": [["The thief dropped the crop. Grab it!", "The thief dropped the %s. Catch it before it runs off!",
|
||
"The thief 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)
|