diff --git a/2d/navigation/character.gd b/2d/navigation/character.gd new file mode 100644 index 00000000..301978c4 --- /dev/null +++ b/2d/navigation/character.gd @@ -0,0 +1,33 @@ +extends Sprite + +export(float) var character_speed = 400.0 + +onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D +onready var navigation_path_debug_line: Line2D = $Line2D + + +func _ready(): + navigation_path_debug_line.set_as_toplevel(true) + + +func set_target_location(target_location: Vector2): + navigation_agent.set_target_location(target_location) + # Alternative way to get a full navigation path with Server API. + var navigation_map: RID = get_world_2d().get_navigation_map() + var start_position: Vector2 = get_global_transform().get_origin() + var target_position: Vector2 = target_location + var optimize: bool = true + navigation_path_debug_line.points = Navigation2DServer.map_get_path( + navigation_map, + start_position, + target_position, + optimize + ) + + +func _physics_process(delta): + if navigation_agent.is_navigation_finished(): + return + + var next_position: Vector2 = navigation_agent.get_next_location() + global_position = global_position.move_toward(next_position, delta * character_speed) diff --git a/2d/navigation/level.tscn b/2d/navigation/level.tscn index b32a27c1..24d98f6d 100644 --- a/2d/navigation/level.tscn +++ b/2d/navigation/level.tscn @@ -1,18 +1,19 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://navigation.gd" type="Script" id=1] [ext_resource path="res://map.png" type="Texture" id=2] [ext_resource path="res://character.png" type="Texture" id=3] +[ext_resource path="res://character.gd" type="Script" id=4] [sub_resource type="NavigationPolygon" id=1] vertices = PoolVector2Array( 587.833, 271.924, 530.464, 284.878, 508.256, 281.177, 497.153, 255.269, 624.926, 359.595, 648.903, 394.065, 620.443, 383.995, 669.26, 297.833, 648.903, 321.891, 650.754, 251.567, 619.293, 510.654, 676.663, 493.998, 706.272, 501.401, 669.26, 529.16, 602.638, 523.608, 587.833, 179.393, 573.028, 140.53, 645.202, 159.036, 710.106, 179.216, 630.397, 212.704, 597.086, 192.348, 471.244, 251.567, 421.277, 270.074, 428.68, 246.015, 502.704, 97.9661, 517.509, 55.4019, 537.866, 99.8167, 536.016, 175.692, 495.302, 164.588, 487.899, 85.0117, 310.24, 75.7586, 308.39, 92.4142, 345.402, 210.854, 360.207, 223.808, 297.286, 258.97, 288.033, 231.211, 319.493, 190.497, 193.651, 423.675, 245.469, 477.343, 221.41, 488.446, 147.386, 408.87, 182.548, 382.961, 145.584, 224.311, 175.145, 332.995, 202.904, 99.8167, 310.24, 62.8043, 695.169, 303.385, 682.214, 284.878, 598.937, 492.148, 571.177, 501.401, 605.437, 456.366, 621.144, 486.596, 538.077, 499.891, 395.879, 501.87, 536.407, 524.944, 371.311, 518.056, 573.028, 94.2648, 582.281, 47.9994, 667.409, 75.7586, 350.954, 447.733, 363.908, 351.501, 384.265, 351.501, 376.862, 418.123, 373.441, 436.494, 424.978, 334.845, 421.277, 360.754, 352.804, 320.04, 321.344, 338.546, 299.136, 283.028, 241.767, 327.443, 234.365, 244.165, 325.228, 486.302, 300.441, 497.494, 317.643, 447.733, 332.441, 457.494, 524.608, 359.37, 526.762, 342.248, 366.441, 467.494, 480.497, 434.779, 496.638, 439.381, 476.441, 468.494, 265.825, 407.019, 184.398, 349.65, 310.24, 112.771, 267.676, 153.485, 221.41, 171.991, 700.721, 268.223, 397.219, 188.646, 415.725, 177.543, 465.692, 179.393, 476.796, 207.152, 443.485, 192.348, 437.933, 170.14, 452.738, 166.439, 460.14, 123.875, 476.796, 149.783, 189.95, 231.211 ) polygons = [ PoolIntArray( 0, 1, 2, 3 ), PoolIntArray( 4, 5, 6 ), PoolIntArray( 7, 8, 9 ), PoolIntArray( 10, 11, 12, 13, 14 ), PoolIntArray( 15, 16, 17, 18, 19, 20 ), PoolIntArray( 21, 3, 2, 22, 23 ), PoolIntArray( 24, 25, 26, 27, 28 ), PoolIntArray( 25, 24, 29 ), PoolIntArray( 30, 25, 29, 31 ), PoolIntArray( 32, 33, 34, 35, 36 ), PoolIntArray( 37, 38, 39, 40 ), PoolIntArray( 41, 37, 40 ), PoolIntArray( 41, 40, 42, 43 ), PoolIntArray( 44, 45, 30, 31 ), PoolIntArray( 46, 12, 11, 7, 47 ), PoolIntArray( 47, 7, 9 ), PoolIntArray( 48, 10, 14, 49 ), PoolIntArray( 50, 6, 5, 51, 48 ), PoolIntArray( 52, 50, 48, 49 ), PoolIntArray( 53, 52, 49, 54, 55 ), PoolIntArray( 17, 56, 57, 58, 18 ), PoolIntArray( 59, 60, 61, 62, 63 ), PoolIntArray( 64, 65, 61, 66 ), PoolIntArray( 66, 61, 60, 67, 68 ), PoolIntArray( 68, 67, 69, 70 ), PoolIntArray( 68, 70, 35, 34 ), PoolIntArray( 71, 53, 55, 72 ), PoolIntArray( 71, 72, 73, 74 ), PoolIntArray( 4, 6, 75, 76 ), PoolIntArray( 63, 77, 74, 59 ), PoolIntArray( 78, 2, 1, 76, 75, 79, 80 ), PoolIntArray( 78, 80, 63, 62 ), PoolIntArray( 81, 59, 74, 73 ), PoolIntArray( 81, 73, 41, 82 ), PoolIntArray( 44, 31, 83, 84, 85 ), PoolIntArray( 18, 86, 47, 9, 19 ), PoolIntArray( 15, 20, 3, 21 ), PoolIntArray( 23, 22, 87, 88 ), PoolIntArray( 89, 28, 27, 90, 91 ), PoolIntArray( 89, 91, 92, 93 ), PoolIntArray( 36, 94, 95, 93, 92 ), PoolIntArray( 36, 92, 88 ), PoolIntArray( 36, 88, 87, 32 ), PoolIntArray( 36, 35, 85, 84 ), PoolIntArray( 42, 44, 85, 96 ), PoolIntArray( 42, 96, 43 ), PoolIntArray( 41, 43, 82 ) ] outlines = [ PoolVector2Array( 221.41, 488.446, 147.386, 408.87, 145.584, 224.311, 202.904, 99.8167, 310.24, 62.8043, 310.24, 75.7586, 517.509, 55.4019, 537.866, 99.8167, 536.016, 175.692, 476.796, 207.152, 443.485, 192.348, 437.933, 170.14, 415.725, 177.543, 428.68, 246.015, 471.244, 251.567, 587.833, 179.393, 573.028, 140.53, 645.202, 159.036, 573.028, 94.2648, 582.281, 47.9994, 667.409, 75.7586, 710.106, 179.216, 700.721, 268.223, 682.214, 284.878, 695.169, 303.385, 706.272, 501.401, 669.26, 529.16, 602.638, 523.608, 571.177, 501.401, 536.407, 524.944, 371.311, 518.056, 300.441, 497.494, 317.643, 447.733, 182.548, 382.961, 193.651, 423.675, 245.469, 477.343 ), PoolVector2Array( 350.954, 447.733, 363.908, 351.501, 321.344, 338.546, 241.767, 327.443, 234.365, 244.165, 288.033, 231.211, 221.41, 171.991, 189.95, 231.211, 175.145, 332.995, 184.398, 349.65, 265.825, 407.019 ), PoolVector2Array( 267.676, 153.485, 310.24, 112.771, 308.39, 92.4142, 487.899, 85.0117, 502.704, 97.9661, 495.302, 164.588, 465.692, 179.393, 452.738, 166.439, 476.796, 149.783, 460.14, 123.875, 319.493, 190.497 ), PoolVector2Array( 397.219, 188.646, 345.402, 210.854, 360.207, 223.808, 297.286, 258.97, 299.136, 283.028, 352.804, 320.04, 424.978, 334.845, 421.277, 360.754, 384.265, 351.501, 376.862, 418.123, 480.497, 434.779, 508.256, 281.177, 421.277, 270.074 ), PoolVector2Array( 497.153, 255.269, 597.086, 192.348, 630.397, 212.704, 650.754, 251.567, 648.903, 321.891, 669.26, 297.833, 676.663, 493.998, 619.293, 510.654, 598.937, 492.148, 621.144, 486.596, 648.903, 394.065, 624.926, 359.595, 526.762, 342.248, 530.464, 284.878, 587.833, 271.924 ), PoolVector2Array( 325.228, 486.302, 332.441, 457.494, 366.441, 467.494, 373.441, 436.494, 476.441, 468.494, 496.638, 439.381, 524.608, 359.37, 620.443, 383.995, 605.437, 456.366, 538.077, 499.891, 395.879, 501.87 ) ] -[node name="Navigation2D" type="Node2D"] +[node name="NavigationDemo" type="Node2D"] script = ExtResource( 1 ) -[node name="Navmesh" type="NavigationPolygonInstance" parent="."] +[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="."] navpoly = SubResource( 1 ) [node name="Map" type="Sprite" parent="."] @@ -24,6 +25,13 @@ position = Vector2( 228.464, 132.594 ) scale = Vector2( 0.5, 0.5 ) texture = ExtResource( 3 ) offset = Vector2( 0, -26 ) +script = ExtResource( 4 ) + +[node name="NavigationAgent2D" type="NavigationAgent2D" parent="Character"] + +[node name="Line2D" type="Line2D" parent="Character"] +width = 4.0 +default_color = Color( 1, 0, 0, 1 ) [node name="Camera2D" type="Camera2D" parent="."] offset = Vector2( 420, 300 ) diff --git a/2d/navigation/navigation.gd b/2d/navigation/navigation.gd index 6e7335fa..21e125e4 100644 --- a/2d/navigation/navigation.gd +++ b/2d/navigation/navigation.gd @@ -1,76 +1,10 @@ extends Node2D -export(float) var character_speed = 400.0 -var path = [] - -var map - onready var character = $Character -func _ready(): - # use call deferred to make sure the entire SceneTree Nodes are setup - # else yield on 'physics_frame' in a _ready() might get stuck - call_deferred("setup_navserver") - - -func _process(delta): - var walk_distance = character_speed * delta - move_along_path(walk_distance) - - -# The "click" event is a custom input action defined in -# Project > Project Settings > Input Map tab. func _unhandled_input(event): if not event.is_action_pressed("click"): return - _update_navigation_path(character.position, get_local_mouse_position()) - - -func setup_navserver(): - - # create a new navigation map - map = Navigation2DServer.map_create() - Navigation2DServer.map_set_active(map, true) - - # create a new navigation region and add it to the map - var region = Navigation2DServer.region_create() - Navigation2DServer.region_set_transform(region, Transform()) - Navigation2DServer.region_set_map(region, map) - - # sets navigation mesh for the region - var navigation_poly = NavigationMesh.new() - navigation_poly = $Navmesh.navpoly - Navigation2DServer.region_set_navpoly(region, navigation_poly) - - # wait for Navigation2DServer sync to adapt to made changes - yield(get_tree(), "physics_frame") - - -func move_along_path(distance): - var last_point = character.position - while path.size(): - var distance_between_points = last_point.distance_to(path[0]) - # The position to move to falls between two points. - if distance <= distance_between_points: - character.position = last_point.linear_interpolate(path[0], distance / distance_between_points) - return - # The position is past the end of the segment. - distance -= distance_between_points - last_point = path[0] - path.remove(0) - # The character reached the end of the path. - character.position = last_point - set_process(false) - - -func _update_navigation_path(start_position, end_position): - # map_get_path is part of the avigation2DServer class. - # It returns a PoolVector2Array of points that lead you - # from the start_position to the end_position. - path = Navigation2DServer.map_get_path(map,start_position, end_position, true) - # The first point is always the start_position. - # We don't need it in this example as it corresponds to the character's position. - path.remove(0) - set_process(true) + character.set_target_location(get_global_mouse_position())