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.

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