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.

310 lines
9.5 KiB

  1. # Original: https://raw.githubusercontent.com/Calinou/fps-test/master/scripts/player.gd
  2. # All heroes extend from here. Implements all common behavior
  3. extends RigidBody
  4. var view_sensitivity = 0.25
  5. # Walking speed and jumping height are defined later.
  6. var walk_speed = 0.8 # Actually acceleration; m/s/s
  7. var jump_speed = 5 # m/s
  8. var air_accel = .1 # m/s/s
  9. var floor_friction = 1-0.08
  10. var air_friction = 1-0.03
  11. var player_info # Set by lobby
  12. var walk_speed_build = 0.006 # `walk_speed` per `switch_charge`
  13. var air_speed_build = 0.006 # `air_accel` per `switch_charge`
  14. sync var switch_charge = 0
  15. var switch_charge_cap = 200 # While switching is always at 100, things like speed boost might go higher!
  16. var movement_charge = 0.1 # In percent per meter (except when heroes change that)
  17. var fall_height = -400 # This is essentially the respawn timer
  18. var switch_height = -150 # At this point, stop adding to switch_charge. This makes falls not charge you too much
  19. var debug_node
  20. var recording
  21. slave var slave_transform = Basis()
  22. slave var slave_lin_v = Vector3()
  23. slave var slave_ang_v = Vector3()
  24. var tp_camera = "TPCamera"
  25. var master_only = "MasterOnly"
  26. var master_player
  27. var friend_color = Color("#4ab0e5") # Blue
  28. var enemy_color = Color("#f04273") # Red
  29. var ai_instanced = false
  30. signal spawn
  31. var colored_meshes = [
  32. "Yaw/MainMesh",
  33. "Yaw/Pitch/RotatedHead",
  34. ]
  35. func _ready():
  36. set_process_input(true)
  37. debug_node = get_node("/root/Level/Debug")
  38. if is_network_master():
  39. get_node("TPCamera/Camera/Ray").add_exception(self)
  40. get_node(tp_camera).set_enabled(true)
  41. spawn()
  42. if "is_ai" in player_info and player_info.is_ai and not ai_instanced:
  43. add_child(preload("res://scenes/ai.tscn").instance())
  44. ai_instanced = true
  45. else:
  46. get_node("PlayerName").set_text(player_info.username)
  47. # Remove HUD
  48. remove_child(get_node(master_only))
  49. func _input(event):
  50. if is_network_master():
  51. if Input.is_action_just_pressed("switch_hero"):
  52. switch_hero_interface()
  53. # Quit the game:
  54. if Input.is_action_pressed("quit"):
  55. quit()
  56. if "record" in player_info:
  57. recording.events.append([recording.time, event_to_obj(event)])
  58. func _process(delta):
  59. # All player code not caused by input, and not causing movement
  60. if is_network_master():
  61. var vel = get_linear_velocity()
  62. if translation.y < switch_height:
  63. vel.y = 0 # Don't gain charge from falling when below switch_height
  64. switch_charge += movement_charge * vel.length() * delta
  65. var switch_node = get_node("MasterOnly/SwitchCharge")
  66. switch_node.set_text("%d%%" % int(switch_charge)) # We truncate, rather than round, so that switch is displayed AT 100%
  67. if switch_charge >= 100:
  68. # Let switch_charge keep building, because we use it for walk_speed and things
  69. switch_node.set_text("100%% (%.f)\nQ - Switch hero" % switch_charge)
  70. if switch_charge > switch_charge_cap:
  71. # There is however a cap
  72. switch_charge = switch_charge_cap
  73. if get_translation().y < fall_height:
  74. rpc("spawn")
  75. if "record" in player_info:
  76. recording.time += delta
  77. rset_unreliable("switch_charge", switch_charge)
  78. func _integrate_forces(state):
  79. if is_network_master():
  80. control_player(state)
  81. var status = get_status()
  82. rpc_unreliable("set_status", status)
  83. record_status(status)
  84. set_rotation()
  85. func _exit_tree():
  86. if "record" in player_info:
  87. write_recording()
  88. # Functions
  89. # =========
  90. sync func spawn():
  91. emit_signal("spawn")
  92. if "record" in player_info:
  93. write_recording() # Write each spawn as a separate recording
  94. var placement = Vector3()
  95. var x_varies = 5
  96. var z_varies = 5
  97. # No Z, because that's the left-right question
  98. if player_info.is_right_team:
  99. placement = get_node("/root/Level/RightSpawn").get_translation()
  100. else:
  101. placement = get_node("/root/Level/LeftSpawn").get_translation()
  102. # So we don't all spawn on top of each other
  103. placement.x += rand_range(0, x_varies)
  104. placement.z += rand_range(0, z_varies)
  105. recording = { "time": 0, "states": [], "events": [], "spawn": Vector3() }
  106. recording.spawn = var2str(placement)
  107. recording.switch_charge = var2str(switch_charge)
  108. set_transform(Basis())
  109. set_translation(placement)
  110. set_linear_velocity(Vector3())
  111. get_node(tp_camera).cam_yaw = 0
  112. get_node(tp_camera).cam_pitch = 0
  113. func event_to_obj(event):
  114. var d = {}
  115. if event is InputEventMouseMotion:
  116. d.relative = {}
  117. d.relative.x = event.relative.x
  118. d.relative.y = event.relative.y
  119. d.type = "motion"
  120. if event is InputEventKey:
  121. d.scancode = event.scancode
  122. d.pressed = event.pressed
  123. d.echo = event.echo
  124. d.type = "key"
  125. if event is InputEventMouseButton:
  126. d.button_index = event.button_index
  127. d.pressed = event.pressed
  128. d.type = "mb"
  129. return d
  130. func begin():
  131. master_player = util.get_master_player()
  132. # Set color to blue (teammate) or red (enemy)
  133. var color
  134. if master_player.player_info.is_right_team == player_info.is_right_team:
  135. color = friend_color
  136. else:
  137. color = enemy_color
  138. # We have a base MaterialSettings to use inheritance with heroes
  139. # Unfortunately we cannot do this with the actual meshes,
  140. # because godot decides if you change the mesh you wanted to change the material as well
  141. # So "MaterialSettings" is a dummy mesh in player.tscn that's hidden
  142. # We call .duplicate() so we can set this color without messing with other players' colors
  143. var mat = get_node("MaterialSettings").get_surface_material(0).duplicate()
  144. mat.albedo_color = color
  145. for mesh in colored_meshes:
  146. get_node(mesh).set_surface_material(0, mat)
  147. func toggle_mouse_capture():
  148. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  149. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  150. view_sensitivity = 0
  151. else:
  152. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  153. view_sensitivity = 0.25
  154. # Update visual yaw + pitch components to match camera
  155. func set_rotation():
  156. get_node("Yaw").set_rotation(Vector3(0, deg2rad(get_node(tp_camera).cam_yaw), 0))
  157. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-get_node(tp_camera).cam_pitch), 0, 0))
  158. func record_status(status):
  159. if "record" in player_info:
  160. for i in range(status.size()):
  161. status[i] = var2str(status[i])
  162. recording.states.append([recording.time, status])
  163. slave func set_status(s):
  164. set_transform(s[0])
  165. set_linear_velocity(s[1])
  166. set_angular_velocity(s[2])
  167. get_node(tp_camera).cam_yaw = s[3]
  168. get_node(tp_camera).cam_pitch = s[4]
  169. func get_status():
  170. return [
  171. get_transform(),
  172. get_linear_velocity(),
  173. get_angular_velocity(),
  174. get_node(tp_camera).cam_yaw,
  175. get_node(tp_camera).cam_pitch,
  176. ]
  177. func control_player(state):
  178. var aim = get_node("Yaw").get_global_transform().basis
  179. var direction = Vector3()
  180. if Input.is_action_pressed("move_forwards"):
  181. direction -= aim[2]
  182. if Input.is_action_pressed("move_backwards"):
  183. direction += aim[2]
  184. if Input.is_action_pressed("move_left"):
  185. direction -= aim[0]
  186. if Input.is_action_pressed("move_right"):
  187. direction += aim[0]
  188. direction = direction.normalized()
  189. var ray = get_node("Ray")
  190. # Detect jumpable
  191. var jumpable = false
  192. var jump_dot = 0.5 # If normal.dot(up) > jump_dot, we can jump
  193. for i in range(state.get_contact_count()):
  194. var n = state.get_contact_local_normal(i)
  195. if n.dot(Vector3(0,1,0)) > jump_dot:
  196. jumpable = true
  197. if jumpable: # We can navigate normally, we have a surface
  198. var up = state.get_total_gravity().normalized()
  199. var normal = ray.get_collision_normal()
  200. var floor_velocity = Vector3()
  201. var object = ray.get_collider()
  202. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  203. state.apply_impulse(Vector3(), direction * accel * get_mass())
  204. var lin_v = state.get_linear_velocity()
  205. lin_v.x *= floor_friction
  206. lin_v.z *= floor_friction
  207. state.set_linear_velocity(lin_v)
  208. if Input.is_action_just_pressed("jump"):
  209. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  210. else:
  211. var accel = (1 + switch_charge * air_speed_build) * air_accel
  212. state.apply_impulse(Vector3(), direction * accel * get_mass())
  213. var lin_v = state.get_linear_velocity()
  214. lin_v.x *= air_friction
  215. lin_v.z *= air_friction
  216. state.set_linear_velocity(lin_v)
  217. state.integrate_forces()
  218. func switch_hero_interface():
  219. if switch_charge >= 100:
  220. # Interface needs the mouse!
  221. toggle_mouse_capture()
  222. # Pause so if we have walls and such nothing funny happens
  223. get_tree().set_pause(true)
  224. var interface = preload("res://scenes/hero_select.tscn").instance()
  225. add_child(interface)
  226. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  227. func switch_hero_master():
  228. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  229. # Remove the mouse and enable looking again
  230. toggle_mouse_capture()
  231. get_tree().set_pause(false)
  232. sync func switch_hero(hero):
  233. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  234. var net_id = int(get_name())
  235. set_name("%d-delete" % net_id) # Can't have duplicate names
  236. new_hero.set_name("%d" % net_id)
  237. new_hero.set_network_master(net_id)
  238. new_hero.player_info = player_info
  239. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  240. # We must wait until after _ready is called, so that we don't end up at spawn
  241. new_hero.call_deferred("set_status", get_status())
  242. new_hero.call_deferred("begin")
  243. queue_free()
  244. func write_recording():
  245. if recording and recording.events.size() > 0:
  246. var save = File.new()
  247. var fname = "res://recordings/%d-%d-%d.rec" % [player_info.level, player_info.hero, randi() % 10000]
  248. save.open(fname, File.WRITE)
  249. save.store_line(to_json(recording))
  250. save.close()
  251. # Quits the game:
  252. func quit():
  253. get_tree().quit()
  254. # These aren't used by vanilla player, but are used by heroes in common
  255. func pick_from(group):
  256. var look_ray = get_node("TPCamera/Camera/Ray")
  257. var looking_at = look_ray.get_collider()
  258. var result = group.find(looking_at)
  259. return result
  260. # =========