Added a navigation agent demo.

This commit is contained in:
Relintai 2023-06-13 18:34:37 +02:00
parent cf6e7ce207
commit 13c7cbd279
6 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,58 @@
extends Sprite
export(float) var character_speed = 400.0
var path = []
var agent : NavigationAgent2D
func _ready() -> void:
agent = get_node("NavigationAgent2D")
agent.max_speed = 10
agent.connect("link_reached", self, "link_reached")
agent.connect("navigation_finished", self, "navigation_finished")
agent.connect("path_changed", self, "path_changed")
agent.connect("target_reached", self, "target_reached")
#agent.connect("velocity_computed", self, "velocity_computed")
agent.connect("waypoint_reached", self, "waypoint_reached")
func _physics_process(delta: float) -> void:
var walk_distance = character_speed * delta
position = agent.get_next_position()
#print(agent.get_nav_path())
#if agent.is_navigation_finished():
# set_physics_process(false)
func _unhandled_input(event):
if not event.is_action_pressed("click"):
return
agent.target_position = get_local_mouse_position()
print(agent.target_position)
set_physics_process(true)
#print(agent.get_nav_path())
func link_reached(details: Dictionary):
print("link_reached")
print(details)
func navigation_finished():
print("navigation_finished")
func path_changed():
print("path_changed")
func target_reached():
print("target_reached")
func velocity_computed(safe_velocity: Vector2):
print("velocity_computed")
print(safe_velocity)
func waypoint_reached(details: Dictionary):
print("waypoint_reached")
print(details)

View File

@ -0,0 +1,76 @@
[gd_scene load_steps=7 format=2]
[ext_resource path="res://icon.png" type="Texture" id=2]
[ext_resource path="res://Character.gd" type="Script" id=3]
[sub_resource type="NavigationPolygon" id=1]
vertices = PoolVector2Array( 194, -109, 123, 99, -49, 219, -142, -40, -83, -133 )
polygons = [ PoolIntArray( 0, 1, 2, 3, 4 ) ]
outlines = [ PoolVector2Array( -83, -133, 194, -109, 123, 99, -49, 219, -142, -40 ) ]
baked_outlines = [ PoolVector2Array( 194, -109, 123, 99, -49, 219, -142, -40, -83, -133 ) ]
[sub_resource type="NavigationPolygon" id=2]
vertices = PoolVector2Array( 78, 44, 97, 249, -146, 41, -110, -85, 169, -149 )
polygons = [ PoolIntArray( 0, 1, 2, 3 ), PoolIntArray( 3, 4, 0 ) ]
outlines = [ PoolVector2Array( -110, -85, 169, -149, 78, 44, 97, 249, -146, 41 ) ]
baked_outlines = [ PoolVector2Array( 78, 44, 97, 249, -146, 41, -110, -85, 169, -149 ) ]
[sub_resource type="NavigationPolygon" id=3]
vertices = PoolVector2Array( 75, -11, -65, 113, -147, 69, -142, -44, 50, -72 )
polygons = [ PoolIntArray( 0, 1, 2, 3, 4 ) ]
outlines = [ PoolVector2Array( -65, 113, 75, -11, 50, -72, -142, -44, -147, 69 ) ]
baked_outlines = [ PoolVector2Array( 75, -11, -65, 113, -147, 69, -142, -44, 50, -72 ) ]
[sub_resource type="NavigationPolygon" id=4]
vertices = PoolVector2Array( 205, -11, 71, 128, -178, 79, -88, -97 )
polygons = [ PoolIntArray( 0, 1, 2, 3 ) ]
outlines = [ PoolVector2Array( -88, -97, 205, -11, 71, 128, -178, 79 ) ]
baked_outlines = [ PoolVector2Array( 205, -11, 71, 128, -178, 79, -88, -97 ) ]
[node name="Node2D" type="Node2D"]
[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="."]
navpoly = SubResource( 1 )
[node name="NavigationLink2D" type="NavigationLink2D" parent="."]
position = Vector2( 188, 27 )
start_position = Vector2( -100, 0 )
end_position = Vector2( 200, 0 )
[node name="NavigationPolygonInstance2" type="NavigationPolygonInstance" parent="."]
position = Vector2( 432, 65 )
navpoly = SubResource( 2 )
[node name="NavigationLink2D2" type="NavigationLink2D" parent="."]
position = Vector2( 452, 325 )
start_position = Vector2( 0, -100 )
end_position = Vector2( 0, 100 )
[node name="NavigationPolygonInstance3" type="NavigationPolygonInstance" parent="."]
position = Vector2( 106, 271 )
navpoly = SubResource( 3 )
[node name="NavigationLink2D3" type="NavigationLink2D" parent="."]
position = Vector2( 136, 371 )
start_position = Vector2( 100, 0 )
end_position = Vector2( -100, 0 )
[node name="NavigationPolygonInstance4" type="NavigationPolygonInstance" parent="."]
position = Vector2( 309, 434 )
navpoly = SubResource( 4 )
[node name="NavigationLink2D4" type="NavigationLink2D" parent="."]
position = Vector2( 57, 184 )
start_position = Vector2( 0, -100 )
end_position = Vector2( 0, 100 )
[node name="Character" type="Sprite" parent="."]
position = Vector2( 64, -52 )
texture = ExtResource( 2 )
script = ExtResource( 3 )
[node name="NavigationAgent2D" type="NavigationAgent2D" parent="Character"]
[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2( 298, 216 )
zoom = Vector2( 1.752, 1.752 )

View File

@ -0,0 +1,7 @@
[gd_resource type="Environment3D" load_steps=2 format=2]
[sub_resource type="ProceduralSky" id=1]
[resource]
background_mode = 2
background_sky = SubResource( 1 )

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View File

@ -0,0 +1,33 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=4
[application]
config/name="Navigation Agents"
run/main_scene="res://Node2D.tscn"
config/icon="res://icon.png"
[input]
click={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
]
}
[physics]
common/enable_pause_aware_picking=true
[rendering]
vram_compression/import_etc=true
vram_compression/import_etc2=false
environment/default_environment="res://default_env.tres"