From 098033bbbf71f3c3bd8f851d1d3360c168a375ae Mon Sep 17 00:00:00 2001 From: KingGurke Date: Sun, 1 Oct 2023 11:26:52 +0200 Subject: [PATCH] Added a (shrinking) finish line, and ensured maximum smoothness of the track at finish --- src/game/level.gd | 18 ++++++++++++++++++ src/game/level1.tscn | 11 +++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/game/level.gd b/src/game/level.gd index 8cdbf82..da41ae5 100644 --- a/src/game/level.gd +++ b/src/game/level.gd @@ -18,18 +18,33 @@ extends Node2D @export var line: Line2D ## reference to the player node @export var player: StaticBody2D +## reference to the line node used to draw the finish line +@export var finish: Line2D #variables var width var running = true var last_offset = 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. func _ready(): 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_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. @@ -39,6 +54,7 @@ func _process(delta): 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) @@ -68,6 +84,8 @@ func _process(delta): func update_line(): 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(): return lap_distance + last_offset diff --git a/src/game/level1.tscn b/src/game/level1.tscn index 4b5a7bc..d4a52a9 100644 --- a/src/game/level1.tscn +++ b/src/game/level1.tscn @@ -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="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://cd7bqj3v0k8yi" path="res://assets/finish.png" id="4_r6pvu"] [sub_resource type="Curve2D" id="Curve2D_ll8bj"] _data = { @@ -10,13 +11,14 @@ _data = { } 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") starting_width = 400.0 shrink_factor = 5.0 path = NodePath("TrackPath") line = NodePath("TrackLine") player = NodePath("player") +finish = NodePath("FinishLine") [node name="TrackPath" type="Path2D" parent="."] curve = SubResource("Curve2D_ll8bj") @@ -26,6 +28,11 @@ texture_repeat = 2 texture = ExtResource("2_knecs") 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")] position = Vector2(42.7545, -14.9219) brake_strength = 800.0