aboutsummaryrefslogtreecommitdiffstats
path: root/Scripts
diff options
context:
space:
mode:
authorLibravatar perivesta <>2023-07-08 11:40:53 +0200
committerLibravatar perivesta <>2023-07-08 11:40:53 +0200
commit892ed4854bcccbac546f5ac32c0c5fa0245f5fa7 (patch)
tree6c3403e1734790c48a90741e62e0369301b1e06a /Scripts
parenta10d0e582a9a614b933f3e689592595e6438513e (diff)
download2023-892ed4854bcccbac546f5ac32c0c5fa0245f5fa7.tar.gz
2023-892ed4854bcccbac546f5ac32c0c5fa0245f5fa7.tar.bz2
2023-892ed4854bcccbac546f5ac32c0c5fa0245f5fa7.zip
draw a path for car to follow
Diffstat (limited to 'Scripts')
-rw-r--r--Scripts/car_behaviour.gd12
-rw-r--r--Scripts/drawing.gd16
-rw-r--r--Scripts/scene_control.gd17
3 files changed, 27 insertions, 18 deletions
diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd
index b71ebb6..afbc32f 100644
--- a/Scripts/car_behaviour.gd
+++ b/Scripts/car_behaviour.gd
@@ -5,6 +5,7 @@ extends StaticBody2D
@export var steer_force = 0.1
@export var look_ahead = 75
@export var num_rays = 32
+@export var driving = false
# context array
var ray_directions = []
@@ -28,6 +29,9 @@ func _ready():
func _physics_process(delta):
+ if not driving:
+ return
+
set_interest()
set_danger()
choose_direction()
@@ -38,8 +42,12 @@ func _physics_process(delta):
func set_interest():
# Set interest in each slot based on world direction
- if owner and owner.has_method("get_path_direction"):
- var path_direction = owner.get_path_direction(position)
+# if owner and owner.has_method("get_path_direction"):
+ if owner and owner.has_method("get_path_next_position"):
+# var path_direction = owner.get_path_direction(position)
+ var next_pos = owner.get_path_next_position(position)
+ var path_direction = (next_pos - position).normalized()
+
for i in num_rays:
var d = ray_directions[i].rotated(rotation).dot(path_direction)
interest[i] = max(0, d)
diff --git a/Scripts/drawing.gd b/Scripts/drawing.gd
index d47e161..2e4af95 100644
--- a/Scripts/drawing.gd
+++ b/Scripts/drawing.gd
@@ -1,24 +1,18 @@
extends Node
var drawing = false
-var driving_progress = -1
-var line: Line2D
-var path: Path2D
+@onready var line: Line2D = $TrackLine
+@onready var path: Path2D = $TrackPath
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)
+ pass
func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
@@ -27,10 +21,10 @@ func _input(event):
# start a new drawing
line.clear_points()
path.curve.clear_points()
- driving_progress = -1
+ owner.set_driving(false)
else:
# start driving
- driving_progress = 0
+ owner.set_driving(true)
if event is InputEventMouseMotion and drawing:
# extend the line
diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd
index 9f231e0..fdaad14 100644
--- a/Scripts/scene_control.gd
+++ b/Scripts/scene_control.gd
@@ -2,12 +2,19 @@ extends Node
class_name SceneControl
-@onready var path : Path2D = $Path2D
-
-@on#ready var path_follow : PathFollow2D = $Path2D/PathFollow2D
-
+@onready var path : Path2D = $DrawNode/TrackPath
+@onready var path_follow : PathFollow2D = $DrawNode/TrackPath/TrackFollower
+@onready var car : StaticBody2D = $Car
func get_path_direction(pos):
var offset = path.curve.get_closest_offset(pos)
- path_follow.h_offset = offset
+ path_follow.progress = offset
return path_follow.transform.x
+
+func get_path_next_position(pos):
+ var offset = path.curve.get_closest_offset(pos)
+ path_follow.progress = offset + 50
+ return path_follow.position
+
+func set_driving(driving: bool):
+ car.driving = driving