|
- class_name PlayerCTRL
- extends Node
-
- const MAX_THROTTLE_TIME = 0.1
- const MAX_BREAKING_TIME = 0.25
- const MAX_STEERING_TIME = 0.1
-
-
- var throttle = 0.0
- var throttle_active = false
- var breaking = 0.0
- var breaking_active = false
- var steering = 0.0
- var steering_left = 0
- var steering_right = 0
- var steering_state = 0
- var rev_tog_down = false
-
- var parent : Vehicle = null
-
- onready var tw_throttle = $Tween_Throttle
- onready var tw_breaking = $Tween_Breaking
- onready var tw_steering = $Tween_Steering
-
- func _ready():
- var pnode = get_parent()
- if pnode is Vehicle:
- parent = pnode
- else:
- print("ERROR: Player Controller not attached to a vehicle object.")
-
- func _time_to_target(cur : float, targ : float, size : float, time : float) -> float:
- var dist = abs(targ - cur)
- return time * (dist / size)
-
- func _set_tween(enable : bool, prop : String, active : bool, cval : float, max_time : float, tween : Tween) -> bool:
- var process = false
- var time = 0.0
- var target = 0.0
-
- if enable != active:
- active = enable
- process = true
- if enable:
- target = 1.0
- time = _time_to_target(cval, target, 1.0, max_time)
-
- if process:
- tween.remove_all()
- tween.interpolate_property(self, prop, cval, target, time, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
- tween.start()
- return active
-
- func _throttle(enable : bool) -> void:
- throttle_active = _set_tween(enable, "throttle", throttle_active, throttle, MAX_THROTTLE_TIME, tw_throttle)
-
- func _break(enable : bool) -> void:
- breaking_active = _set_tween(enable, "breaking", breaking_active, breaking, MAX_BREAKING_TIME, tw_breaking)
-
- func _steer(dir) -> void:
- if dir != steering_state:
- steering_state = dir
- var time = 0.0
- var target = 0.0
- match(steering_state):
- 1: # right
- target = 1
- time = _time_to_target(steering, target, 2, MAX_STEERING_TIME*2)
- -1: # left
- target = -1
- time = _time_to_target(steering, target, 2, MAX_STEERING_TIME*2)
- 0: # center
- time = _time_to_target(steering, target, 1, MAX_STEERING_TIME)
-
- tw_steering.remove_all()
- tw_steering.interpolate_property(self, "steering", steering, target, time, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
- tw_steering.start()
-
- func _unhandled_input(event):
- var time = 0.0
- if event.is_action_pressed("accel"):
- _throttle(true)
- if event.is_action_released("accel"):
- _throttle(false)
- if event.is_action_pressed("break"):
- _break(true)
- if event.is_action_released("break"):
- _break(false)
- if event.is_action_pressed("reverse_toggle"):
- if not rev_tog_down:
- parent.toggle_reverse()
- rev_tog_down = true
- if event.is_action_released("reverse_toggle"):
- rev_tog_down = false
-
- var handle_steering = false
- if event.is_action("turn_left"):
- if event.is_action_pressed("turn_left"):
- steering_left = -1
- elif event.is_action_released("turn_left"):
- steering_left = 0
- handle_steering = true
- if event.is_action("turn_right"):
- if event.is_action_pressed("turn_right"):
- steering_right = 1
- elif event.is_action_released("turn_right"):
- steering_right = 0
- handle_steering = true
- if handle_steering:
- _steer(steering_right + steering_left)
-
- func _process(delta):
- if parent:
- parent.set_throttle(throttle)
- parent.set_break(breaking)
- parent.set_steering(steering)
-
-
-
|