aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Nodes/car.tscn17
-rw-r--r--Nodes/simple_wall.tscn16
-rw-r--r--Scripts/car_behaviour.gd78
-rw-r--r--Scripts/scene_control.gd12
-rw-r--r--project.godot9
-rw-r--r--root_scene.tscn63
6 files changed, 195 insertions, 0 deletions
diff --git a/Nodes/car.tscn b/Nodes/car.tscn
new file mode 100644
index 0000000..25505a0
--- /dev/null
+++ b/Nodes/car.tscn
@@ -0,0 +1,17 @@
+[gd_scene load_steps=4 format=3 uid="uid://c1mvbh2fwqtkb"]
+
+[ext_resource type="Script" path="res://Scripts/car_behaviour.gd" id="1_0e4ux"]
+
+[sub_resource type="RectangleShape2D" id="RectangleShape2D_wvlyu"]
+
+[sub_resource type="CanvasTexture" id="CanvasTexture_aacgf"]
+
+[node name="Car" type="StaticBody2D"]
+script = ExtResource("1_0e4ux")
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
+shape = SubResource("RectangleShape2D_wvlyu")
+
+[node name="Sprite2D" type="Sprite2D" parent="."]
+scale = Vector2(19.92, 19.76)
+texture = SubResource("CanvasTexture_aacgf")
diff --git a/Nodes/simple_wall.tscn b/Nodes/simple_wall.tscn
new file mode 100644
index 0000000..21d714a
--- /dev/null
+++ b/Nodes/simple_wall.tscn
@@ -0,0 +1,16 @@
+[gd_scene load_steps=3 format=3 uid="uid://cqglbcitm2wlb"]
+
+[sub_resource type="RectangleShape2D" id="RectangleShape2D_omkyo"]
+
+[sub_resource type="CanvasTexture" id="CanvasTexture_3s223"]
+
+[node name="SimpleWall" type="StaticBody2D"]
+position = Vector2(0, 27)
+scale = Vector2(15.48, 1)
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
+shape = SubResource("RectangleShape2D_omkyo")
+
+[node name="Sprite2D" type="Sprite2D" parent="."]
+scale = Vector2(20, 19.96)
+texture = SubResource("CanvasTexture_3s223")
diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd
new file mode 100644
index 0000000..dfc9ea4
--- /dev/null
+++ b/Scripts/car_behaviour.gd
@@ -0,0 +1,78 @@
+extends StaticBody2D
+
+# editor variables
+@export var max_speed = 350
+@export var steer_force = 0.1
+@export var look_ahead = 100
+@export var num_rays = 8
+
+# context array
+var ray_directions = []
+var interest = []
+var danger = []
+
+var chosen_dir = Vector2.ZERO
+var velocity = Vector2.ZERO
+var acceleration = Vector2.ZERO
+
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+ interest.resize(num_rays)
+ danger.resize(num_rays)
+ ray_directions.resize(num_rays)
+ for i in num_rays:
+ var angle = i * 2 * PI / num_rays
+ ray_directions[i] = Vector2.RIGHT.rotated(angle)
+ print(ray_directions)
+
+
+func _physics_process(delta):
+ set_interest()
+ set_danger()
+ choose_direction()
+ var desired_velocity = chosen_dir.rotated(rotation) * max_speed
+ velocity = velocity.lerp(desired_velocity, steer_force)
+ rotation = velocity.angle()
+ move_and_collide(velocity * 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)
+ for i in num_rays:
+ var d = ray_directions[i].rotated(rotation).dot(path_direction)
+ interest[i] = max(0, d)
+ # If no world path, use default interest
+ else:
+ set_default_interest()
+
+func set_default_interest():
+ # Default to moving forward
+ for i in num_rays:
+ var d = ray_directions[i].rotated(rotation).dot(transform.x)
+ interest[i] = max(0, d)
+
+func set_danger():
+ # Cast rays to find danger directions
+ var space_state = get_world_2d().direct_space_state
+
+ var params = PhysicsRayQueryParameters2D.new()
+ params.from = position
+ params.exclude = [self]
+
+ for i in num_rays:
+ params.to = position + ray_directions[i].rotated(rotation) * look_ahead
+ var result = space_state.intersect_ray(params)
+ danger[i] = 1.0 if result else 0.0
+
+func choose_direction():
+ # Eliminate interest in slots with danger
+ for i in num_rays:
+ if danger[i] > 0.0:
+ interest[i] = 0.0
+ # Choose direction based on remaining interest
+ chosen_dir = Vector2.ZERO
+ for i in num_rays:
+ chosen_dir += ray_directions[i] * interest[i]
+ chosen_dir = chosen_dir.normalized()
diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd
new file mode 100644
index 0000000..99bc3a2
--- /dev/null
+++ b/Scripts/scene_control.gd
@@ -0,0 +1,12 @@
+extends Node
+
+class_name SceneControl
+
+@onready var path : Path2D = $Path2D
+@onready var path_follow : PathFollow2D = $Path2D/PathFollow2D
+
+
+func get_path_direction(pos):
+ var offset = path.curve.get_closest_offset(pos)
+ path_follow.h_offset = offset
+ return path_follow.transform.x
diff --git a/project.godot b/project.godot
index f23358f..703acfd 100644
--- a/project.godot
+++ b/project.godot
@@ -11,5 +11,14 @@ config_version=5
[application]
config/name="CodeWithYourFriends23"
+run/main_scene="res://root_scene.tscn"
config/features=PackedStringArray("4.0", "Forward Plus")
config/icon="res://icon.svg"
+
+[dotnet]
+
+project/assembly_name="CodeWithYourFriends23"
+
+[rendering]
+
+renderer/rendering_method="gl_compatibility"
diff --git a/root_scene.tscn b/root_scene.tscn
new file mode 100644
index 0000000..58d6e57
--- /dev/null
+++ b/root_scene.tscn
@@ -0,0 +1,63 @@
+[gd_scene load_steps=5 format=3 uid="uid://b205qlgjrpeqi"]
+
+[ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_eopk0"]
+[ext_resource type="PackedScene" uid="uid://cqglbcitm2wlb" path="res://Nodes/simple_wall.tscn" id="1_o5f4t"]
+[ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="2_22f5l"]
+
+[sub_resource type="Curve2D" id="Curve2D_rg63l"]
+bake_interval = 10.0
+_data = {
+"points": PackedVector2Array(0, 0, 0, 0, 495, 54, 0, 0, 0, 0, 567, 56, -17.0089, 38.4276, 17.0089, -38.4276, 627, 32, -18.8988, 13.2292, 18.8988, -13.2292, 648, -102, -31.498, -18.8988, 31.498, 18.8988, 718, -102, 8.18949, -25.8284, -8.18949, 25.8284, 720, 263, 26.4583, 13.2292, -26.4583, -13.2292, 639, 286, 46.6171, 22.6786, -46.6171, -22.6786, 500, 170, 0, 0, 0, 0, 369, 136, -3.1498, 47.247, 3.1498, -47.247, 312, 90, 0, 0, 0, 0, 368, 53, 0, 0, 0, 0, 495, 54)
+}
+point_count = 12
+
+[node name="Node2D" type="Node2D"]
+position = Vector2(0, 1)
+script = ExtResource("1_eopk0")
+
+[node name="SimpleWall" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(520, 240)
+
+[node name="SimpleWall10" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(645, -4)
+
+[node name="SimpleWall8" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(427, 139)
+
+[node name="SimpleWall7" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(236, 321)
+
+[node name="SimpleWall6" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(494, 417)
+rotation = 0.716721
+
+[node name="SimpleWall4" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(618, 493)
+
+[node name="SimpleWall2" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(680, 235)
+scale = Vector2(1.28, 16.6)
+
+[node name="SimpleWall3" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(762, 238)
+scale = Vector2(1.28, 27.28)
+
+[node name="SimpleWall9" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(581, -122)
+scale = Vector2(1.28, 27.28)
+
+[node name="SimpleWall5" parent="." instance=ExtResource("1_o5f4t")]
+position = Vector2(273, 258)
+scale = Vector2(1.28, 27.28)
+
+[node name="Path2D" type="Path2D" parent="."]
+position = Vector2(0, 147)
+curve = SubResource("Curve2D_rg63l")
+
+[node name="PathFollow2D" type="PathFollow2D" parent="Path2D"]
+position = Vector2(495, 54)
+rotation = 0.0277706
+
+[node name="Car" parent="." instance=ExtResource("2_22f5l")]
+position = Vector2(390, 202)
+max_speed = 100