Simplify code in the FollowPath demo

Use draw_polyline to draw the path
Renamed some variables
This commit is contained in:
Nathan Lovato 2020-02-07 09:08:45 -06:00
parent 50a141bbcf
commit 98d3be13f8
2 changed files with 10 additions and 12 deletions

View File

@ -5,39 +5,36 @@ signal path_established(points)
var active_points := []
var drawing := false
var is_drawing := false
var distance_threshold := 100.0
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
if drawing:
if is_drawing:
active_points.append(event.position)
update()
elif event is InputEventMouseButton:
if event.pressed and event.button_index == BUTTON_LEFT:
active_points.clear()
active_points.append(event.position)
drawing = true
is_drawing = true
update()
elif not event.pressed:
drawing = false
is_drawing = false
if active_points.size() >= 2:
_simplify()
func _draw() -> void:
if drawing:
if is_drawing:
for point in active_points:
draw_circle(point, 1, Color.red)
else:
if active_points.size() > 0:
draw_circle(active_points.front(), 2, Color.red)
draw_circle(active_points.back(), 2, Color.yellow)
for i in range(1, active_points.size()):
var start: Vector2 = active_points[i-1]
var end: Vector2 = active_points[i]
draw_line(start, end, Color.skyblue)
draw_polyline(active_points, Color.skyblue, 1.0)
func _simplify() -> void:

View File

@ -6,6 +6,7 @@ var _accel := GSTTargetAcceleration.new()
var _valid := false
var _drag := 0.1
onready var agent := GSTKinematicBody2DAgent.new(self)
onready var path := GSTPath.new([
Vector3(global_position.x, global_position.y, 0),
@ -40,8 +41,8 @@ func _physics_process(delta: float) -> void:
func _on_Drawer_path_established(points: Array) -> void:
var points3 := []
var positions := PoolVector3Array()
for p in points:
points3.append(Vector3(p.x, p.y, 0))
path.create_path(points3)
positions.append(Vector3(p.x, p.y, 0))
path.create_path(positions)
_valid = true