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.

251 lines
7.2 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": [], "events": [], "spawn": Vector3() }
  19. slave var slave_transform = Basis()
  20. slave var slave_lin_v = Vector3()
  21. slave var slave_ang_v = Vector3()
  22. var tp_camera = "TPCamera"
  23. var master_only = "MasterOnly"
  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. if "is_ai" in player_info and player_info.is_ai:
  33. add_child(preload("res://scenes/ai.tscn").instance())
  34. func spawn():
  35. var placement = Vector3()
  36. var x_varies = 5
  37. var z_varies = 5
  38. # No Z, because that's the left-right question
  39. if player_info.is_right_team:
  40. placement = get_node("/root/Level/RightSpawn").get_translation()
  41. else:
  42. placement = get_node("/root/Level/LeftSpawn").get_translation()
  43. # So we don't all spawn on top of each other
  44. placement.x += rand_range(0, x_varies)
  45. placement.z += rand_range(0, z_varies)
  46. recording.spawn = var2str(placement)
  47. set_transform(Basis())
  48. set_translation(placement)
  49. set_linear_velocity(Vector3())
  50. func event_to_obj(event):
  51. var d = {}
  52. if event is InputEventMouseMotion:
  53. d.relative = {}
  54. d.relative.x = event.relative.x
  55. d.relative.y = event.relative.y
  56. d.type = "motion"
  57. if event is InputEventKey:
  58. d.scancode = event.scancode
  59. d.pressed = event.pressed
  60. d.type = "key"
  61. if event is InputEventMouseButton:
  62. d.button_index = event.button_index
  63. d.pressed = event.pressed
  64. d.type = "mb"
  65. return d
  66. func _input(event):
  67. if is_network_master():
  68. if Input.is_action_just_pressed("switch_hero"):
  69. switch_hero_interface()
  70. # Quit the game:
  71. if Input.is_action_pressed("quit"):
  72. quit()
  73. if "record" in player_info:
  74. recording.events.append([recording.time, event_to_obj(event)])
  75. func toggle_mouse_capture():
  76. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  77. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  78. view_sensitivity = 0
  79. else:
  80. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  81. view_sensitivity = 0.25
  82. # Update visual yaw + pitch components to match camera
  83. func set_rotation():
  84. get_node("Yaw").set_rotation(Vector3(0, deg2rad(get_node(tp_camera).cam_yaw), 0))
  85. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-get_node(tp_camera).cam_pitch), 0, 0))
  86. func record_status(status):
  87. if "record" in player_info:
  88. for i in range(status.size()):
  89. status[i] = var2str(status[i])
  90. recording.states.append([recording.time, status])
  91. func _integrate_forces(state):
  92. if is_network_master():
  93. control_player(state)
  94. var status = get_status()
  95. rpc_unreliable("set_status", status)
  96. record_status(status)
  97. set_rotation()
  98. slave func set_status(s):
  99. set_transform(s[0])
  100. set_linear_velocity(s[1])
  101. set_angular_velocity(s[2])
  102. get_node(tp_camera).cam_yaw = s[3]
  103. get_node(tp_camera).cam_pitch = s[4]
  104. func get_status():
  105. return [
  106. get_transform(),
  107. get_linear_velocity(),
  108. get_angular_velocity(),
  109. get_node(tp_camera).cam_yaw,
  110. get_node(tp_camera).cam_pitch,
  111. ]
  112. func control_player(state):
  113. var aim = get_node("Yaw").get_global_transform().basis
  114. var direction = Vector3()
  115. if Input.is_action_pressed("move_forwards"):
  116. direction -= aim[2]
  117. if Input.is_action_pressed("move_backwards"):
  118. direction += aim[2]
  119. if Input.is_action_pressed("move_left"):
  120. direction -= aim[0]
  121. if Input.is_action_pressed("move_right"):
  122. direction += aim[0]
  123. direction = direction.normalized()
  124. var ray = get_node("Ray")
  125. if ray.is_colliding():
  126. var up = state.get_total_gravity().normalized()
  127. var normal = ray.get_collision_normal()
  128. var floor_velocity = Vector3()
  129. var object = ray.get_collider()
  130. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  131. state.apply_impulse(Vector3(), direction * accel * get_mass())
  132. var lin_v = state.get_linear_velocity()
  133. lin_v.x *= floor_friction
  134. lin_v.z *= floor_friction
  135. state.set_linear_velocity(lin_v)
  136. if Input.is_action_pressed("jump"):
  137. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  138. else:
  139. var accel = (1 + switch_charge * air_speed_build) * air_accel
  140. state.apply_impulse(Vector3(), direction * accel * get_mass())
  141. var lin_v = state.get_linear_velocity()
  142. lin_v.x *= air_friction
  143. lin_v.z *= air_friction
  144. state.set_linear_velocity(lin_v)
  145. state.integrate_forces()
  146. func _process(delta):
  147. # All player code not caused by input, and not causing movement
  148. if is_network_master():
  149. var vel = get_linear_velocity()
  150. switch_charge += movement_charge * vel.length() * delta
  151. var switch_node = get_node("MasterOnly/SwitchCharge")
  152. switch_node.set_text("%.f%%" % switch_charge)
  153. if switch_charge >= 100:
  154. # Let switch_charge keep building, because we use it for walk_speed and things
  155. switch_node.set_text("100%% (%.f)\nQ - Switch hero" % switch_charge)
  156. if switch_charge > switch_charge_cap:
  157. # There is however a cap
  158. switch_charge = switch_charge_cap
  159. if get_translation().y < fall_height:
  160. spawn()
  161. switch_hero_interface()
  162. if "record" in player_info:
  163. recording.time += delta
  164. if "record" in player_info:
  165. recording.time += delta
  166. func switch_hero_interface():
  167. if switch_charge >= 100:
  168. # Interface needs the mouse!
  169. toggle_mouse_capture()
  170. # Pause so if we have walls and such nothing funny happens
  171. get_tree().set_pause(true)
  172. var interface = preload("res://scenes/HeroSelect.tscn").instance()
  173. add_child(interface)
  174. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  175. func switch_hero_master():
  176. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  177. # Remove the mouse and enable looking again
  178. toggle_mouse_capture()
  179. get_tree().set_pause(false)
  180. sync func switch_hero(hero):
  181. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  182. var net_id = int(get_name())
  183. set_name("%d-delete" % net_id) # Can't have duplicate names
  184. new_hero.set_name("%d" % net_id)
  185. new_hero.set_network_master(net_id)
  186. new_hero.player_info = player_info
  187. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  188. # We must wait until after _ready is called, so that we don't end up at spawn
  189. new_hero.call_deferred("set_status", get_status())
  190. queue_free()
  191. func _exit_scene():
  192. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  193. func _exit_tree():
  194. if "record" in player_info:
  195. write_recording()
  196. # Functions
  197. # =========
  198. func write_recording():
  199. var save = File.new()
  200. var fname = "res://recordings/%d-%d-%d.rec" % [player_info.level, player_info.hero, randi() % 10000]
  201. save.open(fname, File.WRITE)
  202. save.store_line(to_json(recording))
  203. save.close()
  204. # Quits the game:
  205. func quit():
  206. if "record" in player_info:
  207. write_recording()
  208. get_tree().quit()