OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / gate_terrain_material_panel.gd

62 lines · gd ref:
extends Node
## GATE — the Terrain material panel IN THE GAME'S CONTEXT (BuildDock + GameState
## autoloads, which a --script run does not have): map_geiss_world registers the panel,
## a change through it reaches the shader and is SAVED to the room's durable key after
## the debounce, a remote value is applied, and leaving the map removes the panel.
##
## ⚠ WINDOWED:  env -u CI HYPERTPS_DAEMON_TCP=127.0.0.1:1 DISPLAY=:0 "$HYPER_TPS_GODOT" \
##       --path . res://scenes/test/gate_terrain_material_panel.tscn

const KEY := "world:terrain:material"
var _fails := 0
var _checks := 0


func _p(c: bool, m: String, got: Variant = "") -> void:
	_checks += 1
	print(("ok   " if c else "FAIL: ") + m + ("" if str(got) == "" else "  (" + str(got) + ")"))
	if not c:
		_fails += 1


func _ready() -> void:
	_run.call_deferred()


func _run() -> void:
	OS.set_environment("GEISS_CAM", "0,35,30,0,-45")
	var map: Node = (load("res://scenes/maps/map_geiss_world.tscn") as PackedScene).instantiate()
	get_tree().root.add_child(map)
	for i in 10:
		await get_tree().process_frame
	var dock := get_node_or_null(^"/root/BuildDock")
	var id := "terrain_material_%s" % map.name
	_p(dock != null and dock.get("_panels") is Dictionary and (dock.get("_panels") as Dictionary).has(id),
		"P1 the map registered '%s' in the BuildDock" % id)
	var tm: TerrainMaterial = map.get("_tm")
	_p(tm != null, "P1 the map's look is a TerrainMaterial")
	if tm == null:
		return _end()
	var mat: ShaderMaterial = tm.mat
	tm.set_value(tm.cfg["layers"][3], "rot", 33.0)
	_p(is_equal_approx((mat.get_shader_parameter(&"uv_rot_deg") as PackedFloat32Array)[3], 33.0), "P2 the change reaches the shader at once")
	await get_tree().create_timer(0.8).timeout
	var saved: Variant = GameState.get_state(KEY)
	_p(saved is Dictionary and is_equal_approx(float(saved["layers"][3]["rot"]), 33.0),
		"P3 after the debounce it is saved under %s" % KEY, str(saved).substr(0, 80) if saved != null else "null")
	# A remote value (another peer) is applied.
	var remote: Dictionary = tm.cfg.duplicate(true)
	remote["normal"] = 0.3
	GameState.state_changed.emit(KEY, remote, 0)
	_p(is_equal_approx(float(mat.get_shader_parameter(&"normal_strength")), 0.3), "P4 a remote value is applied")
	get_tree().root.remove_child(map)
	map.free()
	await get_tree().process_frame
	_p(not (dock.get("_panels") as Dictionary).has(id), "P5 leaving the map removes the panel")
	_end()


func _end() -> void:
	print("GATE terrain_material_panel: %s (%d checks, %d failed)" % ["PASS" if _fails == 0 else "FAIL", _checks, _fails])
	get_tree().quit(0 if _fails == 0 else 1)