Browse Source

Added 'tire trails' to the vehicle.

master
Bryan Miller 3 years ago
parent
commit
6f01ff612e
No known key found for this signature in database
7 changed files with 202 additions and 1 deletions
  1. +52
    -0
      Objects/Trail/Trail.gd
  2. +16
    -0
      Objects/Trail/Trail.tscn
  3. +51
    -0
      Objects/Trail/TrailLine.gd
  4. +5
    -0
      Objects/Vehicle/Tire_Curve_Basic.tres
  5. +49
    -0
      Objects/Vehicle/Vehicle.gd
  6. +25
    -1
      Objects/Vehicle/Vehicle.tscn
  7. +4
    -0
      World.tscn

+ 52
- 0
Objects/Trail/Trail.gd View File

@@ -0,0 +1,52 @@
extends Position2D


export var max_length : int = 100 setget _set_max_length
export var distance_between_points : float = 10 setget _set_distance_between_points
export var width : float = 2 setget _set_width
export (Curve) var width_curve = null setget _set_width_curve
export (Color) var default_color = Color("6680ff") setget _set_default_color


onready var trailline_node = $TrailLine


func _set_max_length(l : int) -> void:
if l > 0:
max_length = l
if trailline_node:
trailline_node.max_length = max_length


func _set_distance_between_points(d : float) -> void:
if d > 0.0:
distance_between_points = d
if trailline_node:
trailline_node.distance_between_points = distance_between_points

func _set_width(w : float) -> void:
if w > 0:
width = w
if trailline_node:
trailline_node.width = w

func _set_width_curve(c) -> void:
width_curve = c
if trailline_node:
trailline_node.width_curve = c

func _set_default_color(c : Color) -> void:
default_color = c
if trailline_node:
trailline_node.default_color = c

func _ready():
trailline_node.max_length = max_length
trailline_node.distance_between_points = distance_between_points
trailline_node.width = width
trailline_node.width_curve = width_curve
trailline_node.default_color = default_color





+ 16
- 0
Objects/Trail/Trail.tscn View File

@@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://Objects/Trail/TrailLine.gd" type="Script" id=1]
[ext_resource path="res://Objects/Trail/Trail.gd" type="Script" id=2]

[node name="Trail" type="Position2D"]
script = ExtResource( 2 )

[node name="TrailLine" type="Line2D" parent="."]
default_color = Color( 0.4, 0.501961, 1, 1 )
script = ExtResource( 1 )

[node name="Timer" type="Timer" parent="TrailLine"]
one_shot = true

[connection signal="timeout" from="TrailLine/Timer" to="TrailLine" method="_on_decay_timeout"]

+ 51
- 0
Objects/Trail/TrailLine.gd View File

@@ -0,0 +1,51 @@
extends Line2D


export var max_length : int = 100 setget _set_max_length
export var distance_between_points : float = 10
export var decay_time : float = 0.1 setget _set_decay_time

var last_point = null
onready var parent = get_parent()
onready var timer = $Timer

func _set_max_length(l : int) -> void:
if l > 0:
max_length = l

func _set_decay_time(d : float) -> void:
if d >= 0:
decay_time = 0


func _ready():
set_as_toplevel(true)
show_behind_parent = true
_reset_timer()

func _process(delta):
rotation = parent.rotation
if points.size() < max_length:
var point = _get_point()
if point != null:
add_point(point)
if points.size() >= max_length:
remove_point(0)

func _get_point():
var point = parent.global_position
if last_point != null and point.distance_to(last_point) < distance_between_points:
return null
last_point = point
return point

func _reset_timer():
if decay_time > 0:
timer.stop()
timer.start(decay_time)


func _on_decay_timeout():
if points.size() > 0:
remove_point(0)
_reset_timer()

+ 5
- 0
Objects/Vehicle/Tire_Curve_Basic.tres View File

@@ -0,0 +1,5 @@
[gd_resource type="Curve" format=2]

[resource]
max_value = 3.0
_data = [ Vector2( 0, 0 ), 0.0, 0.0674788, 0, 0, Vector2( 0.288889, 1.93636 ), 5.34545, 5.34545, 0, 0, Vector2( 1, 3 ), 0.145454, 0.0, 0, 0 ]

+ 49
- 0
Objects/Vehicle/Vehicle.gd View File

@@ -7,7 +7,9 @@ class_name Vehicle

export var camera_enabled : bool = false setget _set_camera_enabled
export var forward_axel : float = 10 setget _set_forward_axel
export var forward_axel_length : float = 10 setget _set_forward_axel_length
export var rear_axel : float = 10 setget _set_rear_axel
export var rear_axel_length : float = 10 setget _set_rear_axel_length
export (float, 0.0, 1.0, 0.001) var traction_fast = 0.1
export (float, 0.0, 1.0, 0.001) var traction_slow = 0.7
export var traction_slow_speed : float = 100.0 setget _set_traction_slow_speed
@@ -29,6 +31,11 @@ var reverse = true

onready var camera = $Camera2D

onready var trail_fd = $Trail_FD
onready var trail_fp = $Trail_FP
onready var trail_rd = $Trail_RD
onready var trail_rp = $Trail_RP

# -----------------------------------------------------------------------------
# Setter/Getter Methods
# -----------------------------------------------------------------------------
@@ -41,10 +48,22 @@ func _set_camera_enabled(e : bool) -> void:
func _set_forward_axel(a : float) -> void:
if a > 0:
forward_axel = a
_position_wheel_trails()

func _set_forward_axel_length(l : float) -> void:
if l > 0:
forward_axel_length = l
_position_wheel_trails()

func _set_rear_axel(a : float) -> void:
if a > 0:
rear_axel = a
_position_wheel_trails()

func _set_rear_axel_length(l : float) -> void:
if l > 0:
rear_axel_length = l
_position_wheel_trails()

func _set_traction_slow_speed(s : float) -> void:
if s >= 0:
@@ -79,6 +98,7 @@ func _set_max_breaking_power(p : float) -> void:

func _ready():
camera.current = camera_enabled
_position_wheel_trails()
if Engine.editor_hint:
$Sprite.show_behind_parent = true
$Collision.visible = false
@@ -86,12 +106,30 @@ func _ready():
func _draw():
if Engine.editor_hint:
var wheel_base_color = Color(1.0, 0.5, 0.0)
var wheel_axel_color = Color(0.0, 1.0, 0.0)
var wheel_color = Color(0.1, 0.1, 0.1)
draw_line(
Vector2(0, -forward_axel),
Vector2(0, rear_axel),
wheel_base_color,
2
)
draw_line(
Vector2(-forward_axel_length * 0.5, -forward_axel),
Vector2(forward_axel_length * 0.5, -forward_axel),
wheel_axel_color,
2
)
draw_line(
Vector2(-rear_axel_length * 0.5, rear_axel),
Vector2(rear_axel_length * 0.5, rear_axel),
wheel_axel_color,
2
)
draw_circle(Vector2(-forward_axel_length * 0.5, -forward_axel), 4, wheel_color)
draw_circle(Vector2(forward_axel_length * 0.5, -forward_axel), 4, wheel_color)
draw_circle(Vector2(-rear_axel_length * 0.5, rear_axel), 4, wheel_color)
draw_circle(Vector2(rear_axel_length * 0.5, rear_axel), 4, wheel_color)

func _process(delta):
update()
@@ -121,6 +159,17 @@ func _physics_process(delta):
# Private "Helper" Methods
# -----------------------------------------------------------------------------

func _position_wheel_trails():
if trail_fd:
trail_fd.position = Vector2(-forward_axel_length * 0.5, -forward_axel)
if trail_fp:
trail_fp.position = Vector2(forward_axel_length * 0.5, -forward_axel)
if trail_rd:
trail_rd.position = Vector2(-rear_axel_length * 0.5, rear_axel)
if trail_rp:
trail_rp.position = Vector2(rear_axel_length * 0.5, rear_axel)


func _generate_traction() -> float:
var traction = 1.0
var speed = velocity.length()

+ 25
- 1
Objects/Vehicle/Vehicle.tscn View File

@@ -1,20 +1,44 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=6 format=2]

[ext_resource path="res://Assets/Graphics/Car-Yellow.png" type="Texture" id=1]
[ext_resource path="res://Objects/Vehicle/Vehicle.gd" type="Script" id=2]
[ext_resource path="res://Objects/Trail/Trail.tscn" type="PackedScene" id=3]
[ext_resource path="res://Objects/Vehicle/Tire_Curve_Basic.tres" type="Curve" id=4]

[sub_resource type="CapsuleShape2D" id=1]
radius = 14.0
height = 32.0

[node name="Vehicle" type="KinematicBody2D"]
z_index = 1
script = ExtResource( 2 )

[node name="Collision" type="CollisionShape2D" parent="."]
visible = false
shape = SubResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
show_behind_parent = true
texture = ExtResource( 1 )

[node name="Camera2D" type="Camera2D" parent="."]
smoothing_enabled = true

[node name="Trail_FD" parent="." instance=ExtResource( 3 )]
position = Vector2( -5, -10 )
width_curve = ExtResource( 4 )

[node name="Trail_FP" parent="." instance=ExtResource( 3 )]
position = Vector2( 5, -10 )
width_curve = ExtResource( 4 )
default_color = Color( 0.4, 0.831373, 1, 1 )

[node name="Trail_RD" parent="." instance=ExtResource( 3 )]
position = Vector2( -5, 10 )
width_curve = ExtResource( 4 )
default_color = Color( 1, 0.780392, 0.4, 1 )

[node name="Trail_RP" parent="." instance=ExtResource( 3 )]
position = Vector2( 5, 10 )
width_curve = ExtResource( 4 )
default_color = Color( 1, 0.552941, 0.4, 1 )

+ 4
- 0
World.tscn View File

@@ -976,5 +976,9 @@ tile_data = PoolIntArray( -1703934, 0, 7, -1703933, 0, 196612, -1703932, 0, 1966
[node name="Vehicle" parent="." instance=ExtResource( 1 )]
position = Vector2( 946.288, 469.696 )
camera_enabled = true
forward_axel = 18.0
forward_axel_length = 26.0
rear_axel = 18.0
rear_axel_length = 28.0

[node name="PlayerCTRL" parent="Vehicle" instance=ExtResource( 2 )]

Loading…
Cancel
Save