diff --git a/src/game/level.gd b/src/game/level.gd index 91bdb7e..8cdbf82 100644 --- a/src/game/level.gd +++ b/src/game/level.gd @@ -6,6 +6,10 @@ extends Node2D @export var shrink_factor := .1 ## the diefference between the track's width and the maximum distance of the car from the center of the track. Should be about half the width of the car @export var max_distance_offset := .5 +## minimum distance traversed in a single frame to be counted as cheese +@export var cheese_distance := 500 +## margin for error when checking for laps being driven +@export var lap_count_margin := 50 #references ## reference to the path describing the track @@ -18,6 +22,8 @@ extends Node2D #variables var width var running = true +var last_offset = 0. +var lap_distance = 0. # Called when the node enters the scene tree for the first time. func _ready(): @@ -29,20 +35,43 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): if (running): + # shrinking the track width -= shrink_factor * delta update_line() + # checking whether the player is still on the track var p_pos = player.position var closest = path.curve.get_closest_point(p_pos) var p_dist = (p_pos - closest).length() - print_debug(" distance from track center: ", p_dist, " current track width: ", width) +# print_debug(" distance from track center: ", p_dist, " current track width: ", width) if p_dist > width/2 - max_distance_offset: game_over() + + # counting the distance driven + var offset = path.curve.get_closest_offset(p_pos) + var off_diff = offset - last_offset + if off_diff > 0: + if off_diff > path.curve.get_baked_length() - lap_count_margin && off_diff < path.curve.get_baked_length() + lap_count_margin: + print_debug("subtracting a lap") + lap_distance -= path.curve.get_baked_length() + else: if off_diff > cheese_distance: + print_debug("cheese detected") + game_over() + last_offset = offset + if off_diff < 0: + if -off_diff < path.curve.get_baked_length() + lap_count_margin && -off_diff > path.curve.get_baked_length() - lap_count_margin: + print_debug("adding a lap") + lap_distance += path.curve.get_baked_length() + last_offset = offset +# print_debug("player distance driven: ", get_total_distance(), " distance since last check: ", off_diff) func update_line(): line.set_width(width) +func get_total_distance(): + return lap_distance + last_offset func game_over(): + print_debug("final score: ", get_total_distance()) get_tree().reload_current_scene() #temp