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.

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