harvest/game/scripts/opening_intro.gd
Forrest 9f4a1895bd #36: prompts in touch words
Prompts (game/scripts/prompts.gd) holds every key-naming prompt: on a phone
(MobilePort.active()) it says each in touch words, elsewhere exactly the old
string. World labels drop the key (Gather, Repair, Upgrade to, Upgrades, Shop),
the build ghost says Place / Dismantle, the build bar's line is
"Place · Rotate · Dismantle · Done" and its slots drop the number keys, the
cutscenes say "Hold to skip" / "Tap to skip", the tip footer says
"× hides tips", and the tutorial's tips and the first morning's sign name the
thumbs (sides from TouchSticks), Use, Build, Place, Fear, Bomb, Patch, the
hotbar, and the backpack. MobilePort gains the `forced` test override.
game/tests/mobile/prompts_test.gd checks every phone string and that the
desktop strings are byte for byte as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:30:26 -04:00

1551 lines
54 KiB
GDScript

class_name OpeningIntro
extends Node3D
## The opening intro, at the start of every New vacation (Main.new_vacation; never on Continue or
## when hosting co-op; solo and local, so nothing of it is sent or saved). Stages (stage):
## leave A summer morning (DayNight.forced). The farmers come out of the house's front door
## (the house is one mesh, so no door swings: a creak) and walk to their station wagon,
## parked nose-out in the yard with the luggage piled on its roof (CutsceneKit), talking
## (intro/departure.tres); they wait at its doors until they're done.
## board They get in (a door slam each, the luggage jiggles) and the engine starts.
## drive The car drives the ending's route backwards and on down the road south; the camera
## follows it out of GapDriveway (a toot of the horn), stops there and watches it go.
## card Black: "Later that night...." is written in the handwritten font; the night falls
## under it (DayNight.forced, the lanterns kept dark, the night ambience).
## flight A big UFO (EnemySystem.build_ufo_mesh at UFO_SCALE, ufo.gdshader) comes over from the
## north-east, jerking and banking at each line of its crew's squabble over the controls
## (intro/flight.tres).
## wobble HANDS OFF! Over the scarecrow it sways harder and faster and spins up, whining.
## drop A part flies off its rim and tumbles down, trailing blue, in front of the scarecrow,
## which hangs on a wooden post (its arms out: the mannequin's rest pose is a T-pose).
## flash The part pulses, then a blue flash (a light spike, the screen, a ring over the
## ground); it streaks into the scarecrow's chest (its core glow comes on), and the
## scarecrow drops off the post, alive: its lantern lights, it looks around, then up.
## uh_oh The crew (intro/uh_oh.tres), the UFO shifting guiltily.
## escape It tilts and zooms off, its lights streaking.
## loading Black, then the LoadingScreen. Under it the farm is put back as new_campaign() left
## it: the scarecrow idle at its start, its input, the HUD, the light, the game camera,
## the sounds; then the first morning's sign shows and the screen fades out.
## end finished is emitted; DialogueDirector plays night 1's morning conversations (if any)
## before the sign.
## running: from start() until the farm is handed back (the morning sign, the tips, and the
## talking-head conversations wait meanwhile: ReviewPanel, Tutorial, DialogueDirector); busy():
## until the end. The HUD layer hides behind letterbox bars, the scarecrow takes no input
## (FarmSim.freeze_local), and the listener follows the shot (FarmAudio.focus, set_scene).
## The lines play in its own DialogueBox, in its overlay: they go on by themselves, a tap hurries
## them, Backspace skips one conversation. Holding Space / Enter / A (or E, or the mouse button)
## for SKIP_HOLD seconds skips the rest: a fade to black, then straight to the sign (no loading
## screen), the farm left exactly as at the normal end; skip() does the same from code (Main's
## Restart the night). Escape pauses it with the farm (the pause menu over it; its box waits:
## blocked). Everything is built in start() and freed by the end or clear() (Main's Save & quit
## to menu), so nothing of it exists during play. step(delta), from _process, drives all of it,
## its box and loading screen included, so tests can step it by hand.
signal finished
const STAGES: Array[String] = ["leave", "board", "drive", "card", "flight", "wobble", "drop", "flash", "uh_oh",
"escape", "loading", "end"]
const DEPARTURE := "res://game/resources/dialogue/intro/departure.tres"
const FLIGHT_LINES := "res://game/resources/dialogue/intro/flight.tres"
const UH_OH_LINES := "res://game/resources/dialogue/intro/uh_oh.tres"
## The scarecrow's start (farm.tscn's Player, game/LAYOUT.md), where it stands after the intro,
## facing north as it does there. Its post stands there too, the scarecrow hung POST_LIFT up on it
## facing FACING (east, over the crop field).
const START := Vector3(3.0, 0.0, -13.0)
const START_YAW := 0.0
const FACING := Vector3(1.0, 0.0, 0.0)
const POST_LIFT := 0.6
## The farmers come out of the front door (the porch steps on the house's north side,
## FarmersReturn.DOOR) and walk round to the car's doors along these points (the farmer to its
## left door, the wife round its nose to its right one), starting OUT_AT seconds in.
const DOOR := Vector3(19.6, 0.0, -29.8)
const OUT_AT: Array[float] = [0.3, 0.9]
const WALK_SPEED := 1.9
## The car's way out: the ending's route backwards (the car parks nose-out at its end), then on
## south down the road past the farm. GapDriveway is where the camera stops.
const ROAD_SOUTH: Array[Vector3] = [Vector3(52.0, 0.0, 40.0), Vector3(52.0, 0.0, 80.0)]
const GAP := Vector3(48.0, 0.0, -39.5)
const DRIVE_SPEED := 11.0
const DRIVE_ACCEL := 4.5
## The UFO: its size (EnemySystem.build_ufo_mesh's scale), where it comes from, the bend of its
## way in, where it stops (over the scarecrow), where it backs off to after the flash, and the way
## it leaves (up and away to the east-north-east).
const UFO_SCALE := 8.0
const UFO_FROM := Vector3(125.0, 62.0, -150.0)
const UFO_BEND := Vector3(70.0, 40.0, -40.0)
const HOVER := Vector3(5.0, 19.0, -13.0)
const HOVER_AFTER := Vector3(26.0, 15.0, -16.0)
const ESCAPE_DIR := Vector3(0.74, 0.46, -0.49)
## Its lights underneath (the hull hides the rim's from below): how many, how far out, and their
## colors (the rim's), and the glow of its drive in the middle.
const UFO_LIGHTS := 10
const UFO_LIGHT_COLORS: Array[Color] = [Color(1.0, 0.72, 0.2), Color(0.4, 1.0, 0.35)]
const UFO_DRIVE_COLOR := Color(0.55, 1.0, 0.85, 0.75)
## The shots: camera positions and the subjects they look at. The departure looks south over the
## yard from the north (the porch and the car side by side, clear of the box's heads); the night
## looks up from the south-west, far enough off that the UFO is seen from the side; the uh-oh looks
## over the scarecrow's right shoulder (it stands at the frame's left edge, off the box).
const LEAVE_CAMERA := Vector3(26.2, 13.5, -59.0)
const LEAVE_SUBJECT := Vector3(26.2, 1.0, -30.5)
const LEAVE_LIFT := 7.0
const GAP_CAMERA := Vector3(46.8, 3.0, -43.6)
const FLIGHT_CAMERA := Vector3(-9.0, 1.5, 19.0)
const UH_OH_BACK := 3.2
const UH_OH_SIDE := 4.0
const UH_OH_HEIGHT := 1.2
const UH_OH_FOV := 55.0
## The scarecrow comes down this far in front of its post, which tips over (to its left).
const HOP := 0.6
## Seconds: the departure's lines start; boarding; the drive (the camera leaves the car this far
## before the gap); the card (its fade, its writing, the fade out of its words); the flight's
## lines start and how long it takes to come over; the wobble; the part's fall; the flash's
## beats; the escape; the loading screen and its fade, the skip's fade to black and back.
const LINES_AT := 1.2
const BOARD_TIME := 2.2
const DRIVE_TIME := 12.0
const GAP_LEAD := 6.0
const CARD_FADE := 0.5
const CARD_TEXT := "Later that night...."
const CARD_LETTERS := 12.0
const CARD_DOT := 0.22
const CARD_TIME := 3.6
const FLIGHT_FADE := 0.8
const FLIGHT_LINES_AT := 2.0
const FLIGHT_TIME := 13.0
const WOBBLE_TIME := 4.0
const DROP_CUT := 0.45
const FALL_TIME := 2.0
const DROP_AFTER := 0.6
const FLASH_AT := 1.2
const STREAK_AT := 1.3
const STREAK_TIME := 0.25
const HOP_AT := 1.85
const LOOK_AROUND_AT := 2.6
const LOOK_UP_AT := 4.7
const FLASH_TIME := 5.4
const ESCAPE_TIME := 2.6
const LOADING_BLACK := 0.5
const LOADING_TIME := 3.0
const LOADING_FADE := 0.6
const SKIP_HOLD := 1.0
const SKIP_FADE := 0.4
const REVEAL_FADE := 0.5
## What holding skips: Space / Enter / A (ui_accept), E / X (interact), the mouse button / RT
## (fire), and on a phone, a finger anywhere on the screen.
const SKIP_ACTIONS: Array[StringName] = [&"ui_accept", &"interact", &"fire"]
## Degrees the lines' shots look under their subject, so it stands in the frame's upper half,
## clear of the dialogue box.
const LINE_LIFT := 10.0
const PART_BLUE := Color(0.45, 0.78, 1.0)
const FLASH_COLOR := Color(0.8, 0.92, 1.0)
var sim: FarmSim
## The intro has the farm (start() until it's handed back under the loading screen or the
## skip's black), it's over (finished emitted), and its stage (STAGES; "" when idle).
var running := false
var done := false
var stage := ""
## Seconds since start() (game time; it stops while the game is paused).
var elapsed: float = 0.0
## Set by Main: whether a menu is up over it (its box waits; holding doesn't skip).
var blocked: Callable
var _t: float = 0.0 # seconds into the stage
var _took := false # the farm is the intro's (put it back)
var _skipping := false
var _revealing := false
var _hold: float = 0.0
var _armed := false
var _fingers := {} # touch index -> true while that finger is down (phones' taps no longer press fire)
var _last_frame: int = 0
var _talked := false
var _line: int = -1
var _quiet: float = 0.0
# The world (freed when the farm is handed back) and the screen (freed at the end).
var _world: Node3D
var _ears: Node3D
var _camera: Camera3D
var _overlay: CutsceneKit.Letterbox
var _ring: SkipRing
var _box: DialogueBox
var _flash: ColorRect
var _black: ColorRect
var _card: Label
var _loading_layer: CanvasLayer
var _loading: LoadingScreen
# The departure.
var _car: Node3D
var _engine: AudioStreamPlayer3D
var _curve: Curve3D
var _driven: float = 0.0
var _speed: float = 0.0
var _gap_at: float = 0.0
var _past_gap := false
var _farmers: Array[Node3D] = []
var _anims: Array[AnimationPlayer] = []
var _paths: Array[PackedVector3Array] = []
var _legs: Array[int] = [0, 0]
var _doors: Array[Vector3] = []
var _shake: float = 0.0
# The night.
var _night := false
var _ufo: MeshInstance3D
var _ufo_mat: ShaderMaterial
var _hum: AudioStreamPlayer3D
var _ufo_light: SpotLight3D
var _streak: CPUParticles3D
var _ufo_lamps: Array[StandardMaterial3D] = []
var _chase: float = 6.0
var _lamp_phase: float = 0.0
var _u: float = 0.0
var _lurch: float = 0.0
var _tug := Vector2.ZERO
var _tug_speed := Vector2.ZERO
var _spin: float = 0.0
var _spin_rate: float = 1.0
var _phase: float = 0.0
var _sway: float = 0.0
var _center := Vector3.ZERO
var _escape_speed: float = 0.0
var _post: Node3D
var _post_down := false
var _ropes: Array[Node3D] = []
var _part: Node3D
var _core_mat: StandardMaterial3D
var _part_light: OmniLight3D
var _trail: Trail
var _dust: CPUParticles3D
var _part_from := Vector3.ZERO
var _part_velocity := Vector3.ZERO
var _part_spin := Vector3.ZERO
var _landed := false
var _land := Vector3.ZERO
var _ground: float = 0.0
var _burst: OmniLight3D
var _ring_mesh: MeshInstance3D
var _ring_mat: ShaderMaterial
var _look: LookAtModifier3D
var _skeleton: Skeleton3D
var _marker_was := true
var _alive_step: int = 0 # how far the flash stage has got (its one-off beats)
static var _dot: GradientTexture2D
## A fading trail of glowing specks behind whatever it's under (in world space): while emitting,
## a speck every SPACING along the way it moved (so a fast fall leaves an even line), each
## shrinking and fading over life seconds. step() moves it on.
class Trail extends MultiMeshInstance3D:
const COUNT := 64
const SPACING := 0.1
var emitting := false
var color := Color.WHITE
var size: float = 0.3
var life: float = 0.5
var _specks: Array[Vector4] = [] # x, y, z, age
var _last := Vector3.INF
func _init(p_color: Color, p_size: float, p_life: float) -> void:
color = p_color
size = p_size
life = p_life
top_level = true
cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
var quad := QuadMesh.new()
quad.size = Vector2.ONE
multimesh = MultiMesh.new()
multimesh.transform_format = MultiMesh.TRANSFORM_3D
multimesh.use_colors = true
multimesh.mesh = quad
multimesh.instance_count = COUNT
multimesh.visible_instance_count = 0
material_override = OpeningIntro._glow_material(Color.WHITE, BaseMaterial3D.BILLBOARD_ENABLED)
func step(delta: float, at: Vector3) -> void:
var kept: Array[Vector4] = []
for s in _specks:
if s.w + delta < life:
kept.append(Vector4(s.x, s.y, s.z, s.w + delta))
_specks = kept
if emitting:
if _last == Vector3.INF:
_last = at
var n := mini(floori(_last.distance_to(at) / SPACING), COUNT)
for i in n:
var p := _last.lerp(at, float(i + 1) / n)
_specks.append(Vector4(p.x, p.y, p.z, delta * (1.0 - float(i + 1) / n)))
if n > 0:
_last = at
else:
_last = Vector3.INF
while _specks.size() > COUNT:
_specks.pop_front()
multimesh.visible_instance_count = _specks.size()
for i in _specks.size():
var s := _specks[i]
var k := 1.0 - s.w / life
multimesh.set_instance_transform(i, Transform3D(Basis.from_scale(Vector3.ONE * size * (0.35 + 0.65 * k)), Vector3(s.x, s.y, s.z)))
multimesh.set_instance_color(i, Color(color, color.a * k))
## The skip hint's ring, filling while the skip is held.
class SkipRing extends Control:
var progress: float = 0.0
func set_progress(p: float) -> void:
if not is_equal_approx(p, progress):
progress = p
queue_redraw()
func _draw() -> void:
var c := size * 0.5
var r := minf(size.x, size.y) * 0.5 - 3.0
draw_arc(c, r, 0.0, TAU, 40, Color(1.0, 1.0, 1.0, 0.28), 3.0, true)
if progress > 0.0:
draw_arc(c, r, -PI * 0.5, -PI * 0.5 + TAU * clampf(progress, 0.0, 1.0), 40, Color(0.95, 0.85, 0.55), 4.5, true)
func setup(p_sim: FarmSim) -> void:
sim = p_sim
sim.campaign.phase_changed.connect(func(phase: CampaignController.Phase, _p: CampaignController.Phase) -> void:
if busy() and phase != CampaignController.Phase.MORNING:
clear()) # it's the first morning's (debug phase skips end it)
## Whether it's still on screen (start() until the end).
func busy() -> bool:
return stage != "" and not done
## Plays it from the top (solo only: never while a co-op session is up).
func start() -> void:
if sim == null or (sim.coop != null and sim.coop.is_active()):
return
clear()
running = true
done = false
elapsed = 0.0
_took = true
_armed = false
_last_frame = Engine.get_process_frames()
_world = Node3D.new()
_world.name = "World"
add_child(_world)
_ears = Node3D.new()
_ears.name = "Ears"
_world.add_child(_ears)
_build_departure()
_build_night()
_build_screen()
# The farm: the scarecrow up on its post, frozen, no glow; a summer morning; no HUD.
_mount_scarecrow()
if not sim.local_frozen:
sim.freeze_local(true)
var dn := sim.day_night
if dn != null:
dn.forced = "morning"
dn.lanterns_dark = true
dn.snap()
CutsceneKit.set_hud(sim, false)
if sim.review_panel != null:
sim.review_panel.refresh() # down until the intro's over
_camera = CutsceneKit.make_camera(_world, 45.0)
CutsceneKit.move_camera(_camera, LEAVE_CAMERA, _lifted(LEAVE_CAMERA, LEAVE_SUBJECT, LEAVE_LIFT), 1.0, 1.0)
if sim.audio != null:
sim.audio.stop_cue("morning") # new_campaign()'s stinger: it plays with the sign instead
sim.audio.set_scene(Vector2(1.0, 0.0), "farm")
sim.audio.focus = _ears
sim.audio.play("door_creak", DOOR + Vector3(0.0, 1.2, 0.0))
_enter("leave")
## Skips the rest: a fade to black, and the farm is put back; then the sign.
func skip() -> void:
if not busy() or _skipping or not running:
return
_skipping = true
if _box != null:
_box.stop()
if _ring != null:
_ring.visible = false
_overlay.hint.visible = false
if stage == "loading" and _loading != null and _loading.visible:
_hand_back(true)
_loading.fade_out(LOADING_FADE)
## Takes it all down at once and gives the farm back, without the sign (Main's Save & quit to
## menu; a new start()).
func clear() -> void:
if _took:
_hand_back(false)
for n in get_children():
remove_child(n)
n.queue_free()
running = false
done = false
stage = ""
_skipping = false
_revealing = false
_world = null
_ears = null
_camera = null
_overlay = null
_ring = null
_box = null
_flash = null
_black = null
_card = null
_loading_layer = null
_loading = null
_forget_world()
func _process(delta: float) -> void:
if busy():
step(delta)
func _exit_tree() -> void:
for p in find_children("*", "AudioStreamPlayer3D", true, false):
(p as AudioStreamPlayer3D).stop()
## Moves it on delta seconds.
func step(delta: float) -> void:
if not busy() or delta <= 0.0:
return
elapsed += delta
_t += delta
if _box != null:
_box._process(delta)
if _loading != null:
_loading.step(delta)
if running and not _skipping and not _revealing:
_poll_skip(delta)
if _revealing:
_reveal(delta)
return
if _skipping:
_black.color.a = move_toward(_black.color.a, 1.0, delta / SKIP_FADE)
if _black.color.a >= 1.0:
_hand_back(true)
return
match stage:
"leave":
_leave(delta)
"board":
_board(delta)
"drive":
_drive(delta)
"card":
_card_step(delta)
"flight":
_flight(delta)
"wobble":
_wobble(delta)
"drop":
_drop(delta)
"flash":
_flash_step(delta)
"uh_oh":
_uh_oh(delta)
"escape":
_escape(delta)
"loading":
_loading_step(delta)
_hear()
_light_ufo(delta)
## The UFO's night glow (as DayNight gives the enemies'), and its lamps chasing round underneath.
func _light_ufo(delta: float) -> void:
if _ufo_mat == null or not _night:
return
if sim.day_night != null:
_ufo_mat.set_shader_parameter("night_glow", float(sim.day_night.current["glow"]))
_ufo_mat.set_shader_parameter("chase_speed", _chase)
_lamp_phase += _chase * delta
for k in _ufo_lamps.size():
var a := TAU * k / _ufo_lamps.size()
var pulse := 0.4 + 0.6 * maxf(sin(a * 2.0 - _lamp_phase), 0.0)
_ufo_lamps[k].albedo_color.a = pulse
func _enter(next: String) -> void:
stage = next
_t = 0.0
_talked = false
_line = -1
_quiet = 0.0
# --- The stages ----------------------------------------------------------------------------------
func _leave(delta: float) -> void:
CutsceneKit.move_camera(_camera, LEAVE_CAMERA, _lifted(LEAVE_CAMERA, LEAVE_SUBJECT, LEAVE_LIFT), delta, 0.6)
CutsceneKit.sway_luggage(_car, delta, 0.0, 0.0)
for i in 2:
_walk(i, delta)
if _t >= LINES_AT and not _talked:
_talk(DEPARTURE)
if _t >= 3.0 and _arrived(0) and _arrived(1) and _talked and not _box.is_playing():
_quiet += delta
if _quiet >= 0.3:
_enter("board")
func _board(delta: float) -> void:
CutsceneKit.move_camera(_camera, LEAVE_CAMERA, _lifted(LEAVE_CAMERA, LEAVE_SUBJECT, LEAVE_LIFT), delta, 0.6)
var luggage := CutsceneKit.luggage_of(_car)
for i in 2:
if _farmers[i].visible and _t >= 0.15 + 0.6 * i:
_farmers[i].visible = false # in, and the door slams
if sim.audio != null:
sim.audio.play("car_door", _doors[i] + Vector3(0.0, 1.0, 0.0))
if luggage != null:
luggage.nudge(Vector2(0.15, 0.6 if i == 0 else -0.6))
if _t >= 1.3 and _engine == null:
_engine = CutsceneKit.add_engine(_car, sim.audio)
if luggage != null:
luggage.nudge(Vector2(-0.5, 0.35))
if _engine != null and luggage != null:
_shake -= delta
if _shake <= 0.0: # the engine idling under it
_shake = 0.11
luggage.nudge(Vector2(randf_range(-0.05, 0.05), randf_range(-0.07, 0.07)))
CutsceneKit.sway_luggage(_car, delta, 0.0, 0.0)
if _t >= BOARD_TIME:
_enter("drive")
_speed = 0.0
func _drive(delta: float) -> void:
var length := _curve.get_baked_length()
var was := _driven
var heading := _car.rotation.y
var last := _speed
_speed = minf(_speed + DRIVE_ACCEL * delta, DRIVE_SPEED)
_driven = minf(_driven + _speed * delta, length)
CutsceneKit.place_on_curve(_car, _curve, _driven)
var moved := (_driven - was) / delta
CutsceneKit.sway_luggage(_car, delta, angle_difference(heading, _car.rotation.y) / delta, (moved - last) / delta)
if not _past_gap and _driven >= _gap_at:
_past_gap = true
if sim.audio != null:
sim.audio.play("horn", _car.global_position + Vector3(0.0, 1.0, 0.0))
if _driven < _gap_at - GAP_LEAD:
CutsceneKit.follow_camera(_camera, _car, delta if _t > delta else 1.0) # (cut in behind it)
else: # it stops at the gap and watches the car go
CutsceneKit.move_camera(_camera, GAP_CAMERA, _car.global_position + Vector3(0.0, 1.0, 0.0), delta, 1.8)
if _t >= DRIVE_TIME or _driven >= length:
_enter("card")
if sim.audio != null:
sim.audio.set_scene(Vector2(0.0, 1.0), "") # the night comes in under the black
func _card_step(_delta: float) -> void:
_black.color.a = clampf(_t / CARD_FADE, 0.0, 1.0)
if _t >= CARD_FADE and not _night:
_go_night()
_card.visible = _t >= CARD_FADE
_card.visible_characters = _card_letters(_t - CARD_FADE - 0.15)
_card.modulate.a = clampf((CARD_TIME - _t) / 0.3, 0.0, 1.0)
if _t >= CARD_TIME:
_card.visible = false
_enter("flight")
_u = 0.0
func _flight(delta: float) -> void:
_black.color.a = clampf(1.0 - _t / FLIGHT_FADE, 0.0, 1.0)
if _t >= FLIGHT_LINES_AT and not _talked:
_talk(FLIGHT_LINES)
if _talked and _box.is_playing() and _box.index != _line:
_line = _box.index
_tug_line(_line)
_lurch = maxf(_lurch - delta, 0.0)
_u = minf(_u + delta / FLIGHT_TIME * (1.0 + _lurch * 1.6), 1.0)
var k := _u * _u * (3.0 - 2.0 * _u)
var at := _bezier(UFO_FROM, UFO_BEND, HOVER, k)
var ahead := _bezier(UFO_FROM, UFO_BEND, HOVER, minf(k + 0.02, 1.0)) - _bezier(UFO_FROM, UFO_BEND, HOVER, maxf(k - 0.02, 0.0))
ahead.y = 0.0
var fwd := ahead.normalized() if ahead.length_squared() > 1e-6 else Vector3.FORWARD
_step_tug(delta, 5.0, 1.8)
var side := fwd.cross(Vector3.UP).normalized()
_spin += _spin_rate * delta
var bank := clampf(-_tug_speed.x * 0.045 + _tug.x * 0.01, -0.6, 0.6)
var pitch := clampf(0.12 * (1.0 - k) + _tug_speed.y * 0.03, -0.4, 0.4)
_place_ufo(at + side * _tug.x + Vector3.UP * _tug.y, fwd, bank, pitch)
_track(FLIGHT_CAMERA, _ufo.global_position, delta, 3.0)
if _talked and (_box.index >= 4 or not _box.is_playing()):
_enter("wobble")
_center = _ufo.global_position
_phase = 0.0
if sim.audio != null:
sim.audio.play("ufo_whine", _ufo.global_position)
func _wobble(delta: float) -> void:
var k := clampf(_t / WOBBLE_TIME, 0.0, 1.0)
_phase += TAU * lerpf(0.45, 2.4, k * k) * delta
_center = _center.lerp(HOVER, 1.0 - exp(-2.5 * delta))
_sway = sin(_phase) * lerpf(1.2, 3.0, k)
_spin_rate = lerpf(1.5, 30.0, k * k * k)
_spin += _spin_rate * delta
var across := _across()
var tilt := cos(_phase) * deg_to_rad(20.0) * lerpf(0.5, 1.0, k)
_place_ufo(_center + across * _sway, across.cross(Vector3.UP), tilt, 0.0)
_chase = lerpf(6.0, 26.0, k)
if _hum != null:
_hum.pitch_scale = lerpf(1.0, 1.7, k)
_track(FLIGHT_CAMERA, _center, delta, 3.0)
if _t >= WOBBLE_TIME and not _box.is_playing():
_enter("drop")
_pop_part()
func _drop(delta: float) -> void:
# The UFO lurches still and slows down.
_sway *= exp(-3.0 * delta)
_spin_rate = lerpf(_spin_rate, 1.2, 1.0 - exp(-2.0 * delta))
_spin += _spin_rate * delta
_center = _center.lerp(HOVER, 1.0 - exp(-2.0 * delta))
var across := _across()
_place_ufo(_center + across * _sway, across.cross(Vector3.UP), _sway / 3.0 * deg_to_rad(12.0), 0.0)
_chase = lerpf(_chase, 6.0, 1.0 - exp(-2.0 * delta))
if _hum != null:
_hum.pitch_scale = lerpf(_hum.pitch_scale, 1.0, 1.0 - exp(-2.0 * delta))
if _t < DROP_CUT:
_track(FLIGHT_CAMERA, _center, delta, 3.0)
else: # cut to the scarecrow on its post looking up at the part; down with it as it falls
var right := FACING.cross(Vector3.UP)
var frame := START + FACING * 0.4 + Vector3(0.0, _ground + 1.5, 0.0)
var high := clampf((_part.global_position.y - _ground - 2.5) / 10.0, 0.0, 1.0) if not _landed else 0.0
CutsceneKit.move_camera(_camera, START + FACING * 5.4 + right * 1.0 + Vector3(0.0, _ground + 1.4, 0.0),
frame.lerp(_part.global_position, high * high * (3.0 - 2.0 * high)), 1.0 if _t - delta < DROP_CUT else delta, 7.0)
if not _landed:
var t := minf(_t, FALL_TIME)
_part.global_position = _part_from + _part_velocity * t + Vector3(0.0, -4.9, 0.0) * t * t
_part.rotation += _part_spin * delta
if _t >= FALL_TIME:
_land_part()
elif _t >= FALL_TIME + DROP_AFTER:
_enter("flash")
_alive_step = 0
if _trail != null:
_trail.step(delta, _part.global_position)
func _flash_step(delta: float) -> void:
var right := FACING.cross(Vector3.UP)
CutsceneKit.move_camera(_camera, START + FACING * 5.2 - right * 0.9 + Vector3(0.0, _ground + 1.5, 0.0),
START + FACING * 0.6 + Vector3(0.0, _ground + 1.3, 0.0), 1.0 if _t <= delta else delta, 0.5)
_hover_ufo(delta)
var body := sim.player
if _t < FLASH_AT: # three pulses
var p := absf(sin(_t / FLASH_AT * 3.0 * PI))
_pulse_part(1.0 + 3.0 * p * p)
if _alive_step == 0 and _t >= FLASH_AT:
_alive_step = 1
_burst.light_energy = 16.0
_flash.color.a = 0.92
_ring_mesh.visible = true
if sim.audio != null:
sim.audio.play("life_flash", _land + Vector3(0.0, 0.5, 0.0))
if _alive_step >= 1:
_burst.light_energy = maxf(_burst.light_energy - delta * 18.0, 0.0)
_flash.color.a = maxf(_flash.color.a - delta / 0.9, 0.0)
_ring_mat.set_shader_parameter("t", clampf((_t - FLASH_AT) / 1.2, 0.0, 1.0))
if _alive_step == 1 and _t >= STREAK_AT: # into the chest
var chest := body.core_glow.global_position if body.core_glow != null else body.global_position + Vector3(0.0, 1.0, 0.0)
var k := clampf((_t - STREAK_AT) / STREAK_TIME, 0.0, 1.0)
_part.global_position = _land.lerp(chest, k * k)
_part.scale = Vector3.ONE * lerpf(1.0, 0.3, k)
_pulse_part(3.0)
if k >= 1.0:
_alive_step = 2
_part.visible = false
body.set_core_glow(true)
if _alive_step == 2 and _t >= HOP_AT: # off the post
_alive_step = 3
body.animation_player.play(&"Hit_A", 0.05)
_post_ropes_off()
if _alive_step == 3:
var fall := _t - HOP_AT
var y := _ground + POST_LIFT + 1.4 * fall - 8.0 * fall * fall # a little hop, then down
var ahead := minf(fall / 0.45, 1.0) * HOP
if y <= _ground and fall > 0.1:
y = _ground
_alive_step = 4
if sim.audio != null:
sim.audio.play("fall", _stand())
if sim.day_night != null:
sim.day_night.lanterns_dark = false # its lantern lights
body.global_position = START + FACING * ahead + Vector3(0.0, y, 0.0)
if _alive_step >= 3 and _post != null: # the post tips over
var k := clampf((_t - HOP_AT - 0.15) / 0.7, 0.0, 1.0)
_post.rotation.z = 1.5 * k * k
if k >= 1.0 and not _post_down:
_post_down = true
if sim.audio != null:
sim.audio.play("dismantle", START - FACING.cross(Vector3.UP) * 1.5)
if _alive_step == 4 and _t >= LOOK_AROUND_AT:
_alive_step = 5
body.animation_player.play(&"Idle_B", 0.25)
if _alive_step == 5 and _t >= LOOK_UP_AT:
_alive_step = 6
body.animation_player.play(body.idle_animation, 0.3)
_look_up()
if _look != null:
_look.influence = move_toward(_look.influence, 0.75, delta * 1.2)
if _t >= FLASH_TIME:
_enter("uh_oh")
_ring_mesh.visible = false
func _uh_oh(delta: float) -> void:
_uh_oh_shot(delta, 1.0 if _t <= delta else delta, 1.5)
if _t >= 0.4 and not _talked:
_talk(UH_OH_LINES)
if _talked and _box.is_playing() and _box.index != _line:
_line = _box.index
_shuffle(_line)
_center = _center.lerp(HOVER_AFTER, 1.0 - exp(-0.7 * delta))
_step_tug(delta, 7.0, 2.4)
_spin += _spin_rate * delta
var across := _across()
var fidget := sin(_t * 2.3) * 0.5 + sin(_t * 5.1) * 0.15
_place_ufo(_center + across * (_tug.x + fidget) + Vector3.UP * _tug.y, across.cross(Vector3.UP),
clampf(-_tug_speed.x * 0.05, -0.35, 0.35) + fidget * 0.05, 0.05)
if _look != null:
_look.influence = move_toward(_look.influence, 0.75, delta * 1.2)
if _talked and not _box.is_playing():
_quiet += delta
if _quiet >= 0.3:
_enter("escape")
_escape_speed = 0.0
func _escape(delta: float) -> void:
_uh_oh_shot(delta, delta, 0.4)
var tip := clampf(_t / 0.4, 0.0, 1.0)
_spin_rate = lerpf(_spin_rate, 6.0, 1.0 - exp(-3.0 * delta))
_spin += _spin_rate * delta
var away := ESCAPE_DIR.normalized()
var flat := Vector3(away.x, 0.0, away.z).normalized()
if _t >= 0.35:
if not _streak.emitting:
_streak.emitting = true
_ufo_mat.set_shader_parameter("glow_energy", 3.5)
if sim.audio != null:
sim.audio.play("ufo_zoom", _ufo.global_position)
_escape_speed += 70.0 * delta
_center += away * _escape_speed * delta
else:
_center += Vector3.UP * 1.5 * delta # gathers itself
_place_ufo(_center, flat, 0.0, -deg_to_rad(28.0) * tip)
if _look != null:
_look.influence = move_toward(_look.influence, 0.6, delta)
if _t >= ESCAPE_TIME:
_enter("loading")
func _loading_step(_delta: float) -> void:
if not _loading.visible:
_black.color.a = clampf(_t / LOADING_BLACK, 0.0, 1.0)
if _t >= LOADING_BLACK:
_loading.play(LOADING_TIME)
return
if not _loading.running: # the bar's full: the farm is put back under it, then it fades
_hand_back(true)
_loading.fade_out(LOADING_FADE)
## After the farm's back: the black (a skip's) and the loading screen fade away; then the end.
func _reveal(delta: float) -> void:
_black.color.a = move_toward(_black.color.a, 0.0, delta / REVEAL_FADE)
if _black.color.a <= 0.0 and not _loading.visible:
_finish()
func _finish() -> void:
stage = "end"
done = true
_skipping = false
_revealing = false
for n in get_children():
remove_child(n)
n.queue_free()
_overlay = null
_ring = null
_box = null
_flash = null
_black = null
_card = null
_loading_layer = null
_loading = null
if sim.dialogue != null:
sim.dialogue.intro_over() # night 1's morning conversations (none today), then the sign
finished.emit()
# --- Putting the farm back -----------------------------------------------------------------------
## The farm as new_campaign() left it: the intro's world gone, the scarecrow idle at its start
## with its input and glow, the HUD, the light un-forced and snapped, the game camera, the sounds;
## with show_sign, the morning sign (unless a conversation comes first) and its stinger.
func _hand_back(show_sign: bool) -> void:
if not _took:
return
_took = false
running = false
if _world != null:
for p in _world.find_children("*", "AudioStreamPlayer3D", true, false):
(p as AudioStreamPlayer3D).stop()
remove_child(_world)
_world.queue_free()
_world = null
_ears = null
_camera = null
_forget_world()
if _look != null:
if is_instance_valid(_look):
_look.get_parent().remove_child(_look)
_look.queue_free()
_look = null
var body := sim.player
body.set_physics_process(true)
body.velocity = Vector3.ZERO
body.global_position = START
body.model.rotation = Vector3(0.0, START_YAW, 0.0)
body.net_position = START
body.net_yaw = START_YAW
body.stop_channel()
body.animation_player.speed_scale = 1.0
body.animation_player.play(body.idle_animation)
body.set_core_glow(true)
body.reset_physics_interpolation()
sim.scarecrow.alive = true
sim.combat.show_marker = _marker_was
if sim.local_frozen:
sim.freeze_local(false)
var dn := sim.day_night
if dn != null:
dn.forced = ""
dn.lanterns_dark = false
dn.snap()
CutsceneKit.set_hud(sim, true)
if sim.camera != null:
sim.camera.make_current()
if sim.camera is FollowCamera:
(sim.camera as FollowCamera).snap_to_target()
if sim.audio != null:
sim.audio.focus = sim.player
sim.audio.end_scene()
if _overlay != null: # only the black stays (a skip's, faded out next)
for n in _overlay.get_children():
(n as CanvasItem).visible = n == _black
if _loading != null and _loading.visible:
_black.color.a = 0.0
_revealing = true
if not show_sign:
return
if sim.audio != null:
sim.audio.play("morning")
if sim.review_panel != null and (sim.dialogue == null or not sim.dialogue.has_due()):
sim.review_panel.refresh()
func _forget_world() -> void:
_car = null
_engine = null
_curve = null
_farmers.clear()
_anims.clear()
_paths.clear()
_doors.clear()
_ufo = null
_ufo_mat = null
_hum = null
_ufo_light = null
_streak = null
_post = null
_post_down = false
_ropes.clear()
_ufo_lamps.clear()
_part = null
_core_mat = null
_part_light = null
_trail = null
_dust = null
_burst = null
_ring_mesh = null
_ring_mat = null
_skeleton = null
_night = false
_landed = false
_past_gap = false
# --- Building it ---------------------------------------------------------------------------------
## The car, nose-out at the end of the ending's route, its way out, and the farmers (hidden until
## they come out of the door).
func _build_departure() -> void:
var route := CutsceneKit.reversed(FarmersReturn.ROUTE)
route.append_array(ROAD_SOUTH)
_curve = CutsceneKit.route_curve(route)
_gap_at = _curve.get_closest_offset(GAP)
_driven = 0.0
_speed = 0.0
_car = CutsceneKit.make_car(_world)
CutsceneKit.place_on_curve(_car, _curve, 0.0)
var left := _car.global_basis.x
var ahead := _car.global_basis.z
_doors = [_car.global_position + left * 1.35 + ahead * 0.45, _car.global_position - left * 1.35 + ahead * 0.45]
# Off the porch, then along the yard (the farmer to the left door; the wife round the nose).
var nose := _car.global_position + ahead * 4.2
_paths = [
PackedVector3Array([DOOR + Vector3(0.6, 0.0, -1.2), _doors[0] + Vector3(-1.2, 0.0, -1.0), _doors[0]]),
PackedVector3Array([DOOR + Vector3(1.4, 0.0, -1.8), nose + left * 1.2, nose - left * 1.6, _doors[1] + ahead * 1.6, _doors[1]]),
]
_legs = [0, 0]
for i in 2:
var farmer := CutsceneKit.make_farmer(_world, CutsceneKit.FARMER_TINTS[i])
farmer.name = "Farmer" if i == 0 else "FarmersWife"
farmer.global_position = DOOR
farmer.visible = false
_farmers.append(farmer)
_anims.append(CutsceneKit.anim_of(farmer))
## The UFO (hidden till the night), the part, the ground ring, and the flash's light.
func _build_night() -> void:
_ufo = MeshInstance3D.new()
_ufo.name = "Ufo"
_ufo.mesh = EnemySystem.build_ufo_mesh(UFO_SCALE)
_ufo_mat = ShaderMaterial.new()
_ufo_mat.shader = load("res://game/resources/ufo.gdshader") as Shader
_ufo_mat.set_shader_parameter("bob_amount", 0.25)
_ufo_mat.set_shader_parameter("spin_speed", 0.0) # the node turns it (and its lamps)
_ufo.material_override = _ufo_mat
_ufo.extra_cull_margin = 4.0 # the shader bobs it
_ufo.visible = false
_world.add_child(_ufo)
_ufo.global_position = UFO_FROM
_hum = AudioStreamPlayer3D.new()
_hum.name = "Hum"
_hum.bus = &"SFX"
_hum.unit_size = 28.0
_hum.max_distance = 600.0
_hum.volume_db = -4.0
_hum.attenuation_filter_cutoff_hz = 20500.0
_chase = 6.0
if sim.audio != null:
_hum.stream = sim.audio.stream_of("ufo_hum")
_ufo.add_child(_hum)
_ufo_light = SpotLight3D.new() # a cold light under it, on whatever it's over
_ufo_light.light_color = Color(0.7, 1.0, 0.85)
_ufo_light.light_energy = 2.0
_ufo_light.spot_range = 40.0
_ufo_light.spot_angle = 32.0
_ufo_light.spot_attenuation = 0.6
_ufo_light.shadow_enabled = false
_ufo_light.rotation.x = -PI / 2.0
_ufo_light.position = Vector3(0.0, -1.2, 0.0)
_ufo.add_child(_ufo_light)
var lamp := QuadMesh.new()
lamp.size = Vector2(1.7, 1.7)
for k in UFO_LIGHTS:
var a := TAU * k / UFO_LIGHTS
var mi := MeshInstance3D.new()
mi.mesh = lamp
var m := _glow_material(UFO_LIGHT_COLORS[k % 2], BaseMaterial3D.BILLBOARD_ENABLED)
mi.material_override = m
mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
mi.position = Vector3(cos(a), 0.0, sin(a)) * 0.63 * UFO_SCALE + Vector3(0.0, -0.06 * UFO_SCALE, 0.0)
_ufo.add_child(mi)
_ufo_lamps.append(m)
var drive := QuadMesh.new()
drive.size = Vector2(0.75, 0.75) * UFO_SCALE
var glow := MeshInstance3D.new()
glow.name = "Drive"
glow.mesh = drive
glow.material_override = _glow_material(UFO_DRIVE_COLOR, BaseMaterial3D.BILLBOARD_ENABLED)
glow.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
glow.position = Vector3(0.0, -0.16 * UFO_SCALE, 0.0)
_ufo.add_child(glow)
_streak = _particles(Color(1.0, 0.8, 0.4), 0.9, 120, 0.5)
_streak.emission_shape = CPUParticles3D.EMISSION_SHAPE_RING
_streak.emission_ring_axis = Vector3.UP
_streak.emission_ring_radius = 0.76 * UFO_SCALE
_streak.emission_ring_inner_radius = 0.7 * UFO_SCALE
_streak.emission_ring_height = 0.2
_ufo.add_child(_streak)
_part = _make_part()
_part.visible = false
_world.add_child(_part)
_dust = _particles(Color(0.62, 0.52, 0.38, 0.6), 0.9, 22, 0.9, false)
_dust.one_shot = true
_dust.explosiveness = 0.95
_dust.direction = Vector3.UP
_dust.spread = 80.0
_dust.initial_velocity_min = 1.0
_dust.initial_velocity_max = 2.4
_dust.gravity = Vector3(0.0, -1.5, 0.0)
_dust.damping_min = 2.0
_dust.damping_max = 3.0
_world.add_child(_dust)
_ring_mat = ShaderMaterial.new()
_ring_mat.shader = load("res://game/resources/fear_burst.gdshader") as Shader
_ring_mat.set_shader_parameter("color", Color(0.6, 0.85, 1.0, 1.0))
var plane := PlaneMesh.new()
plane.size = Vector2(16.0, 16.0)
_ring_mesh = MeshInstance3D.new()
_ring_mesh.name = "Ring"
_ring_mesh.mesh = plane
_ring_mesh.material_override = _ring_mat
_ring_mesh.visible = false
_world.add_child(_ring_mesh)
_burst = OmniLight3D.new()
_burst.name = "Burst"
_burst.light_color = Color(0.6, 0.85, 1.0)
_burst.omni_range = 14.0
_burst.light_energy = 0.0
_burst.shadow_enabled = false
_world.add_child(_burst)
## The overlay (letterbox, the skip hint and its ring, the box, the flash, the black, the card)
## and the loading screen over it.
func _build_screen() -> void:
_overlay = CutsceneKit.make_letterbox(self, "", Prompts.intro_skip(), 5)
_overlay.name = "Overlay"
_overlay.caption.visible = false
_ring = SkipRing.new()
_ring.mouse_filter = Control.MOUSE_FILTER_IGNORE
_overlay.add_child(_ring)
_ring.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_RIGHT)
_ring.offset_left = -404.0
_ring.offset_right = -372.0
_ring.offset_top = -68.0
_ring.offset_bottom = -36.0
_box = DialogueBox.new()
_box.name = "DialogueBox"
_overlay.add_child(_box)
_box.set_process(false) # stepped by step()
_box.blocked = blocked
_flash = _screen_rect(FLASH_COLOR)
_black = _screen_rect(Color.BLACK)
_card = Label.new()
_card.name = "Card"
Sketch.style(_card, 84, Sketch.TEXT, 0)
_card.text = CARD_TEXT
_card.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_card.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
_card.visible_characters = 0
_card.visible = false
_overlay.add_child(_card)
_card.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_loading_layer = CanvasLayer.new()
_loading_layer.name = "Loading"
_loading_layer.layer = 8 # over the overlay, under the menus
add_child(_loading_layer)
_loading = LoadingScreen.new()
_loading_layer.add_child(_loading)
_loading.set_process(false) # stepped by step()
func _screen_rect(color: Color) -> ColorRect:
var r := ColorRect.new()
r.color = Color(color, 0.0)
r.mouse_filter = Control.MOUSE_FILTER_IGNORE
_overlay.add_child(r)
r.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
return r
## Player 1's scarecrow hung on its post at START: lifted POST_LIFT, facing FACING, arms out (the
## mannequin's rest pose, a T-pose: the animation stopped), its physics off; a wooden post behind
## it with a crossbar behind its arms, tied at the wrists.
func _mount_scarecrow() -> void:
var body := sim.player
_ground = _ground_at(START)
body.set_physics_process(false)
body.velocity = Vector3.ZERO
body.stop_channel()
var yaw := atan2(-FACING.x, -FACING.z)
body.global_position = START + Vector3(0.0, _ground + POST_LIFT, 0.0)
body.model.rotation = Vector3(0.0, yaw, 0.0)
body.net_position = body.global_position
body.net_yaw = yaw
body.animation_player.stop()
body.set_core_glow(false)
# Not alive yet as far as the game goes: no fear ring at its feet, no prompts, no aim marker.
sim.scarecrow.alive = false
_marker_was = sim.combat.show_marker
sim.combat.show_marker = false
var skeletons := body.model.find_children("*", "Skeleton3D", true, false)
_skeleton = skeletons[0] as Skeleton3D if not skeletons.is_empty() else null
if _skeleton != null:
_skeleton.reset_bone_poses()
body.reset_physics_interpolation()
# The post: behind the body, up to its neck; the crossbar behind the arms.
var wood := StandardMaterial3D.new()
wood.albedo_color = Color(0.45, 0.31, 0.18)
var rope := StandardMaterial3D.new()
rope.albedo_color = Color(0.7, 0.58, 0.36)
_post = Node3D.new()
_post.name = "Post"
_world.add_child(_post)
_post.global_position = START + Vector3(0.0, _ground, 0.0) - FACING * 0.3
_post.rotation.y = yaw
var top := POST_LIFT + 1.62
var arms := POST_LIFT + _arm_height()
_cylinder(_post, 0.075, top + 0.25, Vector3(0.0, (top - 0.25) * 0.5, 0.0), Vector3.ZERO, wood)
_cylinder(_post, 0.06, 2.0, Vector3(0.0, arms, -0.1), Vector3(0.0, 0.0, PI / 2.0), wood)
for x: float in [-0.72, 0.72]: # rope round each wrist
var ring := TorusMesh.new()
ring.inner_radius = 0.13
ring.outer_radius = 0.17
var r := MeshInstance3D.new()
r.name = "Rope%d" % _ropes.size()
_ropes.append(r)
r.mesh = ring
r.rotation = Vector3(0.0, 0.0, PI / 2.0)
r.position = Vector3(x, arms, -0.3)
r.material_override = rope
_post.add_child(r)
## The ropes come loose as it drops off the post.
func _post_ropes_off() -> void:
for r in _ropes:
if is_instance_valid(r):
r.visible = false
## The height of the scarecrow's arms over its feet (its upper arm in the rest pose).
func _arm_height() -> float:
if _skeleton != null:
var bone := _skeleton.find_bone("upperarm.l")
if bone >= 0:
return _skeleton.get_bone_global_rest(bone).origin.y
return 1.1
## The part: a dark metal capsule with a gyroscope ring round a glowing blue core (unshaded, so
## the toon look leaves it bright), a halo, a small light, and its trail (off till it falls).
func _make_part() -> Node3D:
var root := Node3D.new()
root.name = "Part"
var metal := StandardMaterial3D.new()
metal.albedo_color = Color(0.22, 0.23, 0.27)
metal.metallic = 0.75
metal.roughness = 0.35
var trim := StandardMaterial3D.new()
trim.albedo_color = Color(0.5, 0.52, 0.58)
trim.metallic = 0.8
trim.roughness = 0.3
_cylinder(root, 0.08, 0.46, Vector3.ZERO, Vector3.ZERO, metal)
_cylinder(root, 0.11, 0.05, Vector3(0.0, 0.2, 0.0), Vector3.ZERO, trim)
_cylinder(root, 0.11, 0.05, Vector3(0.0, -0.2, 0.0), Vector3.ZERO, trim)
var torus := TorusMesh.new()
torus.inner_radius = 0.17
torus.outer_radius = 0.215
var ring := MeshInstance3D.new()
ring.mesh = torus
ring.rotation.x = PI / 2.0
ring.material_override = trim
root.add_child(ring)
_core_mat = StandardMaterial3D.new()
_core_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
_core_mat.albedo_color = PART_BLUE
var sphere := SphereMesh.new()
sphere.radius = 0.12
sphere.height = 0.24
var core := MeshInstance3D.new()
core.name = "Core"
core.mesh = sphere
core.material_override = _core_mat
core.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
root.add_child(core)
var quad := QuadMesh.new()
quad.size = Vector2(1.0, 1.0)
var halo := MeshInstance3D.new()
halo.name = "Halo"
halo.mesh = quad
halo.material_override = _glow_material(Color(PART_BLUE, 0.8), BaseMaterial3D.BILLBOARD_ENABLED)
halo.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
root.add_child(halo)
_part_light = OmniLight3D.new()
_part_light.light_color = PART_BLUE
_part_light.omni_range = 3.5
_part_light.light_energy = 1.2
_part_light.shadow_enabled = false
root.add_child(_part_light)
_trail = Trail.new(Color(0.35, 0.65, 1.0, 0.85), 0.34, 0.5)
root.add_child(_trail)
return root
## Loose glowing specks (additive) or puffs (not): emitted in world space, so they stay behind
## what emits them; they shrink and fade over their life.
func _particles(color: Color, size: float, amount: int, life: float, additive := true) -> CPUParticles3D:
var p := CPUParticles3D.new()
p.emitting = false
p.amount = amount
p.lifetime = life
p.local_coords = false
var quad := QuadMesh.new()
quad.size = Vector2(size, size)
var mat := _glow_material(Color.WHITE, BaseMaterial3D.BILLBOARD_PARTICLES)
if not additive:
mat.blend_mode = BaseMaterial3D.BLEND_MODE_MIX
quad.material = mat
p.mesh = quad
p.direction = Vector3.UP
p.spread = 180.0
p.initial_velocity_min = 0.05
p.initial_velocity_max = 0.4
p.gravity = Vector3.ZERO
var curve := Curve.new()
curve.add_point(Vector2(0.0, 1.0))
curve.add_point(Vector2(1.0, 0.25 if additive else 1.6))
p.scale_amount_curve = curve
var ramp := Gradient.new()
ramp.set_color(0, color)
ramp.set_color(1, Color(color.lightened(0.3), 0.0))
p.color_ramp = ramp
return p
static func _glow_material(color: Color, billboard: BaseMaterial3D.BillboardMode) -> StandardMaterial3D:
var m := StandardMaterial3D.new()
m.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
m.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
m.blend_mode = BaseMaterial3D.BLEND_MODE_ADD
m.albedo_color = color
m.albedo_texture = _soft_dot()
m.vertex_color_use_as_albedo = true
m.billboard_mode = billboard
m.billboard_keep_scale = true
m.cull_mode = BaseMaterial3D.CULL_DISABLED
m.disable_receive_shadows = true
return m
## A soft round dot (white, fading out to the edge).
static func _soft_dot() -> GradientTexture2D:
if _dot == null:
var g := Gradient.new()
g.set_color(0, Color(1.0, 1.0, 1.0, 1.0))
g.set_color(1, Color(1.0, 1.0, 1.0, 0.0))
_dot = GradientTexture2D.new()
_dot.gradient = g
_dot.fill = GradientTexture2D.FILL_RADIAL
_dot.fill_from = Vector2(0.5, 0.5)
_dot.fill_to = Vector2(1.0, 0.5)
_dot.width = 64
_dot.height = 64
return _dot
static func _cylinder(parent: Node3D, radius: float, height: float, at: Vector3, rot: Vector3, mat: Material) -> MeshInstance3D:
var mesh := CylinderMesh.new()
mesh.top_radius = radius
mesh.bottom_radius = radius
mesh.height = height
mesh.radial_segments = 12
var mi := MeshInstance3D.new()
mi.mesh = mesh
mi.position = at
mi.rotation = rot
mi.material_override = mat
parent.add_child(mi)
return mi
# --- Pieces of the action ------------------------------------------------------------------------
## A farmer steps out (at OUT_AT) and walks their path to the car's door, then faces the car.
func _walk(i: int, delta: float) -> void:
var f := _farmers[i]
if not f.visible:
if _t < OUT_AT[i]:
return
f.visible = true
f.global_position = DOOR
_anims[i].play(&"Walking_A", 0.1)
var path := _paths[i]
if _legs[i] >= path.size():
return
var to := path[_legs[i]] - f.global_position
to.y = 0.0
var step := WALK_SPEED * delta
if to.length() <= step:
f.global_position = path[_legs[i]]
_legs[i] += 1
if _legs[i] >= path.size():
CutsceneKit.face(f, _car.global_position)
_anims[i].play(&"Idle_A", 0.25)
return
f.global_position += to.normalized() * step
var turn := atan2(to.x, to.z)
f.rotation.y = lerp_angle(f.rotation.y, turn, 1.0 - exp(-10.0 * delta))
func _arrived(i: int) -> bool:
return _legs[i] >= _paths[i].size()
func _talk(path: String) -> void:
_talked = true
var c := load(path) as Conversation
if c == null or not _box.play(c):
_box.stop()
## The farm at night under the black: the car and the farmers gone, the night look snapped with
## the lanterns dark, the UFO far off with its hum, the camera low on the farm.
func _go_night() -> void:
_night = true
if _engine != null:
_engine.stop()
if _car != null:
_car.queue_free()
_car = null
_engine = null
for f in _farmers:
f.queue_free()
_farmers.clear()
_anims.clear()
var dn := sim.day_night
if dn != null:
dn.forced = "night"
dn.lanterns_dark = true
dn.snap()
_ufo.visible = true
_spin_rate = 1.0
_tug = Vector2.ZERO
_tug_speed = Vector2.ZERO
_place_ufo(UFO_FROM, (HOVER - UFO_FROM).normalized(), 0.0, 0.0)
if _hum.stream != null:
_hum.play()
CutsceneKit.move_camera(_camera, FLIGHT_CAMERA, _lifted(FLIGHT_CAMERA, UFO_FROM, LINE_LIFT), 1.0, 1.0)
## A jerk on the controls at each new line: alternately to one side and the other (who's got the
## stick), a dip or a climb, and a lurch forward.
func _tug_line(line: int) -> void:
var side := 1.0 if line % 2 == 0 else -1.0
_tug_speed += Vector2(side * 12.0, -5.0 if line % 2 == 0 else 4.0)
_lurch = 0.6
_spin_rate = 1.0 + 4.0 * side
## A smaller, guilty shuffle at each line of the uh-oh.
func _shuffle(line: int) -> void:
var side := -1.0 if line % 2 == 0 else 1.0
_tug_speed += Vector2(side * 4.0, 1.5)
_spin_rate = 1.0 - 1.5 * side
## The tug's spring (stiffness, damping) back to the path.
func _step_tug(delta: float, stiffness: float, damping: float) -> void:
var left := minf(delta, 0.25)
while left > 0.0:
var h := minf(left, 1.0 / 60.0)
_tug_speed += (-_tug * stiffness - _tug_speed * damping) * h
_tug += _tug_speed * h
left -= h
_spin_rate = lerpf(_spin_rate, 1.0, 1.0 - exp(-1.5 * delta))
## Puts the UFO at `at`, tilted (bank: about fwd, its travel; pitch: nose down about its side), spun.
func _place_ufo(at: Vector3, fwd: Vector3, bank: float, pitch: float) -> void:
var f := Vector3(fwd.x, 0.0, fwd.z)
f = f.normalized() if f.length_squared() > 1e-6 else Vector3.FORWARD
var side := f.cross(Vector3.UP).normalized()
var tilt := Basis(f, bank) * Basis(side, -pitch)
_ufo.global_transform = Transform3D(tilt * Basis(Vector3.UP, _spin), at)
## The UFO hanging over the farm (the flash stage), settling; once the flash has gone off it backs
## away (out of the shot) to where it hangs, guilty, for the uh-oh.
func _hover_ufo(delta: float) -> void:
_sway *= exp(-3.0 * delta)
_spin += _spin_rate * delta
_center = _center.lerp(HOVER_AFTER if _alive_step >= 1 else HOVER, 1.0 - exp(-1.2 * delta))
var across := _across()
_place_ufo(_center + across * _sway, across.cross(Vector3.UP), 0.0, 0.0)
## Across the shot (the camera's right, level).
func _across() -> Vector3:
var r := _camera.global_basis.x
r.y = 0.0
return r.normalized() if r.length_squared() > 1e-6 else Vector3.RIGHT
## The part pops off the rim on the scarecrow's side, a little round from the camera, and
## tumbles down to land in front of it; the trail comes on, and a falling whistle.
func _pop_part() -> void:
_land = START + FACING * 0.8
_land.y = _ground_at(_land) + 0.12
var toward := Vector3(_land.x - _center.x, 0.0, _land.z - _center.z)
toward = toward.normalized() if toward.length_squared() > 1e-6 else -FACING
var rim := toward * 0.78 * UFO_SCALE
_part_from = _ufo.global_position + rim + Vector3(0.0, -0.4, 0.0)
# From there to the landing spot in FALL_TIME, falling at 9.8 m/s².
_part_velocity = (_land - _part_from - Vector3(0.0, -4.9, 0.0) * FALL_TIME * FALL_TIME) / FALL_TIME
_part_spin = Vector3(7.0, 4.0, 9.0)
_part.global_position = _part_from
_part.visible = true
_trail.emitting = true
_pulse_part(1.0)
if sim.audio != null:
sim.audio.play("part_fall", _part_from)
func _land_part() -> void:
_landed = true
_part.global_position = _land
_part.rotation = Vector3(0.0, 0.4, deg_to_rad(78.0)) # on its side
_trail.emitting = false
_dust.global_position = _land
_dust.restart()
_dust.emitting = true
_burst.global_position = _land + Vector3(0.0, 0.6, 0.0)
_ring_mesh.global_position = Vector3(_land.x, _ground + 0.03, _land.z)
if sim.audio != null:
sim.audio.play("fall", _land)
## The part's core and light at strength k (1 at rest).
func _pulse_part(k: float) -> void:
if _core_mat != null:
_core_mat.albedo_color = PART_BLUE.lerp(Color.WHITE, clampf((k - 1.0) / 3.0, 0.0, 0.8))
if _part_light != null:
_part_light.light_energy = 1.2 * k
## Its head turns up toward the UFO (a LookAtModifier3D on the head, easing in).
func _look_up() -> void:
if _skeleton == null or _ufo == null:
return
_look = LookAtModifier3D.new()
_look.name = "IntroLookUp"
_look.bone_name = "head"
_look.influence = 0.0
_look.duration = 0.6
_skeleton.add_child(_look)
_look.target_node = _look.get_path_to(_ufo)
## Low behind the scarecrow's right shoulder, looking up past it at the UFO (rate: how closely it
## follows the UFO).
func _uh_oh_shot(_delta: float, rate_delta: float, rate: float) -> void:
var right := FACING.cross(Vector3.UP)
var at := _stand() - FACING * UH_OH_BACK + right * UH_OH_SIDE + Vector3(0.0, UH_OH_HEIGHT, 0.0)
_camera.fov = UH_OH_FOV
CutsceneKit.move_camera(_camera, at, _lifted(at, _ufo.global_position, LINE_LIFT), rate_delta, rate)
## Where the scarecrow stands once it's off its post.
func _stand() -> Vector3:
return START + FACING * HOP + Vector3(0.0, _ground, 0.0)
## Eases the camera at `at` to look under target by LINE_LIFT degrees (the target in the upper
## half of the frame, over the box's lines).
func _track(at: Vector3, target: Vector3, delta: float, rate: float) -> void:
CutsceneKit.move_camera(_camera, at, _lifted(at, target, LINE_LIFT), delta, rate)
static func _lifted(from: Vector3, target: Vector3, degrees: float) -> Vector3:
var d := target - from
var right := d.cross(Vector3.UP)
if right.length_squared() < 1e-6:
return target
return from + d.rotated(right.normalized(), -deg_to_rad(degrees))
## Where the listener is: on the shot's subject.
func _hear() -> void:
if _ears == null:
return
match stage:
"leave":
_ears.global_position = (DOOR + _doors[0]) * 0.5 if not _doors.is_empty() else DOOR
"board":
_ears.global_position = _car.global_position
"drive":
_ears.global_position = _car.global_position if _driven < _gap_at - GAP_LEAD else _camera.global_position
"card", "flight", "wobble":
_ears.global_position = _camera.global_position
_:
_ears.global_position = _stand()
## How many letters of the card are written t seconds into it.
static func _card_letters(t: float) -> int:
var at := 0.0
for i in CARD_TEXT.length():
at += CARD_DOT if CARD_TEXT[i] == "." else 1.0 / CARD_LETTERS
if at > t:
return i
return CARD_TEXT.length()
static func _bezier(a: Vector3, b: Vector3, c: Vector3, t: float) -> Vector3:
return a.lerp(b, t).lerp(b.lerp(c, t), t)
## The ground's height at (x, z) (the farm's collision; 0 where there's none).
func _ground_at(at: Vector3) -> float:
var space := get_world_3d().direct_space_state if is_inside_tree() else null
if space == null:
return 0.0
var q := PhysicsRayQueryParameters3D.create(Vector3(at.x, 20.0, at.z), Vector3(at.x, -5.0, at.z), 1)
var hit := space.intersect_ray(q)
return float((hit["position"] as Vector3).y) if not hit.is_empty() else 0.0
func _input(event: InputEvent) -> void:
var touch := event as InputEventScreenTouch
if touch != null:
if touch.pressed:
_fingers[touch.index] = true
else:
_fingers.erase(touch.index)
func _notification(what: int) -> void:
if what == NOTIFICATION_PAUSED or what == NOTIFICATION_UNPAUSED:
_fingers.clear() # a finger lifted while paused never told us
func _poll_skip(delta: float) -> void:
var frame := Engine.get_process_frames()
if frame > _last_frame + 2:
_armed = false # back from a pause: a key still down from the menu doesn't count
_last_frame = frame
var held := not _fingers.is_empty()
for action in SKIP_ACTIONS:
held = held or Input.is_action_pressed(action)
if not held or (blocked.is_valid() and bool(blocked.call())):
_armed = _armed or not held
_hold = 0.0
elif _armed:
_hold += delta
if _hold >= SKIP_HOLD:
skip()
if _ring != null:
_ring.set_progress(_hold / SKIP_HOLD)