From 2236b0c2050f98aa11c6c631ee6ddc9dd7e57df0 Mon Sep 17 00:00:00 2001 From: perivesta <> Date: Fri, 7 Jul 2023 23:26:08 +0200 Subject: follow the drawn path --- Nodes/drawing.tscn | 13 ++++++++++++- Scripts/drawing.gd | 24 ++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Nodes/drawing.tscn b/Nodes/drawing.tscn index 4c04ea7..3f65fee 100644 --- a/Nodes/drawing.tscn +++ b/Nodes/drawing.tscn @@ -1,8 +1,19 @@ -[gd_scene load_steps=2 format=3 uid="uid://covygojlfmx6x"] +[gd_scene load_steps=4 format=3 uid="uid://covygojlfmx6x"] [ext_resource type="Script" path="res://Scripts/drawing.gd" id="1_03nmp"] +[ext_resource type="Texture2D" uid="uid://bpasf8is2xnfb" path="res://icon.svg" id="2_sgpkt"] + +[sub_resource type="Curve2D" id="Curve2D_dtc4h"] [node name="Drawing" type="Node2D"] script = ExtResource("1_03nmp") [node name="TrackLine" type="Line2D" parent="."] + +[node name="TrackPath" type="Path2D" parent="."] +curve = SubResource("Curve2D_dtc4h") + +[node name="TrackFollower" type="PathFollow2D" parent="TrackPath"] + +[node name="Sprite2D" type="Sprite2D" parent="TrackPath/TrackFollower"] +texture = ExtResource("2_sgpkt") diff --git a/Scripts/drawing.gd b/Scripts/drawing.gd index 34129ac..d47e161 100644 --- a/Scripts/drawing.gd +++ b/Scripts/drawing.gd @@ -1,18 +1,38 @@ extends Node var drawing = false +var driving_progress = -1 var line: Line2D +var path: Path2D func _ready(): line = $TrackLine + path = $TrackPath Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) +func _process(delta): + pass + +func _physics_process(delta): + if driving_progress >= 0: + driving_progress += delta*200 + $TrackPath/TrackFollower.progress = driving_progress + #print(driving_progress) + func _input(event): if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT: - # start/stop drawing drawing = event.pressed - + if drawing: + # start a new drawing + line.clear_points() + path.curve.clear_points() + driving_progress = -1 + else: + # start driving + driving_progress = 0 + if event is InputEventMouseMotion and drawing: # extend the line line.add_point(event.position) + path.curve.add_point(event.position) -- cgit