Skip to content

Commit 840777a

Browse files
authored
Merge pull request #236 from GDquest/astar-demo
Add 2d/navigation_astar demo for grid-based pathfinding
2 parents 8d93b17 + 2c2c8e7 commit 840777a

17 files changed

+635
-0
lines changed

2d/navigation_astar/Game.tscn

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[gd_scene load_steps=5 format=2]
2+
3+
[ext_resource path="res://tileset/tileset.tres" type="TileSet" id=1]
4+
[ext_resource path="res://pathfind_astar.gd" type="Script" id=2]
5+
[ext_resource path="res://character.gd" type="Script" id=3]
6+
[ext_resource path="res://sprites/character.png" type="Texture" id=4]
7+
8+
[node name="Game" type="Node" index="0"]
9+
10+
[node name="TileMap" type="TileMap" parent="." index="0"]
11+
12+
mode = 0
13+
tile_set = ExtResource( 1 )
14+
cell_size = Vector2( 64, 64 )
15+
cell_quadrant_size = 16
16+
cell_custom_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
17+
cell_half_offset = 2
18+
cell_tile_origin = 0
19+
cell_y_sort = false
20+
cell_clip_uv = false
21+
collision_use_kinematic = false
22+
collision_friction = 1.0
23+
collision_bounce = 0.0
24+
collision_layer = 1
25+
collision_mask = 1
26+
occluder_light_mask = 1
27+
format = 1
28+
tile_data = PoolIntArray( 65537, 0, 0, 65541, 0, 0, 65545, 0, 0, 65550, 0, 0, 196614, 0, 0, 196615, 0, 0, 196616, 0, 0, 196617, 0, 0, 196618, 0, 0, 196619, 0, 0, 262145, 0, 0, 262146, 0, 0, 262147, 0, 0, 327683, 0, 0, 393219, 0, 0, 393220, 0, 0, 393221, 0, 0, 393222, 0, 0, 393226, 0, 0, 393227, 0, 0, 393228, 0, 0, 458761, 0, 0, 524290, 0, 0, 524291, 0, 0, 524292, 0, 0, 524293, 0, 0, 524294, 0, 0, 524295, 0, 0, 524296, 0, 0, 524297, 0, 0 )
29+
script = ExtResource( 2 )
30+
_sections_unfolded = [ "Cell" ]
31+
map_size = Vector2( 16, 16 )
32+
33+
[node name="Character" type="Position2D" parent="." index="1"]
34+
35+
script = ExtResource( 3 )
36+
SPEED = 200.0
37+
38+
[node name="Sprite" type="Sprite" parent="Character" index="0"]
39+
40+
position = Vector2( 7, 0 )
41+
texture = ExtResource( 4 )
42+
43+

2d/navigation_astar/character.gd

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
extends Position2D
2+
3+
export(float) var SPEED = 200.0
4+
5+
enum STATES { IDLE, FOLLOW }
6+
var _state = null
7+
8+
var path = []
9+
var target_point_world = Vector2()
10+
var target_position = Vector2()
11+
12+
var velocity = Vector2()
13+
14+
func _ready():
15+
_change_state(IDLE)
16+
17+
18+
func _change_state(new_state):
19+
if new_state == FOLLOW:
20+
path = get_parent().get_node('TileMap').get_path(position, target_position)
21+
if not path:
22+
_change_state(IDLE)
23+
return
24+
# The index 0 is the starting cell
25+
# we don't want the character to move back to it in this example
26+
target_point_world = path[1]
27+
_state = new_state
28+
29+
30+
func _process(delta):
31+
if not _state == FOLLOW:
32+
return
33+
var arrived_to_next_point = move_to(target_point_world)
34+
if arrived_to_next_point:
35+
path.remove(0)
36+
if len(path) == 0:
37+
_change_state(IDLE)
38+
return
39+
target_point_world = path[0]
40+
41+
42+
func move_to(world_position):
43+
var MASS = 10.0
44+
var ARRIVE_DISTANCE = 10.0
45+
46+
var desired_velocity = (world_position - position).normalized() * SPEED
47+
var steering = desired_velocity - velocity
48+
velocity += steering / MASS
49+
position += velocity * get_process_delta_time()
50+
rotation = velocity.angle()
51+
return position.distance_to(world_position) < ARRIVE_DISTANCE
52+
53+
54+
func _input(event):
55+
if event.is_action_pressed('click'):
56+
if Input.is_key_pressed(KEY_SHIFT):
57+
global_position = get_global_mouse_position()
58+
else:
59+
target_position = get_global_mouse_position()
60+
_change_state(FOLLOW)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
5+
radiance_size = 4
6+
sky_top_color = Color( 0.0470588, 0.454902, 0.976471, 1 )
7+
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
8+
sky_curve = 0.25
9+
sky_energy = 1.0
10+
ground_bottom_color = Color( 0.101961, 0.145098, 0.188235, 1 )
11+
ground_horizon_color = Color( 0.482353, 0.788235, 0.952941, 1 )
12+
ground_curve = 0.01
13+
ground_energy = 1.0
14+
sun_color = Color( 1, 1, 1, 1 )
15+
sun_latitude = 35.0
16+
sun_longitude = 0.0
17+
sun_angle_min = 1.0
18+
sun_angle_max = 100.0
19+
sun_curve = 0.05
20+
sun_energy = 16.0
21+
texture_size = 2
22+
23+
[resource]
24+
25+
background_mode = 2
26+
background_sky = SubResource( 1 )
27+
background_sky_custom_fov = 0.0
28+
background_color = Color( 0, 0, 0, 1 )
29+
background_energy = 1.0
30+
background_canvas_max_layer = 0
31+
ambient_light_color = Color( 0, 0, 0, 1 )
32+
ambient_light_energy = 1.0
33+
ambient_light_sky_contribution = 1.0
34+
fog_enabled = false
35+
fog_color = Color( 0.5, 0.6, 0.7, 1 )
36+
fog_sun_color = Color( 1, 0.9, 0.7, 1 )
37+
fog_sun_amount = 0.0
38+
fog_depth_enabled = true
39+
fog_depth_begin = 10.0
40+
fog_depth_curve = 1.0
41+
fog_transmit_enabled = false
42+
fog_transmit_curve = 1.0
43+
fog_height_enabled = false
44+
fog_height_min = 0.0
45+
fog_height_max = 100.0
46+
fog_height_curve = 1.0
47+
tonemap_mode = 0
48+
tonemap_exposure = 1.0
49+
tonemap_white = 1.0
50+
auto_exposure_enabled = false
51+
auto_exposure_scale = 0.4
52+
auto_exposure_min_luma = 0.05
53+
auto_exposure_max_luma = 8.0
54+
auto_exposure_speed = 0.5
55+
ss_reflections_enabled = false
56+
ss_reflections_max_steps = 64
57+
ss_reflections_fade_in = 0.15
58+
ss_reflections_fade_out = 2.0
59+
ss_reflections_depth_tolerance = 0.2
60+
ss_reflections_roughness = true
61+
ssao_enabled = false
62+
ssao_radius = 1.0
63+
ssao_intensity = 1.0
64+
ssao_radius2 = 0.0
65+
ssao_intensity2 = 1.0
66+
ssao_bias = 0.01
67+
ssao_light_affect = 0.0
68+
ssao_color = Color( 0, 0, 0, 1 )
69+
ssao_quality = 0
70+
ssao_blur = 3
71+
ssao_edge_sharpness = 4.0
72+
dof_blur_far_enabled = false
73+
dof_blur_far_distance = 10.0
74+
dof_blur_far_transition = 5.0
75+
dof_blur_far_amount = 0.1
76+
dof_blur_far_quality = 1
77+
dof_blur_near_enabled = false
78+
dof_blur_near_distance = 2.0
79+
dof_blur_near_transition = 1.0
80+
dof_blur_near_amount = 0.1
81+
dof_blur_near_quality = 1
82+
glow_enabled = false
83+
glow_levels/1 = false
84+
glow_levels/2 = false
85+
glow_levels/3 = true
86+
glow_levels/4 = false
87+
glow_levels/5 = true
88+
glow_levels/6 = false
89+
glow_levels/7 = false
90+
glow_intensity = 0.8
91+
glow_strength = 1.0
92+
glow_bloom = 0.0
93+
glow_blend_mode = 2
94+
glow_hdr_threshold = 1.0
95+
glow_hdr_scale = 2.0
96+
glow_bicubic_upscale = false
97+
adjustment_enabled = false
98+
adjustment_brightness = 1.0
99+
adjustment_contrast = 1.0
100+
adjustment_saturation = 1.0
101+

2d/navigation_astar/icon.png

2.5 KB
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
7+
[deps]
8+
9+
source_file="res://icon.png"
10+
source_md5="0b0cb045ce690875fab066adeea76aba"
11+
12+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
13+
dest_md5="58323f97585db7bd8d6e03320f9ebb33"
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/normal_map=0
21+
flags/repeat=0
22+
flags/filter=true
23+
flags/mipmaps=false
24+
flags/anisotropic=false
25+
flags/srgb=2
26+
process/fix_alpha_border=true
27+
process/premult_alpha=false
28+
process/HDR_as_SRGB=false
29+
stream=false
30+
size_limit=0
31+
detect_3d=true
32+
svg/scale=1.0

0 commit comments

Comments
 (0)