From c3301e15dbe5006aa36b8d9ac35d54f972b4c7f1 Mon Sep 17 00:00:00 2001 From: karl Date: Wed, 30 Oct 2019 00:45:37 +0100 Subject: [PATCH] Add NodeGroupNotifier to generalize PathNavigator The NodeGroupNotifier notifies nodes within a specific group of the existence of a specific Node. With this new node, the Navigation does not need to be specifically assigned to the PathNavigatorForKinematicBody, making it easier to use in different scenarios. --- .../Util/PathNavigatorForKinematicBody.gd | 28 ++++++++++++++--- .../Util/PathNavigatorForKinematicBody.tscn | 9 +++--- Level/PathTestWorld.tscn | 7 ++++- Util/NodeGroupNotifier.gd | 31 +++++++++++++++++++ Util/NodeGroupNotifier.tscn | 6 ++++ default_env.tres | 2 ++ 6 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 Util/NodeGroupNotifier.gd create mode 100644 Util/NodeGroupNotifier.tscn diff --git a/Characters/Util/PathNavigatorForKinematicBody.gd b/Characters/Util/PathNavigatorForKinematicBody.gd index 87cbccf..fe2d39b 100644 --- a/Characters/Util/PathNavigatorForKinematicBody.gd +++ b/Characters/Util/PathNavigatorForKinematicBody.gd @@ -2,21 +2,35 @@ extends Path var _current_nav_path: PoolVector3Array -var _current_nav_index = 0 -var _current_path_index = 1 +var _current_nav_index +var _current_path_index var _arrived_distance_threshold = 0.1 export(float) var speed = 8 -export(NodePath) var navigation_nodepath export(NodePath) var body_nodepath var navigation: Navigation var body: KinematicBody +# React to the NodeGroupNotifier of the Navigation +func set_navigator_node(navigation_node: Navigation): + navigation = navigation_node as Navigation + + # Our path could change significantly with the Navigation node, so restart + _restart_navigation() + + func _ready(): - navigation = get_node(navigation_nodepath) as Navigation + _restart_navigation() + + +# Reset the body to the starting position and start the navigation freshly +func _restart_navigation(): + _current_nav_index = 0 + _current_path_index = 1 + body = get_node(body_nodepath) as KinematicBody # Initialize the position @@ -67,7 +81,11 @@ func _get_new_navigation(): _current_nav_index = 0 var goal = curve.get_point_position(_current_path_index) - _current_nav_path = navigation.get_simple_path(_get_body_position(), goal) + + if navigation: + _current_nav_path = navigation.get_simple_path(_get_body_position(), goal) + else: + _current_nav_path = PoolVector3Array([goal]) # Return the current position of the body we're controlling diff --git a/Characters/Util/PathNavigatorForKinematicBody.tscn b/Characters/Util/PathNavigatorForKinematicBody.tscn index 8f9b2e0..13021f2 100644 --- a/Characters/Util/PathNavigatorForKinematicBody.tscn +++ b/Characters/Util/PathNavigatorForKinematicBody.tscn @@ -2,14 +2,15 @@ [ext_resource path="res://Characters/Util/PathNavigatorForKinematicBody.gd" type="Script" id=1] -[sub_resource type="Curve3D" id=4] +[sub_resource type="Curve3D" id=1] _data = { "points": PoolVector3Array( 0, 0, 0, 0, 0, 0, -7.92509, 2.86102e-05, -6.86376, 0, 0, 0, 0, 0, 0, 0.703341, 0.398803, 1.99739, 0, 0, 0, 0, 0, 0, 9.39952, 0.263628, 7.15156, 0, 0, 0, 0, 0, 0, -4.38803, 1.73835, 6.52798 ), "tilts": PoolRealArray( 0, 0, 0, 0 ) } -[node name="PathNavigatorForKinematicBody" type="Path"] -curve = SubResource( 4 ) +[node name="PathNavigatorForKinematicBody" type="Path" groups=[ +"Navigator", +]] +curve = SubResource( 1 ) script = ExtResource( 1 ) -navigation_nodepath = NodePath("..") body_nodepath = NodePath("../KinematicBody") diff --git a/Level/PathTestWorld.tscn b/Level/PathTestWorld.tscn index 7fe2d4d..62ff79d 100644 --- a/Level/PathTestWorld.tscn +++ b/Level/PathTestWorld.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://Characters/Util/PathNavigatorForKinematicBody.tscn" type="PackedScene" id=1] [ext_resource path="res://Characters/Meldewesen/Meldewesen.tscn" type="PackedScene" id=2] +[ext_resource path="res://Util/NodeGroupNotifier.tscn" type="PackedScene" id=3] [sub_resource type="NavigationMesh" id=1] vertices = PoolVector3Array( -1.3, 0.4, -1.6, 0.200001, 0.4, -1.6, 0.200001, 0.4, -9.4, -9.4, 0.4, -0.0999994, -1.6, 0.4, -0.0999994, -1.3, 0.4, -1.6, 0.200001, 0.4, -9.4, -9.4, 0.4, -9.4, 0.200001, 0.4, -9.4, 0.200001, 0.4, -1.6, 1.4, 0.4, -1.6, 1.4, 0.4, -1.6, 1.7, 0.4, -0.0999994, 9.5, 0.4, -0.0999994, 9.5, 0.4, -9.4, 0.200001, 0.4, -9.4, 0.200001, 0.4, 9.5, 0.200001, 0.4, 8.3, -1.6, 0.4, 8, -9.4, 0.4, 9.5, -1.6, 0.4, 5, 0.200001, 0.4, 4.7, 0.200001, 0.4, 1.7, -1.6, 0.4, 1.4, -1.6, 0.4, 5, -1.6, 0.4, 1.4, -9.4, 0.4, -0.0999994, -9.4, 0.4, 9.5, -1.6, 0.4, 1.4, -1.6, 0.4, -0.0999994, -9.4, 0.4, -0.0999994, -9.4, 0.4, 9.5, -1.6, 0.4, 8, -1.6, 0.4, 5, 1.7, 0.4, 8, 0.200001, 0.4, 8.3, 0.200001, 0.4, 9.5, 9.5, 0.4, 9.5, 0.200001, 0.4, 1.7, 0.200001, 0.4, 4.7, 1.7, 0.4, 5, 1.7, 0.4, 1.4, 1.7, 0.4, 5, 9.5, 0.4, 9.5, 9.5, 0.4, -0.0999994, 1.7, 0.4, 1.4, 1.7, 0.4, 5, 1.7, 0.4, 8, 9.5, 0.4, 9.5, 9.5, 0.4, -0.0999994, 1.7, 0.4, -0.0999994, 1.7, 0.4, 1.4 ) @@ -41,3 +42,7 @@ material/0 = null body_nodepath = NodePath("Meldewesen") [node name="Meldewesen" parent="PathNavigatorForKinematicBody" instance=ExtResource( 2 )] + +[node name="NodeGroupNotifier" parent="." instance=ExtResource( 3 )] +group_name = "Navigator" +node_to_send = NodePath("..") diff --git a/Util/NodeGroupNotifier.gd b/Util/NodeGroupNotifier.gd new file mode 100644 index 0000000..5cf5062 --- /dev/null +++ b/Util/NodeGroupNotifier.gd @@ -0,0 +1,31 @@ +tool +extends Node + + +# +# Notifies a specific group of the existence of a node which can be assigned to this one. +# The nodes in the group are notified via the function "set_groupname_node", e.g. "set_notifier_node", +# which has the Node as an argument. +# + + +export(String) var group_name: String +export(NodePath) var node_to_send: NodePath + + +func _ready(): + var function_name = "set_%s_node" % [group_name.to_lower()] + + print("Calling %s" % [function_name]) + get_tree().call_group(group_name, function_name, get_node(node_to_send)) + + +# Display a warning in the editor if the group or node is invalid +func _get_configuration_warning(): + if !get_tree().has_group(group_name): + return "Group does not exist!" + + if !node_to_send: + return "A node to send needs to be assigned!" + + return "" diff --git a/Util/NodeGroupNotifier.tscn b/Util/NodeGroupNotifier.tscn new file mode 100644 index 0000000..6011b97 --- /dev/null +++ b/Util/NodeGroupNotifier.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Util/NodeGroupNotifier.gd" type="Script" id=1] + +[node name="NodeGroupNotifier" type="Node"] +script = ExtResource( 1 ) diff --git a/default_env.tres b/default_env.tres index 98f26a7..20207a4 100644 --- a/default_env.tres +++ b/default_env.tres @@ -1,5 +1,7 @@ [gd_resource type="Environment" load_steps=2 format=2] + [sub_resource type="ProceduralSky" id=1] + [resource] background_mode = 2 background_sky = SubResource( 1 )