harvest/game/scripts/farmers_return.gd
2026-10-04 12:07:36 -04:00

350 lines
12 KiB
GDScript

class_name FarmersReturn
extends Node3D
## The ending once the last night's dawn is over (FARMERS_RETURN): the farmers' station wagon
## (car_002, roof rack and all) comes up the road east of the farm, turns in at the driveway gap,
## and parks in the farmyard by the house with a honk; the two farmers (tinted mannequins) get
## out, walk to the house door, wave to the scarecrow waiting by the yard, and cheer. A cutscene
## camera follows the car, then frames the yard; the HUD hides behind letterbox bars. Fire,
## interact, or Space / Enter / A skips to the end. finished is emitted once it's
## over (or skipped), and EndPanel shows the final assessment over the scene.
## Built when it starts, after the game is over, so nothing is created during play.
signal finished
const CAR_SCENE := "res://assets/farm/ithappy/farm/prefabs/assets/transport/car_002.tscn"
const FARMER_SCENE := "res://assets/kaykit_characters/characters/Mannequin_Medium.glb"
const ANIMATIONS := "res://game/resources/player_animations.res"
## The road from the south along the east side, in at GapDriveway, through the farmyard to the
## parking spot east of the house (game/LAYOUT.md); the car faces the way it drives.
const ROUTE: Array[Vector3] = [
Vector3(52.0, 0.0, 6.0), Vector3(52.0, 0.0, -30.0), Vector3(51.0, 0.0, -38.0), Vector3(46.0, 0.0, -39.6),
Vector3(38.0, 0.0, -39.4), Vector3(34.5, 0.0, -36.5), Vector3(33.0, 0.0, -32.5), Vector3(32.8, 0.0, -29.5),
]
const DRIVE_SPEED := 11.0
## Where the farmers walk (from the car's left and right doors to the porch steps on the house's
## north side) and where the scarecrow waits, beside their way. The yard shot looks south at the
## porch.
const DOOR := Vector3(19.6, 0.0, -29.8)
const FARMER_SPOTS: Array[Vector3] = [Vector3(18.9, 0.0, -31.4), Vector3(20.5, 0.0, -31.5)]
const SCARECROW_SPOT := Vector3(24.6, 0.0, -34.0)
const YARD_CAMERA := Vector3(28.0, 10.0, -45.5)
const YARD_LOOK := Vector3(24.8, 0.8, -30.8)
const WALK_SPEED := 2.0
const TINTS: Array[Color] = [Color(0.35, 0.5, 0.85), Color(0.85, 0.45, 0.4)]
## Seconds: honk after parking, farmers out, then waving, cheering, and the end.
const OUT_AT := 0.9
const WAVE_TIME := 2.2
const CHEER_TIME := 2.6
var sim: FarmSim
var running := false
var done := false
## Seconds since the start, and the stage: "drive", "walk", "wave", "cheer", "end".
var elapsed: float = 0.0
var stage := ""
var _car: Node3D
var _engine: AudioStreamPlayer3D
var _curve := Curve3D.new()
var _driven: float = 0.0
var _parked_for: float = 0.0
var _farmers: Array[Node3D] = []
var _anims: Array[AnimationPlayer] = []
var _camera: Camera3D
var _previous_camera: Camera3D
var _stage_left: float = 0.0
var _overlay: CanvasLayer
var _caption: Label
func setup(p_sim: FarmSim) -> void:
sim = p_sim
sim.campaign.campaign_ended.connect(func(outcome: CampaignController.Outcome) -> void:
if outcome == CampaignController.Outcome.VACATION_COMPLETE:
start())
sim.campaign.phase_changed.connect(func(phase: CampaignController.Phase, _p: CampaignController.Phase) -> void:
if phase == CampaignController.Phase.MORNING:
clear())
func start() -> void:
if running:
return
clear()
running = true
done = false
elapsed = 0.0
stage = "drive"
_driven = 0.0
_parked_for = 0.0
for p in ROUTE:
_curve.add_point(p)
for i in range(1, ROUTE.size() - 1): # round the corners
var t := (ROUTE[i + 1] - ROUTE[i - 1]) * 0.25
_curve.set_point_in(i, -t)
_curve.set_point_out(i, t)
_car = (load(CAR_SCENE) as PackedScene).instantiate() as Node3D
add_child(_car)
for body in _car.find_children("*", "CollisionObject3D", true, false):
(body as CollisionObject3D).collision_layer = 0
_engine = AudioStreamPlayer3D.new()
_engine.bus = &"SFX"
_engine.unit_size = 10.0
_engine.volume_db = -6.0
if sim.audio != null:
_engine.stream = sim.audio.stream_of("truck_engine")
_car.add_child(_engine)
if _engine.stream != null:
_engine.play()
_place_car(0.0)
var library := load(ANIMATIONS) as AnimationLibrary
for i in 2:
var root := Node3D.new()
root.visible = false
add_child(root)
var model := (load(FARMER_SCENE) as PackedScene).instantiate() as Node3D
root.add_child(model)
for mi in model.find_children("*", "MeshInstance3D", true, false):
var m := mi as MeshInstance3D
var base := m.get_active_material(0)
if base is BaseMaterial3D:
var tinted := (base as BaseMaterial3D).duplicate() as BaseMaterial3D
tinted.albedo_color = TINTS[i]
m.material_override = tinted
var straw := StandardMaterial3D.new() # a straw sun hat for the trip
straw.albedo_color = Color(0.92, 0.8, 0.45)
for part in [[0.62, 0.05, 2.12], [0.34, 0.26, 2.26]]: # brim, crown: radius, height, y
var mesh := CylinderMesh.new()
mesh.top_radius = part[0]
mesh.bottom_radius = part[0]
mesh.height = part[1]
var hat := MeshInstance3D.new()
hat.mesh = mesh
hat.position = Vector3(0.0, part[2], 0.0)
hat.material_override = straw
model.add_child(hat)
var anim := AnimationPlayer.new()
root.add_child(anim)
anim.root_node = anim.get_path_to(model)
anim.add_animation_library(&"", library)
_farmers.append(root)
_anims.append(anim)
# The scarecrow waits by the yard (in co-op, both, side by side).
for a in sim.avatars:
var at := SCARECROW_SPOT + Vector3(1.6, 0.0, 0.6) * a.slot
var yaw := atan2(-(DOOR.x - at.x), -(DOOR.z - at.z))
a.body.global_position = at
a.body.velocity = Vector3.ZERO
a.body.model.rotation.y = yaw
a.body.net_position = at
a.body.net_yaw = yaw
a.body.start_channel(&"Idle_A")
_previous_camera = get_viewport().get_camera_3d()
_camera = Camera3D.new()
_camera.fov = 45.0
add_child(_camera)
_camera.make_current()
_follow_camera(1.0)
if sim.audio != null:
sim.audio.focus = _car
_build_overlay()
_set_hud(false)
## Jumps to the end: parked, the farmers at the door cheering.
func skip() -> void:
if not running or done:
return
_driven = _curve.get_baked_length()
_place_car(_driven)
_stop_engine()
for i in 2:
_farmers[i].visible = true
_farmers[i].global_position = FARMER_SPOTS[i]
_face(_farmers[i], SCARECROW_SPOT)
_anims[i].play(&"Cheering")
_frame_yard(1.0)
_finish()
## Removes everything (a new vacation or a retry after the ending).
func clear() -> void:
running = false
stage = ""
for n in get_children():
n.queue_free()
_farmers.clear()
_anims.clear()
_car = null
_engine = null
_camera = null
_overlay = null
_curve = Curve3D.new()
if sim != null:
_set_hud(true)
if sim.audio != null:
sim.audio.focus = sim.player
if _previous_camera != null and is_instance_valid(_previous_camera):
_previous_camera.make_current()
for a in sim.avatars:
a.body.stop_channel()
func _process(delta: float) -> void:
if running and not done:
step(delta)
func step(delta: float) -> void:
elapsed += delta
match stage:
"drive":
var length := _curve.get_baked_length()
var left := length - _driven
var speed := DRIVE_SPEED * clampf(left / 9.0, 0.18, 1.0) # ease into the spot
_driven = minf(_driven + speed * delta, length)
_place_car(_driven)
_follow_camera(delta)
if _driven >= length:
_parked_for += delta
if _parked_for >= 0.15 and _engine != null and _engine.playing:
_stop_engine()
if sim.audio != null:
sim.audio.play("horn", _car.global_position)
if _parked_for >= OUT_AT:
stage = "walk"
var right := _car.global_basis.x
for i in 2:
_farmers[i].visible = true
_farmers[i].global_position = _car.global_position + right * (1.4 if i == 0 else -1.4)
_anims[i].play(&"Walking_A", 0.1)
"walk":
_frame_yard(delta)
var arrived := 0
for i in 2:
var f := _farmers[i]
var to := FARMER_SPOTS[i] - f.global_position
to.y = 0.0
if to.length() <= 0.05:
arrived += 1
continue
f.global_position += to.limit_length(WALK_SPEED * delta)
_face(f, f.global_position + to)
if arrived == 2:
stage = "wave"
_stage_left = WAVE_TIME
for i in 2:
_face(_farmers[i], SCARECROW_SPOT)
_anims[i].play(&"Waving", 0.15)
if sim.audio != null:
sim.audio.play("victory")
if _caption != null:
_caption.text = "The farmers are home!"
"wave", "cheer":
_frame_yard(delta)
_stage_left -= delta
if _stage_left <= 0.0:
if stage == "wave":
stage = "cheer"
_stage_left = CHEER_TIME
for a in _anims:
a.play(&"Cheering", 0.2)
else:
_finish()
func _finish() -> void:
done = true
stage = "end"
if _overlay != null:
_overlay.visible = false
_set_hud(true) # the final assessment is on it
finished.emit()
func _set_hud(on: bool) -> void:
if sim.hud != null and sim.hud.get_parent() is CanvasLayer:
(sim.hud.get_parent() as CanvasLayer).visible = on
func _place_car(distance: float) -> void:
var length := _curve.get_baked_length()
var at := _curve.sample_baked(distance, true)
var ahead := _curve.sample_baked(minf(distance + 1.0, length), true)
var behind := _curve.sample_baked(maxf(distance - 1.0, 0.0), true)
var dir := ahead - behind
dir.y = 0.0
if dir.length_squared() > 1e-6:
_car.rotation = Vector3(0.0, atan2(dir.x, dir.z), 0.0)
_car.global_position = Vector3(at.x, 0.0, at.z)
## Behind and above the car, looking a little ahead of it.
func _follow_camera(delta: float) -> void:
var fwd := _car.global_basis.z
var want := _car.global_position - fwd * 11.0 + Vector3(0.0, 8.0, 0.0) + _car.global_basis.x * 3.0
var look := _car.global_position + fwd * 4.0 + Vector3(0.0, 1.0, 0.0)
_move_camera(want, look, delta, 2.5)
## A wide shot of the yard from the north-east: the car, the porch, and the scarecrow.
func _frame_yard(delta: float) -> void:
_move_camera(YARD_CAMERA, YARD_LOOK, delta, 1.2)
func _move_camera(want: Vector3, look: Vector3, delta: float, rate: float) -> void:
var k := 1.0 - exp(-rate * delta) if delta < 1.0 else 1.0
_camera.global_position = _camera.global_position.lerp(want, k) if _camera.global_position != Vector3.ZERO else want
var target := _camera.global_transform.looking_at(look, Vector3.UP)
_camera.global_basis = _camera.global_basis.slerp(target.basis, k).orthonormalized() if k < 1.0 else target.basis
func _face(node: Node3D, at: Vector3) -> void:
var d := at - node.global_position
if Vector2(d.x, d.z).length_squared() > 1e-6:
node.rotation.y = atan2(d.x, d.z)
func _stop_engine() -> void:
if _engine != null:
_engine.stop()
func _build_overlay() -> void:
_overlay = CanvasLayer.new()
_overlay.layer = 5
add_child(_overlay)
for top in [true, false]:
var bar := ColorRect.new()
bar.color = Color.BLACK
bar.mouse_filter = Control.MOUSE_FILTER_IGNORE
_overlay.add_child(bar)
bar.set_anchors_and_offsets_preset(Control.PRESET_TOP_WIDE if top else Control.PRESET_BOTTOM_WIDE)
if top:
bar.offset_bottom = 96.0
else:
bar.offset_top = -96.0
_caption = Label.new()
_caption.text = "The farmers are on their way home"
_caption.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_caption.add_theme_font_size_override("font_size", 38)
_caption.add_theme_color_override("font_color", Color(0.95, 0.85, 0.55))
_overlay.add_child(_caption)
_caption.set_anchors_and_offsets_preset(Control.PRESET_TOP_WIDE)
_caption.offset_top = 22.0
var hint := Label.new()
hint.text = "[Space / Enter / A] skip"
hint.add_theme_font_size_override("font_size", 22)
hint.add_theme_color_override("font_color", Color(0.8, 0.8, 0.75))
_overlay.add_child(hint)
hint.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_RIGHT)
hint.offset_left = -360.0
hint.offset_top = -70.0
func _unhandled_input(event: InputEvent) -> void:
if not running or done:
return
if event.is_action_pressed("ui_accept") or event.is_action_pressed("interact") or event.is_action_pressed("fire"):
skip()
get_viewport().set_input_as_handled()