Arcade racing demo got Godot 3.3
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

121 lines
3.2KB

  1. class_name PlayerCTRL
  2. extends Node
  3. const MAX_THROTTLE_TIME = 0.1
  4. const MAX_BREAKING_TIME = 0.25
  5. const MAX_STEERING_TIME = 0.1
  6. var throttle = 0.0
  7. var throttle_active = false
  8. var breaking = 0.0
  9. var breaking_active = false
  10. var steering = 0.0
  11. var steering_left = 0
  12. var steering_right = 0
  13. var steering_state = 0
  14. var rev_tog_down = false
  15. var parent : Vehicle = null
  16. onready var tw_throttle = $Tween_Throttle
  17. onready var tw_breaking = $Tween_Breaking
  18. onready var tw_steering = $Tween_Steering
  19. func _ready():
  20. var pnode = get_parent()
  21. if pnode is Vehicle:
  22. parent = pnode
  23. else:
  24. print("ERROR: Player Controller not attached to a vehicle object.")
  25. func _time_to_target(cur : float, targ : float, size : float, time : float) -> float:
  26. var dist = abs(targ - cur)
  27. return time * (dist / size)
  28. func _set_tween(enable : bool, prop : String, active : bool, cval : float, max_time : float, tween : Tween) -> bool:
  29. var process = false
  30. var time = 0.0
  31. var target = 0.0
  32. if enable != active:
  33. active = enable
  34. process = true
  35. if enable:
  36. target = 1.0
  37. time = _time_to_target(cval, target, 1.0, max_time)
  38. if process:
  39. tween.remove_all()
  40. tween.interpolate_property(self, prop, cval, target, time, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
  41. tween.start()
  42. return active
  43. func _throttle(enable : bool) -> void:
  44. throttle_active = _set_tween(enable, "throttle", throttle_active, throttle, MAX_THROTTLE_TIME, tw_throttle)
  45. func _break(enable : bool) -> void:
  46. breaking_active = _set_tween(enable, "breaking", breaking_active, breaking, MAX_BREAKING_TIME, tw_breaking)
  47. func _steer(dir) -> void:
  48. if dir != steering_state:
  49. steering_state = dir
  50. var time = 0.0
  51. var target = 0.0
  52. match(steering_state):
  53. 1: # right
  54. target = 1
  55. time = _time_to_target(steering, target, 2, MAX_STEERING_TIME*2)
  56. -1: # left
  57. target = -1
  58. time = _time_to_target(steering, target, 2, MAX_STEERING_TIME*2)
  59. 0: # center
  60. time = _time_to_target(steering, target, 1, MAX_STEERING_TIME)
  61. tw_steering.remove_all()
  62. tw_steering.interpolate_property(self, "steering", steering, target, time, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
  63. tw_steering.start()
  64. func _unhandled_input(event):
  65. var time = 0.0
  66. if event.is_action_pressed("accel"):
  67. _throttle(true)
  68. if event.is_action_released("accel"):
  69. _throttle(false)
  70. if event.is_action_pressed("break"):
  71. _break(true)
  72. if event.is_action_released("break"):
  73. _break(false)
  74. if event.is_action_pressed("reverse_toggle"):
  75. if not rev_tog_down:
  76. parent.toggle_reverse()
  77. rev_tog_down = true
  78. if event.is_action_released("reverse_toggle"):
  79. rev_tog_down = false
  80. var handle_steering = false
  81. if event.is_action("turn_left"):
  82. if event.is_action_pressed("turn_left"):
  83. steering_left = -1
  84. elif event.is_action_released("turn_left"):
  85. steering_left = 0
  86. handle_steering = true
  87. if event.is_action("turn_right"):
  88. if event.is_action_pressed("turn_right"):
  89. steering_right = 1
  90. elif event.is_action_released("turn_right"):
  91. steering_right = 0
  92. handle_steering = true
  93. if handle_steering:
  94. _steer(steering_right + steering_left)
  95. func _process(delta):
  96. if parent:
  97. parent.set_throttle(throttle)
  98. parent.set_break(breaking)
  99. parent.set_steering(steering)