Public repository for MUR pre alpha
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.9 KiB

  1. extends Node
  2. class_name Player
  3. const MAX_SPEED:float = 0.2
  4. const START_OFFSET = [1.70223, 1.20223, 0.702227, 0.202227 ]
  5. const LANE_OFFSET = [-0.085, 0.085, -0.255, 0.255 ]
  6. export var player_color:Color
  7. var player_name:String
  8. var thrust:int = 0
  9. var current_speed:float = 0.0
  10. export var speed_factor:float = 0.001
  11. export var brake_factor:float = 2.0
  12. export var friction_brake_factor:float = 0.8
  13. onready var race_car = get_node("Path/PathFollow/raceCar")
  14. onready var timer:Timer = get_node("ResetTimer")
  15. onready var path:Path = get_node("Path")
  16. onready var follow:PathFollow = get_node("Path/PathFollow")
  17. onready var route:Route
  18. var lane:int
  19. var start_init:bool = true
  20. var has_next:bool = false
  21. var is_resetable:bool = false
  22. var buffered_curve:Curve3D
  23. var buffered_translate:Vector3 = Vector3(0,0,0)
  24. var buffered_rotation:Vector3 = Vector3(0,0,0)
  25. var last_rotation:Vector3 = Vector3(0,0,0)
  26. var torque_penalty:Vector3
  27. var force_penalty:Vector3
  28. var path_penalty:float
  29. var is_out:bool = false
  30. var position:Vector2
  31. puppet var slave_position:Vector2
  32. func _ready():
  33. route = gamestate.game.route
  34. race_car.road_ray.set_collision_mask_bit(get_index(),1)
  35. path.curve = path.curve.duplicate()
  36. path.curve.set_bake_interval(0.05)
  37. func _physics_process(delta):
  38. if is_network_master():
  39. if not is_out:
  40. var max_speed = MAX_SPEED
  41. if get_road() != null:
  42. var road = get_road()
  43. if is_in_group("first"):
  44. max_speed *= road.get_first_speed_factor()
  45. if road.get_creator() == get_index():
  46. max_speed *= road.get_creator_speed_factor()
  47. else:
  48. max_speed *= road.get_chasers_speed_factor()
  49. if thrust == 1:
  50. current_speed += speed_factor
  51. elif thrust == -1:
  52. current_speed -= speed_factor * brake_factor
  53. else:
  54. current_speed -= speed_factor * friction_brake_factor
  55. if current_speed >= max_speed:
  56. current_speed = max_speed
  57. elif current_speed < 0:
  58. current_speed = 0
  59. follow.set_offset(follow.get_offset() + current_speed)
  60. if follow.get_offset() < 0:
  61. follow.set_offset(0)
  62. if has_next && (path.curve.get_point_count() == 0 || follow.get_unit_offset() >= 1.0):
  63. has_next = false
  64. if path.curve.get_point_count() > 0:
  65. path.curve.clear_points()
  66. path.curve.set_bake_interval(0.05)
  67. for i in range(buffered_curve.get_point_count()):
  68. var _pos = buffered_curve.get_point_position(i)
  69. var _in = buffered_curve.get_point_in(i)
  70. var _out = buffered_curve.get_point_out(i)
  71. if buffered_rotation.length() != 0:
  72. _pos = _pos.rotated(buffered_rotation.normalized(), buffered_rotation.length())
  73. _in = _in.rotated(buffered_rotation.normalized(), buffered_rotation.length())
  74. _out = _out.rotated(buffered_rotation.normalized(), buffered_rotation.length())
  75. path.curve.add_point(buffered_translate + _pos, _in, _out)
  76. path.curve.set_point_tilt(i, buffered_curve.get_point_tilt(i))
  77. if not start_init:
  78. follow.set_offset(0.001)
  79. else:
  80. follow.set_offset(START_OFFSET[lane])
  81. start_init = false
  82. if get_road() != null:
  83. var road = get_road()
  84. var penalty_index = road.penalty_index(follow.get_offset(), current_speed)
  85. if penalty_index >= 0:
  86. torque_penalty = road.get_torque_penalty(penalty_index)
  87. force_penalty = road.get_force_penalty(penalty_index)
  88. path_penalty = road.get_path_penalty(penalty_index)
  89. is_out = true
  90. is_resetable = false
  91. timer.start()
  92. check_position()
  93. else:
  94. race_car.get_node("ray").set_enabled(false)
  95. if force_penalty.length() != 0:
  96. race_car.move_and_slide(force_penalty * current_speed + Vector3(0,-0.5,0))
  97. if torque_penalty.length() != 0:
  98. race_car.rotate_object_local(torque_penalty.normalized(), torque_penalty.length() * current_speed)
  99. if path_penalty > 0 && follow.get_offset() > 0.0:
  100. follow.set_offset(follow.get_offset() - 0.01)
  101. path_penalty -= 0.01
  102. current_speed -= 0.0002
  103. if current_speed < 0:
  104. current_speed = 0
  105. else:
  106. position = slave_position
  107. func get_road():
  108. return race_car.road
  109. func _on_raceCar_road_entered(road):
  110. var prev_road = route.get_road(road.get_index()-1)
  111. if prev_road != null:
  112. prev_road.body.set_collision_layer_bit(get_index(),0)
  113. buffered_curve = road.get_lane_curve(lane)
  114. buffered_rotation = last_rotation
  115. if path.curve.get_point_count() > 0:
  116. buffered_translate = Util.curve_get_last_point(path.curve)
  117. if road.get_end_rotation().length() != 0:
  118. last_rotation += road.get_end_rotation()
  119. has_next = true
  120. func get_player_name():
  121. return player_name
  122. func set_player_name(new_name):
  123. player_name = new_name
  124. func get_player_color():
  125. return player_color
  126. func set_player_color(new_color):
  127. player_color = new_color
  128. var material = SpatialMaterial.new()
  129. material.set_albedo(player_color)
  130. race_car.get_node("body").set_surface_material(1,material)
  131. remotesync func set_first(is_first:bool):
  132. if is_first:
  133. add_to_group("first")
  134. else:
  135. remove_from_group("first")
  136. master func check_position():
  137. position = Vector2(race_car.road_index, follow.get_offset())
  138. rset("slave_position", position)
  139. master func reset():
  140. if is_resetable:
  141. has_next = false
  142. current_speed = 0
  143. var road = race_car.road
  144. road.body.set_collision_layer_bit(get_index(),1)
  145. while road.reset_index != 0:
  146. road = route.get_road(road.get_index() - road.reset_index)
  147. road.body.set_collision_layer_bit(get_index(),1)
  148. path.curve.clear_points()
  149. force_penalty = Vector3(0,0,0)
  150. torque_penalty = Vector3(0,0,0)
  151. buffered_translate = road.get_translation()
  152. last_rotation = road.get_rotation()
  153. race_car.road_index = road.get_index()
  154. race_car.road = null
  155. race_car.collider = null
  156. race_car.transform = Transform()
  157. race_car.get_node("ray").set_enabled(true)
  158. follow.set_transform(road.get_transform())
  159. is_out = false
  160. is_resetable = false
  161. return true
  162. else:
  163. return false
  164. func set_start(new_lane:int):
  165. lane = new_lane
  166. follow.set_h_offset(LANE_OFFSET[lane])
  167. func _on_ResetTimer_timeout():
  168. is_resetable = true