aboutsummaryrefslogtreecommitdiffstats
path: root/Scripts/scene_control.gd
blob: 268c92a7e67156898a23f03675d15eb489701481 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
extends Node

class_name SceneControl

@export var gold_medal_time = 5.0
@export var silver_medal_time = 7.0
@export var bronze_medal_time = 10.0

@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 drawing = $DrawNode
@onready var start = $Start

var is_driving = false
var current_time = 0.0

var game_control


func _ready():
	game_control = get_tree().root.get_child(0)
	
	$FinishLine/Area2D.body_entered.connect(_on_finish_line_body_entered)
	$UI/Control/PostGameUI/RestartButton.pressed.connect(reset_level)
	$UI/Control/PostGameUI/NextLevelButton.pressed.connect(next_level)
	
	var start_button = $Start/Start as TextureButton
	start_button.button_down.connect(start_drawing)
	
	reset_level()

func _physics_process(delta):
	if car.driving:
		current_time += delta
		ui.update_timer_label(current_time)


func get_path_direction(pos):
	var offset = path.curve.get_closest_offset(pos)
	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):
	print('control driving: ' , driving)
	car.driving = driving
	is_driving = driving

func reset_level():
	ui.update_timer_label(-1)
	ui.switch_to_in_game_UI()
	
	$DrawNode.reset()
	
	for cp in checkpoints:
		cp.set_was_visited(false)
	
	car.global_position = start.global_position
	car.global_rotation = start.rotation;
	car.velocity = Vector2.ZERO 
	
	current_time = 0.0
	
	set_driving(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 evaluate_time():
	if current_time < gold_medal_time:
		return 0
	if current_time < silver_medal_time:
		return 1
	if current_time < bronze_medal_time:
		return 2
	return 3


func _on_finish_line_body_entered(node: Node2D):
	if evaluate_driving():
		ui.switch_to_post_game_UI(evaluate_time())
		set_driving(false)
		return
	
	set_driving(false)
	reset_level()

func next_level():
	game_control.next_level()


func start_drawing():
	reset_level()
	drawing.start_drawing()