harvest/game/scripts/shed_stock.gd
2026-10-07 13:41:38 -04:00

130 lines
4.9 KiB
GDScript

class_name ShedStock
extends Node3D
## The supply shed's stock piles, which grow and shrink with what the shed holds: logs for wood,
## crates for scrap, hay bales for straw. Each pile is a list of props (some already in the scene,
## the rest added here); a pile shows one prop per `per` units, up to all of them. Every prop has
## collision, and all of them are in place when the farm's navigation is built, so a pile's
## footprint never changes for enemies; a hidden prop only stops colliding for the scarecrow and
## shots. Each pile has a spot in front of it where supply thieves stand to fill their sacks. (How
## much the shed holds shows in the HUD's pouch panel, ResourceHud.)
const PREF := "res://assets/farm/ithappy/farm/prefabs/assets/"
## material -> adopt (scene props under the shed zone, in fill order), extra ([prefab, world
## position, yaw in degrees, scale]), per (units per prop), front (thieves' spot).
const PILES := {
"wood": {
"adopt": ["firewood_010_1", "firewood_010_2"],
"extra": [
["prop/firewood_010", Vector3(4.2, 0.0, -19.0), -4.0, 1.0],
["prop/firewood_010", Vector3(4.2, 0.35, -19.0), 5.0, 1.0],
["prop/firewood_010", Vector3(4.2, 0.7, -18.45), 3.0, 1.0],
["prop/firewood_010", Vector3(4.2, 0.7, -19.0), -2.0, 1.0],
["prop/firewood_010", Vector3(4.2, 1.05, -18.7), 6.0, 1.0],
],
"per": 4,
"front": Vector2(2.6, -18.6),
},
"scrap": {
"adopt": ["box_004_1", "box_004_2", "case_001_1", "box_004_3"],
"extra": [
["box/box_004", Vector3(-2.0, 0.5, -20.3), 4.0, 1.0],
["box/box_004", Vector3(-3.1, 0.5, -20.4), -6.0, 1.0],
["prop/barrel_010", Vector3(-4.3, 0.0, -20.3), 0.0, 1.0],
["box/box_004", Vector3(-2.55, 1.5, -20.35), 12.0, 1.0],
["prop/barrel_001", Vector3(-5.4, 0.0, -20.2), 0.0, 1.0],
["box/box_004", Vector3(-3.1, 2.5, -20.35), -15.0, 1.0],
],
"per": 3,
"front": Vector2(-3.0, -15.1),
},
"straw": {
"adopt": ["hay_001_1"],
"extra": [
["prop/hay_001", Vector3(7.3, 0.0, -18.9), 15.0, 0.55],
["prop/hay_001", Vector3(7.3, 0.0, -17.7), -10.0, 0.55],
["prop/hay_001", Vector3(6.3, 0.0, -20.1), 30.0, 0.55],
["prop/hay_001", Vector3(5.9, 1.9, -18.0), 0.0, 0.55],
["prop/hay_001", Vector3(5.1, 0.0, -20.1), -25.0, 0.55],
],
"per": 4,
"front": Vector2(6.75, -16.25),
},
## No pile of its own yet (placeholder: it never shows props), but it needs a real front
## spot so a supply thief sent for it (once it's the shed's most-stocked material) has
## somewhere reachable to stand; the wood pile's spot is already proven walkable.
"alien_goop": {
"adopt": [],
"extra": [],
"per": 1,
"front": Vector2(2.6, -18.6),
},
}
var campaign: CampaignController
## material -> Array[Node3D], in fill order.
var slots := {}
var shown := {}
var _bodies := {} # material -> Array (per slot: Array[CollisionObject3D])
var _last := {}
## Before the navigation is built: adopts the scene's piles and adds the extra props (every prop
## present and colliding).
func setup(p_campaign: CampaignController, shed_zone: Node3D) -> void:
campaign = p_campaign
for m: String in PILES:
var pile: Dictionary = PILES[m]
var list: Array[Node3D] = []
for prop_name: String in pile["adopt"]:
var n := shed_zone.get_node_or_null(prop_name) as Node3D
if n != null:
list.append(n)
for e: Array in pile["extra"]:
var n := (load(PREF + String(e[0]) + ".tscn") as PackedScene).instantiate() as Node3D
n.name = "Stock_%s_%d" % [m, list.size()]
shed_zone.add_child(n)
n.global_transform = Transform3D(Basis(Vector3.UP, deg_to_rad(float(e[2]))).scaled(Vector3.ONE * float(e[3])), e[1])
list.append(n)
slots[m] = list
var bodies: Array = []
for n in list:
bodies.append(n.find_children("*", "CollisionObject3D", true, false))
_bodies[m] = bodies
## Where supply thieves stand to take this material.
func spot(material: String) -> Vector2:
return PILES[material]["front"] if PILES.has(material) else Vector2.ZERO
## Props a pile shows for an amount.
static func props_for(material: String, amount: int, slot_count: int) -> int:
return clampi(ceili(amount / float(PILES[material]["per"])), 0, slot_count)
## Shows each pile for the shed's stock (after the navigation is built; then on every change).
## Reads the shed with get(m, 0): a shed dict replaced elsewhere without every material present
## (tests, old saves) is treated as 0 for a pile not in it, rather than erroring.
func refresh() -> void:
var shed := campaign.state.resources.shed
for m: String in slots:
var list: Array = slots[m]
var n := props_for(m, int(shed.get(m, 0)), list.size())
shown[m] = n
_last[m] = int(shed.get(m, 0))
for i in list.size():
var on := i < n
(list[i] as Node3D).visible = on
for body: CollisionObject3D in _bodies[m][i]:
body.collision_layer = 1 if on else 0
func _process(_delta: float) -> void:
if campaign == null:
return
var shed := campaign.state.resources.shed
for m: String in slots:
if int(shed.get(m, 0)) != int(_last.get(m, -1)):
refresh()
break