extends "res://designer/scripts/studio.gd" # Only the initial impulse is authored. Existing asset colliders and iron-ball # forces own all motion after release; guide lines have no physics bodies. const LEVEL = "res://playtest/underpass/data/underpass_validation.json" const LOWER_IDS = ["lower_entry", "crossing", "lower_turn_a", "lower_middle_a", "lower_middle_b", "lower_turn_b", "lower_exit"] const BLUE = Color("287ca0") const ORANGE = Color("d27c36") var speed_slider: HSlider var speed_label: Label var view_button: Button var route_label: Label var xray := false var guides := Node3D.new() var checkpoints: Array = [] var passed_checkpoints: Array = [] var route_contacts: Dictionary = {} var ball_contacts := 0 var lower_trace: Array = [] var stalled_time := 0.0 var launch_speed := 2.4 var snapshot_time := -1.0 func _ready() -> void: super._ready() get_window().title = "IntoHole · 底部通孔验证" for arg in OS.get_cmdline_user_args(): if arg == "--xray": toggle_xray() if arg.begins_with("--snapshot-time="): snapshot_time = float(arg.trim_prefix("--snapshot-time=")); capture_count = -1 func setup_environment() -> void: super.setup_environment() camera.h_offset = -1.4 yaw = 1.05; pitch = .74 add_child(guides) func setup_ui() -> void: var layer := CanvasLayer.new(); add_child(layer) var panel := PanelContainer.new(); panel.position = Vector2(20, 20); panel.custom_minimum_size.x = 288 panel.add_theme_stylebox_override("panel", style(PAPER)); layer.add_child(panel) var col := VBoxContainer.new(); col.add_theme_constant_override("separation", 12); panel.add_child(col) col.add_child(label("INTO / HOLE", 27)) col.add_child(label("底部通孔验证", 22)) col.add_child(label("下层 7 块 · 2 次转弯\n上层横穿同一块滑槽", 16)) run_button = button("▶ 同时释放", toggle_run, true); col.add_child(run_button) col.add_child(button("↺ 重置", reset_simulation)) speed_label = label("初速度 2.4 m/s", 16); col.add_child(speed_label) speed_slider = HSlider.new(); speed_slider.min_value = 0; speed_slider.max_value = 5 speed_slider.step = .1; speed_slider.value = launch_speed; speed_slider.custom_minimum_size = Vector2(260, 26) speed_slider.value_changed.connect(func(value): launch_speed = value; speed_label.text = "初速度 %.1f m/s" % value) col.add_child(speed_slider) view_button = button("观察方式:实体 → 透视", toggle_xray); col.add_child(view_button) col.add_child(label("蓝线 / 1 下层孔道\n橙线 / 2 上层滑槽", 16)) count_label = label("", 19); col.add_child(count_label) route_label = label("", 15); col.add_child(route_label) status_label = label("", 15); status_label.custom_minimum_size.x = 260 status_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART; col.add_child(status_label) col.add_child(label("空格 释放 / 暂停 T 重置\nX 实体 / 透视 F 全景\n右键旋转 · 中键平移 · 滚轮缩放", 13)) col.add_child(label("水平通孔需要初始动能。\n调为 0 可观察静止对照。", 14, Color("7a7366"))) func initialize_level() -> void: apply_data(JSON.parse_string(FileAccess.get_file_as_string(LEVEL))) func rebuild() -> void: super.rebuild() clear_children(guides); checkpoints.clear() for id in LOWER_IDS: var r: Dictionary = records[index_for(id)] var basis := Basis(Vector3.UP, -r.quarter * PI / 2) var points := PackedVector3Array() for p in lib.catalog[r.kind].lowerPaths[0]: points.append(lib.record_center(r) + basis * lib.vector(p)) draw_path(points, BLUE) checkpoints.append({"id": id, "point": points[-1], "direction": (points[-1] - points[-2]).normalized()}) draw_path(PackedVector3Array([Vector3(-1.5,1.025,1), Vector3(1.5,1.025,1)]), ORANGE) apply_view() func draw_path(points: PackedVector3Array, color: Color) -> void: var mesh := ImmediateMesh.new(); mesh.surface_begin(Mesh.PRIMITIVE_LINE_STRIP) for point in points: mesh.surface_add_vertex(point) mesh.surface_end() var view := MeshInstance3D.new(); view.mesh = mesh var mat := material(color); mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED view.material_override = mat; guides.add_child(view) func toggle_xray() -> void: xray = not xray; apply_view() view_button.text = "观察方式:透视 → 实体" if xray else "观察方式:实体 → 透视" func apply_view() -> void: for piece in pieces: for node in piece.find_children("*", "MeshInstance3D", true, false): if str(node.name).begins_with("ink_"): node.visible = not xray; continue if xray: var mat := material(Color(.68,.75,.72,.19)) mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA node.material_override = mat else: node.material_override = null guides.visible = xray func reset_simulation() -> void: super.reset_simulation() passed_checkpoints.clear(); route_contacts.clear(); ball_contacts = 0; lower_trace.clear(); stalled_time = 0 for i in range(tags.size()): tags[i].modulate = BLUE if i == 0 else ORANGE refresh_ui() func initial_velocity(index: int) -> Vector3: return lib.vector(spawn_specs[index].launchDirection).normalized() * launch_speed func _physics_process(delta: float) -> void: super._physics_process(delta) if not started or paused or balls.is_empty(): return var b = balls[0] if not entered.has(0) and not lost.has(0): if Engine.get_physics_frames() % 6 == 0: lower_trace.append({"time":elapsed,"position":codec.array_from(b.position),"speed":b.linear_velocity.length()}) ball_contacts += b.ball_contact_count for collider in b.get_colliding_bodies(): var node: Node = collider while node != null and not node.has_meta("record_id"): node = node.get_parent() if node != null: route_contacts[str(node.get_meta("record_id"))] = true if passed_checkpoints.size() < checkpoints.size(): var gate: Dictionary = checkpoints[passed_checkpoints.size()] var offset: Vector3 = b.position - gate.point var axial: float = offset.dot(gate.direction) if axial > .015 and (offset - axial * gate.direction).length() < .09: passed_checkpoints.append({"id":gate.id,"time":elapsed}) stalled_time = stalled_time + delta if b.linear_velocity.length() < .03 else 0.0 if stalled_time > 2: status = "下层球已停下。当前动能不足以走完整条路线。" refresh_ui() func refresh_ui() -> void: if status_label == null: return super.refresh_ui() speed_slider.editable = not started route_label.text = "下层出口检查 %d / 7\n上下球接触 %d" % [passed_checkpoints.size(), ball_contacts] func _unhandled_input(event: InputEvent) -> void: if event is InputEventKey and event.pressed and not event.echo and event.keycode == KEY_X: toggle_xray(); return super._unhandled_input(event) func _process(delta: float) -> void: if snapshot_time >= 0 and elapsed >= snapshot_time: snapshot_time = -1; capture_count = 2 super._process(delta)