-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeliveryTruck.gd
101 lines (85 loc) · 2.49 KB
/
DeliveryTruck.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
extends VehicleBody3D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
@export var gearChangeSpeed: float = 0.5
@export var gearChangeTimeout: float = 0.4
@export var gearTimeoutRemaining: float = 0.0
@export var steeringSpeedPercent: float = 0.95
@export var acceleration: float = 1300.0
@export var reverseAcceleration: float = 1000.0
@export var brakeForce: float = 80.0
@export var idleBreak: float = 5.0
@export var maxSteeringAngleFast: float = 15.0
@export var maxSteeringAngleSlow: float = 60.0
var gear = 0
var targetGear = 0
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func interact(player):
player.unmount()
self.engine_force = 0
setBrake(idleBreak)
func get_mount_off_point(_player):
return $MountOffPoint.global_position
func process_mounted(delta):
var maxSteeringAngle = maxSteeringAngleFast \
+ (maxSteeringAngleSlow - maxSteeringAngleFast) \
* smoothstep(1.0, 0.0, self.linear_velocity.length() / 25.0);
var x = (Input.get_action_strength("left") - Input.get_action_strength("right")) * deg_to_rad(maxSteeringAngle)
var accel = Input.get_action_strength("accelerate")
var back = Input.get_action_strength("reverse")
gearTimeoutRemaining -= delta
self.steering = (self.steering - x) * pow(1 - steeringSpeedPercent, delta) + x;
if gearTimeoutRemaining <= 0:
if gear == 0:
if accel > 0.01:
setEngineForce(acceleration * accel)
else:
setEngineForce(0)
if back > 0.01:
setBrake(brakeForce * back)
if abs(accel) < 0.1:
nextGear(-1)
else:
setBrake(0)
elif gear == -1:
if back > 0.01:
setEngineForce(reverseAcceleration * -back)
else:
setEngineForce(0)
if accel > 0.01:
setBrake(brakeForce * accel)
if abs(back) < 0.1:
nextGear(0)
else:
setBrake(0)
else:
nextGear(0)
else:
setBrake(0)
setEngineForce(0)
func nextGear(newGear):
if self.linear_velocity.length_squared() < gearChangeSpeed * gearChangeSpeed:
if targetGear != newGear:
gearTimeoutRemaining = gearChangeTimeout
targetGear = newGear
if gearTimeoutRemaining <= 0:
gear = newGear
setBrake(0)
setEngineForce(0)
func setBrake(v):
if v != 0:
v *= max(0.8, self.linear_velocity.length() / 5.0)
$WheelFL.brake = v
if v != 0:
$WheelFL.engine_force = 0
$WheelFR.brake = v
if v != 0:
$WheelFR.engine_force = 0
func setEngineForce(v):
$WheelFL.engine_force = v
$WheelFR.engine_force = v
$WheelBL.engine_force = v
$WheelBR.engine_force = v