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.

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