166 lines
6.4 KiB
GDScript
166 lines
6.4 KiB
GDScript
extends Control
|
|
|
|
const State = preload("res://workshop_state.gd")
|
|
const World = preload("res://pixel_world.gd")
|
|
const Audio = preload("res://lab_audio.gd")
|
|
const SAVE_PATH := "user://workshop_lab_v1.json"
|
|
var model := State.new()
|
|
var audio: Node
|
|
var world: Control
|
|
var column: VBoxContainer
|
|
var scroll: ScrollContainer
|
|
var body: VBoxContainer
|
|
var footer: VBoxContainer
|
|
var title: Label
|
|
var stats: Label
|
|
var guidance: Label
|
|
var notice: Label
|
|
var actions: Dictionary = {}
|
|
var zh: bool = false
|
|
var large_text: bool = false
|
|
var touch_events: int = 0
|
|
var drag_events: int = 0
|
|
var save_enabled: bool = true
|
|
|
|
func _ready() -> void:
|
|
audio = Audio.new()
|
|
add_child(audio)
|
|
var back := ColorRect.new()
|
|
back.color = Color("14232e")
|
|
back.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
back.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
add_child(back)
|
|
var margin := MarginContainer.new()
|
|
margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
for side in ["left", "right", "top", "bottom"]:
|
|
margin.add_theme_constant_override("margin_" + side, 14)
|
|
add_child(margin)
|
|
column = VBoxContainer.new()
|
|
column.add_theme_constant_override("separation", 8)
|
|
margin.add_child(column)
|
|
title = label("Workshop Lab", column)
|
|
var settings := GridContainer.new()
|
|
settings.columns = 2
|
|
column.add_child(settings)
|
|
var language := button("EN / 中文", settings)
|
|
language.pressed.connect(func(): zh = not zh; refresh_text())
|
|
var scale_button := button("130%", settings)
|
|
scale_button.pressed.connect(func(): large_text = not large_text; refresh_text())
|
|
var mute := button("Mute", settings)
|
|
mute.pressed.connect(func(): audio.set_muted(not audio.muted); mute.text = "Muted" if audio.muted else "Mute")
|
|
var motion := button("Motion", settings)
|
|
motion.pressed.connect(func(): world.reduced_motion = not world.reduced_motion; motion.text = "Still" if world.reduced_motion else "Motion")
|
|
scroll = ScrollContainer.new()
|
|
scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
|
|
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
|
|
column.add_child(scroll)
|
|
body = VBoxContainer.new()
|
|
body.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
body.add_theme_constant_override("separation", 10)
|
|
scroll.add_child(body)
|
|
world = World.new()
|
|
world.model = model
|
|
world.custom_minimum_size = Vector2(0, 160)
|
|
world.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
body.add_child(world)
|
|
stats = label("", body)
|
|
guidance = label("", body)
|
|
for action in ["collect", "sell", "craft", "hire", "expand", "transform"]:
|
|
var target := button(action, body)
|
|
target.pressed.connect(command.bind(action))
|
|
actions[action] = target
|
|
notice = label("", body)
|
|
footer = VBoxContainer.new()
|
|
column.add_child(footer)
|
|
var files := HBoxContainer.new()
|
|
footer.add_child(files)
|
|
button("Save", files).pressed.connect(save_lab)
|
|
button("Load", files).pressed.connect(load_lab)
|
|
label("Reference exercise · offline progress and final art remain exercises.", footer)
|
|
refresh_text()
|
|
|
|
func label(value: String, parent: Node) -> Label:
|
|
var node := Label.new()
|
|
node.text = value
|
|
node.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
node.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
parent.add_child(node)
|
|
return node
|
|
|
|
func button(value: String, parent: Node) -> Button:
|
|
var node := Button.new()
|
|
node.text = value
|
|
node.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
node.custom_minimum_size = Vector2(0, 48)
|
|
node.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
parent.add_child(node)
|
|
return node
|
|
|
|
func refresh_text() -> void:
|
|
var style := Theme.new()
|
|
style.default_font_size = 21 if large_text else 16
|
|
var font := SystemFont.new()
|
|
font.font_names = PackedStringArray(["PingFang SC", "Noto Sans CJK SC", "sans-serif"])
|
|
style.default_font = font
|
|
theme = style
|
|
title.text = "工坊教学实验" if zh else "Workshop teaching lab"
|
|
var names: Dictionary = {"collect": ["采集一份木料", "Collect one timber"],
|
|
"sell": ["出售库存:立刻腾出仓库", "Sell stock: clear storage now"],
|
|
"craft": ["加工:收益更高,但增加热量", "Craft: higher yield, adds heat"],
|
|
"hire": ["雇佣工人:7 金币,每秒采集", "Hire: 7 coins, gather each second"],
|
|
"expand": ["扩建:4 金币,增加 10 容量", "Expand: 4 coins, add 10 capacity"],
|
|
"transform": ["完成 3 次加工后开启下一阶段", "Next chapter after 3 crafts"]}
|
|
for key in names:
|
|
actions[key].text = names[key][0 if zh else 1]
|
|
guidance.text = ("先采集并出售,再雇工。库存满时出售或扩建;工坊过热时先出售。每 5 秒散去 1 热量。下一阶段保留工人与仓库,加工需求改变。"
|
|
if zh else "Gather, sell, then hire. Full storage: sell or expand. Hot workshop: sell while it cools. Heat falls by one every 5 seconds. Next chapter keeps workers and capacity, but changes craft requirements.")
|
|
refresh_stats()
|
|
|
|
func refresh_stats() -> void:
|
|
stats.text = ("木料 %d/%d · 金币 %d · 工人 %d\n热量 %d/4 · 加工 %d/3 · 阶段 %d · 加工需 %d" if zh else
|
|
"Timber %d/%d · Coins %d · Workers %d\nHeat %d/4 · Crafts %d/3 · Chapter %d · Craft cost %d") % [
|
|
model.stock, model.capacity, model.coins, model.workers, model.heat, model.crafted, model.chapter, model.craft_cost()]
|
|
|
|
func command(action: String) -> void:
|
|
var accepted: bool = model.call(action)
|
|
notice.text = ("已完成:" if zh else "Committed: ") + action if accepted else ("条件未满足;资源没有改变。" if zh else "Requirements unmet; resources unchanged.")
|
|
if accepted:
|
|
world.pulse = 0.18
|
|
audio.emit_cue(action if accepted else "denied")
|
|
refresh_stats()
|
|
|
|
func _process(delta: float) -> void:
|
|
model.advance(delta)
|
|
if stats != null:
|
|
refresh_stats()
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventScreenTouch:
|
|
touch_events += 1
|
|
if event is InputEventScreenDrag:
|
|
drag_events += 1
|
|
|
|
func save_lab() -> void:
|
|
if not save_enabled:
|
|
return
|
|
var file := FileAccess.open(SAVE_PATH, FileAccess.WRITE)
|
|
if file == null:
|
|
notice.text = "Save failed; no success claimed."
|
|
return
|
|
file.store_string(JSON.stringify(model.snapshot()))
|
|
file.close()
|
|
notice.text = "Saved to the lab's own user directory."
|
|
|
|
func load_lab() -> void:
|
|
if not save_enabled:
|
|
return
|
|
if not FileAccess.file_exists(SAVE_PATH):
|
|
notice.text = "No lab save exists."
|
|
return
|
|
var value: Variant = JSON.parse_string(FileAccess.get_file_as_string(SAVE_PATH))
|
|
if not model.restore(value):
|
|
notice.text = "Invalid lab save; current state and file preserved."
|
|
return
|
|
notice.text = "Loaded; no offline reward applied."
|
|
refresh_stats()
|