Added a live distance counter (in code, not yet UI), which also acts as checkpoints; when a "jump" in distance driven is detected it's game over (for now)

This commit is contained in:
KingGurke 2023-10-01 10:34:45 +02:00
parent fa2d82475b
commit 8c7870092f

View file

@ -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