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. The car (with ## its roof luggage, which sways on the turns and tips forward as it stops), the farmers, the ## camera moves, and the letterbox come from CutsceneKit, which the opening intro shares. signal finished ## 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 ## 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 _speed: float = 0.0 # the last step's, for the luggage's sway 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: CutsceneKit.Letterbox 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 _speed = DRIVE_SPEED _parked_for = 0.0 _curve = CutsceneKit.route_curve(ROUTE) _car = CutsceneKit.make_car(self) _engine = CutsceneKit.add_engine(_car, sim.audio) _place_car(0.0) for i in 2: var farmer := CutsceneKit.make_farmer(self, CutsceneKit.FARMER_TINTS[i]) farmer.visible = false _farmers.append(farmer) _anims.append(CutsceneKit.anim_of(farmer)) # 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 = CutsceneKit.make_camera(self) CutsceneKit.follow_camera(_camera, _car, 1.0) if sim.audio != null: sim.audio.focus = _car _overlay = CutsceneKit.make_letterbox(self, "The farmers are on their way home", "[Space / Enter / A] skip") _caption = _overlay.caption _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() var luggage := CutsceneKit.luggage_of(_car) if luggage != null: luggage.settle() for i in 2: _farmers[i].visible = true _farmers[i].global_position = FARMER_SPOTS[i] CutsceneKit.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 _caption = 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 var was := _driven var heading := _car.rotation.y _driven = minf(_driven + speed * delta, length) _place_car(_driven) if delta > 0.0: # the luggage leans out on the turns and tips forward as the car stops var moved := (_driven - was) / delta CutsceneKit.sway_luggage(_car, delta, angle_difference(heading, _car.rotation.y) / delta, (moved - _speed) / delta) _speed = moved CutsceneKit.follow_camera(_camera, _car, 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) CutsceneKit.sway_luggage(_car, delta, 0.0, 0.0) 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) CutsceneKit.face(f, f.global_position + to) if arrived == 2: stage = "wave" _stage_left = WAVE_TIME for i in 2: CutsceneKit.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) CutsceneKit.sway_luggage(_car, delta, 0.0, 0.0) _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: CutsceneKit.set_hud(sim, on) func _place_car(distance: float) -> void: CutsceneKit.place_on_curve(_car, _curve, distance) ## A wide shot of the yard from the north-east: the car, the porch, and the scarecrow. func _frame_yard(delta: float) -> void: CutsceneKit.move_camera(_camera, YARD_CAMERA, YARD_LOOK, delta, 1.2) func _stop_engine() -> void: if _engine != null: _engine.stop() 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()