aboutsummaryrefslogtreecommitdiffstats
path: root/Scripts
diff options
context:
space:
mode:
authorLibravatar perivesta <>2023-07-08 16:45:32 +0200
committerLibravatar perivesta <>2023-07-08 16:45:32 +0200
commit8b08fec313ac3517e782d946cbd203958801e6be (patch)
treecab276d81c92fd0ff8d75694c4c252812b4b827c /Scripts
parentd98cee8592e15f23ce85e8d54abb29aa341c97da (diff)
parent78b6db9f792e18d848383856693cff4ef223a10f (diff)
download2023-8b08fec313ac3517e782d946cbd203958801e6be.tar.gz
2023-8b08fec313ac3517e782d946cbd203958801e6be.tar.bz2
2023-8b08fec313ac3517e782d946cbd203958801e6be.zip
Merge branch 'game-control'
Diffstat (limited to '')
-rw-r--r--Scripts/Checkpoint.gd14
-rw-r--r--Scripts/UI_Control.gd27
-rw-r--r--Scripts/car_behaviour.gd4
-rw-r--r--Scripts/scene_control.gd52
4 files changed, 91 insertions, 6 deletions
diff --git a/Scripts/Checkpoint.gd b/Scripts/Checkpoint.gd
new file mode 100644
index 0000000..ed10f74
--- /dev/null
+++ b/Scripts/Checkpoint.gd
@@ -0,0 +1,14 @@
+extends Node2D
+
+
+var was_visited = false
+
+@onready var sprite : Sprite2D = $Sprite2D
+
+
+func set_was_visited(visited: bool):
+ was_visited = visited
+ # TODO change the sprite to indicate the change
+
+func _on_area_2d_body_entered(body):
+ set_was_visited(true)
diff --git a/Scripts/UI_Control.gd b/Scripts/UI_Control.gd
new file mode 100644
index 0000000..b7ce2a5
--- /dev/null
+++ b/Scripts/UI_Control.gd
@@ -0,0 +1,27 @@
+extends Control
+
+@onready var in_game_ui = $InGameUI
+@onready var post_game_ui = $PostGameUI
+
+@onready var time_label = $InGameUI/CurrentTime
+
+
+func update_timer_label(new_time: float):
+ if(new_time < 0):
+ time_label.text = "Waiting for player to start driving..."
+ return
+ time_label.text = str(new_time).pad_decimals(3)
+
+func switch_to_post_game_UI():
+ in_game_ui.visible = false
+ post_game_ui.visible = true
+ $PostGameUI/FinalTime.text = time_label.text
+
+func switch_to_in_game_UI():
+ in_game_ui.visible = true
+ post_game_ui.visible = false
+
+func _on_start_button_pressed():
+ var root = owner.owner
+ if root and root.has_method("start_driving"):
+ root.start_driving()
diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd
index 03546d3..2ef9fb9 100644
--- a/Scripts/car_behaviour.gd
+++ b/Scripts/car_behaviour.gd
@@ -1,4 +1,4 @@
-extends StaticBody2D
+extends CharacterBody2D
# editor variables
@export var max_speed = 300
@@ -14,7 +14,6 @@ var interest = []
var danger = []
var chosen_dir = Vector2.ZERO
-var velocity = Vector2.ZERO
var current_speed = 0
@@ -26,7 +25,6 @@ func _ready():
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):
if not driving:
diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd
index fdaad14..0b55d0f 100644
--- a/Scripts/scene_control.gd
+++ b/Scripts/scene_control.gd
@@ -2,9 +2,28 @@ extends Node
class_name SceneControl
-@onready var path : Path2D = $DrawNode/TrackPath
-@onready var path_follow : PathFollow2D = $DrawNode/TrackPath/TrackFollower
-@onready var car : StaticBody2D = $Car
+@onready var path: Path2D = $DrawNode/TrackPath
+@onready var path_follow: PathFollow2D = $DrawNode/TrackPath/TrackFollower
+@onready var car = $Car
+@onready var checkpoints = $Checkpoints.get_children()
+@onready var ui = $UI/Control
+@onready var finish_line: Area2D
+
+var is_driving = false
+var current_time = 0.0
+
+
+func _ready():
+ finish_line = $FinishLine as Area2D
+ finish_line.body_entered.connect(_on_finish_line_body_entered)
+ reset_level()
+
+func _physics_process(delta):
+ if car.driving:
+ current_time += delta
+ print(current_time)
+ ui.update_timer_label(current_time)
+
func get_path_direction(pos):
var offset = path.curve.get_closest_offset(pos)
@@ -18,3 +37,30 @@ func get_path_next_position(pos):
func set_driving(driving: bool):
car.driving = driving
+ is_driving = driving
+
+func reset_level():
+ ui.update_timer_label(-1)
+ for cp in checkpoints:
+ cp.set_was_visited(false)
+
+func start_driving():
+ current_time = 0.0
+ set_driving(true)
+
+func evaluate_driving():
+ for cp in checkpoints:
+ if not cp.was_visited:
+ return false
+ return true
+
+
+func _on_finish_line_body_entered(node: Node2D):
+ if evaluate_driving():
+ ui.switch_to_post_game_UI()
+
+ set_driving(false)
+ reset_level()
+ # TODO get this from the start position
+ car.global_position = Vector2.ZERO
+ car.global_rotation = 0;