first flow field resource draft

This commit is contained in:
2025-09-10 21:55:51 +10:00
parent c0859e26cb
commit 703e75d1d8
13 changed files with 208 additions and 24 deletions

View File

@@ -3,12 +3,16 @@ extends Node3D
signal path_updated()
@export var data_file: FlowFieldData
@export var flow_node_scene: PackedScene
@export var nodes: Array[FlowNode] = []
@export var goals: Array[FlowNode] = []
@export var starts: Array[FlowNode] = []
@export var start_points: Array[Node3D]
@export var goal_points: Array[Node3D]
@export var nodes_visible: bool = false
var nodes: Array[FlowNode] = []
var start_nodes: Array[FlowNode] = []
var goal_nodes: Array[FlowNode] = []
func _ready() -> void:
if !nodes_visible:
@@ -16,6 +20,27 @@ func _ready() -> void:
node.visible = false
func load_from_data(data: FlowFieldData = data_file) -> void:
for node: FlowNode in nodes:
delete_node(node)
nodes = []
start_nodes = []
goal_nodes = []
var dict: Dictionary[FlowNodeData, FlowNode] = {}
for node_data: FlowNodeData in data_file.nodes:
var new_flow_node: FlowNode = create_node(node_data.position)
new_flow_node.buildable = node_data.buildable
dict[node_data] = new_flow_node
nodes.append(new_flow_node)
if node_data.type == FlowNodeData.NodeType.START:
start_nodes.append(new_flow_node)
elif node_data.type == FlowNodeData.NodeType.GOAL:
goal_nodes.append(new_flow_node)
for node_data: FlowNodeData in dict.keys():
for neighbor: FlowNodeData in node_data.connected_nodes:
dict[node_data].add_connection(dict[neighbor])
@warning_ignore("unused_parameter")
func _process(delta: float) -> void:
if !nodes_visible:
@@ -27,9 +52,9 @@ func _process(delta: float) -> void:
node.set_color(Color.CORAL)
else:
node.set_color(Color.BLACK)
if goals.has(node):
if goal_nodes.has(node):
node.set_color(Color.BLUE)
if starts.has(node):
if start_nodes.has(node):
node.set_color(Color.PINK)
if magic_node:
magic_node.set_color(Color.DEEP_PINK)
@@ -66,7 +91,7 @@ func get_closest_buildable_point(pos: Vector3) -> FlowNode:
func test_traversability() -> bool:
for node: FlowNode in starts:
for node: FlowNode in start_nodes:
while node.best_path != null:
if node.best_path.traversable:
node = node.best_path
@@ -88,7 +113,7 @@ func iterate_search(search_frontier: Array[FlowNode], reached: Array[FlowNode])
func calculate() -> void:
var reached: Array[FlowNode] = []
var search_frontier: Array[FlowNode] = []
for node: FlowNode in goals:
for node: FlowNode in goal_nodes:
node.best_path = null
reached.append(node)
search_frontier.append(node)
@@ -135,18 +160,18 @@ func connect_many_nodes(common_node: FlowNode, child_nodes: Array[FlowNode]) ->
func toggle_goal(nodes_to_toggle: Array[FlowNode]) -> void:
for node: FlowNode in nodes_to_toggle:
if goals.has(node):
goals.erase(node)
if goal_nodes.has(node):
goal_nodes.erase(node)
else:
goals.append(node)
goal_nodes.append(node)
func toggle_start(nodes_to_toggle: Array[FlowNode]) -> void:
for node: FlowNode in nodes_to_toggle:
if starts.has(node):
starts.erase(node)
if start_nodes.has(node):
start_nodes.erase(node)
else:
starts.append(node)
start_nodes.append(node)
func toggle_traversable(node: FlowNode) -> bool:

View File

@@ -6,7 +6,8 @@
ambient_light_source = 2
ambient_light_color = Color(0.728822, 0.728822, 0.728822, 1)
[node name="FlowFieldTool" type="Node" unique_id=730814717 node_paths=PackedStringArray("raycast", "project_raycast", "camera", "camera_pivot", "position_field", "x_field", "y_field", "z_field", "x_size_field", "y_size_field", "gap_field")]
[node name="FlowFieldTool" type="Node" node_paths=PackedStringArray("raycast", "project_raycast", "camera", "camera_pivot", "position_field", "x_field", "y_field", "z_field", "x_size_field", "y_size_field", "gap_field", "save_path")]
script = ExtResource("1_e7pmn")
raycast = NodePath("CameraFocus/Camera3D/RayCast3D")
project_raycast = NodePath("RayCast3D")
@@ -19,6 +20,7 @@ z_field = NodePath("Position/z")
x_size_field = NodePath("VBoxContainer/HBoxContainer/x_size")
y_size_field = NodePath("VBoxContainer/HBoxContainer/y_size")
gap_field = NodePath("VBoxContainer/HBoxContainer/gap")
save_path = NodePath("VBoxContainer/FileNameInput")
metadata/_custom_type_script = "uid://05c5q1v2nv8p"
[node name="VBoxContainer" type="VBoxContainer" parent="." unique_id=1159336300]
@@ -87,9 +89,27 @@ text = "Extrude"
layout_mode = 2
text = "Calculate"
<<<<<<< HEAD
[node name="Finalize" type="Button" parent="VBoxContainer" unique_id=1604655281]
=======
[node name="FileNameInput" type="LineEdit" parent="VBoxContainer"]
>>>>>>> 48b1add (first flow field resource draft)
layout_mode = 2
text = "Finalize"
placeholder_text = "level title"
alignment = 1
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
[node name="Save" type="Button" parent="VBoxContainer/HBoxContainer2"]
layout_mode = 2
size_flags_horizontal = 3
text = "Save"
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer2"]
layout_mode = 2
size_flags_horizontal = 3
text = "Load"
[node name="Position" type="HBoxContainer" parent="." unique_id=1194117729]
visible = false
@@ -134,7 +154,8 @@ collision_mask = 64
[connection signal="pressed" from="VBoxContainer/ToggleBuildable" to="." method="_on_toggle_buildable_button_pressed"]
[connection signal="pressed" from="VBoxContainer/Extrude" to="." method="_on_extrude_button_pressed"]
[connection signal="pressed" from="VBoxContainer/Calculate" to="." method="_on_calculate_button_pressed"]
[connection signal="pressed" from="VBoxContainer/Finalize" to="." method="_on_finalize_button_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Save" to="." method="_on_save_button_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Load" to="." method="_on_load_button_pressed"]
[connection signal="text_changed" from="Position/x" to="." method="_on_x_field_changed"]
[connection signal="text_changed" from="Position/y" to="." method="_on_y_field_changed"]
[connection signal="text_changed" from="Position/z" to="." method="_on_z_field_changed"]