Extract out player controls into player_controller
parent
82b3feecf4
commit
dcea94ac6b
|
@ -0,0 +1,28 @@
|
||||||
|
class_name PlayerVehicleController
|
||||||
|
extends Node
|
||||||
|
|
||||||
|
export(NodePath) var vehicle_path = null
|
||||||
|
|
||||||
|
onready var _vehicle: BuggedVehicle = get_node(vehicle_path)
|
||||||
|
|
||||||
|
|
||||||
|
func _input(_event: InputEvent) -> void:
|
||||||
|
# Using _input just to make sure events are captured right away
|
||||||
|
_vehicle.inputs.throttle = Input.get_action_strength("throttle")
|
||||||
|
_vehicle.inputs.clutch = Input.get_action_strength("clutch")
|
||||||
|
_vehicle.inputs.brake = Input.get_action_strength("brake")
|
||||||
|
_vehicle.inputs.handbrake = Input.get_action_strength("handbrake")
|
||||||
|
var steering_input = (
|
||||||
|
Input.get_action_strength("steer_left")
|
||||||
|
- Input.get_action_strength("steer_right")
|
||||||
|
)
|
||||||
|
# TODO: deadzone config
|
||||||
|
if abs(steering_input) < 0.05:
|
||||||
|
steering_input = 0.0
|
||||||
|
_vehicle.inputs.steering = steering_input
|
||||||
|
if Input.is_action_just_pressed("gear_up"):
|
||||||
|
_vehicle.inputs.gear_request = BuggedVehicle.GearRequest.UP
|
||||||
|
elif Input.is_action_pressed("gear_down"):
|
||||||
|
_vehicle.inputs.gear_request = BuggedVehicle.GearRequest.DOWN
|
||||||
|
else:
|
||||||
|
_vehicle.inputs.gear_request = BuggedVehicle.GearRequest.NONE
|
|
@ -24,6 +24,11 @@ _global_script_classes=[ {
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://player/cameras/camera_controller.gd"
|
"path": "res://player/cameras/camera_controller.gd"
|
||||||
}, {
|
}, {
|
||||||
|
"base": "Node",
|
||||||
|
"class": "PlayerVehicleController",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://player/vehicle_controller.gd"
|
||||||
|
}, {
|
||||||
"base": "Particles",
|
"base": "Particles",
|
||||||
"class": "TireSmoke",
|
"class": "TireSmoke",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
|
@ -38,6 +43,7 @@ _global_script_class_icons={
|
||||||
"BaseTrackLevel": "",
|
"BaseTrackLevel": "",
|
||||||
"BuggedVehicle": "",
|
"BuggedVehicle": "",
|
||||||
"CameraController": "",
|
"CameraController": "",
|
||||||
|
"PlayerVehicleController": "",
|
||||||
"TireSmoke": "",
|
"TireSmoke": "",
|
||||||
"Track": ""
|
"Track": ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,12 @@ class_name BaseTrackLevel
|
||||||
extends Spatial
|
extends Spatial
|
||||||
|
|
||||||
const CAMERA_CONTROLLER = preload("res://player/cameras/camera_controller.gd")
|
const CAMERA_CONTROLLER = preload("res://player/cameras/camera_controller.gd")
|
||||||
|
const PLAYER_CONTROLLER = preload("res://player/vehicle_controller.gd")
|
||||||
|
|
||||||
var player_node: BuggedVehicle
|
var player_node: BuggedVehicle
|
||||||
var gui: Node
|
var gui: Node
|
||||||
var camera_controller: CameraController
|
var camera_controller: CameraController
|
||||||
|
var player_controller: PlayerVehicleController
|
||||||
|
|
||||||
onready var track = $Track
|
onready var track = $Track
|
||||||
|
|
||||||
|
@ -14,12 +16,16 @@ func _ready() -> void:
|
||||||
reset_player_to(track.get_furthest_checkpoint(), player_node)
|
reset_player_to(track.get_furthest_checkpoint(), player_node)
|
||||||
add_child(player_node)
|
add_child(player_node)
|
||||||
add_child(gui)
|
add_child(gui)
|
||||||
|
player_controller = PLAYER_CONTROLLER.new()
|
||||||
|
player_controller.vehicle_path = player_node.get_path()
|
||||||
|
player_node.add_child(player_controller)
|
||||||
camera_controller = CAMERA_CONTROLLER.new()
|
camera_controller = CAMERA_CONTROLLER.new()
|
||||||
camera_controller.attach_cameras_to(player_node)
|
camera_controller.attach_cameras_to(player_node)
|
||||||
|
|
||||||
|
|
||||||
func spawn_player(player_node: BuggedVehicle, gui: Node) -> void:
|
func spawn_player(player_node: BuggedVehicle, gui: Node) -> void:
|
||||||
self.player_node = player_node
|
self.player_node = player_node
|
||||||
|
self.player_node.add_child(player_controller)
|
||||||
self.gui = gui
|
self.gui = gui
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,8 @@ signal clutch_updated(clutch_percent)
|
||||||
signal gear_updated(gear)
|
signal gear_updated(gear)
|
||||||
signal steering_updated(steering_angle, steering_percent)
|
signal steering_updated(steering_angle, steering_percent)
|
||||||
|
|
||||||
|
enum GearRequest { NONE, UP, DOWN }
|
||||||
|
|
||||||
export(float) var max_steer_angle = 25
|
export(float) var max_steer_angle = 25
|
||||||
export(float) var speed_steer_angle = 10
|
export(float) var speed_steer_angle = 10
|
||||||
export(float) var max_steer_speed = 100.0
|
export(float) var max_steer_speed = 100.0
|
||||||
|
@ -42,6 +44,8 @@ var gear_timer = 0
|
||||||
var traction_wheels: Array
|
var traction_wheels: Array
|
||||||
var reset_transform: Transform = Transform.IDENTITY
|
var reset_transform: Transform = Transform.IDENTITY
|
||||||
|
|
||||||
|
var inputs: VehicleInputs = VehicleInputs.new()
|
||||||
|
|
||||||
onready var frwheel: VehicleWheel = $front_right
|
onready var frwheel: VehicleWheel = $front_right
|
||||||
onready var flwheel: VehicleWheel = $front_left
|
onready var flwheel: VehicleWheel = $front_left
|
||||||
onready var rrwheel: VehicleWheel = $rear_right
|
onready var rrwheel: VehicleWheel = $rear_right
|
||||||
|
@ -51,11 +55,6 @@ onready var flsmoke: TireSmoke = $fl_tire_smoke
|
||||||
onready var rrsmoke: TireSmoke = $rr_tire_smoke
|
onready var rrsmoke: TireSmoke = $rr_tire_smoke
|
||||||
onready var rlsmoke: TireSmoke = $rl_tire_smoke
|
onready var rlsmoke: TireSmoke = $rl_tire_smoke
|
||||||
|
|
||||||
onready var cockpit: Position3D = $cockpit
|
|
||||||
onready var hood: Position3D = $hood
|
|
||||||
onready var bumper: Position3D = $bumper
|
|
||||||
onready var static_follow: Position3D = $static_follow
|
|
||||||
|
|
||||||
onready var engine_sound_player: AudioStreamPlayer3D = $engine_sound_player
|
onready var engine_sound_player: AudioStreamPlayer3D = $engine_sound_player
|
||||||
onready var engine_sound_playback: AudioStreamPlayback = $engine_sound_player.get_stream_playback()
|
onready var engine_sound_playback: AudioStreamPlayback = $engine_sound_player.get_stream_playback()
|
||||||
|
|
||||||
|
@ -66,6 +65,16 @@ onready var max_steer_input_rad: float = deg2rad(max_steer_input)
|
||||||
onready var auto_clutch_rpm_limit = max_rpm * automatic_gear_down_threshold
|
onready var auto_clutch_rpm_limit = max_rpm * automatic_gear_down_threshold
|
||||||
|
|
||||||
|
|
||||||
|
class VehicleInputs:
|
||||||
|
var gear_request = GearRequest.NONE
|
||||||
|
var clutch = 0.0
|
||||||
|
var throttle = 0.0
|
||||||
|
var brake = 0.0
|
||||||
|
var handbrake = 0.0
|
||||||
|
# Left is positive, right is negative
|
||||||
|
var steering = 0.0
|
||||||
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
for wheel in [frwheel, flwheel, rrwheel, rlwheel]:
|
for wheel in [frwheel, flwheel, rrwheel, rlwheel]:
|
||||||
if wheel.use_as_traction:
|
if wheel.use_as_traction:
|
||||||
|
@ -76,19 +85,19 @@ func _ready():
|
||||||
|
|
||||||
|
|
||||||
func get_cockpit_position() -> Node:
|
func get_cockpit_position() -> Node:
|
||||||
return cockpit
|
return $cockpit
|
||||||
|
|
||||||
|
|
||||||
func get_hood_position() -> Node:
|
func get_hood_position() -> Node:
|
||||||
return hood
|
return $hood
|
||||||
|
|
||||||
|
|
||||||
func get_bumper_position() -> Node:
|
func get_bumper_position() -> Node:
|
||||||
return bumper
|
return $bumper
|
||||||
|
|
||||||
|
|
||||||
func get_static_follow_position() -> Node:
|
func get_static_follow_position() -> Node:
|
||||||
return static_follow
|
return $static_follow
|
||||||
|
|
||||||
|
|
||||||
func _integrate_forces(state: PhysicsDirectBodyState) -> void:
|
func _integrate_forces(state: PhysicsDirectBodyState) -> void:
|
||||||
|
@ -112,9 +121,10 @@ func _handle_gear_switch(delta: float):
|
||||||
if gear_timer > 0:
|
if gear_timer > 0:
|
||||||
gear_timer = max(0, gear_timer - delta)
|
gear_timer = max(0, gear_timer - delta)
|
||||||
if clutch_position > 0.8 or GlobalSettings.auto_clutch or GlobalSettings.automatic_transmission:
|
if clutch_position > 0.8 or GlobalSettings.auto_clutch or GlobalSettings.automatic_transmission:
|
||||||
if Input.is_action_just_pressed("gear_up"):
|
match inputs.gear_request:
|
||||||
|
GearRequest.UP:
|
||||||
_gear_up()
|
_gear_up()
|
||||||
if Input.is_action_just_pressed("gear_down"):
|
GearRequest.DOWN:
|
||||||
_gear_down()
|
_gear_down()
|
||||||
|
|
||||||
|
|
||||||
|
@ -158,10 +168,10 @@ func _lerp_rpm(from, to, delta, factor):
|
||||||
|
|
||||||
func _physics_process(delta: float):
|
func _physics_process(delta: float):
|
||||||
_update_wheels_smoke()
|
_update_wheels_smoke()
|
||||||
clutch_position = Input.get_action_strength("clutch")
|
clutch_position = inputs.clutch
|
||||||
_handle_gear_switch(delta)
|
_handle_gear_switch(delta)
|
||||||
var throttle = Input.get_action_strength("throttle")
|
var throttle = inputs.throttle
|
||||||
var brake_input = Input.get_action_strength("brake")
|
var brake_input = inputs.brake
|
||||||
|
|
||||||
var wheel_rpm = traction_wheels[0].get_rpm()
|
var wheel_rpm = traction_wheels[0].get_rpm()
|
||||||
var speed = wheel_rpm * 2.0 * PI * rrwheel.wheel_radius / 60.0 * 3600.0 / 1000.0
|
var speed = wheel_rpm * 2.0 * PI * rrwheel.wheel_radius / 60.0 * 3600.0 / 1000.0
|
||||||
|
@ -215,7 +225,7 @@ func _physics_process(delta: float):
|
||||||
brake = brake_input * max_brake_force
|
brake = brake_input * max_brake_force
|
||||||
engine_force = throttle * final_input * max_engine_force
|
engine_force = throttle * final_input * max_engine_force
|
||||||
|
|
||||||
var handbrake = Input.get_action_strength("handbrake")
|
var handbrake = inputs.handbrake
|
||||||
rrwheel.brake = handbrake * max_brake_force
|
rrwheel.brake = handbrake * max_brake_force
|
||||||
rlwheel.brake = handbrake * max_brake_force
|
rlwheel.brake = handbrake * max_brake_force
|
||||||
|
|
||||||
|
@ -228,12 +238,7 @@ func _physics_process(delta: float):
|
||||||
emit_signal("speed_updated", speed, speed / expected_max_speed)
|
emit_signal("speed_updated", speed, speed / expected_max_speed)
|
||||||
emit_signal("rpm_updated", rpm, rpm_factor)
|
emit_signal("rpm_updated", rpm, rpm_factor)
|
||||||
|
|
||||||
var steering_input = (
|
var steering_input = inputs.steering
|
||||||
Input.get_action_strength("steer_left")
|
|
||||||
- Input.get_action_strength("steer_right")
|
|
||||||
)
|
|
||||||
if abs(steering_input) < 0.05:
|
|
||||||
steering_input = 0.0
|
|
||||||
|
|
||||||
var steer_speed_factor = clamp(speed / max_steer_speed, 0.0, 1.0)
|
var steer_speed_factor = clamp(speed / max_steer_speed, 0.0, 1.0)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue