diff --git a/2d/navigation/agent.png b/2d/navigation/character.png similarity index 100% rename from 2d/navigation/agent.png rename to 2d/navigation/character.png diff --git a/2d/navigation/agent.png.import b/2d/navigation/character.png.import similarity index 71% rename from 2d/navigation/agent.png.import rename to 2d/navigation/character.png.import index fba8973f..6fcc7124 100644 --- a/2d/navigation/agent.png.import +++ b/2d/navigation/character.png.import @@ -2,14 +2,14 @@ importer="texture" type="StreamTexture" -path="res://.import/agent.png-22bdd448030a3457042db97db7630b4c.stex" +path="res://.import/character.png-7a996d3b758d22c506b76a7c15391284.stex" [deps] -source_file="res://agent.png" +source_file="res://character.png" source_md5="c7937b15f89b021d3c888c677b1e1677" -dest_files=[ "res://.import/agent.png-22bdd448030a3457042db97db7630b4c.stex" ] +dest_files=[ "res://.import/character.png-7a996d3b758d22c506b76a7c15391284.stex" ] dest_md5="fd0675f788288554927ef3a6c9686573" [params] diff --git a/2d/navigation/level.tscn b/2d/navigation/level.tscn new file mode 100644 index 00000000..9ece01ec --- /dev/null +++ b/2d/navigation/level.tscn @@ -0,0 +1,35 @@ +[gd_scene load_steps=5 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] + +[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="Navigation2D" index="0"] + +script = ExtResource( 1 ) +CHARACTER_SPEED = 400.0 + +[node name="Navmesh" type="NavigationPolygonInstance" parent="." index="0"] + +navpoly = SubResource( 1 ) +enabled = true + +[node name="Map" type="Sprite" parent="." index="1"] + +position = Vector2( 429.585, 287.32 ) +texture = ExtResource( 2 ) + +[node name="Character" type="Sprite" parent="." index="2"] + +position = Vector2( 228.464, 132.594 ) +scale = Vector2( 0.5, 0.5 ) +texture = ExtResource( 3 ) +offset = Vector2( 0, -26 ) + + diff --git a/2d/navigation/path.png b/2d/navigation/map.png similarity index 100% rename from 2d/navigation/path.png rename to 2d/navigation/map.png diff --git a/2d/navigation/path.png.import b/2d/navigation/map.png.import similarity index 73% rename from 2d/navigation/path.png.import rename to 2d/navigation/map.png.import index 02acc9f9..7a1dd135 100644 --- a/2d/navigation/path.png.import +++ b/2d/navigation/map.png.import @@ -2,14 +2,14 @@ importer="texture" type="StreamTexture" -path="res://.import/path.png-03249d8c3f5af613913d0eb997e480a3.stex" +path="res://.import/map.png-9eea34967fae34f4388f4a32a16da936.stex" [deps] -source_file="res://path.png" +source_file="res://map.png" source_md5="e72fbffb80ea739cf373a488a6d0ab27" -dest_files=[ "res://.import/path.png-03249d8c3f5af613913d0eb997e480a3.stex" ] +dest_files=[ "res://.import/map.png-9eea34967fae34f4388f4a32a16da936.stex" ] dest_md5="9b012237e342f634293969fed09b3966" [params] diff --git a/2d/navigation/navigation.gd b/2d/navigation/navigation.gd index bd7433f0..691d8f3a 100644 --- a/2d/navigation/navigation.gd +++ b/2d/navigation/navigation.gd @@ -1,48 +1,45 @@ extends Navigation2D -# Member variables -const SPEED = 200.0 - -var begin = Vector2() -var end = Vector2() +export(float) var CHARACTER_SPEED = 400.0 var path = [] - -func _process(delta): - if path.size() > 1: - var to_walk = delta * SPEED - while to_walk > 0 and path.size() >= 2: - var pfrom = path[path.size() - 1] - var pto = path[path.size() - 2] - var d = pfrom.distance_to(pto) - if d <= to_walk: - path.remove(path.size() - 1) - to_walk -= d - else: - path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk/d) - to_walk = 0 - - var atpos = path[path.size() - 1] - $agent.position = atpos - - if path.size() < 2: - path = [] - set_process(false) - else: - set_process(false) +# The 'click' event is a custom input action defined in +# Project > Project Settings > Input Map tab +func _input(event): + if not event.is_action_pressed('click'): + return + _update_navigation_path($Character.position, get_local_mouse_position()) -func _update_path(): - var p = get_simple_path(begin, end, true) - path = Array(p) # PoolVector2Array too complex to use, convert to regular array - path.invert() - +func _update_navigation_path(start_position, end_position): + # get_simple_path is part of the Navigation2D class + # it returns a PoolVector2Array of points that lead you from the + # start_position to the end_position + path = get_simple_path(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) -func _input(event): - if event is InputEventMouseButton and event.pressed and event.button_index == 1: - begin = $agent.position - # Mouse to local navigation coordinates - end = event.position - position - _update_path() +func _process(delta): + var walk_distance = CHARACTER_SPEED * delta + move_along_path(walk_distance) + + +func move_along_path(distance): + var last_point = $Character.position + for index in range(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) + break + # the character reached the end of the path + elif distance < 0.0: + $Character.position = path[0] + set_process(false) + break + distance -= distance_between_points + last_point = path[0] + path.remove(0) diff --git a/2d/navigation/navigation.tscn b/2d/navigation/navigation.tscn deleted file mode 100644 index effd1549..00000000 --- a/2d/navigation/navigation.tscn +++ /dev/null @@ -1,34 +0,0 @@ -[gd_scene load_steps=5 format=2] - -[ext_resource path="res://navigation.gd" type="Script" id=1] -[ext_resource path="res://path.png" type="Texture" id=2] -[ext_resource path="res://agent.png" type="Texture" id=3] - -[sub_resource type="NavigationPolygon" id=1] - -vertices = PoolVector2Array( 587.833, 271.924, 530.464, 284.878, 508.256, 281.177, 497.153, 255.269, 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, 648.903, 394.065, 621.144, 486.596, 618.011, 402.479, 624.926, 359.595, 605.437, 456.366, 598.937, 492.148, 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, 524.608, 359.37, 526.762, 342.248, 538.077, 499.891, 571.177, 501.401, 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, 366.441, 467.494, 480.497, 434.779, 518.035, 461.477, 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, 10, 11 ), PoolIntArray( 12, 13, 14, 15, 16, 17 ), PoolIntArray( 18, 19, 20, 21 ), PoolIntArray( 22, 20, 19, 23 ), PoolIntArray( 24, 3, 2, 25, 26 ), PoolIntArray( 27, 28, 29, 30, 31 ), PoolIntArray( 28, 27, 32 ), PoolIntArray( 33, 28, 32, 34 ), PoolIntArray( 35, 36, 37, 38, 39 ), PoolIntArray( 40, 41, 42, 43 ), PoolIntArray( 44, 40, 43 ), PoolIntArray( 44, 43, 45, 46 ), PoolIntArray( 47, 48, 33, 34 ), PoolIntArray( 49, 9, 8, 4, 50 ), PoolIntArray( 50, 4, 6 ), PoolIntArray( 21, 20, 51, 52 ), PoolIntArray( 53, 22, 23, 54 ), PoolIntArray( 23, 7, 11, 54 ), PoolIntArray( 55, 53, 54, 56, 57 ), PoolIntArray( 14, 58, 59, 60, 15 ), PoolIntArray( 61, 62, 63, 64, 65 ), PoolIntArray( 66, 67, 63, 68 ), PoolIntArray( 68, 63, 62, 69, 70 ), PoolIntArray( 70, 69, 71, 72 ), PoolIntArray( 70, 72, 38, 37 ), PoolIntArray( 73, 55, 57, 74 ), PoolIntArray( 73, 74, 75, 76 ), PoolIntArray( 65, 77, 76, 61 ), PoolIntArray( 78, 2, 1, 52, 51 ), PoolIntArray( 78, 51, 79, 80 ), PoolIntArray( 78, 80, 65, 64 ), PoolIntArray( 81, 61, 76, 75 ), PoolIntArray( 81, 75, 44, 82 ), PoolIntArray( 47, 34, 83, 84, 85 ), PoolIntArray( 15, 86, 50, 6, 16 ), PoolIntArray( 12, 17, 3, 24 ), PoolIntArray( 26, 25, 87, 88 ), PoolIntArray( 89, 31, 30, 90, 91 ), PoolIntArray( 89, 91, 92, 93 ), PoolIntArray( 39, 94, 95, 93, 92 ), PoolIntArray( 39, 92, 88 ), PoolIntArray( 39, 88, 87, 35 ), PoolIntArray( 39, 38, 85, 84 ), PoolIntArray( 45, 47, 85, 96 ), PoolIntArray( 45, 96, 46 ), PoolIntArray( 44, 46, 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, 518.035, 461.477, 524.608, 359.37, 618.011, 402.479, 605.437, 456.366, 538.077, 499.891, 395.879, 501.87 ) ] - -[node name="navigation" type="Navigation2D"] - -script = ExtResource( 1 ) - -[node name="path" type="Sprite" parent="."] - -position = Vector2( 429.585, 287.32 ) -texture = ExtResource( 2 ) - -[node name="navpoly" type="NavigationPolygonInstance" parent="."] - -navpoly = SubResource( 1 ) -enabled = true - -[node name="agent" type="Sprite" parent="."] - -position = Vector2( 228.464, 132.594 ) -scale = Vector2( 0.5, 0.5 ) -texture = ExtResource( 3 ) -offset = Vector2( 0, -26 ) - - diff --git a/2d/navigation/project.godot b/2d/navigation/project.godot index 1929fd3e..73a4dfc8 100644 --- a/2d/navigation/project.godot +++ b/2d/navigation/project.godot @@ -11,7 +11,7 @@ config_version=3 [application] config/name="Navigation Polygon (2D)" -run/main_scene="res://navigation.tscn" +run/main_scene="res://level.tscn" config/icon="res://icon.png" [display] @@ -23,3 +23,8 @@ stretch/mode="2d" [gdnative] singletons=[ ] + +[input] + +click=[ 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) + ]