Files
godot-demo-projects/2d/navigation/navigation.gd
Hugo Locurcio a45b84a5ad Handle multiple resolutions in most demos
This makes demos render correctly on hiDPI displays,
while also demonstrating how to handle multiple resolutions.

The 3D in 2D demo now uses "3D No-Effects" for the 3D viewport,
which is faster to render. Thanks to this, 4× MSAA is now enabled
for a better result.

The background loading demo now uses mipmaps for better-looking images.

The material testers demo now samples mouse input in a
resolution-independent manner when panning.

Default clear colors were also changed in some projects for visual
consistency with the project's theme.
2020-01-28 19:08:03 +01:00

47 lines
1.4 KiB
GDScript

extends Navigation2D
export(float) var CHARACTER_SPEED = 400.0
var path = []
# 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_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 _process(delta):
var walk_distance = CHARACTER_SPEED * delta
move_along_path(walk_distance)
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)