2015-02-14 16:09:52 +01:00
|
|
|
extends Navigation2D
|
|
|
|
|
2020-02-03 08:36:35 +01:00
|
|
|
export(float) var character_speed = 400.0
|
2015-11-21 16:13:43 +01:00
|
|
|
var path = []
|
2015-02-14 16:09:52 +01:00
|
|
|
|
2020-06-28 09:29:20 +02:00
|
|
|
onready var character = $Character
|
|
|
|
|
2020-02-03 08:36:35 +01:00
|
|
|
func _process(delta):
|
|
|
|
var walk_distance = character_speed * delta
|
|
|
|
move_along_path(walk_distance)
|
|
|
|
|
|
|
|
|
2020-06-28 09:29:20 +02:00
|
|
|
# The "click" event is a custom input action defined in
|
2020-02-03 08:36:35 +01:00
|
|
|
# Project > Project Settings > Input Map tab.
|
2020-06-09 04:43:19 +02:00
|
|
|
func _unhandled_input(event):
|
2020-02-03 08:36:35 +01:00
|
|
|
if not event.is_action_pressed("click"):
|
2018-03-27 08:06:22 +02:00
|
|
|
return
|
2020-06-28 09:29:20 +02:00
|
|
|
_update_navigation_path(character.position, get_local_mouse_position())
|
2018-03-27 08:06:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
func move_along_path(distance):
|
2020-06-28 09:29:20 +02:00
|
|
|
var last_point = character.position
|
2019-04-08 15:46:00 +02:00
|
|
|
while path.size():
|
2018-03-27 08:06:22 +02:00
|
|
|
var distance_between_points = last_point.distance_to(path[0])
|
2020-02-03 08:36:35 +01:00
|
|
|
# The position to move to falls between two points.
|
2019-04-08 15:46:00 +02:00
|
|
|
if distance <= distance_between_points:
|
2020-06-28 09:29:20 +02:00
|
|
|
character.position = last_point.linear_interpolate(path[0], distance / distance_between_points)
|
2019-04-08 15:46:00 +02:00
|
|
|
return
|
2020-02-03 08:36:35 +01:00
|
|
|
# The position is past the end of the segment.
|
2018-03-27 08:06:22 +02:00
|
|
|
distance -= distance_between_points
|
|
|
|
last_point = path[0]
|
|
|
|
path.remove(0)
|
2020-02-03 08:36:35 +01:00
|
|
|
# The character reached the end of the path.
|
2020-06-28 09:29:20 +02:00
|
|
|
character.position = last_point
|
2019-04-08 15:46:00 +02:00
|
|
|
set_process(false)
|
2020-02-03 08:36:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
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)
|