diff --git a/2d/navigation_astar/README.md b/2d/navigation_astar/README.md index b599fff1..4044ba95 100644 --- a/2d/navigation_astar/README.md +++ b/2d/navigation_astar/README.md @@ -1,11 +1,11 @@ -# Grid-based Navigation with Astar +# Grid-based Navigation with AStarGrid2D -This is an example of using AStar for navigation in 2D, +This is an example of using AStarGrid2D for navigation in 2D, complete with Steering Behaviors in order to smooth the movement out. Language: GDScript -Renderer: GLES 2 +Renderer: Compatibility Check out this demo on the asset library: https://godotengine.org/asset-library/asset/519 diff --git a/2d/navigation_astar/character.gd b/2d/navigation_astar/character.gd index 3063033f..b6b9ef2a 100644 --- a/2d/navigation_astar/character.gd +++ b/2d/navigation_astar/character.gd @@ -1,61 +1,65 @@ extends Node2D -enum States { IDLE, FOLLOW } +enum State { IDLE, FOLLOW } const MASS = 10.0 const ARRIVE_DISTANCE = 10.0 @export var speed: float = 200.0 -var _state = States.IDLE - -var _path = [] -var _target_point_world = Vector2() -var _target_position = Vector2() +var _state = State.IDLE var _velocity = Vector2() +@onready var _tile_map = $"../TileMap" + +var _click_position = Vector2() +var _path = PackedVector2Array() +var _next_point = Vector2() + func _ready(): - _change_state(States.IDLE) + _change_state(State.IDLE) func _process(_delta): - if _state != States.FOLLOW: + if _state != State.FOLLOW: return - var _arrived_to_next_point = _move_to(_target_point_world) - if _arrived_to_next_point: + var arrived_to_next_point = _move_to(_next_point) + if arrived_to_next_point: _path.remove_at(0) - if len(_path) == 0: - _change_state(States.IDLE) + if _path.is_empty(): + _change_state(State.IDLE) return - _target_point_world = _path[0] + _next_point = _path[0] func _unhandled_input(event): - if event.is_action_pressed("click"): - var global_mouse_pos = get_global_mouse_position() - if Input.is_key_pressed(KEY_SHIFT) and get_parent().get_node("TileMap").check_start_position(global_mouse_pos): - global_position = global_mouse_pos - else: - _target_position = global_mouse_pos - _change_state(States.FOLLOW) + _click_position = get_global_mouse_position() + if _tile_map.is_point_walkable(_click_position): + if event.is_action_pressed(&"teleport_to", false, true): + _change_state(State.IDLE) + global_position = _tile_map.round_local_position(_click_position) + elif event.is_action_pressed(&"move_to"): + _change_state(State.FOLLOW) -func _move_to(world_position): - var desired_velocity = (world_position - position).normalized() * speed +func _move_to(local_position): + var desired_velocity = (local_position - position).normalized() * speed var steering = desired_velocity - _velocity _velocity += steering / MASS position += _velocity * get_process_delta_time() rotation = _velocity.angle() - return position.distance_to(world_position) < ARRIVE_DISTANCE + return position.distance_to(local_position) < ARRIVE_DISTANCE func _change_state(new_state): - if new_state == States.FOLLOW: - _path = get_parent().get_node(^"TileMap").get_astar_path(position, _target_position) - if _path.is_empty() or len(_path) == 1: - _change_state(States.IDLE) + if new_state == State.IDLE: + _tile_map.clear_path() + elif new_state == State.FOLLOW: + _path = _tile_map.find_path(position, _click_position) + if _path.size() < 2: + _change_state(State.IDLE) return # The index 0 is the starting cell. # We don't want the character to move back to it in this example. - _target_point_world = _path[1] + _next_point = _path[1] _state = new_state diff --git a/2d/navigation_astar/Game.tscn b/2d/navigation_astar/game.tscn similarity index 97% rename from 2d/navigation_astar/Game.tscn rename to 2d/navigation_astar/game.tscn index 46a9fcc8..8007c71f 100644 --- a/2d/navigation_astar/Game.tscn +++ b/2d/navigation_astar/game.tscn @@ -1,25 +1,25 @@ -[gd_scene load_steps=5 format=3 uid="uid://byom4xiqb8ovo"] +[gd_scene load_steps=5 format=3 uid="uid://df87clr3y8vcr"] -[ext_resource type="TileSet" uid="uid://y111l4w24pml" path="res://tileset/tileset.tres" id="1"] -[ext_resource type="Script" path="res://pathfind_astar.gd" id="2"] -[ext_resource type="Script" path="res://character.gd" id="3"] -[ext_resource type="Texture2D" uid="uid://bfedhloqga3jl" path="res://sprites/character.png" id="4"] +[ext_resource type="TileSet" uid="uid://y111l4w24pml" path="res://tileset.tres" id="1_ywle6"] +[ext_resource type="Script" path="res://pathfind_astar.gd" id="2_1eg46"] +[ext_resource type="Script" path="res://character.gd" id="3_8vgae"] +[ext_resource type="Texture2D" uid="uid://bfedhloqga3jl" path="res://sprites/character.png" id="4_0auyy"] [node name="Game" type="Node2D"] [node name="TileMap" type="TileMap" parent="."] -tile_set = ExtResource("1") +tile_set = ExtResource("1_ywle6") format = 2 layer_0/tile_data = PackedInt32Array(393217, 0, 0, 327681, 0, 0, 262145, 0, 0, 262146, 0, 0, 196611, 0, 0, 196612, 0, 0, 131076, 0, 0, 131077, 0, 0, 458757, 0, 0, 458758, 0, 0, 458759, 0, 0, 458760, 0, 0, 458761, 0, 0, 393225, 0, 0, 393226, 0, 0, 327690, 0, 0, 327691, 0, 0, 262155, 0, 0, 262156, 0, 0, 65549, 0, 0, 65550, 0, 0, 131086, 0, 0, 131087, 0, 0, 196623, 0, 0, 262159, 0, 0, 262160, 0, 0, 327696, 0, 0, 393232, 0, 0, 458768, 0, 0, 458767, 0, 0, 524303, 0, 0, -1, 0, 0, 65535, 0, 0, 131071, 0, 0, 196607, 0, 0, 262143, 0, 0, 327679, 0, 0, 393215, 0, 0, 458751, 0, 0, 524287, 0, 0, 589823, 0, 0, 655359, 0, 0, 720895, 0, 0, 655360, 0, 0, 655361, 0, 0, 655362, 0, 0, 655363, 0, 0, 655364, 0, 0, 655365, 0, 0, 655366, 0, 0, 655367, 0, 0, 655368, 0, 0, 655369, 0, 0, 655370, 0, 0, 655371, 0, 0, 655372, 0, 0, 655373, 0, 0, 655374, 0, 0, 655375, 0, 0, 655376, 0, 0, 655377, 0, 0, 655378, 0, 0, 589842, 0, 0, 524306, 0, 0, 458770, 0, 0, 393234, 0, 0, 327698, 0, 0, 262162, 0, 0, 196626, 0, 0, 131090, 0, 0, 65554, 0, 0, 18, 0, 0, -65518, 0, 0, -65519, 0, 0, -65520, 0, 0, -65521, 0, 0, -65522, 0, 0, -65523, 0, 0, -65524, 0, 0, -65525, 0, 0, -65526, 0, 0, -65527, 0, 0, -65528, 0, 0, -65529, 0, 0, -65530, 0, 0, -65531, 0, 0, -65532, 0, 0, -65533, 0, 0, -65534, 0, 0, -65535, 0, 0, -65536, 0, 0, 131080, 0, 0, 65544, 0, 0, 8, 0, 0, 131081, 0, 0, 11, 0, 0, 10, 0, 0, 589834, 0, 0, 589833, 0, 0, 589832, 0, 0, 589826, 0, 0, 524290, 0, 0, 458755, 0, 0, 327685, 0, 0, 262150, 0, 0, 196615, 0, 0, 786430, 0, 0, 720894, 0, 0, 655358, 0, 0, 589822, 0, 0, 524286, 0, 0, 458750, 0, 0, 393214, 0, 0, 327678, 0, 0, 262142, 0, 0, 196606, 0, 0, 131070, 0, 0, 65534, 0, 0, -2, 0, 0, -65538, 0, 0, -65537, 0, 0, -131072, 0, 0, -131071, 0, 0, -131070, 0, 0, -131069, 0, 0, -131068, 0, 0, -131067, 0, 0, -131066, 0, 0, -131065, 0, 0, -131064, 0, 0, -131063, 0, 0, -131062, 0, 0, -131061, 0, 0, -131060, 0, 0, -131059, 0, 0, -131058, 0, 0, -131057, 0, 0, -131056, 0, 0, -131055, 0, 0, -131054, 0, 0, -131053, 0, 0, -65517, 0, 0, 19, 0, 0, 65555, 0, 0, 131091, 0, 0, 196627, 0, 0, 262163, 0, 0, 327699, 0, 0, 393235, 0, 0, 458771, 0, 0, 524307, 0, 0, 589843, 0, 0, 655379, 0, 0, 720915, 0, 0, 720914, 0, 0, 720913, 0, 0, 720912, 0, 0, 720911, 0, 0, 720910, 0, 0, 720909, 0, 0, 720908, 0, 0, 786444, 0, 0, 786443, 0, 0, 786442, 0, 0, 786441, 0, 0, 786440, 0, 0, 786439, 0, 0, 786438, 0, 0, 720902, 0, 0, 720901, 0, 0, 720900, 0, 0, 720899, 0, 0, 720898, 0, 0, 720897, 0, 0, 720896, 0, 0, 786431, 0, 0, 851967, 0, 0, 786432, 0, 0, 786433, 0, 0, 786434, 0, 0, 786435, 0, 0, 786436, 0, 0, 786437, 0, 0, 720904, 0, 0, 720905, 0, 0, 720906, 0, 0, 720907, 0, 0, 720903, 0, 0, 851966, 0, 0, 851965, 0, 0, 786445, 0, 0, 786446, 0, 0, 786447, 0, 0, 786448, 0, 0, 786449, 0, 0, 786450, 0, 0, 786451, 0, 0, -262124, 0, 0, -196588, 0, 0, -131052, 0, 0, -65516, 0, 0, 20, 0, 0, 65556, 0, 0, 131092, 0, 0, 196628, 0, 0, 262164, 0, 0, 327700, 0, 0, 393236, 0, 0, 458772, 0, 0, 524308, 0, 0, 589844, 0, 0, 655380, 0, 0, 720916, 0, 0, 786452, 0, 0, 851988, 0, 0, -196589, 0, 0, -196590, 0, 0, -196591, 0, 0, -196592, 0, 0, -196593, 0, 0, -196594, 0, 0, -196595, 0, 0, -196596, 0, 0, -196597, 0, 0, -196598, 0, 0, -196599, 0, 0, -196600, 0, 0, -196601, 0, 0, -196602, 0, 0, -196603, 0, 0, -196604, 0, 0, -196605, 0, 0, -196606, 0, 0, -196607, 0, 0, -196608, 0, 0, -131073, 0, 0, -131074, 0, 0, -131075, 0, 0, -196611, 0, 0, -196610, 0, 0, -196609, 0, 0, -262144, 0, 0, -262143, 0, 0, -262142, 0, 0, -262141, 0, 0, -262140, 0, 0, -262139, 0, 0, -262138, 0, 0, -262137, 0, 0, -262136, 0, 0, -262135, 0, 0, -262134, 0, 0, -262133, 0, 0, -262132, 0, 0, -262131, 0, 0, -262130, 0, 0, -262129, 0, 0, -262128, 0, 0, -262127, 0, 0, -262126, 0, 0, -262125, 0, 0, 851987, 0, 0, 851986, 0, 0, 851985, 0, 0, 851984, 0, 0, 851983, 0, 0, 851982, 0, 0, 851981, 0, 0, 851980, 0, 0, 851979, 0, 0, 851978, 0, 0, 851977, 0, 0, 851976, 0, 0, 851975, 0, 0, 851974, 0, 0, 851973, 0, 0, 851972, 0, 0, 851971, 0, 0, 851970, 0, 0, 851969, 0, 0, 851968, 0, 0, 917503, 0, 0, 917502, 0, 0, 917501, 0, 0, 786429, 0, 0, 720893, 0, 0, 655357, 0, 0, 589821, 0, 0, 524285, 0, 0, 458749, 0, 0, 393213, 0, 0, 327677, 0, 0, 262141, 0, 0, 196605, 0, 0, 131069, 0, 0, 65533, 0, 0, -3, 0, 0, -65539, 0, 0, -262123, 0, 0, -196587, 0, 0, -131051, 0, 0, -65515, 0, 0, 21, 0, 0, 65557, 0, 0, 131093, 0, 0, 196629, 0, 0, 262165, 0, 0, 327701, 0, 0, 393237, 0, 0, 458773, 0, 0, 524309, 0, 0, 589845, 0, 0, 655381, 0, 0, 720917, 0, 0, 786453, 0, 0, 851989, 0, 0, 917500, 0, 0, 851964, 0, 0, 786428, 0, 0, 720892, 0, 0, 655356, 0, 0, 589820, 0, 0, 524284, 0, 0, 458748, 0, 0, 393212, 0, 0, 327676, 0, 0, 262140, 0, 0, 196604, 0, 0, 131068, 0, 0, 65532, 0, 0, -4, 0, 0, -65540, 0, 0, -131076, 0, 0, -196612, 0, 0, -196615, 0, 0, -131079, 0, 0, -65543, 0, 0, -7, 0, 0, 65529, 0, 0, 131065, 0, 0, 196601, 0, 0, 262137, 0, 0, 327673, 0, 0, 393209, 0, 0, 458745, 0, 0, 524281, 0, 0, 589817, 0, 0, 655353, 0, 0, 720889, 0, 0, 786425, 0, 0, 851961, 0, 0, 917497, 0, 0, -196614, 0, 0, -131078, 0, 0, -65542, 0, 0, -6, 0, 0, 65530, 0, 0, 131066, 0, 0, 196602, 0, 0, 262138, 0, 0, 327674, 0, 0, 393210, 0, 0, 458746, 0, 0, 524282, 0, 0, 589818, 0, 0, 655354, 0, 0, 720890, 0, 0, 786426, 0, 0, 851962, 0, 0, 917498, 0, 0, -196613, 0, 0, -131077, 0, 0, -65541, 0, 0, -5, 0, 0, 65531, 0, 0, 131067, 0, 0, 196603, 0, 0, 262139, 0, 0, 327675, 0, 0, 393211, 0, 0, 458747, 0, 0, 524283, 0, 0, 589819, 0, 0, 655355, 0, 0, 720891, 0, 0, 786427, 0, 0, 851963, 0, 0, 917499, 0, 0, -262122, 0, 0, -196586, 0, 0, -131050, 0, 0, -65514, 0, 0, 22, 0, 0, 65558, 0, 0, 131094, 0, 0, 196630, 0, 0, 262166, 0, 0, 327702, 0, 0, 393238, 0, 0, 458774, 0, 0, 524310, 0, 0, 589846, 0, 0, 655382, 0, 0, 720918, 0, 0, 786454, 0, 0, 851990, 0, 0, -262121, 0, 0, -196585, 0, 0, -131049, 0, 0, -65513, 0, 0, 23, 0, 0, 65559, 0, 0, 131095, 0, 0, 196631, 0, 0, 262167, 0, 0, 327703, 0, 0, 393239, 0, 0, 458775, 0, 0, 524311, 0, 0, 589847, 0, 0, 655383, 0, 0, 720919, 0, 0, 786455, 0, 0, 851991, 0, 0, -262120, 0, 0, -196584, 0, 0, -131048, 0, 0, -65512, 0, 0, 24, 0, 0, 65560, 0, 0, 131096, 0, 0, 196632, 0, 0, 262168, 0, 0, 327704, 0, 0, 393240, 0, 0, 458776, 0, 0, 524312, 0, 0, 589848, 0, 0, 655384, 0, 0, 720920, 0, 0, 786456, 0, 0, 851992, 0, 0, -393223, 0, 0, -327687, 0, 0, -262151, 0, 0, -393222, 0, 0, -327686, 0, 0, -262150, 0, 0, -393221, 0, 0, -327685, 0, 0, -262149, 0, 0, -393220, 0, 0, -327684, 0, 0, -262148, 0, 0, -393219, 0, 0, -327683, 0, 0, -262147, 0, 0, -393218, 0, 0, -327682, 0, 0, -262146, 0, 0, -393217, 0, 0, -327681, 0, 0, -262145, 0, 0, -458752, 0, 0, -393216, 0, 0, -327680, 0, 0, -458751, 0, 0, -393215, 0, 0, -327679, 0, 0, -458750, 0, 0, -393214, 0, 0, -327678, 0, 0, -458749, 0, 0, -393213, 0, 0, -327677, 0, 0, -458748, 0, 0, -393212, 0, 0, -327676, 0, 0, -458747, 0, 0, -393211, 0, 0, -327675, 0, 0, -458746, 0, 0, -393210, 0, 0, -327674, 0, 0, -458745, 0, 0, -393209, 0, 0, -327673, 0, 0, -458744, 0, 0, -393208, 0, 0, -327672, 0, 0, -458743, 0, 0, -393207, 0, 0, -327671, 0, 0, -458742, 0, 0, -393206, 0, 0, -327670, 0, 0, -458741, 0, 0, -393205, 0, 0, -327669, 0, 0, -458740, 0, 0, -393204, 0, 0, -327668, 0, 0, -458739, 0, 0, -393203, 0, 0, -327667, 0, 0, -458738, 0, 0, -393202, 0, 0, -327666, 0, 0, -458737, 0, 0, -393201, 0, 0, -327665, 0, 0, -458736, 0, 0, -393200, 0, 0, -327664, 0, 0, -458735, 0, 0, -393199, 0, 0, -327663, 0, 0, -458734, 0, 0, -393198, 0, 0, -327662, 0, 0, -458733, 0, 0, -393197, 0, 0, -327661, 0, 0, -458732, 0, 0, -393196, 0, 0, -327660, 0, 0, -458731, 0, 0, -393195, 0, 0, -327659, 0, 0, -458730, 0, 0, -393194, 0, 0, -327658, 0, 0, -458729, 0, 0, -393193, 0, 0, -327657, 0, 0, -458728, 0, 0, -393192, 0, 0, -327656, 0, 0, 983033, 0, 0, 1048569, 0, 0, 1114105, 0, 0, 983034, 0, 0, 1048570, 0, 0, 1114106, 0, 0, 983035, 0, 0, 1048571, 0, 0, 1114107, 0, 0, 983036, 0, 0, 1048572, 0, 0, 1114108, 0, 0, 983037, 0, 0, 1048573, 0, 0, 1114109, 0, 0, 983038, 0, 0, 1048574, 0, 0, 1114110, 0, 0, 983039, 0, 0, 1048575, 0, 0, 1114111, 0, 0, 917504, 0, 0, 983040, 0, 0, 1048576, 0, 0, 917505, 0, 0, 983041, 0, 0, 1048577, 0, 0, 917506, 0, 0, 983042, 0, 0, 1048578, 0, 0, 917507, 0, 0, 983043, 0, 0, 1048579, 0, 0, 917508, 0, 0, 983044, 0, 0, 1048580, 0, 0, 917509, 0, 0, 983045, 0, 0, 1048581, 0, 0, 917510, 0, 0, 983046, 0, 0, 1048582, 0, 0, 917511, 0, 0, 983047, 0, 0, 1048583, 0, 0, 917512, 0, 0, 983048, 0, 0, 1048584, 0, 0, 917513, 0, 0, 983049, 0, 0, 1048585, 0, 0, 917514, 0, 0, 983050, 0, 0, 1048586, 0, 0, 917515, 0, 0, 983051, 0, 0, 1048587, 0, 0, 917516, 0, 0, 983052, 0, 0, 1048588, 0, 0, 917517, 0, 0, 983053, 0, 0, 1048589, 0, 0, 917518, 0, 0, 983054, 0, 0, 1048590, 0, 0, 917519, 0, 0, 983055, 0, 0, 1048591, 0, 0, 917520, 0, 0, 983056, 0, 0, 1048592, 0, 0, 917521, 0, 0, 983057, 0, 0, 1048593, 0, 0, 917522, 0, 0, 983058, 0, 0, 1048594, 0, 0, 917523, 0, 0, 983059, 0, 0, 1048595, 0, 0, 917524, 0, 0, 983060, 0, 0, 1048596, 0, 0, 917525, 0, 0, 983061, 0, 0, 1048597, 0, 0, 917526, 0, 0, 983062, 0, 0, 1048598, 0, 0, 917527, 0, 0, 983063, 0, 0, 1048599, 0, 0, 917528, 0, 0, 983064, 0, 0, 1048600, 0, 0, -589834, 0, 0, -524298, 0, 0, -458762, 0, 0, -393226, 0, 0, -327690, 0, 0, -262154, 0, 0, -196618, 0, 0, -131082, 0, 0, -65546, 0, 0, -10, 0, 0, 65526, 0, 0, 131062, 0, 0, 196598, 0, 0, 262134, 0, 0, 327670, 0, 0, 393206, 0, 0, 458742, 0, 0, 524278, 0, 0, 589814, 0, 0, 655350, 0, 0, 720886, 0, 0, 786422, 0, 0, 851958, 0, 0, 917494, 0, 0, 983030, 0, 0, 1048566, 0, 0, 1114102, 0, 0, 1179638, 0, 0, 1245174, 0, 0, 1310710, 0, 0, -589833, 0, 0, -524297, 0, 0, -458761, 0, 0, -393225, 0, 0, -327689, 0, 0, -262153, 0, 0, -196617, 0, 0, -131081, 0, 0, -65545, 0, 0, -9, 0, 0, 65527, 0, 0, 131063, 0, 0, 196599, 0, 0, 262135, 0, 0, 327671, 0, 0, 393207, 0, 0, 458743, 0, 0, 524279, 0, 0, 589815, 0, 0, 655351, 0, 0, 720887, 0, 0, 786423, 0, 0, 851959, 0, 0, 917495, 0, 0, 983031, 0, 0, 1048567, 0, 0, 1114103, 0, 0, 1179639, 0, 0, 1245175, 0, 0, 1310711, 0, 0, -589832, 0, 0, -524296, 0, 0, -458760, 0, 0, -393224, 0, 0, -327688, 0, 0, -262152, 0, 0, -196616, 0, 0, -131080, 0, 0, -65544, 0, 0, -8, 0, 0, 65528, 0, 0, 131064, 0, 0, 196600, 0, 0, 262136, 0, 0, 327672, 0, 0, 393208, 0, 0, 458744, 0, 0, 524280, 0, 0, 589816, 0, 0, 655352, 0, 0, 720888, 0, 0, 786424, 0, 0, 851960, 0, 0, 917496, 0, 0, 983032, 0, 0, 1048568, 0, 0, 1114104, 0, 0, 1179640, 0, 0, 1245176, 0, 0, 1310712, 0, 0, 1179641, 0, 0, 1245177, 0, 0, 1310713, 0, 0, 1179642, 0, 0, 1245178, 0, 0, 1310714, 0, 0, 1179643, 0, 0, 1245179, 0, 0, 1310715, 0, 0, 1179644, 0, 0, 1245180, 0, 0, 1310716, 0, 0, 1179645, 0, 0, 1245181, 0, 0, 1310717, 0, 0, 1179646, 0, 0, 1245182, 0, 0, 1310718, 0, 0, 1179647, 0, 0, 1245183, 0, 0, 1310719, 0, 0, 1114112, 0, 0, 1179648, 0, 0, 1245184, 0, 0, 1114113, 0, 0, 1179649, 0, 0, 1245185, 0, 0, 1114114, 0, 0, 1179650, 0, 0, 1245186, 0, 0, 1114115, 0, 0, 1179651, 0, 0, 1245187, 0, 0, 1114116, 0, 0, 1179652, 0, 0, 1245188, 0, 0, 1114117, 0, 0, 1179653, 0, 0, 1245189, 0, 0, 1114118, 0, 0, 1179654, 0, 0, 1245190, 0, 0, 1114119, 0, 0, 1179655, 0, 0, 1245191, 0, 0, 1114120, 0, 0, 1179656, 0, 0, 1245192, 0, 0, 1114121, 0, 0, 1179657, 0, 0, 1245193, 0, 0, 1114122, 0, 0, 1179658, 0, 0, 1245194, 0, 0, 1114123, 0, 0, 1179659, 0, 0, 1245195, 0, 0, 1114124, 0, 0, 1179660, 0, 0, 1245196, 0, 0, 1114125, 0, 0, 1179661, 0, 0, 1245197, 0, 0, 1114126, 0, 0, 1179662, 0, 0, 1245198, 0, 0, 1114127, 0, 0, 1179663, 0, 0, 1245199, 0, 0, 1114128, 0, 0, 1179664, 0, 0, 1245200, 0, 0, 1114129, 0, 0, 1179665, 0, 0, 1245201, 0, 0, 1114130, 0, 0, 1179666, 0, 0, 1245202, 0, 0, 1114131, 0, 0, 1179667, 0, 0, 1245203, 0, 0, 1114132, 0, 0, 1179668, 0, 0, 1245204, 0, 0, 1114133, 0, 0, 1179669, 0, 0, 1245205, 0, 0, 1114134, 0, 0, 1179670, 0, 0, 1245206, 0, 0, 1114135, 0, 0, 1179671, 0, 0, 1245207, 0, 0, 1114136, 0, 0, 1179672, 0, 0, 1245208, 0, 0, 1114137, 0, 0, 1179673, 0, 0, 1245209, 0, 0, 1114138, 0, 0, 1179674, 0, 0, 1245210, 0, 0, 1114139, 0, 0, 1179675, 0, 0, 1245211, 0, 0, -655335, 0, 0, -589799, 0, 0, -524263, 0, 0, -458727, 0, 0, -393191, 0, 0, -327655, 0, 0, -262119, 0, 0, -196583, 0, 0, -131047, 0, 0, -65511, 0, 0, 25, 0, 0, 65561, 0, 0, 131097, 0, 0, 196633, 0, 0, 262169, 0, 0, 327705, 0, 0, 393241, 0, 0, 458777, 0, 0, 524313, 0, 0, 589849, 0, 0, 655385, 0, 0, 720921, 0, 0, 786457, 0, 0, 851993, 0, 0, 917529, 0, 0, 983065, 0, 0, 1048601, 0, 0, -655334, 0, 0, -589798, 0, 0, -524262, 0, 0, -458726, 0, 0, -393190, 0, 0, -327654, 0, 0, -262118, 0, 0, -196582, 0, 0, -131046, 0, 0, -65510, 0, 0, 26, 0, 0, 65562, 0, 0, 131098, 0, 0, 196634, 0, 0, 262170, 0, 0, 327706, 0, 0, 393242, 0, 0, 458778, 0, 0, 524314, 0, 0, 589850, 0, 0, 655386, 0, 0, 720922, 0, 0, 786458, 0, 0, 851994, 0, 0, 917530, 0, 0, 983066, 0, 0, 1048602, 0, 0, -655333, 0, 0, -589797, 0, 0, -524261, 0, 0, -458725, 0, 0, -393189, 0, 0, -327653, 0, 0, -262117, 0, 0, -196581, 0, 0, -131045, 0, 0, -65509, 0, 0, 27, 0, 0, 65563, 0, 0, 131099, 0, 0, 196635, 0, 0, 262171, 0, 0, 327707, 0, 0, 393243, 0, 0, 458779, 0, 0, 524315, 0, 0, 589851, 0, 0, 655387, 0, 0, 720923, 0, 0, 786459, 0, 0, 851995, 0, 0, 917531, 0, 0, 983067, 0, 0, 1048603, 0, 0, -589831, 0, 0, -524295, 0, 0, -458759, 0, 0, -589830, 0, 0, -524294, 0, 0, -458758, 0, 0, -589829, 0, 0, -524293, 0, 0, -458757, 0, 0, -589828, 0, 0, -524292, 0, 0, -458756, 0, 0, -589827, 0, 0, -524291, 0, 0, -458755, 0, 0, -589826, 0, 0, -524290, 0, 0, -458754, 0, 0, -589825, 0, 0, -524289, 0, 0, -458753, 0, 0, -655360, 0, 0, -589824, 0, 0, -524288, 0, 0, -655359, 0, 0, -589823, 0, 0, -524287, 0, 0, -655358, 0, 0, -589822, 0, 0, -524286, 0, 0, -655357, 0, 0, -589821, 0, 0, -524285, 0, 0, -655356, 0, 0, -589820, 0, 0, -524284, 0, 0, -655355, 0, 0, -589819, 0, 0, -524283, 0, 0, -655354, 0, 0, -589818, 0, 0, -524282, 0, 0, -655353, 0, 0, -589817, 0, 0, -524281, 0, 0, -655352, 0, 0, -589816, 0, 0, -524280, 0, 0, -655351, 0, 0, -589815, 0, 0, -524279, 0, 0, -655350, 0, 0, -589814, 0, 0, -524278, 0, 0, -655349, 0, 0, -589813, 0, 0, -524277, 0, 0, -655348, 0, 0, -589812, 0, 0, -524276, 0, 0, -655347, 0, 0, -589811, 0, 0, -524275, 0, 0, -655346, 0, 0, -589810, 0, 0, -524274, 0, 0, -655345, 0, 0, -589809, 0, 0, -524273, 0, 0, -655344, 0, 0, -589808, 0, 0, -524272, 0, 0, -655343, 0, 0, -589807, 0, 0, -524271, 0, 0, -655342, 0, 0, -589806, 0, 0, -524270, 0, 0, -655341, 0, 0, -589805, 0, 0, -524269, 0, 0, -655340, 0, 0, -589804, 0, 0, -524268, 0, 0, -655339, 0, 0, -589803, 0, 0, -524267, 0, 0, -655338, 0, 0, -589802, 0, 0, -524266, 0, 0, -655337, 0, 0, -589801, 0, 0, -524265, 0, 0, -655336, 0, 0, -589800, 0, 0, -524264, 0, 0) -script = ExtResource("2") +script = ExtResource("2_1eg46") [node name="Character" type="Marker2D" parent="."] position = Vector2(32, 32) -script = ExtResource("3") +script = ExtResource("3_8vgae") [node name="Sprite2D" type="Sprite2D" parent="Character"] position = Vector2(7, 0) -texture = ExtResource("4") +texture = ExtResource("4_0auyy") [node name="Camera2D" type="Camera2D" parent="."] offset = Vector2(576, 324) diff --git a/2d/navigation_astar/pathfind_astar.gd b/2d/navigation_astar/pathfind_astar.gd index ea659c39..dc163683 100644 --- a/2d/navigation_astar/pathfind_astar.gd +++ b/2d/navigation_astar/pathfind_astar.gd @@ -1,177 +1,83 @@ extends TileMap +enum Tile { OBSTACLE, START_POINT, END_POINT } + +const CELL_SIZE = Vector2(64, 64) const BASE_LINE_WIDTH = 3.0 const DRAW_COLOR = Color.WHITE -# The Tilemap node doesn't have clear bounds so we're defining the map's limits here. -@export var map_size: Vector2i = Vector2.ONE * 18 +# The object for pathfinding on 2D grids. +var _astar = AStarGrid2D.new() +var _map_rect = Rect2i() -# The path start and end variables use setter methods, defined below the initial values. -var path_start_position = Vector2i(): - set(value): - if value in obstacles: - return - if is_outside_map_bounds(value): - return - - set_cell(0, path_start_position, -1) - set_cell(0, value, 1, Vector2i()) - path_start_position = value - if path_end_position and path_end_position != path_start_position: - _recalculate_path() - -var path_end_position = Vector2i(): - set(value): - if value in obstacles: - return - if is_outside_map_bounds(value): - return - - set_cell(0, path_start_position, -1) - set_cell(0, value, 2, Vector2i()) - path_end_position = value - if path_start_position != value: - _recalculate_path() - -var _point_path = [] - -# You can only create an AStar node from code, not from the Scene tab. -@onready var astar_node = AStar3D.new() -# get_used_cells_by_id is a method from the TileMap node. -# Here the id 0 corresponds to the grey tile, the obstacles. -@onready var obstacles = get_used_cells(0) +var _start_point = Vector2i() +var _end_point = Vector2i() +var _path = PackedVector2Array() func _ready(): - var walkable_cells_list = astar_add_walkable_cells(obstacles) - astar_connect_walkable_cells(walkable_cells_list) + # Let's assume that the entire map is located at non-negative coordinates. + var map_size = get_used_rect().end + _map_rect = Rect2i(Vector2i(), map_size) + + _astar.size = map_size + _astar.cell_size = CELL_SIZE + _astar.offset = CELL_SIZE * 0.5 + _astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN + _astar.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN + _astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER + _astar.update() + + for i in map_size.x: + for j in map_size.y: + var pos = Vector2i(i, j) + if get_cell_source_id(0, pos) == Tile.OBSTACLE: + _astar.set_point_solid(pos) func _draw(): - if _point_path.is_empty(): + if _path.is_empty(): return - var point_start = _point_path[0] - var point_end = _point_path[len(_point_path) - 1] - set_cell(0, Vector2i(point_start.x, point_start.y), 1, Vector2i()) - set_cell(0, Vector2i(point_end.x, point_end.y), 2, Vector2i()) - - var last_point = map_to_local(Vector2i(point_start.x, point_start.y)) - for index in range(1, len(_point_path)): - var current_point = map_to_local(Vector2i(_point_path[index].x, _point_path[index].y)) + var last_point = _path[0] + for index in range(1, len(_path)): + var current_point = _path[index] draw_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true) draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR) last_point = current_point -# Loops through all cells within the map's bounds and -# adds all points to the astar_node, except the obstacles. -func astar_add_walkable_cells(obstacle_list = []): - var points_array = [] - for y in range(map_size.y): - for x in range(map_size.x): - var point = Vector2i(x, y) - if point in obstacle_list: - continue - - points_array.append(point) - # The AStar class references points with indices. - # Using a function to calculate the index from a point's coordinates - # ensures we always get the same index with the same input point. - var point_index = calculate_point_index(point) - # AStar works for both 2d and 3d, so we have to convert the point - # coordinates from and to Vector3s. - astar_node.add_point(point_index, Vector3(point.x, point.y, 0.0)) - return points_array +func round_local_position(local_position): + return map_to_local(local_to_map(local_position)) -# Once you added all points to the AStar node, you've got to connect them. -# The points don't have to be on a grid: you can use this class -# to create walkable graphs however you'd like. -# It's a little harder to code at first, but works for 2d, 3d, -# orthogonal grids, hex grids, tower defense games... -func astar_connect_walkable_cells(points_array): - for point in points_array: - var point_index = calculate_point_index(point) - # For every cell in the map, we check the one to the top, right. - # left and bottom of it. If it's in the map and not an obstalce. - # We connect the current point with it. - var points_relative = PackedVector2Array([ - point + Vector2i.RIGHT, - point + Vector2i.LEFT, - point + Vector2i.DOWN, - point + Vector2i.UP, - ]) - for point_relative in points_relative: - var point_relative_index = calculate_point_index(point_relative) - if is_outside_map_bounds(point_relative): - continue - if not astar_node.has_point(point_relative_index): - continue - # Note the 3rd argument. It tells the astar_node that we want the - # connection to be bilateral: from point A to B and B to A. - # If you set this value to false, it becomes a one-way path. - # As we loop through all points we can set it to false. - astar_node.connect_points(point_index, point_relative_index, false) +func is_point_walkable(local_position): + var map_position = local_to_map(local_position) + if _map_rect.has_point(map_position): + return not _astar.is_point_solid(map_position) + return false -# This is a variation of the method above. -# It connects cells horizontally, vertically AND diagonally. -func astar_connect_walkable_cells_diagonal(points_array): - for point in points_array: - var point_index = calculate_point_index(point) - for local_y in range(3): - for local_x in range(3): - var point_relative = Vector2i(point.x + local_x - 1, point.y + local_y - 1) - var point_relative_index = calculate_point_index(point_relative) - if point_relative == point or is_outside_map_bounds(point_relative): - continue - if not astar_node.has_point(point_relative_index): - continue - astar_node.connect_points(point_index, point_relative_index, true) +func clear_path(): + if not _path.is_empty(): + _path.clear() + erase_cell(0, _start_point) + erase_cell(0, _end_point) + # Queue redraw to clear the lines and circles. + queue_redraw() -func calculate_point_index(point): - return point.x + map_size.x * point.y +func find_path(local_start_point, local_end_point): + clear_path() + _start_point = local_to_map(local_start_point) + _end_point = local_to_map(local_end_point) + _path = _astar.get_point_path(_start_point, _end_point) -func clear_previous_path_drawing(): - if _point_path.is_empty(): - return - var point_start = _point_path[0] - var point_end = _point_path[len(_point_path) - 1] - set_cell(0, Vector2i(point_start.x, point_start.y), -1) - set_cell(0, Vector2i(point_end.x, point_end.y), -1) + if not _path.is_empty(): + set_cell(0, _start_point, Tile.START_POINT, Vector2i()) + set_cell(0, _end_point, Tile.END_POINT, Vector2i()) - -func is_outside_map_bounds(point): - return point.x < 0 or point.y < 0 or point.x >= map_size.x or point.y >= map_size.y - - -func check_start_position(world_start): - var start_point = local_to_map(world_start) - if start_point in obstacles: - return false - - return true - - -func get_astar_path(world_start, world_end): - self.path_start_position = local_to_map(world_start) - self.path_end_position = local_to_map(world_end) - _recalculate_path() - var path_world = [] - for point in _point_path: - var point_world = map_to_local(Vector2i(point.x, point.y)) - path_world.append(point_world) - return path_world - - -func _recalculate_path(): - clear_previous_path_drawing() - var start_point_index = calculate_point_index(path_start_position) - var end_point_index = calculate_point_index(path_end_position) - # This method gives us an array of points. Note you need the start and - # end points' indices as input. - _point_path = astar_node.get_point_path(start_point_index, end_point_index) # Redraw the lines and circles from the start to the end point. queue_redraw() + + return _path.duplicate() diff --git a/2d/navigation_astar/project.godot b/2d/navigation_astar/project.godot index bad974f3..ee0156d2 100644 --- a/2d/navigation_astar/project.godot +++ b/2d/navigation_astar/project.godot @@ -10,10 +10,10 @@ config_version=5 [application] -config/name="Grid-based Pathfinding with Astar" -config/description="This is an example of using AStar for navigation in 2D, +config/name="Grid-based Pathfinding with AStarGrid2D" +config/description="This is an example of using AStarGrid2D for navigation in 2D, complete with Steering Behaviors in order to smooth the movement out." -run/main_scene="res://Game.tscn" +run/main_scene="res://game.tscn" config/features=PackedStringArray("4.0") config/icon="res://icon.png" @@ -24,14 +24,17 @@ window/stretch/aspect="expand" [input] -click={ +move_to={ "deadzone": 0.5, -"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":false,"double_click":false,"script":null) +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":true,"double_click":false,"script":null) +] +} +teleport_to={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":true,"double_click":false,"script":null) ] } [rendering] -quality/driver/driver_name="GLES2" -vram_compression/import_etc=true -vram_compression/import_etc2=false +renderer/rendering_method="gl_compatibility" diff --git a/2d/navigation_astar/tileset/tileset.tres b/2d/navigation_astar/tileset.tres similarity index 100% rename from 2d/navigation_astar/tileset/tileset.tres rename to 2d/navigation_astar/tileset.tres diff --git a/2d/navigation_astar/tileset/tileset_source.tscn b/2d/navigation_astar/tileset/tileset_source.tscn deleted file mode 100644 index 79b5b816..00000000 --- a/2d/navigation_astar/tileset/tileset_source.tscn +++ /dev/null @@ -1,19 +0,0 @@ -[gd_scene load_steps=4 format=2] - -[ext_resource path="res://sprites/obstacle.png" type="Texture2D" id=1] -[ext_resource path="res://sprites/path_start.png" type="Texture2D" id=2] -[ext_resource path="res://sprites/path_end.png" type="Texture2D" id=3] - -[node name="Tileset" type="Node2D"] - -[node name="Obstacle" type="Sprite2D" parent="."] -position = Vector2(32, 32) -texture = ExtResource( 1 ) - -[node name="PathStart" type="Sprite2D" parent="."] -position = Vector2(112, 32) -texture = ExtResource( 2 ) - -[node name="PathEnd" type="Sprite2D" parent="."] -position = Vector2(192, 32) -texture = ExtResource( 3 )