A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth
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.

223 lines
6.5 KiB

  1. # Original: https://raw.githubusercontent.com/Calinou/fps-test/master/scripts/player.gd
  2. extends RigidBody
  3. var view_sensitivity = 0.25
  4. # Walking speed and jumping height are defined later.
  5. var walk_speed = 0.8 # Actually acceleration; m/s/s
  6. var jump_speed = 1 # m/s
  7. var air_accel = .1 # m/s/s
  8. var floor_friction = 1-0.08
  9. var air_friction = 1-0.03
  10. var player_info # Set by lobby
  11. var walk_speed_build = 0.006 # `walk_speed` per `switch_charge`
  12. var air_speed_build = 0.006 # `air_accel` per `switch_chare`
  13. var switch_charge = 0
  14. var switch_charge_cap = 200 # While switching is always at 100, things like speed boost might go higher!
  15. var movement_charge = 0.15 # In percent per meter (except when heroes change that)
  16. const fall_height = -50
  17. var debug_node
  18. var recording = { "time": 0, "states": [], "spawn": Vector3() }
  19. slave var slave_transform = Basis()
  20. slave var slave_lin_v = Vector3()
  21. slave var slave_ang_v = Vector3()
  22. export(NodePath) var tp_camera
  23. export(NodePath) var master_only
  24. func _ready():
  25. set_process_input(true)
  26. debug_node = get_node("/root/Level/Debug")
  27. if is_network_master():
  28. get_node(tp_camera).set_enabled(true)
  29. spawn()
  30. else:
  31. remove_child(get_node(master_only))
  32. func spawn():
  33. var placement = Vector3()
  34. var x_varies = 10
  35. var y_varies = 20
  36. # No Z, because that's the left-right question
  37. if player_info.is_right_team:
  38. placement = get_node("/root/Level/RightSpawn").get_translation()
  39. else:
  40. placement = get_node("/root/Level/LeftSpawn").get_translation()
  41. # So we don't all spawn on top of each other
  42. placement.x += rand_range(0, x_varies)
  43. placement.y += rand_range(0, y_varies)
  44. recording.spawn = placement
  45. set_transform(Basis())
  46. set_translation(placement)
  47. set_linear_velocity(Vector3())
  48. func _input(event):
  49. if is_network_master():
  50. if Input.is_action_just_pressed("switch_hero"):
  51. switch_hero_interface()
  52. # Quit the game:
  53. if Input.is_action_pressed("quit"):
  54. quit()
  55. func toggle_mouse_capture():
  56. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  57. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  58. view_sensitivity = 0
  59. else:
  60. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  61. view_sensitivity = 0.25
  62. # Update visual yaw + pitch components to match camera
  63. func set_rotation():
  64. get_node("Yaw").set_rotation(Vector3(0, deg2rad(get_node(tp_camera).cam_yaw), 0))
  65. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-get_node(tp_camera).cam_pitch), 0, 0))
  66. func record_status(status):
  67. if "record" in player_info:
  68. for i in range(status.size()):
  69. status[i] = var2str(status[i])
  70. recording.states.append([recording.time, status])
  71. func _integrate_forces(state):
  72. if is_network_master():
  73. control_player(state)
  74. var status = get_status()
  75. rpc_unreliable("set_status", status)
  76. record_status(status)
  77. set_rotation()
  78. slave func set_status(s):
  79. set_transform(s[0])
  80. set_linear_velocity(s[1])
  81. set_angular_velocity(s[2])
  82. get_node(tp_camera).cam_yaw = s[3]
  83. get_node(tp_camera).cam_pitch = s[4]
  84. func get_status():
  85. return [
  86. get_transform(),
  87. get_linear_velocity(),
  88. get_angular_velocity(),
  89. get_node(tp_camera).cam_yaw,
  90. get_node(tp_camera).cam_pitch,
  91. ]
  92. func control_player(state):
  93. var aim = get_node("Yaw").get_global_transform().basis
  94. var direction = Vector3()
  95. if Input.is_action_pressed("move_forwards"):
  96. direction -= aim[2]
  97. if Input.is_action_pressed("move_backwards"):
  98. direction += aim[2]
  99. if Input.is_action_pressed("move_left"):
  100. direction -= aim[0]
  101. if Input.is_action_pressed("move_right"):
  102. direction += aim[0]
  103. direction = direction.normalized()
  104. var ray = get_node("Ray")
  105. if ray.is_colliding():
  106. var up = state.get_total_gravity().normalized()
  107. var normal = ray.get_collision_normal()
  108. var floor_velocity = Vector3()
  109. var object = ray.get_collider()
  110. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  111. state.apply_impulse(Vector3(), direction * accel * get_mass())
  112. var lin_v = state.get_linear_velocity()
  113. lin_v.x *= floor_friction
  114. lin_v.z *= floor_friction
  115. state.set_linear_velocity(lin_v)
  116. if Input.is_action_pressed("jump"):
  117. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  118. else:
  119. var accel = (1 + switch_charge * air_speed_build) * air_accel
  120. state.apply_impulse(Vector3(), direction * accel * get_mass())
  121. var lin_v = state.get_linear_velocity()
  122. lin_v.x *= air_friction
  123. lin_v.z *= air_friction
  124. state.set_linear_velocity(lin_v)
  125. state.integrate_forces()
  126. func _process(delta):
  127. # All player code not caused by input, and not causing movement
  128. if is_network_master():
  129. var vel = get_linear_velocity()
  130. switch_charge += movement_charge * vel.length() * delta
  131. var switch_node = get_node("MasterOnly/SwitchCharge")
  132. switch_node.set_text("%.f%%" % switch_charge)
  133. if switch_charge >= 100:
  134. # Let switch_charge keep building, because we use it for walk_speed and things
  135. switch_node.set_text("100%% (%.f)\nQ - Switch hero" % switch_charge)
  136. if switch_charge > switch_charge_cap:
  137. # There is however a cap
  138. switch_charge = switch_charge_cap
  139. if get_translation().y < fall_height:
  140. spawn()
  141. switch_hero_interface()
  142. if "record" in player_info:
  143. recording.time += delta
  144. func switch_hero_interface():
  145. # Interface needs the mouse!
  146. toggle_mouse_capture()
  147. # Pause so if we have walls and such nothing funny happens
  148. get_tree().set_pause(true)
  149. var interface = preload("res://scenes/HeroSelect.tscn").instance()
  150. add_child(interface)
  151. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  152. func switch_hero_master():
  153. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  154. # Remove the mouse and enable looking again
  155. toggle_mouse_capture()
  156. get_tree().set_pause(false)
  157. sync func switch_hero(hero):
  158. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  159. var net_id = int(get_name())
  160. set_name("%d-delete" % net_id) # Can't have duplicate names
  161. new_hero.set_name("%d" % net_id)
  162. new_hero.set_network_master(net_id)
  163. new_hero.player_info = player_info
  164. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  165. # We must wait until after _ready is called, so that we don't end up at spawn
  166. new_hero.call_deferred("set_status", get_status())
  167. queue_free()
  168. func _exit_scene():
  169. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  170. if "record" in player_info:
  171. write_recording()
  172. # Functions
  173. # =========
  174. func write_recording():
  175. var save = File.new()
  176. var fname = "res://recordings/%d-%d-%d.rec" % [player_info.level, player_info.hero, randi() % 10000]
  177. save.open(fname, File.WRITE)
  178. save.store_line(to_json(recording.states))
  179. save.close()
  180. # Quits the game:
  181. func quit():
  182. if "record" in player_info:
  183. write_recording()
  184. get_tree().quit()