aboutsummaryrefslogtreecommitdiffstats
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
parenta10d0e582a9a614b933f3e689592595e6438513e (diff)
download2023-892ed4854bcccbac546f5ac32c0c5fa0245f5fa7.tar.gz
2023-892ed4854bcccbac546f5ac32c0c5fa0245f5fa7.tar.bz2
2023-892ed4854bcccbac546f5ac32c0c5fa0245f5fa7.zip
draw a path for car to follow
-rw-r--r--Nodes/Level_test.tscn4
-rw-r--r--Nodes/drawing.tscn2
-rw-r--r--Nodes/drawing_and_driving.tscn26
-rw-r--r--Scripts/car_behaviour.gd12
-rw-r--r--Scripts/drawing.gd16
-rw-r--r--Scripts/scene_control.gd17
-rw-r--r--project.godot2
7 files changed, 56 insertions, 23 deletions
diff --git a/Nodes/Level_test.tscn b/Nodes/Level_test.tscn
index b3be870..4fa88fe 100644
--- a/Nodes/Level_test.tscn
+++ b/Nodes/Level_test.tscn
@@ -1,9 +1,9 @@
-[gd_scene load_steps=5 format=3 uid="uid://dt5grvd5n2ioj"]
+[gd_scene load_steps=5 format=3 uid="uid://ct2kxw1rrjryo"]
[ext_resource type="Texture2D" uid="uid://5xqahgawme4m" path="res://Assets/start.png" id="1_wenkl"]
[ext_resource type="Texture2D" uid="uid://b8vhe5kt6037n" path="res://Assets/end.png" id="2_gp6ng"]
[ext_resource type="Texture2D" uid="uid://dg3v488vx0oj6" path="res://Assets/checkpoint.png" id="3_cgigr"]
-[ext_resource type="PackedScene" uid="uid://covygojlfmx6x" path="res://Nodes/drawing.tscn" id="4_t4rjk"]
+[ext_resource type="PackedScene" uid="uid://cfolt02ucvo3d" path="res://Nodes/drawing.tscn" id="4_t4rjk"]
[node name="Level_test" type="Node2D"]
diff --git a/Nodes/drawing.tscn b/Nodes/drawing.tscn
index 3f65fee..1057f09 100644
--- a/Nodes/drawing.tscn
+++ b/Nodes/drawing.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=4 format=3 uid="uid://covygojlfmx6x"]
+[gd_scene load_steps=4 format=3 uid="uid://cfolt02ucvo3d"]
[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"]
diff --git a/Nodes/drawing_and_driving.tscn b/Nodes/drawing_and_driving.tscn
index 9e2a36a..32ef3e1 100644
--- a/Nodes/drawing_and_driving.tscn
+++ b/Nodes/drawing_and_driving.tscn
@@ -1,3 +1,27 @@
-[gd_scene format=3 uid="uid://bj5wruvb2hg20"]
+[gd_scene load_steps=6 format=3 uid="uid://bj5wruvb2hg20"]
+
+[ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_6bj13"]
+[ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="1_ktvsb"]
+[ext_resource type="Script" path="res://Scripts/drawing.gd" id="3_el555"]
+[ext_resource type="Texture2D" uid="uid://dg3v488vx0oj6" path="res://Assets/checkpoint.png" id="4_ar5ae"]
+
+[sub_resource type="Curve2D" id="Curve2D_wmfhm"]
[node name="drawing_and_driving" type="Node2D"]
+script = ExtResource("1_6bj13")
+
+[node name="Car" parent="." instance=ExtResource("1_ktvsb")]
+
+[node name="DrawNode" type="Node2D" parent="."]
+script = ExtResource("3_el555")
+
+[node name="TrackLine" type="Line2D" parent="DrawNode"]
+
+[node name="TrackPath" type="Path2D" parent="DrawNode"]
+curve = SubResource("Curve2D_wmfhm")
+
+[node name="TrackFollower" type="PathFollow2D" parent="DrawNode/TrackPath"]
+
+[node name="Sprite2D" type="Sprite2D" parent="DrawNode/TrackPath/TrackFollower"]
+scale = Vector2(0.604, 0.604)
+texture = ExtResource("4_ar5ae")
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
diff --git a/project.godot b/project.godot
index 04de13a..46c0223 100644
--- a/project.godot
+++ b/project.godot
@@ -11,7 +11,7 @@ config_version=5
[application]
config/name="CodeWithYourFriends23"
-run/main_scene="res://Nodes/Level_test.tscn"
+run/main_scene="res://Nodes/drawing_and_driving.tscn"
config/features=PackedStringArray("4.0", "Forward Plus")
config/icon="res://icon.svg"