145 lines
5.2 KiB
GDScript
145 lines
5.2 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, and a
|
|
## label with the amount while the scarecrow is nearby.
|
|
|
|
const PREF := "res://assets/farm/ithappy/farm/prefabs/assets/"
|
|
const LABEL_RANGE := 9.0
|
|
## 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), label (world).
|
|
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),
|
|
"label": Vector3(4.2, 2.2, -18.7),
|
|
},
|
|
"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),
|
|
"label": Vector3(-3.4, 2.8, -18.6),
|
|
},
|
|
"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),
|
|
"label": Vector3(6.2, 3.2, -18.4),
|
|
},
|
|
}
|
|
|
|
var campaign: CampaignController
|
|
var player: Node3D
|
|
## material -> Array[Node3D], in fill order.
|
|
var slots := {}
|
|
var shown := {}
|
|
|
|
var _labels := {}
|
|
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, p_player: Node3D) -> void:
|
|
campaign = p_campaign
|
|
player = p_player
|
|
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
|
|
var l := Label3D.new()
|
|
l.billboard = BaseMaterial3D.BILLBOARD_ENABLED
|
|
l.no_depth_test = true
|
|
l.shaded = false
|
|
l.font_size = 44
|
|
l.outline_size = 10
|
|
l.pixel_size = 0.0075
|
|
l.modulate = GatherNode.COLORS[m]
|
|
l.visible = false
|
|
add_child(l)
|
|
l.global_position = pile["label"]
|
|
_labels[m] = l
|
|
|
|
|
|
## 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).
|
|
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[m]), list.size())
|
|
shown[m] = n
|
|
_last[m] = int(shed[m])
|
|
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[m]) != int(_last.get(m, -1)):
|
|
refresh()
|
|
break
|
|
var near := player != null
|
|
for m: String in _labels:
|
|
var l := _labels[m] as Label3D
|
|
var at: Vector3 = PILES[m]["label"]
|
|
l.visible = near and Vector2(player.global_position.x - at.x, player.global_position.z - at.z).length() < LABEL_RANGE
|
|
if l.visible:
|
|
l.text = "%s %d" % [m.capitalize(), int(shed[m])]
|