130 lines
		
	
	
	
		
			4 KiB
		
	
	
	
		
			GDScript
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
	
		
			4 KiB
		
	
	
	
		
			GDScript
		
	
	
	
	
	
| # The global hub for all the stats.
 | |
| extends CanvasLayer
 | |
| 
 | |
| @onready var control_root = $Root
 | |
| @onready var categories = $Root/Stats/Categories
 | |
| @onready var logs = $Root/LogScroll/Log
 | |
| @onready var scroll_bar = $Root/LogScroll.get_v_scroll_bar()
 | |
| @onready var log_title = $Root/LogTitle
 | |
| 
 | |
| var show_gizmos = false
 | |
| 
 | |
| func _ready():
 | |
| 	control_root.hide()
 | |
| 	scroll_bar.changed.connect(_on_scroll_bar_update)
 | |
| 
 | |
| func _on_scroll_bar_update():
 | |
| 	scroll_bar.value = scroll_bar.max_value
 | |
| 
 | |
| func put_log(value: Dictionary):
 | |
| 	var new_label = Label.new()
 | |
| 	#new_label.bbcode_enabled = true
 | |
| 	#new_label.fit_content = true
 | |
| 	new_label.text = value["description"] + " | " + value["text"]
 | |
| 	logs.add_child(new_label)
 | |
| 	new_label.use_parent_material = true
 | |
| 	log_title.text = "Log +"
 | |
| 	get_tree().create_timer(0.1).connect("timeout", _on_log_update_timeout)
 | |
| 
 | |
| # Called to reset the log's "udpated" indicator
 | |
| func _on_log_update_timeout():
 | |
| 	log_title.text = "Log"
 | |
| 
 | |
| func maybe_put_log(value, text: String):
 | |
| 	if value != null:
 | |
| 		value["text"] = text
 | |
| 		put_log(value)
 | |
| 
 | |
| # The key and string is shown like this 'key: value'
 | |
| func stat_set(stat: Dictionary):
 | |
| 	# Check if label exists
 | |
| 	var found_key = false
 | |
| 	for child in categories.get_children():
 | |
| 		if child.name == stat["key"]:
 | |
| 			set_text(child, stat)
 | |
| 			found_key = true
 | |
| 	if not found_key:
 | |
| 		var new_label = RichTextLabel.new()
 | |
| 		new_label.name = stat["key"]
 | |
| 		categories.add_child(new_label)
 | |
| 		new_label.use_parent_material = true
 | |
| 		set_text(new_label, stat)
 | |
| 
 | |
| func set_text(node, stat):
 | |
| 	var key = ""
 | |
| 	var body = ""
 | |
| 	var end = ""
 | |
| 	# Make sure key exists
 | |
| 	if stat.has("key"):
 | |
| 		key = stat["key"]
 | |
| 	else:
 | |
| 		printerr("dictionary must have key")
 | |
| 	# Optional body
 | |
| 	if stat.has("value"):
 | |
| 		body = stat["value"]
 | |
| 	# Immediate indicators
 | |
| 	if stat.has("indicate"):
 | |
| 		end = "[color=orange]*[/color] "
 | |
| 	node.bbcode_enabled = true
 | |
| 	node.fit_content = true
 | |
| 	node.text = key + ": " + body + " " + end
 | |
| 	if stat.has("indicate"):
 | |
| 		await get_tree().create_timer(0.5).timeout
 | |
| 		node.text = key + ": " + body
 | |
| 
 | |
| # Utility function for your leasure
 | |
| func maybe_put_stat(stat_key, value: String):
 | |
| 	if stat_key != null:
 | |
| 		stat_key["value"] = value
 | |
| 		stat_set(stat_key)
 | |
| 
 | |
| func _process(_delta):
 | |
| 	if Input.is_action_just_pressed("show_stats"):
 | |
| 		control_root.visible = !control_root.visible
 | |
| 	if Input.is_action_just_pressed("debug_show_gizmos"):
 | |
| 		show_gizmos = !show_gizmos
 | |
| 	if Input.is_action_just_pressed("debug_respawn_enemies"):
 | |
| 		for node in get_tree().get_nodes_in_group("debug_enemy_respawn"):
 | |
| 			node.position = node.get_node("EnemyDebug").original_position
 | |
| 	# Add fps stat
 | |
| 	stat_set({ "key": "fps", "value": str(Engine.get_frames_per_second()) })
 | |
| 	# Clear logs on input
 | |
| 	if Input.is_action_just_pressed("debug_clear_log"):
 | |
| 		clear_log()
 | |
| 	
 | |
| 	# Debug properties of things in the world
 | |
| 	process_debug_property("debug_state", func(): return Label.new(), func(item): 
 | |
| 		item.text = item.get_parent().get_node("StateMachine").state.name
 | |
| 	)
 | |
| 	process_debug_property("debug_trail",
 | |
| 		func(): return preload("res://debug/Trail.tscn").instantiate(), func(_item): )
 | |
| 	
 | |
| 	# Respawn player
 | |
| 	if Input.is_action_just_pressed("debug_respawn"):
 | |
| 		var old_player = get_tree().get_first_node_in_group("player")
 | |
| 		old_player.queue_free()
 | |
| 		await old_player.tree_exited
 | |
| 		var p = preload("res://player/Player.tscn")
 | |
| 		var new_player = p.instantiate()
 | |
| 		var respawn_point_node = get_tree().get_first_node_in_group("debug_player_respawn")
 | |
| 		if respawn_point_node:
 | |
| 			new_player.position = respawn_point_node.position
 | |
| 		get_node("/root").add_child(new_player)
 | |
| 
 | |
| func process_debug_property(debug_name, create_func, process_func):
 | |
| 	for node in get_tree().get_nodes_in_group(debug_name):
 | |
| 		var item
 | |
| 		if !node.has_node(debug_name):
 | |
| 			item = create_func.call()
 | |
| 			item.name = debug_name
 | |
| 			node.add_child(item)
 | |
| 		else:
 | |
| 			item = node.get_node(debug_name)
 | |
| 		item.visible = show_gizmos
 | |
| 		item.process_mode = PROCESS_MODE_INHERIT if show_gizmos else PROCESS_MODE_DISABLED
 | |
| 		process_func.call(item)
 | |
| 		return item
 | |
| 
 | |
| func clear_log() -> void:
 | |
| 	for child in logs.get_children():
 | |
| 		logs.remove_child(child)
 |