|
|
@@ -8,6 +8,10 @@ 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 rear_axel : float = 10 setget _set_rear_axel |
|
|
|
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 |
|
|
|
export var traction_fast_speed : float = 400.0 setget _set_traction_fast_speed |
|
|
|
export (float, 0.1, 360.0, 0.1) var max_steering_angle = 15 setget _set_max_steering_angle |
|
|
|
export var max_engine_power : float = 800 setget _set_max_engine_power |
|
|
|
export (float, 0.0, 1.0, 0.001) var reverse_engine_multiplier = 0.85 |
|
|
@@ -21,7 +25,7 @@ var acceleration = Vector2.ZERO |
|
|
|
var steering_angle = 0.0 |
|
|
|
var engine_power = 0.0 |
|
|
|
var breaking_power = 0.0 |
|
|
|
var reverse = false |
|
|
|
var reverse = true |
|
|
|
|
|
|
|
onready var camera = $Camera2D |
|
|
|
|
|
|
@@ -42,6 +46,14 @@ func _set_rear_axel(a : float) -> void: |
|
|
|
if a > 0: |
|
|
|
rear_axel = a |
|
|
|
|
|
|
|
func _set_traction_slow_speed(s : float) -> void: |
|
|
|
if s >= 0: |
|
|
|
traction_slow_speed = s |
|
|
|
|
|
|
|
func _set_traction_fast_speed(s : float) -> void: |
|
|
|
if s >= 0: |
|
|
|
traction_fast_speed = s |
|
|
|
|
|
|
|
func _set_max_steering_angle(a : float) -> void: |
|
|
|
max_steering_angle = a |
|
|
|
if steering_angle > a: |
|
|
@@ -88,34 +100,49 @@ func _physics_process(delta): |
|
|
|
if Engine.editor_hint: |
|
|
|
return |
|
|
|
|
|
|
|
# TODO: Reverse engine_power sign when toggling reverse! |
|
|
|
acceleration = -transform.y * engine_power |
|
|
|
var accel_dir = 1.0 |
|
|
|
var power = engine_power |
|
|
|
if breaking_power > 0: |
|
|
|
if not reverse: |
|
|
|
accel_dir = -1.0 |
|
|
|
power = breaking_power |
|
|
|
elif reverse: |
|
|
|
accel_dir = -1.0 |
|
|
|
|
|
|
|
acceleration = -transform.y * power * accel_dir |
|
|
|
|
|
|
|
_apply_friction() |
|
|
|
print(acceleration) |
|
|
|
_calculate_heading(delta) |
|
|
|
|
|
|
|
if reverse: |
|
|
|
velocity += -acceleration * delta |
|
|
|
else: |
|
|
|
velocity += acceleration * delta |
|
|
|
velocity += acceleration * delta |
|
|
|
velocity = move_and_slide(velocity) |
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
|
|
|
# Private "Helper" Methods |
|
|
|
# ----------------------------------------------------------------------------- |
|
|
|
|
|
|
|
func _generate_traction() -> float: |
|
|
|
var traction = 1.0 |
|
|
|
var speed = velocity.length() |
|
|
|
if speed >= traction_fast_speed: |
|
|
|
traction = traction_fast |
|
|
|
elif speed >= traction_slow_speed: |
|
|
|
var d = speed / (traction_fast_speed - traction_slow_speed) |
|
|
|
traction = traction_slow + ((traction_fast - traction_slow) * d) |
|
|
|
return traction |
|
|
|
|
|
|
|
func _calculate_heading(delta : float) -> void: |
|
|
|
var rear_wheel = position + transform.y * rear_axel |
|
|
|
var fore_wheel = position - transform.y * forward_axel |
|
|
|
rear_wheel += velocity * delta |
|
|
|
fore_wheel += velocity.rotated(deg2rad(steering_angle)) * delta |
|
|
|
var heading = (fore_wheel - rear_wheel).normalized() |
|
|
|
var traction = _generate_traction() |
|
|
|
var d = heading.dot(velocity.normalized()) |
|
|
|
if d > 0: |
|
|
|
velocity = heading * velocity.length() |
|
|
|
velocity = velocity.linear_interpolate(heading * velocity.length(), traction) |
|
|
|
if d < 0: |
|
|
|
velocity = -heading * velocity.length() |
|
|
|
velocity = velocity.linear_interpolate(-heading * velocity.length(), traction) |
|
|
|
#velocity = heading * velocity.length() |
|
|
|
rotation = (heading.angle() + deg2rad(90)) |
|
|
|
|
|
|
@@ -124,9 +151,12 @@ func _apply_friction() -> void: |
|
|
|
if velocity.length() < 5: |
|
|
|
velocity = Vector2.ZERO |
|
|
|
speed = 0 |
|
|
|
var force_friction = (-friction) * velocity |
|
|
|
var force_drag = (-drag) * speed * velocity |
|
|
|
acceleration += force_friction + force_drag |
|
|
|
if breaking_power > 0 and speed <= 0: |
|
|
|
acceleration = Vector2.ZERO |
|
|
|
else: |
|
|
|
var force_friction = (-friction) * velocity |
|
|
|
var force_drag = (-drag) * speed * velocity |
|
|
|
acceleration += force_friction + force_drag |
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
|
|
|
# Public Methods |
|
|
@@ -161,8 +191,11 @@ func set_throttle(v : float) -> void: |
|
|
|
if reverse: |
|
|
|
epm = reverse_engine_multiplier |
|
|
|
engine_power = max_engine_power * epm * v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func set_break(v : float) -> void: |
|
|
|
v = clamp(v, 0.0, 1.0) |
|
|
|
breaking_power = max_breaking_power * v |
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
|
|
|
# Event Handlers |
|
|
|
# ----------------------------------------------------------------------------- |