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.

75 lines
2.0 KiB

  1. extends Spatial
  2. class_name BotPlayer
  3. var player:Player
  4. const MAX_OFFSET = 8.0
  5. const OFFSET_STEPS = 0.1
  6. const PRE_BREAK_DISTANCE = 3.0
  7. var break_constrain = 0.0
  8. var break_distance = 0.0
  9. var thrust_distance = 0.0
  10. export var difficulty:float = 0.0
  11. var rate = 0.0
  12. func _process(delta):
  13. if is_network_master() && player != null:
  14. if player.is_out():
  15. if player.is_resetable:
  16. player.reset()
  17. else:
  18. if player.get_road() != null:
  19. var road = player.get_road()
  20. var speed = player.current_speed
  21. var offset = 0
  22. var distance = player.follow.get_offset() - OFFSET_STEPS
  23. var tmp_constrain = 0.0
  24. break_constrain = 0.0
  25. break_distance = 0.0
  26. thrust_distance = 0.0
  27. while offset <= MAX_OFFSET && road != null:
  28. var constrain_index = road.get_constrain_index(distance)
  29. var road_length = road.get_lane_curve(player.lane).get_baked_length()
  30. if constrain_index >= 0:
  31. tmp_constrain = road.speed_constrains[constrain_index].z
  32. if tmp_constrain > 0 && (break_constrain == 0.0 || tmp_constrain < break_constrain):
  33. break_constrain = tmp_constrain
  34. if break_distance == 0:
  35. break_distance = offset
  36. if tmp_constrain < 0 && thrust_distance == 0:
  37. thrust_distance = offset
  38. distance += OFFSET_STEPS
  39. offset += OFFSET_STEPS
  40. if distance >= road_length && gamestate.game != null:
  41. distance = 0
  42. road = gamestate.game.route.get_road(road.get_index()+1)
  43. if break_constrain > 0 && speed > break_constrain - 0.005 && break_distance < thrust_distance + PRE_BREAK_DISTANCE && rate < difficulty:
  44. player.thrust = -1
  45. elif speed - 0.01 < break_constrain:
  46. player.thrust = 1
  47. elif break_constrain == 0.0 && rate < difficulty || speed < 0.02:
  48. player.thrust = 1
  49. else:
  50. player.thrust = 0
  51. func set_player(path:String):
  52. player = get_node(path)
  53. get_node("inventory").set_player(player)
  54. func set_difficulty(new_difficulty:float):
  55. difficulty = new_difficulty
  56. func _on_rate_timer_timeout():
  57. rate = util.randf()