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.

229 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. elif 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. var road = get_road()
  95. race_car.get_node("ray").set_enabled(false)
  96. if force_penalty.length() != 0:
  97. race_car.move_and_slide(force_penalty * current_speed + Vector3(0,-0.5,0))
  98. if torque_penalty.length() != 0:
  99. race_car.rotate_object_local(torque_penalty.normalized(), torque_penalty.length() * current_speed)
  100. if path_penalty > 0 && follow.get_offset() > 0.0:
  101. follow.set_offset(follow.get_offset() - 0.01)
  102. path_penalty -= 0.01
  103. current_speed -= 0.0002
  104. if current_speed < 0:
  105. current_speed = 0
  106. else:
  107. position = slave_position
  108. func get_road():
  109. return race_car.road
  110. func _on_raceCar_road_entered(road):
  111. var prev_road = route.get_road(road.get_index()-1)
  112. if prev_road != null:
  113. prev_road.body.set_collision_layer_bit(get_index(),0)
  114. buffered_curve = road.get_lane_curve(lane)
  115. buffered_rotation = last_rotation
  116. if path.curve.get_point_count() > 0:
  117. buffered_translate = util.curve_get_last_point(path.curve)
  118. if road.get_end_rotation().length() != 0:
  119. last_rotation += road.get_end_rotation()
  120. has_next = true
  121. func get_player_name():
  122. return player_name
  123. func set_player_name(new_name):
  124. player_name = new_name
  125. func get_player_color():
  126. return player_color
  127. func set_player_color(new_color):
  128. player_color = new_color
  129. var material = SpatialMaterial.new()
  130. material.set_albedo(player_color)
  131. race_car.get_node("body").set_surface_material(1,material)
  132. remotesync func set_first(is_first:bool):
  133. if is_first:
  134. add_to_group("first")
  135. else:
  136. remove_from_group("first")
  137. master func check_position():
  138. position = Vector2(race_car.road_index, follow.get_offset())
  139. rset("slave_position", position)
  140. master func reset():
  141. if is_resetable:
  142. has_next = false
  143. current_speed = 0
  144. var road = race_car.road
  145. road.body.set_collision_layer_bit(get_index(),1)
  146. while road.reset_index != 0:
  147. road = route.get_road(road.get_index() - road.reset_index)
  148. road.body.set_collision_layer_bit(get_index(),1)
  149. path.curve.clear_points()
  150. force_penalty = Vector3(0,0,0)
  151. torque_penalty = Vector3(0,0,0)
  152. buffered_translate = road.get_translation()
  153. last_rotation = road.get_rotation()
  154. race_car.road_index = road.get_index()
  155. race_car.road = null
  156. race_car.collider = null
  157. race_car.transform = Transform()
  158. race_car.get_node("ray").set_enabled(true)
  159. follow.set_transform(road.get_transform())
  160. is_out = false
  161. is_resetable = false
  162. return true
  163. else:
  164. return false
  165. func set_start(new_lane:int):
  166. lane = new_lane
  167. follow.set_h_offset(LANE_OFFSET[lane])
  168. func _on_ResetTimer_timeout():
  169. is_resetable = true