small racing game im working on
Diffstat (limited to 'classes/car.gd')
| -rw-r--r-- | classes/car.gd | 58 |
1 files changed, 15 insertions, 43 deletions
diff --git a/classes/car.gd b/classes/car.gd index 51d0be3..bbb3164 100644 --- a/classes/car.gd +++ b/classes/car.gd @@ -38,9 +38,6 @@ const inactive = {active = false}; const trail_scene = preload("res://scenes/trail.tscn") var skids: Array[Array] -var shup := false -var shown := false - func ratio() -> float: match current_gear: 0: return 0 @@ -57,7 +54,6 @@ func is_not_on_ground() -> bool: return wheels.any(func(whl: VehicleWheel3D): return !whl.is_in_contact()) func reset() -> void: - set_collision_mask_value(2, 0) gear_timer = 0 clutch_position = 0 steering = 0 @@ -83,12 +79,8 @@ func _ready() -> void: particles.append(whl.get_node(^"particles")) randomize() reset() - if Globals.playing.night(): - night() func kph() -> float: - if can_accelerate == false: - return 0 return (3 * PI * wheel_radius * wheel_rpm) / 25; # calculate the RPM of wheels @@ -119,14 +111,12 @@ func _process_gear_inputs(delta: float): gear_timer = max(0.0, gear_timer - delta) clutch_position = 0 else: - if shown and current_gear > -1: - shown = false + if shift_down() and current_gear > -1: current_gear = current_gear - 1 gear_timer = gear_shift_time clutch_position = 0 shifted.emit() - elif shup and current_gear < gear_ratios.size(): - shup = false + elif shift_up() and current_gear < gear_ratios.size(): current_gear = current_gear + 1 gear_timer = gear_shift_time clutch_position = 0 @@ -135,17 +125,17 @@ func _process_gear_inputs(delta: float): clutch_position = 1 func _process(delta: float): + if can_shift: + _process_gear_inputs(delta) + steering = -steer_target body_mesh.rotation.z = lerp(body_mesh.rotation.z, clampf((-steering * .001 + randf_range(-0.0005,0.0005)) * whl_rpm(), -.3, .3), 2 * delta) + engine_rpm = clampf(move_toward(engine_rpm, (wheel_rpm * engine_force * 0.0015), 800), 800, MAX_ENGINE_FORCE) func limit(delta: float) -> void: linear_damp = max((.5 * delta) * (kph() - 400), 0) if kph() > 400 else 0.0 angular_damp = max(5 * (angular_velocity.length_squared() - 45), 0) if angular_velocity.length_squared() > 45 else 0.0 func _physics_process(delta: float): - steering = steer_target - if can_shift: - _process_gear_inputs(delta) - if can_accelerate: var power_factor := power_curve.sample_baked(clampf(wheel_rpm / max_engine_rpm, 0.0, 1.0)) if current_gear == -1: @@ -156,7 +146,6 @@ func _physics_process(delta: float): engine_force = 0.0 wheel_rpm = whl_rpm() - engine_rpm = clampf(move_toward(engine_rpm, (wheel_rpm * engine_force * 0.0015), 800), 800, MAX_ENGINE_FORCE) limit(delta) downforce(5) @@ -177,11 +166,6 @@ func _physics_process(delta: float): (skids[i][-1] as Trail3D).add(wheels[i].global_position - Vector3(0, .561, 0)) elif skids[i][-1].active: skids[i][-1].active = false - - if brake > 0: - tail.albedo_color = Color.RED; - else: - tail.albedo_color = Color(1, 0.34902, 0.227451, 1) func start() -> void: brake = 0 @@ -189,26 +173,14 @@ func start() -> void: can_accelerate = true func _integrate_forces(state: PhysicsDirectBodyState3D) -> void: + if kph() < 100: + return var contact := state.get_contact_count() while contact > 0: - contact -= 1 - if state.get_contact_local_velocity_at_position(contact).length() < 0.5: - continue - var p := state.get_contact_local_position(contact) - var direction := state.get_contact_local_normal(contact) - var sprk: GPUParticles3D = sparks.instantiate() - var sprkmat: ParticleProcessMaterial = sprk.process_material - sprkmat.direction = direction - sprkmat.initial_velocity_min = state.get_contact_local_velocity_at_position(contact).length()/5 - sprkmat.initial_velocity_max = state.get_contact_local_velocity_at_position(contact).length()/3 - sprk.amount = clampi(floori(state.get_contact_local_velocity_at_position(contact).length()/4), 0, 20) - get_parent().add_child(sprk) - sprk.position = p - -@onready var tail: Material = $body.mesh.surface_get_material(5); -@onready var head: Material = $body.mesh.surface_get_material(6); - -func night() -> void: - $lights.visible = true; - tail.emission_enabled = true; - head.emission_enabled = true; + contact -= 1 + var p := state.get_contact_local_position(contact) # it says local, but its global. + var direction := state.get_contact_local_normal(contact) + var sprk := sparks.instantiate() + (sprk.process_material as ParticleProcessMaterial).direction = direction + get_parent().add_child(sprk) + sprk.global_position = p |