harvest/game/scripts/tutorial.gd
Forrest 49e94874a6 #39: tappable HUD
On phones (MobilePort.active()) the HUD's widgets take taps through small
STOP hit targets (ScarecrowHud.HitTarget, at least minimum_target()), and
everything beside them is still the thumbsticks':
- hotbar: a tap on a main slot picks it through the number keys' path
  (weapon_k, applied only when combat may switch); no number keys on phones
  (Prompts.hotbar_key)
- backpack: a tap toggles the pouch panel, a finger elsewhere (not on the
  panel) closes it; the panel stays inside safe_rect
- tip box: an × hides tips as Backspace does (Tutorial.hide_tips)
- dialogue: a Skip button on pausing conversations; a tap elsewhere advances
Desktop has no targets and draws as before. Test: mobile/hud_taps_test.gd.

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

243 lines
9.3 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name Tutorial
extends Control
## Short contextual tips on the first day and night (night 1), one at a time in a box above the
## hotbar. Each shows when it's useful and goes away once it's done:
## Day: move -> gather -> store at the shed -> build (also once half the day has gone)
## Dusk: where and when the thieves come
## Night: fight (once enemies are in), fear (when crowded), recover (when something is
## dropped), patch up (when hurt)
## The most urgent one wins (recover, fear, patch, shoot, dusk, then the day's in order).
## [Backspace / Back] hides them until the game is restarted (hide_tips); on a phone so does the ×
## on the tip box's corner (a ScarecrowHud.HitTarget). Settings turns them off.
## Finished tips stay finished for the session, so a retry or a second vacation doesn't repeat
## them.
## None show while a talking-head conversation or the opening intro is up.
## The tips as the desktop shows them. Phones say the ones that name a control in touch words
## (Prompts.TOUCH_TIPS); Prompts.tip() picks.
const TEXT := {
"move": "Move with [W A S D] or the left stick. You're the scarecrow: keep the farm safe until the farmers are back.",
"gather": "Hold [E / X] at a dead tree, the haystacks, or the scrap heap to gather wood, straw, and scrap.",
"store": "Your pouch only holds so much ([I] shows what's in it): walk onto the barn's apron (the Supply Shed) to store what you carry.",
"build": "Press [B / d-pad up] to build. Pick a fence [1], place it with left mouse / RT. Aliens have to break through fences.",
"dusk": "Dusk: aliens come at nightfall, from every side of the farm.",
"shoot": "Aim with the mouse or the right stick and hold left mouse / RT to stab with your pitchfork. The supply shed sells a corn shooter (switch with [Tab / the number keys]); more weapons arrive there over the days, like pumpkin bombs for your off-hand (throw them with [right mouse / LT]).",
"fear": "Crowded? [Q / LB] Fear scares off everything nearby, and they drop what they carry.",
"recover": "Something was dropped! Walk over it to bring it home. A dropped animal follows you back to the pen.",
"patch": "Hurt? Hold [F / Y] to patch up with straw. Health carries over to the next night.",
}
const PRIORITY: Array[String] = ["recover", "fear", "patch", "shoot", "dusk", "move", "gather", "store", "build"]
const DONE_TIME := 0.9
const CROWD_RADIUS := 7.0
## Phones: the ×'s disc, centered this far in from the tip box's top-right corner.
const HIDE_RADIUS := 20.0
const HIDE_INSET := 6.0
const BOX_FILL := Color(0.08, 0.1, 0.08, 0.86)
const BOX_EDGE := Color(0.95, 0.82, 0.45)
var sim: FarmSim
## Off when the player turned tips off in Settings.
var enabled := true
## The player hid them (for this session).
var dismissed := false
var done := {}
var current := ""
## Phones: the tip box's × (null on desktop).
var hide_target: ScarecrowHud.HitTarget
var _box: PanelContainer
var _text: Label
var _hint: Label
var _start := Vector3.ZERO
var _shots_at_night: int = 0
var _done_left: float = 0.0
func setup(p_sim: FarmSim) -> void:
sim = p_sim
sim.campaign.phase_changed.connect(_on_phase)
sim.interactions.deposited.connect(func(_moved: Dictionary) -> void: _finish("store"))
sim.scarecrow.fear_used.connect(func(_p: Vector3, _n: int) -> void: _finish("fear"))
sim.scarecrow.patched.connect(func(_hp: int) -> void: _finish("patch"))
sim.loot.loot_event.connect(func(kind: StringName, _id: int, _at: Vector3) -> void:
if kind == &"recovered" or kind == &"following":
_finish("recover"))
sim.campaign.campaign_ended.connect(func(_o: CampaignController.Outcome) -> void: _show(""))
_start = sim.player.global_position
func _ready() -> void:
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_IGNORE
_box = PanelContainer.new()
var bg := StyleBoxFlat.new()
bg.bg_color = BOX_FILL
bg.border_color = BOX_EDGE
bg.set_border_width_all(2)
bg.set_corner_radius_all(6)
bg.content_margin_left = 18.0
bg.content_margin_right = 18.0
bg.content_margin_top = 10.0
bg.content_margin_bottom = 10.0
_box.add_theme_stylebox_override("panel", bg)
_box.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_box)
var col := VBoxContainer.new()
_box.add_child(col)
_text = Label.new()
_text.custom_minimum_size = Vector2(760.0, 0.0)
_text.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_text.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_text.add_theme_font_size_override("font_size", 24)
col.add_child(_text)
_hint = Label.new()
_hint.text = Prompts.tip_footer()
_hint.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_hint.add_theme_font_size_override("font_size", 16)
_hint.add_theme_color_override("font_color", Color(0.7, 0.72, 0.65))
col.add_child(_hint)
_box.visible = false
if MobilePort.active():
hide_target = ScarecrowHud.HitTarget.new()
hide_target.name = "HideTips"
hide_target.visible = false
hide_target.tapped.connect(hide_tips)
hide_target.draw.connect(_draw_hide)
add_child(hide_target)
_box.item_rect_changed.connect(_place_hide)
func _process(delta: float) -> void:
if sim == null:
return
var s := sim.campaign.state
if not enabled or dismissed or s.night != 1 or sim.campaign.is_over() or sim._menu_open() \
or (sim.dialogue != null and sim.dialogue.is_playing()) or (sim.intro != null and sim.intro.busy()):
_show("")
return
_check_done()
if _done_left > 0.0:
_done_left -= delta
return
var want := ""
for id in PRIORITY:
if not done.has(id) and _wanted(id):
want = id
break
_show(want)
## Whether the tip is useful right now.
func _wanted(id: String) -> bool:
var c := sim.campaign
var phase := c.state.phase
var day := phase == CampaignController.Phase.DAY or phase == CampaignController.Phase.DUSK
var night := phase == CampaignController.Phase.NIGHT or phase == CampaignController.Phase.DAWN
match id:
"move":
return phase == CampaignController.Phase.DAY
"gather":
return day and done.has("move")
"store":
return day and c.state.resources.pouch_total(sim.local_avatar.slot) > 0
"build":
return day and (done.has("store") or c.state.phase_time_left < c.phase_length(CampaignController.Phase.DAY) * 0.5
or phase == CampaignController.Phase.DUSK)
"dusk":
return phase == CampaignController.Phase.DUSK
"shoot":
return night and sim.enemies.alive_count() > 0
"fear":
return night and sim.scarecrow.fear_left == 0.0 and _enemies_near() >= 2
"recover":
return sim.loot.count(LootSystem.Status.DROPPED) > 0
"patch":
return night and sim.scarecrow.hp() < sim.scarecrow.max_hp()
return false
func _check_done() -> void:
var s := sim.campaign.state
if not done.has("move") and Vector2(sim.player.global_position.x - _start.x, sim.player.global_position.z - _start.z).length() > 4.0:
_finish("move")
var e := s.night_events
if not done.has("gather") and int(e.get("gathered_wood", 0)) + int(e.get("gathered_scrap", 0)) + int(e.get("gathered_straw", 0)) > 0:
_finish("gather")
if not done.has("build") and int(e.get("structures_built", 0)) > 0:
_finish("build")
if not done.has("shoot") and sim.combat.attacks() - _shots_at_night >= 4 and s.phase == CampaignController.Phase.NIGHT:
_finish("shoot")
func _finish(id: String) -> void:
if done.has(id):
return
done[id] = true
if id == current and _box.visible:
_text.text = "Done! " + _text.text
_text.add_theme_color_override("font_color", Color(0.6, 1.0, 0.55))
_done_left = DONE_TIME
## Hides the tips until the game is restarted: [Backspace / Back], or the × on a phone.
func hide_tips() -> void:
dismissed = true
_show("")
func _show(id: String) -> void:
current = id
_box.visible = id != ""
if hide_target != null:
hide_target.visible = _box.visible
if id == "":
return
_text.text = Prompts.tip(id)
_text.remove_theme_color_override("font_color")
_box.reset_size()
_box.position = Vector2((size.x - _box.size.x) * 0.5, size.y - 250.0 - _box.size.y)
if hide_target != null:
_place_hide()
func _enemies_near() -> int:
var p := Vector2(sim.player.global_position.x, sim.player.global_position.z)
var es := sim.enemies
var n := 0
for d in es.active_count:
if es.alive[d] == 1 and Vector2(es.pos_x[d], es.pos_z[d]).distance_to(p) <= CROWD_RADIUS:
n += 1
return n
func _on_phase(phase: CampaignController.Phase, _previous: CampaignController.Phase) -> void:
match phase:
CampaignController.Phase.DAY:
_start = sim.player.global_position
CampaignController.Phase.NIGHT:
_finish("dusk")
_shots_at_night = sim.combat.attacks()
func _unhandled_input(event: InputEvent) -> void:
if _box.visible and event.is_action_pressed("hide_tips"):
hide_tips()
get_viewport().set_input_as_handled()
## Phones: the × at the tip box's top-right corner, over it.
func _place_hide() -> void:
var r := _box.get_rect()
var at := Vector2(r.end.x - HIDE_INSET, r.position.y + HIDE_INSET)
hide_target.cover(Rect2(at - Vector2(HIDE_RADIUS, HIDE_RADIUS), Vector2(HIDE_RADIUS, HIDE_RADIUS) * 2.0))
hide_target.queue_redraw()
## The ×: a disc of the tip box's own fill and edge, on its corner.
func _draw_hide() -> void:
var c := hide_target.size * 0.5
hide_target.draw_circle(c, HIDE_RADIUS, Color(BOX_FILL, 0.97))
Sketch.circle(hide_target, c, HIDE_RADIUS, BOX_EDGE, 2.5, 3.0)
var k := HIDE_RADIUS * 0.42
Sketch.line(hide_target, c + Vector2(-k, -k), c + Vector2(k, k), Sketch.TEXT, 3.5, 11.0, 0.5)
Sketch.line(hide_target, c + Vector2(k, -k), c + Vector2(-k, k), Sketch.TEXT, 3.5, 12.0, 0.5)