From 98d3be13f82c3a64f3389fed8935b3dd9af2823d Mon Sep 17 00:00:00 2001 From: Nathan Lovato Date: Fri, 7 Feb 2020 09:08:45 -0600 Subject: [PATCH] Simplify code in the FollowPath demo Use draw_polyline to draw the path Renamed some variables --- project/demos/FollowPath/Drawer.gd | 15 ++++++--------- project/demos/FollowPath/PathFollower.gd | 7 ++++--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/project/demos/FollowPath/Drawer.gd b/project/demos/FollowPath/Drawer.gd index 0f8e912..e4a6d34 100644 --- a/project/demos/FollowPath/Drawer.gd +++ b/project/demos/FollowPath/Drawer.gd @@ -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: diff --git a/project/demos/FollowPath/PathFollower.gd b/project/demos/FollowPath/PathFollower.gd index d9b54cb..1777ea1 100644 --- a/project/demos/FollowPath/PathFollower.gd +++ b/project/demos/FollowPath/PathFollower.gd @@ -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