Added a (shrinking) finish line, and ensured maximum smoothness of the track at finish

This commit is contained in:
KingGurke 2023-10-01 11:26:52 +02:00
parent 8c7870092f
commit 098033bbbf
2 changed files with 27 additions and 2 deletions

View file

@ -18,18 +18,33 @@ extends Node2D
@export var line: Line2D @export var line: Line2D
## reference to the player node ## reference to the player node
@export var player: StaticBody2D @export var player: StaticBody2D
## reference to the line node used to draw the finish line
@export var finish: Line2D
#variables #variables
var width var width
var running = true var running = true
var last_offset = 0. var last_offset = 0.
var lap_distance = 0. var lap_distance = 0.
var start
var finish_right
var finish_left
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
width = starting_width width = starting_width
var start_out = path.curve.get_point_out(0)
path.curve.set_point_in(path.curve.point_count - 1, path.curve.get_point_in(0))
path.curve.set_point_out(path.curve.point_count - 1, start_out)
line.set_width(width) line.set_width(width)
line.set_points(path.curve.get_baked_points()) line.set_points(path.curve.get_baked_points())
start = path.curve.get_point_position(0)
finish_right = start_out.normalized().rotated(deg_to_rad(90)) * width/2
finish_left = start_out.normalized().rotated(deg_to_rad(-90)) * width/2
finish.add_point(start + finish_right)
finish.add_point(start + finish_left)
# print_debug("start: ", start, "start out: ", start_out.normalized(), " finish left: ", start + start_out.normalized().rotated(90) * width/2, "start right: ", start + start_out.normalized().rotated(-90) * width/2)
finish.width = 20
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
@ -39,6 +54,7 @@ func _process(delta):
width -= shrink_factor * delta width -= shrink_factor * delta
update_line() update_line()
# checking whether the player is still on the track # checking whether the player is still on the track
var p_pos = player.position var p_pos = player.position
var closest = path.curve.get_closest_point(p_pos) var closest = path.curve.get_closest_point(p_pos)
@ -68,6 +84,8 @@ func _process(delta):
func update_line(): func update_line():
line.set_width(width) line.set_width(width)
finish.points[0] = start + finish_right.normalized() * width/2
finish.points[1] = start + finish_left.normalized() * width/2
func get_total_distance(): func get_total_distance():
return lap_distance + last_offset return lap_distance + last_offset

View file

@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=3 uid="uid://c77xk0mywwt5f"] [gd_scene load_steps=6 format=3 uid="uid://c77xk0mywwt5f"]
[ext_resource type="Script" path="res://src/game/level.gd" id="1_hv75i"] [ext_resource type="Script" path="res://src/game/level.gd" id="1_hv75i"]
[ext_resource type="PackedScene" uid="uid://ckau5s2tsb3oc" path="res://src/game/player.tscn" id="2_5nf51"] [ext_resource type="PackedScene" uid="uid://ckau5s2tsb3oc" path="res://src/game/player.tscn" id="2_5nf51"]
[ext_resource type="Texture2D" uid="uid://bd0jqkgq1gh7p" path="res://assets/track.png" id="2_knecs"] [ext_resource type="Texture2D" uid="uid://bd0jqkgq1gh7p" path="res://assets/track.png" id="2_knecs"]
[ext_resource type="Texture2D" uid="uid://cd7bqj3v0k8yi" path="res://assets/finish.png" id="4_r6pvu"]
[sub_resource type="Curve2D" id="Curve2D_ll8bj"] [sub_resource type="Curve2D" id="Curve2D_ll8bj"]
_data = { _data = {
@ -10,13 +11,14 @@ _data = {
} }
point_count = 7 point_count = 7
[node name="level1" type="Node2D" node_paths=PackedStringArray("path", "line", "player")] [node name="level1" type="Node2D" node_paths=PackedStringArray("path", "line", "player", "finish")]
script = ExtResource("1_hv75i") script = ExtResource("1_hv75i")
starting_width = 400.0 starting_width = 400.0
shrink_factor = 5.0 shrink_factor = 5.0
path = NodePath("TrackPath") path = NodePath("TrackPath")
line = NodePath("TrackLine") line = NodePath("TrackLine")
player = NodePath("player") player = NodePath("player")
finish = NodePath("FinishLine")
[node name="TrackPath" type="Path2D" parent="."] [node name="TrackPath" type="Path2D" parent="."]
curve = SubResource("Curve2D_ll8bj") curve = SubResource("Curve2D_ll8bj")
@ -26,6 +28,11 @@ texture_repeat = 2
texture = ExtResource("2_knecs") texture = ExtResource("2_knecs")
texture_mode = 2 texture_mode = 2
[node name="FinishLine" type="Line2D" parent="."]
texture_repeat = 2
texture = ExtResource("4_r6pvu")
texture_mode = 1
[node name="player" parent="." instance=ExtResource("2_5nf51")] [node name="player" parent="." instance=ExtResource("2_5nf51")]
position = Vector2(42.7545, -14.9219) position = Vector2(42.7545, -14.9219)
brake_strength = 800.0 brake_strength = 800.0