add tire trails

This commit is contained in:
interfisch 2024-11-11 17:34:23 +01:00
parent e7ca0c8c64
commit 27effe4a70
6 changed files with 91 additions and 48 deletions

View file

@ -1,10 +1,10 @@
[gd_scene load_steps=48 format=3 uid="uid://0g7qqh7naniv"]
[gd_scene load_steps=49 format=3 uid="uid://0g7qqh7naniv"]
[ext_resource type="Script" path="res://scripts/car_node.gd" id="1_0tin3"]
[ext_resource type="Script" path="res://scripts/car.gd" id="1_i5tet"]
[ext_resource type="Texture2D" uid="uid://mqdujngircok" path="res://sprites/car_features.png" id="3_ts6mm"]
[ext_resource type="Texture2D" uid="uid://e5aeyl47wi8p" path="res://sprites/car_body.png" id="4_lps13"]
[ext_resource type="PackedScene" uid="uid://c3fdxjrpcgnnp" path="res://scenes/tire_trail.tscn" id="5_2bgdv"]
[ext_resource type="Script" path="res://scripts/tiretrails.gd" id="5_4geqi"]
[ext_resource type="Script" path="res://scripts/label_round.gd" id="5_vheit"]
[ext_resource type="PackedScene" uid="uid://dl7r8s5sxyvlw" path="res://scenes/enginesound.tscn" id="6_v21se"]
[ext_resource type="AudioStream" uid="uid://dq5rei2w1isu4" path="res://sounds/crash/Car_Crash_01.mp3" id="7_7erwn"]
@ -45,6 +45,10 @@
radius = 8.0
height = 34.0
[sub_resource type="Curve" id="Curve_qs34t"]
_data = [Vector2(0, 0), 0.0, 5.0, 0, 0, Vector2(0.5, 1), 0.0, 0.0, 0, 0, Vector2(1, 0), -5.0, 0.0, 0, 0]
point_count = 3
[sub_resource type="Animation" id="Animation_3soig"]
length = 0.001
tracks/0/type = "value"
@ -151,13 +155,14 @@ shape = SubResource("CapsuleShape2D_bj1hp")
[node name="PositionFrontLeftTire" type="Node2D" parent="CharacterBody_Car"]
position = Vector2(10, -7)
[node name="Trail" parent="CharacterBody_Car/PositionFrontLeftTire" instance=ExtResource("5_2bgdv")]
script = ExtResource("5_4geqi")
[node name="PositionFrontRightTire" type="Node2D" parent="CharacterBody_Car"]
position = Vector2(10, 7)
script = ExtResource("5_4geqi")
[node name="Trail" parent="CharacterBody_Car/PositionFrontRightTire" instance=ExtResource("5_2bgdv")]
[node name="Line2D" type="Line2D" parent="CharacterBody_Car"]
width_curve = SubResource("Curve_qs34t")
[node name="RayCast_FL" type="RayCast2D" parent="CharacterBody_Car"]
target_position = Vector2(256, -128)

View file

@ -1,14 +1,38 @@
[gd_scene load_steps=3 format=3 uid="uid://c3fdxjrpcgnnp"]
[ext_resource type="Script" path="res://scripts/tiretrail.gd" id="1_cvwoe"]
[sub_resource type="Curve" id="Curve_ckg0w"]
_data = [Vector2(0, 0), 0.0, 3.77363, 0, 0, Vector2(1, 0), -4.04783, 0.0, 0, 0]
point_count = 2
[sub_resource type="GDScript" id="GDScript_6pryv"]
script/source = "extends Line2D
var point
var isSliding=false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
set_as_top_level(true)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
pass
func _process(delta: float) -> void:
if isSliding:
point = get_parent().global_position
add_point(point)
if points.size()>100:
remove_point(0)
func setSliding(psliding:bool):
#if not isSliding and psliding: #just started sliding
#clear_points()
isSliding=psliding
"
[node name="Trail" type="Line2D"]
z_index = 9
width = 4.0
width_curve = SubResource("Curve_ckg0w")
default_color = Color(0, 0, 0, 1)
script = ExtResource("1_cvwoe")
script = SubResource("GDScript_6pryv")

View file

@ -219,13 +219,13 @@ func calculate_steering(delta:float):
if d > 0:
velocity = velocity.lerp(new_heading * velocity.length(), traction)
if d<0.82 and velocity.length()>slip_speed and applied_engine_power>engine_power/2:
if d<0.85 and velocity.length()>slip_speed and applied_engine_power>engine_power/2:
sfx.sliding(velocity.length())
$PositionFrontLeftTire/Trail.setSliding(true)
$PositionFrontRightTire/Trail.setSliding(true)
$PositionFrontLeftTire.setSliding(true)
$PositionFrontRightTire.setSliding(true)
elif d>0.98: #keep trails a bit longer
$PositionFrontLeftTire/Trail.setSliding(false)
$PositionFrontRightTire/Trail.setSliding(false)
$PositionFrontLeftTire.setSliding(false)
$PositionFrontRightTire.setSliding(false)
elif d<0:
velocity = - new_heading * min(velocity.length(),max_speed_reverse)

View file

@ -1,12 +0,0 @@
extends Node2D
@onready var label: Label = $Label
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View file

@ -1,23 +0,0 @@
extends Line2D
var point
var isSliding=false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
set_as_top_level(true)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
pass
func _process(delta: float) -> void:
if isSliding:
point = get_parent().global_position
add_point(point)
if points.size()>100:
remove_point(0)
func setSliding(psliding:bool):
if not isSliding and psliding: #just started sliding
clear_points()
isSliding=psliding

49
scripts/tiretrails.gd Normal file
View file

@ -0,0 +1,49 @@
extends Node2D
var point
var isSliding=false
var currentTrail:Line2D
#TODO: queue_free(), #clear_points()
const STARTALPHA=0.8
const MAXIMUM_POINTS=100 #maximum points per trail
const FADETIME=120 #in seconds. how long a trail should last (if alpha would start at 1.0)
func _ready() -> void:
pass
func _physics_process(delta: float) -> void:
if isSliding:
point = global_position
currentTrail.add_point(point)
if currentTrail.points.size()>MAXIMUM_POINTS:
currentTrail.remove_point(0) #remove points from start
for c:Line2D in get_children():
c.default_color.a=max(c.default_color.a-1.0/FADETIME*delta,0.0)
if c.default_color.a<=0: #not visible anymore
c.queue_free()
func setSliding(psliding:bool):
if not isSliding and psliding: #just started sliding
#Reduce old lines
const KEEP_LINES=3
#if get_child_count()>KEEP_LINES:
# var trails=get_children()
#Create new Line
currentTrail=Line2D.new()
add_child(currentTrail)
currentTrail.width=4
currentTrail.default_color=Color(0,0,0,STARTALPHA)
var curve=Curve.new()
curve.add_point(Vector2(0,0),0,5,Curve.TANGENT_FREE,Curve.TANGENT_FREE)
curve.add_point(Vector2(0.5,1),0,0,Curve.TANGENT_FREE,Curve.TANGENT_FREE)
curve.add_point(Vector2(1,0),-5,0,Curve.TANGENT_FREE,Curve.TANGENT_FREE)
currentTrail.width_curve=curve
currentTrail.set_as_top_level(true)
currentTrail.z_index=9 #above road, behind car
isSliding=psliding