summaryrefslogtreecommitdiff
path: root/car.gd
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-06-06 04:25:06 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-06-06 04:25:19 +0200
commitabc27d4d6b590fbf6adb2fe136208df0ebe2deeb (patch)
treee1cb6eccc8958a99b6432faf9d2fd459d30b589c /car.gd
parent3cb24b9d4d23b875cc7719c56cd4a664a9ec1b19 (diff)
downloadcar-abc27d4d6b590fbf6adb2fe136208df0ebe2deeb.tar.gz
Better handling, handbrake, follow camera
Diffstat (limited to 'car.gd')
-rw-r--r--car.gd93
1 files changed, 73 insertions, 20 deletions
diff --git a/car.gd b/car.gd
index bbe5872..0b8f9d9 100644
--- a/car.gd
+++ b/car.gd
@@ -1,36 +1,89 @@
extends VehicleBody
-var max_rpm = 5000
-var max_torque = 800
+const max_rpm: float = 2500.0
+const max_torque: float = 2800.0
+var n_jumps: int
+onready var wheels = [$front_left_wheel, $front_right_wheel,
+ $back_left_wheel, $back_right_wheel]
+var slip
+export(NodePath) var target = null
-func _physics_process(delta):
- var can_jump = false
- for w in [$front_left_wheel, $front_right_wheel,
- $back_left_wheel, $back_right_wheel]:
+func car_can_jump() -> bool:
+ for w in wheels:
+ if not w.is_in_contact():
+ return false
+ return true
+
+func is_car_flying() -> bool:
+ for w in wheels:
if w.is_in_contact():
- can_jump = true
- break
+ return false
+ return true
+
+func _physics_process(delta):
+ var can_jump = car_can_jump()
+ var is_flying = is_car_flying()
+
+ if can_jump:
+ n_jumps = 0
var new_steering = (Input.get_action_strength("left")
- - Input.get_action_strength("right")) * 0.4
+ - Input.get_action_strength("right")) * 0.6
var acceleration = (Input.get_action_strength("forward")
- Input.get_action_strength("back"))
var transform = get_transform().basis
+ if Input.is_action_just_pressed("reset"):
+ rotation = Vector3(0, 0, 0)
+ angular_velocity = Vector3(0, 0, 0)
+ linear_velocity = Vector3(0, 0, 0)
+ get_global_transform().origin = Vector3(0, 0, 0)
+
if can_jump:
- steering = lerp(steering, new_steering, 2 * delta)
+ if new_steering != 0:
+ steering = lerp(steering, new_steering, 2 * delta)
+ else:
+ steering = lerp(steering, 0, 8 * delta)
+
var rpm = $back_left_wheel.get_rpm()
- $back_left_wheel.engine_force = acceleration * max_torque * (1 - rpm / max_rpm)
+ if (acceleration < 0 and rpm > 0) or (acceleration > 0 and rpm < 0):
+ for w in wheels:
+ w.brake = 25
+ else:
+ for w in wheels:
+ w.brake = 0
+
+ $back_left_wheel.engine_force = acceleration * max_torque * (1 - rpm / max_rpm)
rpm = $back_right_wheel.get_rpm()
$back_right_wheel.engine_force = acceleration * max_torque * (1 - rpm / max_rpm)
- if Input.is_action_just_pressed("jump"):
- apply_impulse(Vector3(0, 0, 0), Vector3(0, 2000, 0))
- elif not can_jump:
- steering = lerp(steering, 0, 5 * delta)
- apply_torque_impulse(10.0 * acceleration * transform.x)
- apply_torque_impulse(10.0 * new_steering * transform.y)
+
+ if Input.is_action_pressed("toggle"):
+ # handbrake
+ $back_left_wheel.wheel_friction_slip = 1
+ $back_right_wheel.wheel_friction_slip = 1
+ for w in wheels:
+ w.brake = 10
+ else:
+ $back_left_wheel.wheel_friction_slip = slip
+ $back_right_wheel.wheel_friction_slip = slip
+ for w in wheels:
+ w.brake = 0
+ elif is_flying:
+ apply_torque_impulse(50.0 * acceleration * transform.x)
+ if Input.is_action_pressed("toggle"):
+ apply_torque_impulse(-100.0 * new_steering * transform.z)
+ else:
+ apply_torque_impulse(50.0 * new_steering * transform.y)
+
+ if n_jumps < 2 and Input.is_action_just_pressed("jump"):
+ apply_impulse(Vector3(0, 0, 0), Vector3(0, 2000, 0))
+ n_jumps += 1
+
+ if Input.is_action_pressed("turbo"):
+ add_central_force(4000 * transform.x)
+
func _ready():
- steering = 0
- $back_left_wheel.engine_force = 0
- $back_right_wheel.engine_force = 0
+ for w in wheels:
+ add_collision_exception_with(w)
+ slip = $back_left_wheel.wheel_friction_slip