|
| 1 | +extends Node2D |
| 2 | + |
| 3 | +@export var map: Map |
| 4 | + |
| 5 | +@onready var font:Font = ThemeDB.fallback_font |
| 6 | + |
| 7 | +const BASE_LINE_WIDTH = 3.0 |
| 8 | +const DRAW_COLOR:Color = Color.WHITE |
| 9 | +const OFFSET_POSITIONS = Vector2(10,30) |
| 10 | +const OFFSET_WEIGHT = Vector2(10,-10) |
| 11 | + |
| 12 | +var _debug_connections = false |
| 13 | +var _debug_position = false |
| 14 | +var _debug_weights = false |
| 15 | +var _debug_costs = false |
| 16 | +var _debug_path = true |
| 17 | + |
| 18 | + |
| 19 | +func _process(delta): |
| 20 | + queue_redraw() |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +func draw_arrow(src, dst, color, width, aa = true): |
| 25 | + var angle = 0.6 |
| 26 | + var size_head = 20 |
| 27 | + var head : Vector2 = (dst - src).normalized() * size_head |
| 28 | + draw_line(src, dst-head/2, color, width, aa) |
| 29 | + draw_polygon([dst, dst - head.rotated(angle), dst - head.rotated(-angle) ], [color,color,color ]) |
| 30 | + |
| 31 | + |
| 32 | +func _draw(): |
| 33 | + if _debug_connections : |
| 34 | + _draw_connections() |
| 35 | + if _debug_position : |
| 36 | + _draw_positions() |
| 37 | + if _debug_weights : |
| 38 | + _draw_weights() |
| 39 | + if _debug_costs : |
| 40 | + _draw_costs() |
| 41 | + if _debug_path : |
| 42 | + _draw_path() |
| 43 | + |
| 44 | + |
| 45 | +func _draw_path(): |
| 46 | + if not map._point_path: |
| 47 | + return |
| 48 | + var point_start: Vector2i = map._point_path[0] |
| 49 | + var point_end: Vector2i = map._point_path[len(map._point_path) - 1] |
| 50 | + |
| 51 | + var last_point = point_start |
| 52 | + for index in range(1, len(map._point_path)): |
| 53 | + var current_point = map._point_path[index] |
| 54 | + draw_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true) |
| 55 | + draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR) |
| 56 | + last_point = current_point |
| 57 | + |
| 58 | + |
| 59 | +func _draw_weights(): |
| 60 | + for id in map.astar_node.get_point_ids(): |
| 61 | + var position_weight = map.astar_node.get_point_position(id) |
| 62 | + var cost = map.astar_node.get_point_weight_scale(id) |
| 63 | + draw_string( |
| 64 | + font, |
| 65 | + position_weight + OFFSET_WEIGHT, |
| 66 | + str(cost), |
| 67 | + HORIZONTAL_ALIGNMENT_FILL, |
| 68 | + -1, |
| 69 | + 16, |
| 70 | + Color.RED |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +func _draw_positions(): |
| 75 | + for id in map.astar_node.get_point_ids(): |
| 76 | + var position_label = map.astar_node.get_point_position(id) |
| 77 | + var position_map = map.local_to_map(map.to_local(map.astar_node.get_point_position(id))) |
| 78 | + draw_string( |
| 79 | + font, |
| 80 | + position_label + OFFSET_POSITIONS, |
| 81 | + str(position_map), |
| 82 | + HORIZONTAL_ALIGNMENT_FILL, |
| 83 | + -1, |
| 84 | + 16, |
| 85 | + Color.RED |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +func _draw_connections(): |
| 90 | + for id in map.astar_node.get_point_ids(): |
| 91 | + for id_con in map.astar_node.get_point_connections(id): |
| 92 | + var position_start = map.astar_node.get_point_position(id) |
| 93 | + var position_end = map.astar_node.get_point_position(id_con) |
| 94 | + var direction = (position_end - position_start) |
| 95 | + draw_arrow(position_start, position_end - direction/4.0, Color(0.0, 1.0, 1.0, 1.0), BASE_LINE_WIDTH*2, true) |
| 96 | + |
| 97 | + |
| 98 | +func _draw_costs(): |
| 99 | + for id in map.astar_node.get_point_ids(): |
| 100 | + for id_con in map.astar_node.get_point_connections(id): |
| 101 | + var position_cost_start = map.astar_node.get_point_position(id) |
| 102 | + var position_cost_end = map.astar_node.get_point_position(id_con) |
| 103 | + var cost = map.astar_node._compute_cost(id, id_con) |
| 104 | + draw_string( |
| 105 | + font, |
| 106 | + (position_cost_start+position_cost_end)/2.0, |
| 107 | + str("%.2f"%cost), |
| 108 | + HORIZONTAL_ALIGNMENT_CENTER, |
| 109 | + -1, |
| 110 | + 16, |
| 111 | + Color.PINK |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +func _on_d_path_toggled(toggled_on): |
| 116 | + _debug_path = toggled_on |
| 117 | + |
| 118 | + |
| 119 | +func _on_d_costs_toggled(toggled_on): |
| 120 | + _debug_costs = toggled_on |
| 121 | + |
| 122 | + |
| 123 | +func _on_d_positions_toggled(toggled_on): |
| 124 | + _debug_position = toggled_on |
| 125 | + |
| 126 | + |
| 127 | +func _on_d_connections_toggled(toggled_on): |
| 128 | + _debug_connections = toggled_on |
| 129 | + |
| 130 | + |
| 131 | +func _on_d_weights_toggled(toggled_on): |
| 132 | + _debug_weights = toggled_on |
| 133 | + |
0 commit comments